blob: d83c6e79ddcc35234fefb8796f1092a3af0ae0ed [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/
plars865695b2001-08-27 22:15:12 +000034 */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010035
36/*
37 * Test if link(2) fails with EMLINK.
38 */
plars865695b2001-08-27 22:15:12 +000039
40#include <sys/types.h>
41#include <sys/fcntl.h>
42#include <sys/stat.h>
43#include <errno.h>
44#include <string.h>
45#include <signal.h>
46#include "test.h"
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010047#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010049static void setup(void);
50static void cleanup(void);
51static void help(void);
plars865695b2001-08-27 22:15:12 +000052
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020053char *TCID = "link05";
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000055
56#define BASENAME "lkfile"
57
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010058static char fname[255];
plars865695b2001-08-27 22:15:12 +000059
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010060static char *links_arg;
plars865695b2001-08-27 22:15:12 +000061
plars865695b2001-08-27 22:15:12 +000062option_t options[] = {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010063 {"N:", NULL, &links_arg},
subrata_modak56207ce2009-03-23 13:35:39 +000064 {NULL, NULL, NULL}
plars865695b2001-08-27 22:15:12 +000065};
66
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010067static int nlinks = 1000;
plars865695b2001-08-27 22:15:12 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000070{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020071 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020072 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000073 struct stat fbuf, lbuf;
74 int cnt;
75 char lname[255];
plars865695b2001-08-27 22:15:12 +000076
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010077 if ((msg = parse_opts(ac, av, options, &help)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000078 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +000079
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010080 if (links_arg) {
81 nlinks = atoi(links_arg);
82
83 if (nlinks == 0) {
84 tst_brkm(TBROK, NULL,
85 "nlinks is not a positive number");
subrata_modak56207ce2009-03-23 13:35:39 +000086 }
87 }
plars865695b2001-08-27 22:15:12 +000088
subrata_modak56207ce2009-03-23 13:35:39 +000089 setup();
plars865695b2001-08-27 22:15:12 +000090
subrata_modak56207ce2009-03-23 13:35:39 +000091 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +000092
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 for (cnt = 1; cnt < nlinks; cnt++) {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010096 sprintf(lname, "%s%d", fname, cnt);
97 TEST(link(fname, lname));
subrata_modakbdbaec52009-02-26 12:14:51 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (TEST_RETURN == -1) {
100 tst_resm(TFAIL,
101 "link(%s, %s) Failed, errno=%d : %s",
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100102 fname, lname, TEST_ERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +0000103 strerror(TEST_ERRNO));
104 }
105 }
106
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100107 SAFE_STAT(cleanup, fname, &fbuf);
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 for (cnt = 1; cnt < nlinks; cnt++) {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100110 sprintf(lname, "%s%d", fname, cnt);
plars865695b2001-08-27 22:15:12 +0000111
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100112 SAFE_STAT(cleanup, lname, &lbuf);
113 if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
114 (fbuf.st_nlink != lbuf.st_nlink)) {
plars865695b2001-08-27 22:15:12 +0000115
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100116 tst_resm(TFAIL,
117 "link(%s, %s[1-%d]) ret %ld for %d "
118 "files, stat values do not match %d %d",
119 fname, fname, nlinks,
120 TEST_RETURN, nlinks,
121 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
122 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000123 }
124 }
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100125 if (cnt >= nlinks) {
126 tst_resm(TPASS,
127 "link(%s, %s[1-%d]) ret %ld for %d files,"
128 "stat linkcounts match %d",
129 fname, fname, nlinks, TEST_RETURN,
130 nlinks, (int)fbuf.st_nlink);
131 }
subrata_modak56207ce2009-03-23 13:35:39 +0000132
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100133 for (cnt = 1; cnt < nlinks; cnt++) {
134 sprintf(lname, "%s%d", fname, cnt);
135 SAFE_UNLINK(cleanup, lname);
136 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800137 }
plars865695b2001-08-27 22:15:12 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800140 tst_exit();
141}
plars865695b2001-08-27 22:15:12 +0000142
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100143static void help(void)
plars865695b2001-08-27 22:15:12 +0000144{
subrata_modak56207ce2009-03-23 13:35:39 +0000145 printf(" -N #links : create #links hard links every iteration\n");
plars865695b2001-08-27 22:15:12 +0000146}
147
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100148static void setup(void)
plars865695b2001-08-27 22:15:12 +0000149{
subrata_modak56207ce2009-03-23 13:35:39 +0000150 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000151
subrata_modak56207ce2009-03-23 13:35:39 +0000152 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000153
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000155
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100156 sprintf(fname, "%s_%d", BASENAME, getpid());
157 SAFE_TOUCH(cleanup, fname, 0700, NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800158}
plars865695b2001-08-27 22:15:12 +0000159
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100160static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000161{
subrata_modak56207ce2009-03-23 13:35:39 +0000162 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700163}