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 /* 38 * Tests that link(2) succeds with creating n links. 39 */ 40 41#include <sys/types.h> 42#include <fcntl.h> 43#include <sys/stat.h> 44#include <errno.h> 45#include <string.h> 46#include <signal.h> 47#include "test.h" 48#include "safe_macros.h" 49 50static void setup(void); 51static void help(void); 52static void cleanup(void); 53 54char *TCID = "link03"; 55int TST_TOTAL = 2; 56 57#define BASENAME "lkfile" 58 59static char fname[255]; 60static int nlinks = 0; 61static char *links_arg; 62 63option_t options[] = { 64 {"N:", NULL, &links_arg}, 65 {NULL, NULL, NULL} 66}; 67 68int main(int ac, char **av) 69{ 70 int lc; 71 struct stat buf; 72 int i, links; 73 char lname[255]; 74 75 tst_parse_opts(ac, av, options, &help); 76 77 if (links_arg) { 78 nlinks = atoi(links_arg); 79 80 if (nlinks == 0) { 81 tst_brkm(TBROK, NULL, 82 "nlinks is not a positive number"); 83 } 84 85 if (nlinks > 1000) { 86 tst_resm(TINFO, 87 "nlinks > 1000 - may get errno:%d (EMLINK)", 88 EMLINK); 89 } 90 } 91 92 setup(); 93 94 for (lc = 0; TEST_LOOPING(lc); lc++) { 95 tst_count = 0; 96 97 if (nlinks) 98 links = nlinks; 99 else 100 links = (lc % 90) + 10; 101 102 /* Create links - 1 hardlinks so that the st_nlink == links */ 103 for (i = 1; i < links; i++) { 104 sprintf(lname, "%s%d", fname, i); 105 TEST(link(fname, lname)); 106 107 if (TEST_RETURN == -1) { 108 tst_brkm(TFAIL | TTERRNO, cleanup, 109 "link(%s, %s) Failed", fname, lname); 110 } 111 } 112 113 SAFE_STAT(cleanup, fname, &buf); 114 115 if (buf.st_nlink != (nlink_t)links) { 116 tst_resm(TFAIL, "Wrong number of links for " 117 "'%s' have %i, should be %i", 118 fname, (int)buf.st_nlink, links); 119 goto unlink; 120 } 121 122 for (i = 1; i < links; i++) { 123 sprintf(lname, "%s%d", fname, i); 124 SAFE_STAT(cleanup, lname, &buf); 125 if (buf.st_nlink != (nlink_t)links) { 126 tst_resm(TFAIL, 127 "Wrong number of links for " 128 "'%s' have %i, should be %i", 129 lname, (int)buf.st_nlink, links); 130 goto unlink; 131 } 132 } 133 134 tst_resm(TPASS, "link() passed and linkcounts=%d match", links); 135 136unlink: 137 for (i = 1; i < links; i++) { 138 sprintf(lname, "%s%d", fname, i); 139 SAFE_UNLINK(cleanup, lname); 140 } 141 } 142 143 cleanup(); 144 tst_exit(); 145} 146 147static void help(void) 148{ 149 printf(" -N #links : create #links hard links every iteration\n"); 150} 151 152static void setup(void) 153{ 154 tst_sig(NOFORK, DEF_HANDLER, cleanup); 155 156 TEST_PAUSE; 157 158 tst_tmpdir(); 159 160 sprintf(fname, "%s_%d", BASENAME, getpid()); 161 SAFE_TOUCH(cleanup, fname, 0700, NULL); 162} 163 164static void cleanup(void) 165{ 166 tst_rmdir(); 167} 168