blob: 468f7cf119b4f3d412837984738d7149557111d0 [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 : readlink01
22 *
23 * Test Description :
24 * Verify that, readlink will succeed to read the contents of the symbolic
25 * link created the process.
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * Expected Result:
28 * readlink() should return the contents of symbolic link path in the buffer
29 * on success.
30 *
31 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Create temporary directory.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
41 * Issue a FAIL message.
42 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
45 * Issue Functionality-Pass message.
46 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 * Delete the temporary directory created.
51 *
52 * Usage: <for command-line>
53 * readlink01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * This test should be run by 'non-super-user' only.
66 *
67 */
68#include <unistd.h>
69#include <fcntl.h>
70#include <errno.h>
71#include <sys/stat.h>
robbiewf44201d2001-09-25 16:07:44 +000072#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000073
74#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
76#define TESTFILE "testfile"
77#define SYMFILE "slink_file"
78#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
79#define MAX_SIZE 256
80
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020081char *TCID = "readlink01";
82int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000083
84int exp_val; /* strlen of testfile */
85
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020086void setup();
87void cleanup();
plars865695b2001-08-27 22:15:12 +000088
robbiewf44201d2001-09-25 16:07:44 +000089char nobody_uid[] = "nobody";
90struct passwd *ltpuser;
91
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
subrata_modak56207ce2009-03-23 13:35:39 +000094 char buffer[MAX_SIZE]; /* temporary buffer to hold symlink contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020096 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000097
Garrett Cooper53740502010-12-16 00:04:01 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000100
plars865695b2001-08-27 22:15:12 +0000101 setup();
102
plars865695b2001-08-27 22:15:12 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
104
Caspar Zhangd59a6592013-03-07 14:59:12 +0800105 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000106
subrata_modak4bb656a2009-02-26 12:02:09 +0000107 /*
plars865695b2001-08-27 22:15:12 +0000108 * Call readlink(2) to read the contents of
109 * symlink into a buffer.
110 */
111 TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
subrata_modakbdbaec52009-02-26 12:14:51 +0000112
plars865695b2001-08-27 22:15:12 +0000113 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000114 tst_resm(TFAIL,
115 "readlink() on %s failed, errno=%d : %s",
116 SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000117 continue;
118 }
119
120 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 * Compare the return value of readlink()
122 * with the expected value which is the
123 * strlen() of testfile.
plars865695b2001-08-27 22:15:12 +0000124 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200125 if (TEST_RETURN == exp_val) {
126 /* Check for the contents of buffer */
127 if (memcmp(buffer, TESTFILE, exp_val) != 0) {
128 tst_resm(TFAIL, "Pathname %s and buffer"
129 " contents %s differ",
130 TESTFILE, buffer);
plars865695b2001-08-27 22:15:12 +0000131 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200132 tst_resm(TPASS, "readlink() "
133 "functionality on '%s' is "
134 "correct", SYMFILE);
plars865695b2001-08-27 22:15:12 +0000135 }
136 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 tst_resm(TFAIL, "readlink() return value %ld "
138 "does't match, Expected %d",
139 TEST_RETURN, exp_val);
plars865695b2001-08-27 22:15:12 +0000140 }
Garrett Cooper53740502010-12-16 00:04:01 -0800141 }
plars865695b2001-08-27 22:15:12 +0000142
plars865695b2001-08-27 22:15:12 +0000143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
Garrett Cooper53740502010-12-16 00:04:01 -0800145}
plars865695b2001-08-27 22:15:12 +0000146
147/*
148 * setup() - performs all ONE TIME setup for this test.
149 *
150 * Create a temporary directory and change directory to it.
151 * Create a test file under temporary directory and close it
152 * Create a symbolic link of testfile.
153 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400154void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
156 int fd; /* file handle for testfile */
157
Garrett Cooper53740502010-12-16 00:04:01 -0800158 tst_require_root(NULL);
159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
mridge75cee4f2004-09-08 15:31:00 +0000161 tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
subrata_modak56207ce2009-03-23 13:35:39 +0000162 }
163 if (seteuid(ltpuser->pw_uid) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000165 }
plars865695b2001-08-27 22:15:12 +0000166
plars865695b2001-08-27 22:15:12 +0000167 tst_sig(NOFORK, DEF_HANDLER, cleanup);
168
plars865695b2001-08-27 22:15:12 +0000169 TEST_PAUSE;
170
plars865695b2001-08-27 22:15:12 +0000171 tst_tmpdir();
172
subrata_modak56207ce2009-03-23 13:35:39 +0000173 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper53740502010-12-16 00:04:01 -0800175 "open(%s, O_RDWR|O_CREAT, %#o) failed",
176 TESTFILE, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000177 }
178
179 if (close(fd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_resm(TWARN | TERRNO, "close(%s) failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000181 }
182
183 /* Create a symlink of testfile under temporary directory */
184 if (symlink(TESTFILE, SYMFILE) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_brkm(TBROK | TERRNO, cleanup, "symlink(%s, %s) failed",
186 TESTFILE, SYMFILE);
plars865695b2001-08-27 22:15:12 +0000187 }
188
189 /* Get the strlen of testfile */
190 exp_val = strlen(TESTFILE);
191}
192
193/*
194 * cleanup() - performs all ONE TIME cleanup for this test at
195 * completion or premature exit.
196 *
197 * Remove the test directory and testfile created in the setup.
198 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400199void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000200{
plars865695b2001-08-27 22:15:12 +0000201
plars865695b2001-08-27 22:15:12 +0000202 tst_rmdir();
203
Chris Dearmanec6edca2012-10-17 19:54:01 -0700204}