1/*
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
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
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.
26 *
27 * 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,
43 *   	Verify the Functionality of system call
44 *      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>
72#include <pwd.h>
73
74#include "test.h"
75
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
81char *TCID = "readlink01";
82int TST_TOTAL = 1;
83
84const int exp_val = sizeof(TESTFILE) - 1;	/* strlen of testfile */
85
86void setup();
87void cleanup();
88
89char nobody_uid[] = "nobody";
90struct passwd *ltpuser;
91
92int main(int ac, char **av)
93{
94	char buffer[MAX_SIZE];	/* temporary buffer to hold symlink contents */
95	int lc;
96
97	tst_parse_opts(ac, av, NULL, NULL);
98
99	setup();
100
101	for (lc = 0; TEST_LOOPING(lc); lc++) {
102
103		tst_count = 0;
104
105		/*
106		 * Call readlink(2) to read the contents of
107		 * symlink into a buffer.
108		 */
109		TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
110
111		if (TEST_RETURN == -1) {
112			tst_resm(TFAIL,
113				 "readlink() on %s failed, errno=%d : %s",
114				 SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO));
115			continue;
116		}
117
118		/*
119		 * Compare the return value of readlink()
120		 * with the expected value which is the
121		 * strlen() of testfile.
122		 */
123		if (TEST_RETURN == exp_val) {
124			/* Check for the contents of buffer */
125			if (memcmp(buffer, TESTFILE, exp_val) != 0) {
126				tst_resm(TFAIL, "Pathname %s and buffer"
127					 " contents %s differ",
128					 TESTFILE, buffer);
129			} else {
130				tst_resm(TPASS, "readlink() "
131					 "functionality on '%s' is "
132					 "correct", SYMFILE);
133			}
134		} else {
135			tst_resm(TFAIL, "readlink() return value %ld "
136				 "does't match, Expected %d",
137				 TEST_RETURN, exp_val);
138		}
139	}
140
141	cleanup();
142	tst_exit();
143}
144
145/*
146 * setup() - performs all ONE TIME setup for this test.
147 *
148 *  Create a temporary directory and change directory to it.
149 *  Create a test file under temporary directory and close it
150 *  Create a symbolic link of testfile.
151 */
152void setup(void)
153{
154	int fd;			/* file handle for testfile */
155
156	tst_require_root();
157
158	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
159		tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
160	}
161	if (seteuid(ltpuser->pw_uid) == -1) {
162		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
163	}
164
165	tst_sig(NOFORK, DEF_HANDLER, cleanup);
166
167	TEST_PAUSE;
168
169	tst_tmpdir();
170
171	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
172		tst_brkm(TBROK | TERRNO, cleanup,
173			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
174			 TESTFILE, FILE_MODE);
175	}
176
177	if (close(fd) == -1) {
178		tst_resm(TWARN | TERRNO, "close(%s) failed", TESTFILE);
179	}
180
181	/* Create a symlink of testfile under temporary directory */
182	if (symlink(TESTFILE, SYMFILE) < 0) {
183		tst_brkm(TBROK | TERRNO, cleanup, "symlink(%s, %s) failed",
184			 TESTFILE, SYMFILE);
185	}
186}
187
188/*
189 * cleanup() - performs all ONE TIME cleanup for this test at
190 *             completion or premature exit.
191 *
192 *  Remove the test directory and testfile created in the setup.
193 */
194void cleanup(void)
195{
196
197	tst_rmdir();
198
199}
200