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 : William Roske |
| 4 | * CO-PILOT : Dave Fenner |
| 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 | * Tests that link(2) succeds. |
| 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | char *TCID = "link02"; |
| 53 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 55 | #define OLDPATH "oldpath" |
| 56 | #define NEWPATH "newpath" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 58 | static void verify_link(void) |
| 59 | { |
| 60 | struct stat fbuf, lbuf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 62 | TEST(link(OLDPATH, NEWPATH)); |
| 63 | |
| 64 | if (TEST_RETURN == 0) { |
| 65 | SAFE_STAT(cleanup, OLDPATH, &fbuf); |
| 66 | SAFE_STAT(cleanup, NEWPATH, &lbuf); |
| 67 | if (fbuf.st_nlink > 1 && lbuf.st_nlink > 1 && |
| 68 | fbuf.st_nlink == lbuf.st_nlink) { |
| 69 | tst_resm(TPASS, "link("OLDPATH","NEWPATH") " |
| 70 | "returned 0 and link counts match"); |
| 71 | } else { |
| 72 | tst_resm(TFAIL, "link("OLDPATH","NEWPATH") returned 0" |
| 73 | " but stat lin count do not match %d %d", |
| 74 | (int)fbuf.st_nlink, (int)lbuf.st_nlink); |
| 75 | } |
| 76 | SAFE_UNLINK(cleanup, NEWPATH); |
| 77 | } else { |
| 78 | tst_resm(TFAIL | TTERRNO, |
| 79 | "link("OLDPATH","NEWPATH") returned %ld", |
| 80 | TEST_RETURN); |
| 81 | } |
| 82 | } |
| 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 86 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 89 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | setup(); |
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 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 96 | verify_link(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 100 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 103 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | TEST_PAUSE; |
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 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 111 | SAFE_TOUCH(cleanup, OLDPATH, 0700, NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 114 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 117 | } |