plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 3 | * AUTHOR : Richard Logan |
| 4 | * CO-PILOT : William Roske |
| 5 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 23 | * with this program; if not, write the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | */ |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Tests that link(2) succeds with creating n links. |
| 39 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/fcntl.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <errno.h> |
| 45 | #include <string.h> |
| 46 | #include <signal.h> |
| 47 | #include "test.h" |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 48 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 50 | static void setup(void); |
| 51 | static void help(void); |
| 52 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 54 | char *TCID = "link03"; |
| 55 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | #define BASENAME "lkfile" |
| 58 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 59 | static char fname[255]; |
| 60 | static int nlinks = 0; |
| 61 | static char *links_arg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | option_t options[] = { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 64 | {"N:", NULL, &links_arg}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | {NULL, NULL, NULL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 70 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 72 | struct stat buf; |
| 73 | int i, links; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | char lname[255]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 79 | if (links_arg) { |
| 80 | nlinks = atoi(links_arg); |
| 81 | |
| 82 | if (nlinks == 0) { |
| 83 | tst_brkm(TBROK, NULL, |
| 84 | "nlinks is not a positive number"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | } |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 86 | |
| 87 | if (nlinks > 1000) { |
| 88 | tst_resm(TINFO, |
| 89 | "nlinks > 1000 - may get errno:%d (EMLINK)", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | EMLINK); |
| 91 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 97 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 99 | if (nlinks) |
| 100 | links = nlinks; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | else |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 102 | links = (lc % 90) + 10; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 104 | /* Create links - 1 hardlinks so that the st_nlink == links */ |
| 105 | for (i = 1; i < links; i++) { |
| 106 | sprintf(lname, "%s%d", fname, i); |
| 107 | TEST(link(fname, lname)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | if (TEST_RETURN == -1) { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 110 | tst_brkm(TFAIL | TTERRNO, cleanup, |
| 111 | "link(%s, %s) Failed", fname, lname); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 115 | SAFE_STAT(cleanup, fname, &buf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 117 | if (buf.st_nlink != (nlink_t)links) { |
| 118 | tst_resm(TFAIL, "Wrong number of links for " |
| 119 | "'%s' have %i, should be %i", |
| 120 | fname, (int)buf.st_nlink, links); |
| 121 | goto unlink; |
| 122 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 124 | for (i = 1; i < links; i++) { |
| 125 | sprintf(lname, "%s%d", fname, i); |
| 126 | SAFE_STAT(cleanup, lname, &buf); |
| 127 | if (buf.st_nlink != (nlink_t)links) { |
| 128 | tst_resm(TFAIL, |
| 129 | "Wrong number of links for " |
| 130 | "'%s' have %i, should be %i", |
| 131 | lname, (int)buf.st_nlink, links); |
| 132 | goto unlink; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 136 | tst_resm(TPASS, "link() passed and linkcounts=%d match", links); |
| 137 | |
| 138 | unlink: |
| 139 | for (i = 1; i < links; i++) { |
| 140 | sprintf(lname, "%s%d", fname, i); |
| 141 | SAFE_UNLINK(cleanup, lname); |
| 142 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | tst_exit(); |
| 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 149 | static void help(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | printf(" -N #links : create #links hard links every iteration\n"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 154 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 162 | sprintf(fname, "%s_%d", BASENAME, getpid()); |
| 163 | SAFE_TOUCH(cleanup, fname, 0700, NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 164 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 166 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 169 | } |