blob: 10a8d02bf3c0768a545ced72a0a3c2338c7d3a4d [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +01003 * AUTHOR : Richard Logan
4 * CO-PILOT : William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080023 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000025 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010036
37 /*
38 * Tests that link(2) succeds with creating n links.
39 */
plars865695b2001-08-27 22:15:12 +000040
41#include <sys/types.h>
42#include <sys/fcntl.h>
43#include <sys/stat.h>
44#include <errno.h>
45#include <string.h>
46#include <signal.h>
47#include "test.h"
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010048#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000049
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010050static void setup(void);
51static void help(void);
52static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054char *TCID = "link03";
55int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000056
plars865695b2001-08-27 22:15:12 +000057#define BASENAME "lkfile"
58
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010059static char fname[255];
60static int nlinks = 0;
61static char *links_arg;
plars865695b2001-08-27 22:15:12 +000062
plars865695b2001-08-27 22:15:12 +000063option_t options[] = {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010064 {"N:", NULL, &links_arg},
subrata_modak56207ce2009-03-23 13:35:39 +000065 {NULL, NULL, NULL}
plars865695b2001-08-27 22:15:12 +000066};
67
subrata_modak56207ce2009-03-23 13:35:39 +000068int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000069{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020070 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020071 const char *msg;
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010072 struct stat buf;
73 int i, links;
subrata_modak56207ce2009-03-23 13:35:39 +000074 char lname[255];
plars865695b2001-08-27 22:15:12 +000075
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010076 if ((msg = parse_opts(ac, av, options, &help)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000078
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010079 if (links_arg) {
80 nlinks = atoi(links_arg);
81
82 if (nlinks == 0) {
83 tst_brkm(TBROK, NULL,
84 "nlinks is not a positive number");
subrata_modak56207ce2009-03-23 13:35:39 +000085 }
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010086
87 if (nlinks > 1000) {
88 tst_resm(TINFO,
89 "nlinks > 1000 - may get errno:%d (EMLINK)",
subrata_modak56207ce2009-03-23 13:35:39 +000090 EMLINK);
91 }
plars865695b2001-08-27 22:15:12 +000092 }
plars865695b2001-08-27 22:15:12 +000093
subrata_modak56207ce2009-03-23 13:35:39 +000094 setup();
plars865695b2001-08-27 22:15:12 +000095
subrata_modak56207ce2009-03-23 13:35:39 +000096 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080097 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000098
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010099 if (nlinks)
100 links = nlinks;
subrata_modak56207ce2009-03-23 13:35:39 +0000101 else
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100102 links = (lc % 90) + 10;
plars865695b2001-08-27 22:15:12 +0000103
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100104 /* Create links - 1 hardlinks so that the st_nlink == links */
105 for (i = 1; i < links; i++) {
106 sprintf(lname, "%s%d", fname, i);
107 TEST(link(fname, lname));
subrata_modakbdbaec52009-02-26 12:14:51 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 if (TEST_RETURN == -1) {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100110 tst_brkm(TFAIL | TTERRNO, cleanup,
111 "link(%s, %s) Failed", fname, lname);
subrata_modak56207ce2009-03-23 13:35:39 +0000112 }
113 }
114
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100115 SAFE_STAT(cleanup, fname, &buf);
plars865695b2001-08-27 22:15:12 +0000116
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100117 if (buf.st_nlink != (nlink_t)links) {
118 tst_resm(TFAIL, "Wrong number of links for "
119 "'%s' have %i, should be %i",
120 fname, (int)buf.st_nlink, links);
121 goto unlink;
122 }
plars865695b2001-08-27 22:15:12 +0000123
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100124 for (i = 1; i < links; i++) {
125 sprintf(lname, "%s%d", fname, i);
126 SAFE_STAT(cleanup, lname, &buf);
127 if (buf.st_nlink != (nlink_t)links) {
128 tst_resm(TFAIL,
129 "Wrong number of links for "
130 "'%s' have %i, should be %i",
131 lname, (int)buf.st_nlink, links);
132 goto unlink;
subrata_modak56207ce2009-03-23 13:35:39 +0000133 }
plars865695b2001-08-27 22:15:12 +0000134 }
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100136 tst_resm(TPASS, "link() passed and linkcounts=%d match", links);
137
138unlink:
139 for (i = 1; i < links; i++) {
140 sprintf(lname, "%s%d", fname, i);
141 SAFE_UNLINK(cleanup, lname);
142 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800143 }
plars865695b2001-08-27 22:15:12 +0000144
subrata_modak56207ce2009-03-23 13:35:39 +0000145 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 tst_exit();
147}
plars865695b2001-08-27 22:15:12 +0000148
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100149static void help(void)
plars865695b2001-08-27 22:15:12 +0000150{
subrata_modak56207ce2009-03-23 13:35:39 +0000151 printf(" -N #links : create #links hard links every iteration\n");
plars865695b2001-08-27 22:15:12 +0000152}
153
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100154static void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
subrata_modak56207ce2009-03-23 13:35:39 +0000156 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000157
subrata_modak56207ce2009-03-23 13:35:39 +0000158 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000161
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100162 sprintf(fname, "%s_%d", BASENAME, getpid());
163 SAFE_TOUCH(cleanup, fname, 0700, NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800164}
plars865695b2001-08-27 22:15:12 +0000165
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100166static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000167{
subrata_modak56207ce2009-03-23 13:35:39 +0000168 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700169}