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/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Test if link(2) fails with EMLINK. |
| 38 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/fcntl.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <errno.h> |
| 44 | #include <string.h> |
| 45 | #include <signal.h> |
| 46 | #include "test.h" |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 47 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 49 | static void setup(void); |
| 50 | static void cleanup(void); |
| 51 | static void help(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 53 | char *TCID = "link05"; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #define BASENAME "lkfile" |
| 57 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 58 | static char fname[255]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 60 | static char *links_arg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | option_t options[] = { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 63 | {"N:", NULL, &links_arg}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | {NULL, NULL, NULL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 67 | static int nlinks = 1000; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | struct stat fbuf, lbuf; |
| 74 | int cnt; |
| 75 | char lname[255]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 77 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 80 | if (links_arg) { |
| 81 | nlinks = atoi(links_arg); |
| 82 | |
| 83 | if (nlinks == 0) { |
| 84 | tst_brkm(TBROK, NULL, |
| 85 | "nlinks is not a positive number"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | for (cnt = 1; cnt < nlinks; cnt++) { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 96 | sprintf(lname, "%s%d", fname, cnt); |
| 97 | TEST(link(fname, lname)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | if (TEST_RETURN == -1) { |
| 100 | tst_resm(TFAIL, |
| 101 | "link(%s, %s) Failed, errno=%d : %s", |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 102 | fname, lname, TEST_ERRNO, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | strerror(TEST_ERRNO)); |
| 104 | } |
| 105 | } |
| 106 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 107 | SAFE_STAT(cleanup, fname, &fbuf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | for (cnt = 1; cnt < nlinks; cnt++) { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 110 | sprintf(lname, "%s%d", fname, cnt); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 112 | SAFE_STAT(cleanup, lname, &lbuf); |
| 113 | if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 || |
| 114 | (fbuf.st_nlink != lbuf.st_nlink)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 116 | tst_resm(TFAIL, |
| 117 | "link(%s, %s[1-%d]) ret %ld for %d " |
| 118 | "files, stat values do not match %d %d", |
| 119 | fname, fname, nlinks, |
| 120 | TEST_RETURN, nlinks, |
| 121 | (int)fbuf.st_nlink, (int)lbuf.st_nlink); |
| 122 | break; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 125 | if (cnt >= nlinks) { |
| 126 | tst_resm(TPASS, |
| 127 | "link(%s, %s[1-%d]) ret %ld for %d files," |
| 128 | "stat linkcounts match %d", |
| 129 | fname, fname, nlinks, TEST_RETURN, |
| 130 | nlinks, (int)fbuf.st_nlink); |
| 131 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 133 | for (cnt = 1; cnt < nlinks; cnt++) { |
| 134 | sprintf(lname, "%s%d", fname, cnt); |
| 135 | SAFE_UNLINK(cleanup, lname); |
| 136 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 137 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | tst_exit(); |
| 141 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 143 | static void help(void) |
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 | printf(" -N #links : create #links hard links every iteration\n"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 148 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 156 | sprintf(fname, "%s_%d", BASENAME, getpid()); |
| 157 | SAFE_TOUCH(cleanup, fname, 0700, NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 160 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 163 | } |