plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | */ |
robbiew | 1d27cce | 2002-04-03 15:44:30 +0000 | [diff] [blame] | 54 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | #include <stdio.h> |
| 56 | #include <string.h> |
| 57 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 58 | #include "test.h" |
robbiew | 1d27cce | 2002-04-03 15:44:30 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
robbiew | 5832f9b | 2002-08-12 14:57:23 +0000 | [diff] [blame] | 60 | #include <sys/stat.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <stdlib.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | #define FAILED 1 |
| 64 | |
| 65 | int flag; |
| 66 | char *TCID = "getcwd03"; |
| 67 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
| 69 | void cleanup(void); |
| 70 | void setup(void); |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 71 | char *getpwd(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | { |
| 75 | char dir1[BUFSIZ], dir2[BUFSIZ]; |
| 76 | char cwd1[BUFSIZ], cwd2[BUFSIZ]; |
| 77 | char *pwd1, *pwd2; |
| 78 | char link2[BUFSIZ]; |
| 79 | int n; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; /* parse_opts() return message */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 83 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | setup(); |
| 88 | |
| 89 | /* |
| 90 | * The following loop checks looping state if -i option given |
| 91 | */ |
| 92 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 95 | flag = 0; |
| 96 | |
| 97 | /* |
| 98 | * Create dir1, then chdir to dir1, and get the pwd, |
| 99 | * and cwd informations |
| 100 | */ |
| 101 | sprintf(dir1, "getcwd1.%d", getpid()); |
| 102 | if (mkdir(dir1, 00755) < 0) { |
| 103 | tst_brkm(TBROK, cleanup, "mkdir(2) failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | if (chdir(dir1) != 0) { |
| 106 | tst_brkm(TBROK, cleanup, "chdir(2) failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | pwd1 = getpwd(); |
| 110 | if (getcwd(cwd1, sizeof cwd1) == NULL) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 111 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | flag = FAILED; |
| 113 | } |
| 114 | if ((flag != FAILED) && (strcmp(pwd1, cwd1) != 0)) { |
| 115 | tst_brkm(TFAIL, cleanup, "getcwd() returned unexpected " |
| 116 | "working directory: expected: %s, got: %s\n", |
| 117 | pwd1, cwd1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
| 120 | tst_resm(TINFO, "getcwd(2) succeeded in returning correct path " |
| 121 | "for dir1"); |
| 122 | |
| 123 | /* |
| 124 | * Now create dir2, then chdir to dir2, and get the pwd, |
| 125 | * cwd, and link informations |
| 126 | */ |
| 127 | chdir(".."); |
| 128 | flag = 0; |
| 129 | |
| 130 | sprintf(dir2, "getcwd2.%d", getpid()); |
| 131 | if (symlink(dir1, dir2) < 0) { |
| 132 | tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", |
| 133 | errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
| 136 | if (chdir(dir2) != 0) { |
| 137 | tst_brkm(TBROK, cleanup, "chdir(2) failed: errno: %d", |
| 138 | errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
| 141 | pwd2 = getpwd(); |
| 142 | if (getcwd(cwd2, sizeof cwd2) == NULL) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 143 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | flag = FAILED; |
| 145 | } |
| 146 | |
| 147 | chdir(".."); |
| 148 | if ((flag != FAILED) && |
| 149 | ((n = readlink(dir2, link2, sizeof(link2))) < 0)) { |
| 150 | tst_brkm(TBROK, cleanup, "readlink(2) failed: errno:%d", |
| 151 | errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Finally compare the pwd, cwd, link informations: |
| 156 | * The test should pass iff all the following are true: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | * a. pwd1 == pwd2 |
| 158 | * b. cwd1 == cwd2 |
| 159 | * c. link2 == basename(cwd1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | */ |
| 161 | if (flag != FAILED) { |
| 162 | if (strcmp(pwd1, pwd2) != 0) { |
| 163 | tst_resm(TFAIL, "pwd1: %s, pwd2: %s", |
| 164 | pwd1, pwd2); |
| 165 | flag = FAILED; |
| 166 | } |
| 167 | if (strcmp(cwd1, cwd2) != 0) { |
| 168 | tst_resm(TFAIL, "cwd1: %s, cwd2: %s", |
| 169 | cwd1, cwd2); |
| 170 | flag = FAILED; |
| 171 | } |
| 172 | if (memcmp(link2, (char *)basename(cwd1), n) != 0) { |
| 173 | tst_resm(TFAIL, "link2: %s, cwd1: %s", |
| 174 | link2, cwd1); |
| 175 | flag = FAILED; |
| 176 | } |
| 177 | if (flag != FAILED) { |
| 178 | tst_resm(TINFO, "getcwd(2) succeeded in " |
| 179 | "returning correct path for symbolic " |
| 180 | "link dir2 -> dir1"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (flag == FAILED) { |
| 185 | tst_resm(TFAIL, "Test FAILED"); |
| 186 | } else { |
| 187 | tst_resm(TPASS, "Test PASSED"); |
| 188 | } |
| 189 | |
| 190 | /* clean up things in case we are looping */ |
| 191 | if (unlink(dir2) == -1) { |
| 192 | tst_brkm(TBROK, cleanup, "couldnt remove dir2"); |
| 193 | } |
| 194 | if (rmdir(dir1) == -1) { |
| 195 | tst_brkm(TBROK, cleanup, "couldnt remove dir1"); |
| 196 | } |
| 197 | } |
| 198 | cleanup(); |
| 199 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 200 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 203 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | /* FORK is set here because of the popen() call below */ |
| 206 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | TEST_PAUSE; |
| 209 | |
| 210 | /* create a test directory and cd into it */ |
| 211 | tst_tmpdir(); |
| 212 | } |
| 213 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 214 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | { |
| 216 | /* remove the test directory */ |
| 217 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 220 | char *getpwd(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | { |
robbiew | 8957725 | 2004-01-05 20:15:51 +0000 | [diff] [blame] | 222 | FILE *fin; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | char *pwd = "/bin/pwd"; |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 224 | char *cp; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | char *buf; |
| 226 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 227 | buf = malloc(BUFSIZ); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | if ((fin = popen(pwd, "r")) == NULL) { |
vapier | cff4af0 | 2006-02-11 04:46:30 +0000 | [diff] [blame] | 229 | tst_resm(TINFO, "%s: can't run %s", TCID, pwd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | tst_brkm(TBROK, cleanup, "%s FAILED", TCID); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 231 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 232 | while (fgets(buf, BUFSIZ, fin) != NULL) { |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 233 | if ((cp = strchr(buf, '\n')) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | tst_brkm(TBROK, cleanup, "pwd output too long"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | *cp = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | } |
| 238 | pclose(fin); |
| 239 | return buf; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 240 | } |