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 * NAME
22 * getcwd03
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of the getcwd(2) system call
26 * for symbolically linked directories.
27 *
28 * ALGORITHM
29 * This testcase checks for the functionality of the getcwd(2) system call
30 * on a symbolic link. First create a directory (dir1), and create a
31 * symbolic link (dir2) to it at the same directory level. Then, chdir()
32 * to dir1, and get the working directory (cwd1), and its pathname (pwd1).
33 * Then, chdir() to dir2, and get the working directory (cwd2), its
34 * pathname (pwd2), and its readlink info (link2).
35 * Testcase succeeds if:
36 * i. pwd1 == pwd2
37 * ii. cwd1 == cwd2
38 * iii. link2 == basename(cwd1)
39 *
40 * USAGE: <for command-line>
41 * getcwd03 [-c n] [-i n] [-I x] [-P x] [-t]
42 * where, -c n : Run n copies concurrently.
43 * -i n : Execute test n times.
44 * -I x : Execute test for x seconds.
45 * -P x : Pause for x seconds between iterations.
46 * -t : Turn on syscall timing.
47 *
48 * HISTORY
49 * 07/2001 Ported by Wayne Boyer
50 *
51 * RESTRICTIONS
52 * NONE
53 */
54 #define _GNU_SOURCE 1
55 #include <stdio.h>
56 #include <string.h>
57 #include <errno.h>
58 #include "test.h"
59 #include <stdlib.h>
60 #include <sys/stat.h>
61 #include <sys/types.h>
62 #include <stdlib.h>
63 #define FAILED 1
64
65 int flag;
66 char *TCID = "getcwd03";
67 int TST_TOTAL = 1;
68
69 void cleanup(void);
70 void setup(void);
71 char *getpwd(void);
72
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 char dir1[BUFSIZ], dir2[BUFSIZ];
76 char cwd1[BUFSIZ], cwd2[BUFSIZ];
77 char *pwd1, *pwd2;
78 char link2[BUFSIZ];
79 int n;
80 int lc;
81
82 tst_parse_opts(ac, av, NULL, NULL);
83
84 setup();
85
86 /*
87 * The following loop checks looping state if -i option given
88 */
89 for (lc = 0; TEST_LOOPING(lc); lc++) {
90 tst_count = 0;
91
92 flag = 0;
93
94 /*
95 * Create dir1, then chdir to dir1, and get the pwd,
96 * and cwd informations
97 */
98 sprintf(dir1, "getcwd1.%d", getpid());
99 if (mkdir(dir1, 00755) < 0) {
100 tst_brkm(TBROK, cleanup, "mkdir(2) failed");
101 }
102 if (chdir(dir1) != 0) {
103 tst_brkm(TBROK, cleanup, "chdir(2) failed");
104 }
105
106 pwd1 = getpwd();
107 if (getcwd(cwd1, sizeof cwd1) == NULL) {
108 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly");
109 flag = FAILED;
110 }
111 if ((flag != FAILED) && (strcmp(pwd1, cwd1) != 0)) {
112 tst_brkm(TFAIL, cleanup, "getcwd() returned unexpected "
113 "working directory: expected: %s, got: %s\n",
114 pwd1, cwd1);
115 }
116
117 tst_resm(TINFO, "getcwd(2) succeeded in returning correct path "
118 "for dir1");
119
120 /*
121 * Now create dir2, then chdir to dir2, and get the pwd,
122 * cwd, and link informations
123 */
124 chdir("..");
125 flag = 0;
126
127 sprintf(dir2, "getcwd2.%d", getpid());
128 if (symlink(dir1, dir2) < 0) {
129 tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d",
130 errno);
131 }
132
133 if (chdir(dir2) != 0) {
134 tst_brkm(TBROK, cleanup, "chdir(2) failed: errno: %d",
135 errno);
136 }
137
138 pwd2 = getpwd();
139 if (getcwd(cwd2, sizeof cwd2) == NULL) {
140 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly");
141 flag = FAILED;
142 }
143
144 chdir("..");
145 if ((flag != FAILED) &&
146 ((n = readlink(dir2, link2, sizeof(link2))) < 0)) {
147 tst_brkm(TBROK, cleanup, "readlink(2) failed: errno:%d",
148 errno);
149 }
150
151 /*
152 * Finally compare the pwd, cwd, link informations:
153 * The test should pass iff all the following are true:
154 * a. pwd1 == pwd2
155 * b. cwd1 == cwd2
156 * c. link2 == basename(cwd1)
157 */
158 if (flag != FAILED) {
159 if (strcmp(pwd1, pwd2) != 0) {
160 tst_resm(TFAIL, "pwd1: %s, pwd2: %s",
161 pwd1, pwd2);
162 flag = FAILED;
163 }
164 if (strcmp(cwd1, cwd2) != 0) {
165 tst_resm(TFAIL, "cwd1: %s, cwd2: %s",
166 cwd1, cwd2);
167 flag = FAILED;
168 }
169 if (memcmp(link2, (char *)basename(cwd1), n) != 0) {
170 tst_resm(TFAIL, "link2: %s, cwd1: %s",
171 link2, cwd1);
172 flag = FAILED;
173 }
174 if (flag != FAILED) {
175 tst_resm(TINFO, "getcwd(2) succeeded in "
176 "returning correct path for symbolic "
177 "link dir2 -> dir1");
178 }
179 }
180
181 if (flag == FAILED) {
182 tst_resm(TFAIL, "Test FAILED");
183 } else {
184 tst_resm(TPASS, "Test PASSED");
185 }
186
187 /* clean up things in case we are looping */
188 if (unlink(dir2) == -1) {
189 tst_brkm(TBROK, cleanup, "couldnt remove dir2");
190 }
191 if (rmdir(dir1) == -1) {
192 tst_brkm(TBROK, cleanup, "couldnt remove dir1");
193 }
194 }
195 cleanup();
196
197 tst_exit();
198 }
199
setup(void)200 void setup(void)
201 {
202 /* FORK is set here because of the popen() call below */
203 tst_sig(FORK, DEF_HANDLER, cleanup);
204
205 TEST_PAUSE;
206
207 /* create a test directory and cd into it */
208 tst_tmpdir();
209 }
210
cleanup(void)211 void cleanup(void)
212 {
213 /* remove the test directory */
214 tst_rmdir();
215 }
216
getpwd(void)217 char *getpwd(void)
218 {
219 FILE *fin;
220 #ifdef ANDROID
221 char *pwd = "/system/bin/pwd";
222 #else
223 char *pwd = "/bin/pwd";
224 #endif
225 char *cp;
226 char *buf;
227
228 buf = malloc(BUFSIZ);
229 if ((fin = popen(pwd, "r")) == NULL) {
230 tst_resm(TINFO, "%s: can't run %s", TCID, pwd);
231 tst_brkm(TBROK, cleanup, "%s FAILED", TCID);
232 }
233 while (fgets(buf, BUFSIZ, fin) != NULL) {
234 if ((cp = strchr(buf, '\n')) == NULL) {
235 tst_brkm(TBROK, cleanup, "pwd output too long");
236 }
237 *cp = 0;
238 }
239 pclose(fin);
240 return buf;
241 }
242