blob: 5df790f3797a6d1182771e6943a6c63b86b7d473 [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 * Test Name : symlink03
22 *
23 * Test Description :
24 * Verify that,
25 * 1) symlink(2) returns -1 and sets errno to EACCES if search/write
26 * permission is denied in the directory where the symbolic link is
27 * being created.
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * 2) symlink(2) returns -1 and sets errno to EEXIST if the specified
plars865695b2001-08-27 22:15:12 +000029 * symbolic link already exists.
30 * 3) symlink(2) returns -1 and sets errno to EFAULT if the specified
31 * file or symbolic link points to invalid address.
subrata_modak4bb656a2009-02-26 12:02:09 +000032 * 4) symlink(2) returns -1 and sets errno to ENAMETOOLONG if the
plars865695b2001-08-27 22:15:12 +000033 * pathname component of symbolic link is too long (ie, > PATH_MAX).
34 * 5) symlink(2) returns -1 and sets errno to ENOTDIR if the directory
35 * component in pathname of symbolic link is not a directory.
36 * 6) symlink(2) returns -1 and sets errno to ENOENT if the component of
37 * symbolic link points to an empty string.
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
plars865695b2001-08-27 22:15:12 +000039 * Expected Result:
40 * symlink() should fail with return value -1 and set expected errno.
41 *
42 * Algorithm:
43 * Setup:
44 * Setup signal handling.
45 * Create temporary directory.
46 * Pause for SIGUSR1 if option specified.
47 *
48 * Test:
49 * Loop if the proper options are given.
50 * Execute system call
51 * Check return code, if system call failed (return=-1)
52 * if errno set == expected errno
53 * Issue sys call fails with expected return value and errno.
54 * Otherwise,
55 * Issue sys call fails with unexpected errno.
56 * Otherwise,
57 * Issue sys call returns unexpected value.
58 *
59 * Cleanup:
60 * Print errno log and/or timing stats if options given
61 * Delete the temporary directory(s)/file(s) created.
62 *
63 * Usage: <for command-line>
64 * symlink03 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
65 * where, -c n : Run n copies concurrently.
66 * -e : Turn on errno logging.
67 * -i n : Execute test n times.
68 * -I x : Execute test for x seconds.
69 * -P x : Pause for x seconds between iterations.
70 * -t : Turn on syscall timing.
71 *
72 * History
73 * 07/2001 John George
74 * -Ported
75 *
76 * Restrictions:
plars865695b2001-08-27 22:15:12 +000077 */
78
79#include <stdio.h>
80#include <sys/types.h>
81#include <sys/fcntl.h>
plars1ad84512002-07-23 13:11:18 +000082#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000083#include <errno.h>
84#include <string.h>
85#include <signal.h>
86#include <sys/stat.h>
robbiew8b962f32001-09-13 20:30:40 +000087#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000088
89#include "test.h"
plars865695b2001-08-27 22:15:12 +000090
91#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
92#define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH
93#define DIR_TEMP "testdir_1"
94#define TESTFILE "testfile"
95#define TEST_FILE1 "testdir_1/tfile_1"
96#define SYM_FILE1 "testdir_1/sfile_1"
97#define TEST_FILE2 "tfile_2"
98#define SYM_FILE2 "sfile_2"
99#define TEST_FILE3 "tfile_3"
100#define SYM_FILE3 "t_file/sfile_3"
101
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200102char *TCID = "symlink03";
103int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000106
plars865695b2001-08-27 22:15:12 +0000107int no_setup();
subrata_modak56207ce2009-03-23 13:35:39 +0000108int setup1(); /* setup function to test symlink for EACCES */
109int setup2(); /* setup function to test symlink for EEXIST */
110int setup3(); /* setup function to test symlink for ENOTDIR */
111int longpath_setup(); /* setup function to test chmod for ENAMETOOLONG */
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +0000114char High_address_node[64];
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000117 char *file;
118 char *link;
119 char *desc;
120 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000121 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000122} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000123 {
124 TEST_FILE1, SYM_FILE1, "No Search permissions to process",
125 EACCES, setup1}, {
126 TEST_FILE2, SYM_FILE2, "Specified symlink already exists",
127 EEXIST, setup2},
robbiewd34d5812005-07-11 22:28:09 +0000128#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000129 {
130 TESTFILE, High_address_node, "Address beyond address space",
131 EFAULT, no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000132#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000133 {
134 TESTFILE, (char *)-1, "Negative address", EFAULT, no_setup}, {
135 TESTFILE, Longpathname, "Symlink path too long", ENAMETOOLONG,
136 longpath_setup}, {
137 TESTFILE, "", "Symlink Pathname is empty", ENOENT, no_setup}, {
138 TEST_FILE3, SYM_FILE3, "Symlink Path contains regular file",
139 ENOTDIR, setup3}, {
140 NULL, NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000141};
142
robbiew8b962f32001-09-13 20:30:40 +0000143char nobody_uid[] = "nobody";
144struct passwd *ltpuser;
145
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200146void setup();
147void cleanup();
plars865695b2001-08-27 22:15:12 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000150{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200151 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200152 const char *msg;
plars865695b2001-08-27 22:15:12 +0000153 char *test_file; /* testfile name */
154 char *sym_file; /* symbolic link file name */
155 char *test_desc; /* test specific error message */
156 int ind; /* counter to test different test conditions */
157
Garrett Cooper45e285d2010-11-22 12:19:25 -0800158 msg = parse_opts(ac, av, NULL, NULL);
159 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000160 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800161
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 }
plars865695b2001-08-27 22:15:12 +0000163
164 /*
165 * Invoke setup function to call individual test setup functions
166 * to simulate test conditions.
167 */
168 setup();
169
plars865695b2001-08-27 22:15:12 +0000170 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800171
Caspar Zhangd59a6592013-03-07 14:59:12 +0800172 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000173
plars865695b2001-08-27 22:15:12 +0000174 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
175 test_file = Test_cases[ind].file;
176 sym_file = Test_cases[ind].link;
177 test_desc = Test_cases[ind].desc;
robbiewd34d5812005-07-11 22:28:09 +0000178#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000179 if (sym_file == High_address_node) {
180 sym_file = (char *)get_high_address();
181 }
robbiewd34d5812005-07-11 22:28:09 +0000182#endif
subrata_modak4bb656a2009-02-26 12:02:09 +0000183 /*
plars865695b2001-08-27 22:15:12 +0000184 * Call symlink(2) to test different test conditions.
subrata_modak56207ce2009-03-23 13:35:39 +0000185 * verify that it fails with -1 return value and sets
plars865695b2001-08-27 22:15:12 +0000186 * appropriate errno.
187 */
188 TEST(symlink(test_file, sym_file));
subrata_modakbdbaec52009-02-26 12:14:51 +0000189
plars865695b2001-08-27 22:15:12 +0000190 if (TEST_RETURN == -1) {
191 /*
192 * Perform functional verification if
193 * test executed without (-f) option.
194 */
plars865695b2001-08-27 22:15:12 +0000195 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
196 tst_resm(TPASS, "symlink() Fails, %s, "
subrata_modak56207ce2009-03-23 13:35:39 +0000197 "errno=%d", test_desc,
198 TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000199 } else {
200 tst_resm(TFAIL, "symlink() Fails, %s, "
subrata_modak56207ce2009-03-23 13:35:39 +0000201 "errno=%d, expected errno=%d",
202 test_desc, TEST_ERRNO,
203 Test_cases[ind].exp_errno);
plars865695b2001-08-27 22:15:12 +0000204 }
205 } else {
vapiere5fce932009-08-28 14:17:14 +0000206 tst_resm(TFAIL, "symlink() returned %ld, "
subrata_modak56207ce2009-03-23 13:35:39 +0000207 "expected -1, errno:%d", TEST_RETURN,
208 Test_cases[ind].exp_errno);
plars865695b2001-08-27 22:15:12 +0000209 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800210 }
plars865695b2001-08-27 22:15:12 +0000211
Caspar Zhangd59a6592013-03-07 14:59:12 +0800212 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800213 }
plars865695b2001-08-27 22:15:12 +0000214
plars865695b2001-08-27 22:15:12 +0000215 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800216 tst_exit();
plars865695b2001-08-27 22:15:12 +0000217
Garrett Cooper2c282152010-12-16 00:55:50 -0800218}
plars865695b2001-08-27 22:15:12 +0000219
220/*
221 * void
222 * setup() - performs all ONE TIME setup for this test.
223 * Create a temporary directory and change directory to it.
224 * Call test specific setup functions.
225 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400226void setup(void)
plars865695b2001-08-27 22:15:12 +0000227{
228 int ind;
Garrett Cooper2c282152010-12-16 00:55:50 -0800229
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200230 tst_require_root(NULL);
231
plars865695b2001-08-27 22:15:12 +0000232 tst_sig(NOFORK, DEF_HANDLER, cleanup);
233
234 /* Pause if that option was specified
235 * TEST_PAUSE contains the code to fork the test with the -i option.
236 * You want to make sure you do this before you create your temporary
237 * directory.
238 */
239 TEST_PAUSE;
240
robbiew8b962f32001-09-13 20:30:40 +0000241 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000242 ltpuser = getpwnam(nobody_uid);
vapiere5fce932009-08-28 14:17:14 +0000243 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 tst_resm(TINFO | TERRNO, "setuid(%d) failed", ltpuser->pw_uid);
robbiew8b962f32001-09-13 20:30:40 +0000245
plars865695b2001-08-27 22:15:12 +0000246 tst_tmpdir();
247
vapier62b16cf2007-02-09 20:48:23 +0000248#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000249 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000250 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000251 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000252 tst_brkm(TBROK, cleanup, "mmap failed");
253 }
254 Test_cases[3].link = bad_addr;
robbiew6b68d222005-12-22 20:28:14 +0000255#endif
vapier62b16cf2007-02-09 20:48:23 +0000256
plars865695b2001-08-27 22:15:12 +0000257 /* call individual setup functions */
258 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
259 Test_cases[ind].setupfunc();
260 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800261}
plars865695b2001-08-27 22:15:12 +0000262
263/*
264 * int
265 * no_setup() - Some test conditions for mknod(2) do not any setup.
266 * Hence, this function just returns 0.
267 * This function simply returns 0.
268 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400269int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000270{
subrata_modak56207ce2009-03-23 13:35:39 +0000271 return 0;
plars865695b2001-08-27 22:15:12 +0000272}
273
274/*
275 * int
276 * setup1() - setup function for a test condition for which symlink(2)
277 * returns -1 and sets errno to EACCES.
278 * Create a test directory under temporary directory and create a test file
279 * under this directory with mode "0666" permissions.
280 * Modify the mode permissions on test directory such that process will not
281 * have search permissions on test directory.
282 *
283 * The function returns 0.
284 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400285int setup1(void)
plars865695b2001-08-27 22:15:12 +0000286{
subrata_modak56207ce2009-03-23 13:35:39 +0000287 int fd; /* file handle for testfile */
plars865695b2001-08-27 22:15:12 +0000288
subrata_modak56207ce2009-03-23 13:35:39 +0000289 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
290 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800291 }
plars865695b2001-08-27 22:15:12 +0000292
subrata_modak56207ce2009-03-23 13:35:39 +0000293 if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) {
294 tst_brkm(TBROK, cleanup,
295 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
296 TEST_FILE1, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800297 }
subrata_modak56207ce2009-03-23 13:35:39 +0000298 if (close(fd) == -1) {
299 tst_brkm(TBROK, cleanup,
300 "close(%s) Failed, errno=%d : %s",
301 TEST_FILE1, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800302 }
plars865695b2001-08-27 22:15:12 +0000303
304 /* Modify mode permissions on test directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000305 if (chmod(DIR_TEMP, FILE_MODE) < 0) {
306 tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800307 }
subrata_modak56207ce2009-03-23 13:35:39 +0000308 return 0;
plars865695b2001-08-27 22:15:12 +0000309}
310
311/*
312 * int
313 * setup2() - EEXIST
314 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400315int setup2(void)
plars865695b2001-08-27 22:15:12 +0000316{
317 int fd; /* file handle for testfile */
318
subrata_modak56207ce2009-03-23 13:35:39 +0000319 if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1) {
320 tst_brkm(TBROK, cleanup,
321 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
322 TEST_FILE1, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323 }
subrata_modak56207ce2009-03-23 13:35:39 +0000324 if (close(fd) == -1) {
325 tst_brkm(TBROK, cleanup,
326 "close(%s) Failed, errno=%d : %s",
327 TEST_FILE2, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800328 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000329
plars865695b2001-08-27 22:15:12 +0000330 if (symlink(TEST_FILE2, SYM_FILE2) < 0) {
331 tst_brkm(TBROK, cleanup,
332 "symlink() Fails to create %s in setup2, error=%d",
333 SYM_FILE2, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800334 }
plars865695b2001-08-27 22:15:12 +0000335 return 0;
336}
337
338/*
339 * int
340 * longpath_setup() - setup to create a node with a name length exceeding
341 * the MAX. length of PATH_MAX.
342 * This function retruns 0.
343 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400344int longpath_setup(void)
plars865695b2001-08-27 22:15:12 +0000345{
subrata_modak56207ce2009-03-23 13:35:39 +0000346 int ind; /* counter variable */
plars865695b2001-08-27 22:15:12 +0000347
348 for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
349 Longpathname[ind] = 'a';
350 }
351 return 0;
352}
353
354/*
355 * int
356 * setup3() - setup function for a test condition for which symlink(2)
357 * returns -1 and sets errno to ENOTDIR.
358 *
359 * Create a symlink file under temporary directory so that test tries to
360 * create symlink file "tfile_3" under "t_file" which happens to be
361 * another symlink file.
362 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400363int setup3(void)
plars865695b2001-08-27 22:15:12 +0000364{
365 int fd; /* file handle for testfile */
366
367 /* Creat/open a testfile and close it */
subrata_modak56207ce2009-03-23 13:35:39 +0000368 if ((fd = open("t_file", O_RDWR | O_CREAT, MODE_RWX)) == -1) {
plars865695b2001-08-27 22:15:12 +0000369 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000370 "open(2) on t_file failed, errno=%d : %s",
371 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800372 }
plars865695b2001-08-27 22:15:12 +0000373 if (close(fd) == -1) {
374 tst_brkm(TBROK, cleanup, "close(t_file) Failed, errno=%d : %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000375 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800376 }
plars865695b2001-08-27 22:15:12 +0000377 return 0;
378}
379
380/*
381 * void
382 * cleanup() - performs all ONE TIME cleanup for this test at
383 * completion or premature exit.
384 * Restore the mode permissions on test directory.
385 * Remove the temporary directory created in the setup.
386 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400387void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000388{
plars865695b2001-08-27 22:15:12 +0000389
390 /* Restore mode permissions on test directory created in setup2() */
391 if (chmod(DIR_TEMP, MODE_RWX) < 0) {
392 tst_brkm(TBROK, NULL, "chmod(2) of %s failed", DIR_TEMP);
393 }
394
plars865695b2001-08-27 22:15:12 +0000395 tst_rmdir();
396
Chris Dearmanec6edca2012-10-17 19:54:01 -0700397}