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 <[email protected]>
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 #define NLINKS 1000
21
22 static char fname[255];
23
verify_link(void)24 static void verify_link(void)
25 {
26 int cnt = 0, created;
27 char lname[1024];
28 struct stat fbuf, lbuf;
29
30 for (created = 1; created < NLINKS; created++) {
31 sprintf(lname, "%s_%d", fname, created);
32 TST_EXP_PASS_SILENT(link(fname, lname), "%d: link(%s, %s)", created,
33 fname, lname);
34 if (!TST_PASS)
35 goto cleanup;
36 }
37
38 SAFE_STAT(fname, &fbuf);
39
40 for (cnt = 1; cnt < NLINKS; cnt++) {
41 sprintf(lname, "%s_%d", fname, cnt);
42
43 SAFE_STAT(lname, &lbuf);
44 if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 ||
45 (fbuf.st_nlink != lbuf.st_nlink)) {
46
47 tst_res(TFAIL,
48 "link(%s, %s[1-%d]) ret %ld for %d files, stat values do not match %d %d",
49 fname, fname, NLINKS, TST_RET, NLINKS,
50 (int)fbuf.st_nlink, (int)lbuf.st_nlink);
51 break;
52 }
53 }
54
55 if (cnt == NLINKS) {
56 tst_res(TPASS,
57 "link(%s, %s[1-%d]) ret %ld for %d files, stat linkcounts match %d",
58 fname, fname, NLINKS, TST_RET, NLINKS, (int)fbuf.st_nlink);
59 }
60
61 cleanup:
62 for (cnt = 1; cnt < created; cnt++) {
63 sprintf(lname, "%s_%d", fname, cnt);
64 SAFE_UNLINK(lname);
65 }
66 }
67
setup(void)68 static void setup(void)
69 {
70 sprintf(fname, "%s_%d", BASENAME, getpid());
71 SAFE_TOUCH(fname, 0700, NULL);
72 }
73
74 static struct tst_test test = {
75 .test_all = verify_link,
76 .setup = setup,
77 .needs_tmpdir = 1,
78 };
79