1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3 *  AUTHOR		: Richard Logan
4 *  CO-PILOT		: William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6 *
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
23 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
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
36/*
37 * Test if link(2) fails with EMLINK.
38 */
39
40#include <sys/types.h>
41#include <fcntl.h>
42#include <sys/stat.h>
43#include <errno.h>
44#include <string.h>
45#include <signal.h>
46#include "test.h"
47#include "safe_macros.h"
48
49static void setup(void);
50static void cleanup(void);
51static void help(void);
52
53char *TCID = "link05";
54int TST_TOTAL = 1;
55
56#define BASENAME	"lkfile"
57
58static char fname[255];
59
60static char *links_arg;
61
62option_t options[] = {
63	{"N:", NULL, &links_arg},
64	{NULL, NULL, NULL}
65};
66
67static int nlinks = 1000;
68
69int main(int ac, char **av)
70{
71	int lc;
72	struct stat fbuf, lbuf;
73	int cnt;
74	char lname[255];
75
76	tst_parse_opts(ac, av, options, &help);
77
78	if (links_arg) {
79		nlinks = atoi(links_arg);
80
81		if (nlinks == 0) {
82			tst_brkm(TBROK, NULL,
83			         "nlinks is not a positive number");
84		}
85	}
86
87	setup();
88
89	for (lc = 0; TEST_LOOPING(lc); lc++) {
90
91		tst_count = 0;
92
93		for (cnt = 1; cnt < nlinks; cnt++) {
94			sprintf(lname, "%s%d", fname, cnt);
95			TEST(link(fname, lname));
96
97			if (TEST_RETURN == -1) {
98				tst_resm(TFAIL,
99					 "link(%s, %s) Failed, errno=%d : %s",
100					 fname, lname, TEST_ERRNO,
101					 strerror(TEST_ERRNO));
102			}
103		}
104
105		SAFE_STAT(cleanup, fname, &fbuf);
106
107		for (cnt = 1; cnt < nlinks; cnt++) {
108			sprintf(lname, "%s%d", fname, cnt);
109
110			SAFE_STAT(cleanup, lname, &lbuf);
111			if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
112			    (fbuf.st_nlink != lbuf.st_nlink)) {
113
114				tst_resm(TFAIL,
115					 "link(%s, %s[1-%d]) ret %ld for %d "
116				         "files, stat values do not match %d %d",
117					 fname, fname, nlinks,
118					 TEST_RETURN, nlinks,
119					 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
120				break;
121			}
122		}
123		if (cnt >= nlinks) {
124			tst_resm(TPASS,
125				 "link(%s, %s[1-%d]) ret %ld for %d files,"
126			         "stat linkcounts match %d",
127				 fname, fname, nlinks, TEST_RETURN,
128				 nlinks, (int)fbuf.st_nlink);
129		}
130
131		for (cnt = 1; cnt < nlinks; cnt++) {
132			sprintf(lname, "%s%d", fname, cnt);
133			SAFE_UNLINK(cleanup, lname);
134		}
135	}
136
137	cleanup();
138	tst_exit();
139}
140
141static void help(void)
142{
143	printf("  -N #links : create #links hard links every iteration\n");
144}
145
146static void setup(void)
147{
148	tst_sig(NOFORK, DEF_HANDLER, cleanup);
149
150	TEST_PAUSE;
151
152	tst_tmpdir();
153
154	sprintf(fname, "%s_%d", BASENAME, getpid());
155	SAFE_TOUCH(cleanup, fname, 0700, NULL);
156}
157
158static void cleanup(void)
159{
160	tst_rmdir();
161}
162