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