1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * Authors: Richard Logan, William Roske
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6 * Copyright (c) Linux Test Project, 2001-2023
7 */
8
9 /*\
10 * [Description]
11 *
12 * Tests that link(2) succeeds with creating 1000 links.
13 */
14
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include "tst_test.h"
18
19 #define BASENAME "lkfile"
20
21 static char fname[255];
22
23 static int nlinks = 1000;
24
verify_link(void)25 static void verify_link(void)
26 {
27 int cnt;
28 char lname[1024];
29 struct stat fbuf, lbuf;
30
31 for (cnt = 1; cnt < nlinks; cnt++) {
32 sprintf(lname, "%s_%d", fname, cnt);
33 TST_EXP_PASS_SILENT(link(fname, lname), "link(%s, %s)", fname, lname);
34 }
35
36 SAFE_STAT(fname, &fbuf);
37
38 for (cnt = 1; cnt < nlinks; cnt++) {
39 sprintf(lname, "%s_%d", fname, cnt);
40
41 SAFE_STAT(lname, &lbuf);
42 if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
43 (fbuf.st_nlink != lbuf.st_nlink)) {
44
45 tst_res(TFAIL,
46 "link(%s, %s[1-%d]) ret %ld for %d files, stat values do not match %d %d",
47 fname, fname, nlinks, TST_RET, nlinks,
48 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
49 break;
50 }
51 }
52
53 if (cnt >= nlinks) {
54 tst_res(TPASS,
55 "link(%s, %s[1-%d]) ret %ld for %d files, stat linkcounts match %d",
56 fname, fname, nlinks, TST_RET, nlinks, (int)fbuf.st_nlink);
57 }
58
59 for (cnt = 1; cnt < nlinks; cnt++) {
60 sprintf(lname, "%s_%d", fname, cnt);
61 SAFE_UNLINK(lname);
62 }
63 }
64
setup(void)65 static void setup(void)
66 {
67 sprintf(fname, "%s_%d", BASENAME, getpid());
68 SAFE_TOUCH(fname, 0700, NULL);
69 }
70
71 static struct tst_test test = {
72 .test_all = verify_link,
73 .setup = setup,
74 .needs_tmpdir = 1,
75 };
76