blob: eda8bf099014f4a86215477603e9896f55fa5bf6 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * open07.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Test the open(2) system call to ensure that it sets ELOOP correctly.
subrata_modak56207ce2009-03-23 13:35:39 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * CALLS
subrata_modak56207ce2009-03-23 13:35:39 +000028 * open()
plars865695b2001-08-27 22:15:12 +000029 *
30 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000031 * 1. Create a symbolic link to a file, and call open(O_NOFOLLOW). Check
plars865695b2001-08-27 22:15:12 +000032 * that it returns ELOOP.
33 *
subrata_modak56207ce2009-03-23 13:35:39 +000034 * 2. Create a symbolic link to a directory, and call open(O_NOFOLLOW).
plars865695b2001-08-27 22:15:12 +000035 * Check that it returns ELOOP.
36 *
subrata_modak56207ce2009-03-23 13:35:39 +000037 * 3. Create a symbolic link to a symbolic link, and call open(O_NOFOLLOW).
plars865695b2001-08-27 22:15:12 +000038 * Check that it returns ELOOP.
39 *
subrata_modak56207ce2009-03-23 13:35:39 +000040 * 4. Create a symbolic link to a symbolically linked directory, and call
plars865695b2001-08-27 22:15:12 +000041 * open(O_NOFOLLOW). Check that it returns ELOOP.
42 *
Wanlong Gao78d42772013-02-04 17:11:18 +080043 * 5. Create a symbolic link to a directory, and call
Subrata Modak6e9c7352010-05-18 01:31:35 +053044 * open("link/", O_NOFOLLOW). Check that it succeeds.
45 *
plars865695b2001-08-27 22:15:12 +000046 * USAGE: <for command-line>
47 * open07 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
48 * where, -c n : Run n copies concurrently.
49 * -e : Turn on errno logging.
50 * -i n : Execute test n times.
51 * -I x : Execute test for x seconds.
52 * -P x : Pause for x seconds between iterations.
53 * -t : Turn on syscall timing.
54 *
55 * HISTORY
56 * 07/2001 Ported by Wayne Boyer
57 *
58 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000059 * None
plars865695b2001-08-27 22:15:12 +000060 */
plars7b6a0672002-06-05 12:51:39 +000061#define _GNU_SOURCE /* for O_NOFOLLOW */
plars865695b2001-08-27 22:15:12 +000062#include <stdio.h>
63#include <errno.h>
64#include <sys/types.h>
65#include <sys/stat.h>
plars7b6a0672002-06-05 12:51:39 +000066#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000067#include "test.h"
plars865695b2001-08-27 22:15:12 +000068
Wanlong Gao78d42772013-02-04 17:11:18 +080069static void setup(void);
70static void cleanup(void);
71static void setupfunc_test1();
72static void setupfunc_test2();
73static void setupfunc_test3();
74static void setupfunc_test4();
75static void setupfunc_test5();
plars865695b2001-08-27 22:15:12 +000076
77char *TCID = "open07";
Subrata Modak6e9c7352010-05-18 01:31:35 +053078int TST_TOTAL = 5;
plars865695b2001-08-27 22:15:12 +000079
Wanlong Gao78d42772013-02-04 17:11:18 +080080static int fd1, fd2;
plars865695b2001-08-27 22:15:12 +000081
Wanlong Gao78d42772013-02-04 17:11:18 +080082static struct test_case_t {
plars865695b2001-08-27 22:15:12 +000083 char *desc;
Subrata Modak6e9c7352010-05-18 01:31:35 +053084 char filename[100];
plars865695b2001-08-27 22:15:12 +000085 int flags;
86 int mode;
subrata_modak56207ce2009-03-23 13:35:39 +000087 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000088 int exp_errno;
89} TC[] = {
Wanlong Gao78d42772013-02-04 17:11:18 +080090 {"Test for ELOOP on f2: f1 -> f2", {},
91 O_NOFOLLOW, 00700, setupfunc_test1, ELOOP},
92 {"Test for ELOOP on d2: d1 -> d2", {},
93 O_NOFOLLOW, 00700, setupfunc_test2, ELOOP},
94 {"Test for ELOOP on f3: f1 -> f2 -> f3", {},
95 O_NOFOLLOW, 00700, setupfunc_test3, ELOOP},
96 {"Test for ELOOP on d3: d1 -> d2 -> d3", {},
97 O_NOFOLLOW, 00700, setupfunc_test4, ELOOP},
98 {"Test for success on d2: d1 -> d2", {},
99 O_NOFOLLOW, 00700, setupfunc_test5, 0},
100 {NULL, {}, 0, 0, NULL, 0}
plars865695b2001-08-27 22:15:12 +0000101};
102
plars74948ad2002-11-14 16:16:14 +0000103int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000104{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200106 const char *msg;
plars865695b2001-08-27 22:15:12 +0000107 int i;
108
Wanlong Gao78d42772013-02-04 17:11:18 +0800109 msg = parse_opts(ac, av, NULL, NULL);
110 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000112
113 setup();
114
plars865695b2001-08-27 22:15:12 +0000115 /* run the setup routines for the individual tests */
subrata_modak56207ce2009-03-23 13:35:39 +0000116 for (i = 0; i < TST_TOTAL; i++) {
Wanlong Gao78d42772013-02-04 17:11:18 +0800117 if (TC[i].setupfunc != NULL)
plars865695b2001-08-27 22:15:12 +0000118 TC[i].setupfunc();
plars865695b2001-08-27 22:15:12 +0000119 }
120
plars865695b2001-08-27 22:15:12 +0000121 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800122 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000123
124 for (i = 0; TC[i].desc != NULL; ++i) {
plars865695b2001-08-27 22:15:12 +0000125 TEST(open(TC[i].filename, TC[i].flags, TC[i].mode));
126
Subrata Modak6e9c7352010-05-18 01:31:35 +0530127 if (TC[i].exp_errno != 0) {
128 if (TEST_RETURN != -1) {
129 tst_resm(TFAIL, "open succeeded "
130 "unexpectedly");
131 }
plars865695b2001-08-27 22:15:12 +0000132
Subrata Modak6e9c7352010-05-18 01:31:35 +0530133 if (TEST_ERRNO != TC[i].exp_errno) {
134 tst_resm(TFAIL, "open returned "
135 "unexpected errno, expected: "
136 "%d, got: %d",
137 TC[i].exp_errno, TEST_ERRNO);
138 } else {
139 tst_resm(TPASS, "open returned "
140 "expected ELOOP error");
141 }
plars865695b2001-08-27 22:15:12 +0000142 } else {
Subrata Modak6e9c7352010-05-18 01:31:35 +0530143 if (TEST_RETURN == -1) {
144 tst_resm(TFAIL, "open failed "
145 "unexpectedly with errno %d",
146 TEST_ERRNO);
147 } else {
148 tst_resm(TPASS, "open succeeded as "
149 "expected");
150 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 }
Wanlong Gao78d42772013-02-04 17:11:18 +0800152
Subrata Modak6e9c7352010-05-18 01:31:35 +0530153 if (TEST_RETURN != -1)
154 close(TEST_RETURN);
plars865695b2001-08-27 22:15:12 +0000155 }
156 }
Wanlong Gao78d42772013-02-04 17:11:18 +0800157
plars865695b2001-08-27 22:15:12 +0000158 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800159 tst_exit();
plars865695b2001-08-27 22:15:12 +0000160}
161
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162static void setupfunc_test1(void)
plars865695b2001-08-27 22:15:12 +0000163{
Subrata Modak6e9c7352010-05-18 01:31:35 +0530164 char file1[100], file2[100];
165
plars865695b2001-08-27 22:15:12 +0000166 sprintf(file1, "open03.1.%d", getpid());
167 sprintf(file2, "open03.2.%d", getpid());
Wanlong Gao78d42772013-02-04 17:11:18 +0800168 fd1 = creat(file1, 00700);
169 if (fd1 < 0)
plars865695b2001-08-27 22:15:12 +0000170 tst_brkm(TBROK, cleanup, "creat(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800171
172 if (symlink(file1, file2) < 0)
plars865695b2001-08-27 22:15:12 +0000173 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800174
Subrata Modak6e9c7352010-05-18 01:31:35 +0530175 strcpy(TC[0].filename, file2);
plars865695b2001-08-27 22:15:12 +0000176}
177
Mike Frysingerc57fba52014-04-09 18:56:30 -0400178static void setupfunc_test2(void)
plars865695b2001-08-27 22:15:12 +0000179{
Subrata Modak6e9c7352010-05-18 01:31:35 +0530180 char file1[100], file2[100];
181
plars865695b2001-08-27 22:15:12 +0000182 sprintf(file1, "open03.3.%d", getpid());
183 sprintf(file2, "open03.4.%d", getpid());
Wanlong Gao78d42772013-02-04 17:11:18 +0800184 if (mkdir(file1, 00700) < 0)
plars865695b2001-08-27 22:15:12 +0000185 tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800186
187 if (symlink(file1, file2) < 0)
plars865695b2001-08-27 22:15:12 +0000188 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800189
Subrata Modak6e9c7352010-05-18 01:31:35 +0530190 strcpy(TC[1].filename, file2);
plars865695b2001-08-27 22:15:12 +0000191}
192
Mike Frysingerc57fba52014-04-09 18:56:30 -0400193static void setupfunc_test3(void)
plars865695b2001-08-27 22:15:12 +0000194{
Subrata Modak6e9c7352010-05-18 01:31:35 +0530195 char file1[100], file2[100], file3[100];
196
plars865695b2001-08-27 22:15:12 +0000197 sprintf(file1, "open03.5.%d", getpid());
198 sprintf(file2, "open03.6.%d", getpid());
199 sprintf(file3, "open03.7.%d", getpid());
Wanlong Gao78d42772013-02-04 17:11:18 +0800200 fd2 = creat(file1, 00700);
201 if (fd2 < 0)
plars865695b2001-08-27 22:15:12 +0000202 tst_brkm(TBROK, cleanup, "creat(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800203
204 if (symlink(file1, file2) < 0)
plars865695b2001-08-27 22:15:12 +0000205 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800206
207 if (symlink(file2, file3) < 0)
plars865695b2001-08-27 22:15:12 +0000208 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800209
Subrata Modak6e9c7352010-05-18 01:31:35 +0530210 strcpy(TC[2].filename, file3);
plars865695b2001-08-27 22:15:12 +0000211}
212
Mike Frysingerc57fba52014-04-09 18:56:30 -0400213static void setupfunc_test4(void)
plars865695b2001-08-27 22:15:12 +0000214{
Subrata Modak6e9c7352010-05-18 01:31:35 +0530215 char file1[100], file2[100], file3[100];
216
plars865695b2001-08-27 22:15:12 +0000217 sprintf(file1, "open03.8.%d", getpid());
218 sprintf(file2, "open03.9.%d", getpid());
219 sprintf(file3, "open03.10.%d", getpid());
Wanlong Gao78d42772013-02-04 17:11:18 +0800220 if (mkdir(file1, 00700) < 0)
plars865695b2001-08-27 22:15:12 +0000221 tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800222
223 if (symlink(file1, file2) < 0)
plars865695b2001-08-27 22:15:12 +0000224 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800225
226 if (symlink(file2, file3) < 0)
plars865695b2001-08-27 22:15:12 +0000227 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800228
Subrata Modak6e9c7352010-05-18 01:31:35 +0530229 strcpy(TC[3].filename, file3);
230}
231
Mike Frysingerc57fba52014-04-09 18:56:30 -0400232static void setupfunc_test5(void)
Subrata Modak6e9c7352010-05-18 01:31:35 +0530233{
234 char file1[100], file2[100];
235
236 sprintf(file1, "open11.3.%d", getpid());
237 sprintf(file2, "open12.4.%d", getpid());
Wanlong Gao78d42772013-02-04 17:11:18 +0800238 if (mkdir(file1, 00700) < 0)
Subrata Modak6e9c7352010-05-18 01:31:35 +0530239 tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800240
241 if (symlink(file1, file2) < 0)
Subrata Modak6e9c7352010-05-18 01:31:35 +0530242 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
Wanlong Gao78d42772013-02-04 17:11:18 +0800243
Subrata Modak6e9c7352010-05-18 01:31:35 +0530244 strcpy(TC[4].filename, file2);
245 strcat(TC[4].filename, "/");
plars865695b2001-08-27 22:15:12 +0000246}
247
Wanlong Gao78d42772013-02-04 17:11:18 +0800248static void setup(void)
plars865695b2001-08-27 22:15:12 +0000249{
250 umask(0);
251
plars865695b2001-08-27 22:15:12 +0000252 tst_sig(NOFORK, DEF_HANDLER, cleanup);
253
plars865695b2001-08-27 22:15:12 +0000254 TEST_PAUSE;
255
plars865695b2001-08-27 22:15:12 +0000256 tst_tmpdir();
257}
258
Wanlong Gao78d42772013-02-04 17:11:18 +0800259static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000260{
subrata_modak8293edd2007-08-28 06:45:50 +0000261 close(fd1);
262 close(fd2);
plars865695b2001-08-27 22:15:12 +0000263
plars865695b2001-08-27 22:15:12 +0000264 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700265}