blob: b9a32f1100affa871f73cbd5f148583bfdf73737 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +01003 * AUTHOR : William Roske
4 * CO-PILOT : Dave Fenner
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
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 Gaofed96412012-10-24 10:10:29 +080023 * with this program; if not, write the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000025 *
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/
plars865695b2001-08-27 22:15:12 +000034 */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010035
36/*
37 * Tests that link(2) succeds.
38 */
plars865695b2001-08-27 22:15:12 +000039
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 Hrubisaf0ea7b2014-03-03 19:53:46 +010047#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010049static void setup(void);
50static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000051
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052char *TCID = "link02";
53int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000054
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010055#define OLDPATH "oldpath"
56#define NEWPATH "newpath"
plars865695b2001-08-27 22:15:12 +000057
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010058static void verify_link(void)
59{
60 struct stat fbuf, lbuf;
plars865695b2001-08-27 22:15:12 +000061
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010062 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_modak56207ce2009-03-23 13:35:39 +000084int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000085{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000088
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010089 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000090 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080091
subrata_modak56207ce2009-03-23 13:35:39 +000092 setup();
plars865695b2001-08-27 22:15:12 +000093
subrata_modak56207ce2009-03-23 13:35:39 +000094 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010096 verify_link();
Garrett Cooper2c282152010-12-16 00:55:50 -080097 }
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800100 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800101}
plars865695b2001-08-27 22:15:12 +0000102
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100103static void setup(void)
plars865695b2001-08-27 22:15:12 +0000104{
subrata_modak56207ce2009-03-23 13:35:39 +0000105 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000110
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100111 SAFE_TOUCH(cleanup, OLDPATH, 0700, NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800112}
plars865695b2001-08-27 22:15:12 +0000113
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100114static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000115{
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700117}