1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *  AUTHOR		: Richard Logan
4f08c3bdfSopenharmony_ci *  CO-PILOT		: William Roske
5f08c3bdfSopenharmony_ci * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
8f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
9f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
12f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
13f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is
16f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement
17f08c3bdfSopenharmony_ci * or the like.  Any license provided herein, whether implied or
18f08c3bdfSopenharmony_ci * otherwise, applies only to this software file.  Patent licenses, if
19f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with
20f08c3bdfSopenharmony_ci * other software, or any other product whatsoever.
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
23f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
24f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27f08c3bdfSopenharmony_ci * Mountain View, CA  94043, or:
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * http://www.sgi.com
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * For further information regarding this notice, see:
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci */
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci /*
38f08c3bdfSopenharmony_ci  * Tests that link(2) succeds with creating n links.
39f08c3bdfSopenharmony_ci  */
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci#include <sys/types.h>
42f08c3bdfSopenharmony_ci#include <fcntl.h>
43f08c3bdfSopenharmony_ci#include <sys/stat.h>
44f08c3bdfSopenharmony_ci#include <errno.h>
45f08c3bdfSopenharmony_ci#include <string.h>
46f08c3bdfSopenharmony_ci#include <signal.h>
47f08c3bdfSopenharmony_ci#include "test.h"
48f08c3bdfSopenharmony_ci#include "safe_macros.h"
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void setup(void);
51f08c3bdfSopenharmony_cistatic void help(void);
52f08c3bdfSopenharmony_cistatic void cleanup(void);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cichar *TCID = "link03";
55f08c3bdfSopenharmony_ciint TST_TOTAL = 2;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci#define BASENAME	"lkfile"
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic char fname[255];
60f08c3bdfSopenharmony_cistatic int nlinks = 0;
61f08c3bdfSopenharmony_cistatic char *links_arg;
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cioption_t options[] = {
64f08c3bdfSopenharmony_ci	{"N:", NULL, &links_arg},
65f08c3bdfSopenharmony_ci	{NULL, NULL, NULL}
66f08c3bdfSopenharmony_ci};
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ciint main(int ac, char **av)
69f08c3bdfSopenharmony_ci{
70f08c3bdfSopenharmony_ci	int lc;
71f08c3bdfSopenharmony_ci	struct stat buf;
72f08c3bdfSopenharmony_ci	int i, links;
73f08c3bdfSopenharmony_ci	char lname[255];
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, options, &help);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	if (links_arg) {
78f08c3bdfSopenharmony_ci		nlinks = atoi(links_arg);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci		if (nlinks == 0) {
81f08c3bdfSopenharmony_ci			tst_brkm(TBROK, NULL,
82f08c3bdfSopenharmony_ci			         "nlinks is not a positive number");
83f08c3bdfSopenharmony_ci		}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci		if (nlinks > 1000) {
86f08c3bdfSopenharmony_ci			tst_resm(TINFO,
87f08c3bdfSopenharmony_ci				 "nlinks > 1000 - may get errno:%d (EMLINK)",
88f08c3bdfSopenharmony_ci				 EMLINK);
89f08c3bdfSopenharmony_ci		}
90f08c3bdfSopenharmony_ci	}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	setup();
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
95f08c3bdfSopenharmony_ci		tst_count = 0;
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci		if (nlinks)
98f08c3bdfSopenharmony_ci			links = nlinks;
99f08c3bdfSopenharmony_ci		else
100f08c3bdfSopenharmony_ci			links = (lc % 90) + 10;
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci		/* Create links - 1 hardlinks so that the st_nlink == links */
103f08c3bdfSopenharmony_ci		for (i = 1; i < links; i++) {
104f08c3bdfSopenharmony_ci			sprintf(lname, "%s%d", fname, i);
105f08c3bdfSopenharmony_ci			TEST(link(fname, lname));
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci			if (TEST_RETURN == -1) {
108f08c3bdfSopenharmony_ci				tst_brkm(TFAIL | TTERRNO, cleanup,
109f08c3bdfSopenharmony_ci					 "link(%s, %s) Failed", fname, lname);
110f08c3bdfSopenharmony_ci			}
111f08c3bdfSopenharmony_ci		}
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci		SAFE_STAT(cleanup, fname, &buf);
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci		if (buf.st_nlink != (nlink_t)links) {
116f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "Wrong number of links for "
117f08c3bdfSopenharmony_ci			         "'%s' have %i, should be %i",
118f08c3bdfSopenharmony_ci				 fname, (int)buf.st_nlink, links);
119f08c3bdfSopenharmony_ci			goto unlink;
120f08c3bdfSopenharmony_ci		}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci		for (i = 1; i < links; i++) {
123f08c3bdfSopenharmony_ci			sprintf(lname, "%s%d", fname, i);
124f08c3bdfSopenharmony_ci			SAFE_STAT(cleanup, lname, &buf);
125f08c3bdfSopenharmony_ci			if (buf.st_nlink != (nlink_t)links) {
126f08c3bdfSopenharmony_ci				tst_resm(TFAIL,
127f08c3bdfSopenharmony_ci				         "Wrong number of links for "
128f08c3bdfSopenharmony_ci					 "'%s' have %i, should be %i",
129f08c3bdfSopenharmony_ci					 lname, (int)buf.st_nlink, links);
130f08c3bdfSopenharmony_ci				goto unlink;
131f08c3bdfSopenharmony_ci			}
132f08c3bdfSopenharmony_ci		}
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci		tst_resm(TPASS, "link() passed and linkcounts=%d match", links);
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ciunlink:
137f08c3bdfSopenharmony_ci		for (i = 1; i < links; i++) {
138f08c3bdfSopenharmony_ci			sprintf(lname, "%s%d", fname, i);
139f08c3bdfSopenharmony_ci			SAFE_UNLINK(cleanup, lname);
140f08c3bdfSopenharmony_ci		}
141f08c3bdfSopenharmony_ci	}
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	cleanup();
144f08c3bdfSopenharmony_ci	tst_exit();
145f08c3bdfSopenharmony_ci}
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_cistatic void help(void)
148f08c3bdfSopenharmony_ci{
149f08c3bdfSopenharmony_ci	printf("  -N #links : create #links hard links every iteration\n");
150f08c3bdfSopenharmony_ci}
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_cistatic void setup(void)
153f08c3bdfSopenharmony_ci{
154f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
155f08c3bdfSopenharmony_ci
156f08c3bdfSopenharmony_ci	TEST_PAUSE;
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	tst_tmpdir();
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	sprintf(fname, "%s_%d", BASENAME, getpid());
161f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, fname, 0700, NULL);
162f08c3bdfSopenharmony_ci}
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_cistatic void cleanup(void)
165f08c3bdfSopenharmony_ci{
166f08c3bdfSopenharmony_ci	tst_rmdir();
167f08c3bdfSopenharmony_ci}
168