Home | History | Annotate | Line # | Download | only in getcwd
getcwd.c revision 1.3
      1 /* $NetBSD: getcwd.c,v 1.3 1999/03/26 22:23:58 sommerfe Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * test SYS___getcwd.
     41  */
     42 
     43 #include <assert.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <pwd.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <time.h>
     51 #include <unistd.h>
     52 
     53 #include <sys/param.h>		/* for MAXPATHLEN */
     54 #include <sys/types.h>
     55 #include <sys/stat.h>
     56 #include <sys/wait.h>
     57 
     58 #include "getcwd.h"
     59 
     60 int	main __P((int, char *[]));
     61 
     62 static void check1 __P((char *dir, char *buf, char *calltext,
     63     int actual, int expected, int experr));
     64 
     65 static void time_old_getcwd __P((void));
     66 static void time_kern_getcwd __P((void));
     67 static void time_func __P((char *name,
     68     void (*func)(void)));
     69 
     70 static void test_speed __P((void));
     71 static void test___getcwd  __P((void));
     72 static void test___getcwd_perms  __P((void));
     73 static void test___getcwd_chroot __P((void));
     74 
     75 static void stress_test_getcwd __P((void));
     76 static void usage __P((char *progname));
     77 
     78 /* libc-private interface */
     79 int __getcwd __P((char *, size_t));
     80 
     81 /*
     82  * test cases:
     83  * 	NULL pointer
     84  *	broken pointer
     85  * 	zero-length buffer
     86  *	negative length
     87  *	one-character buffer
     88  * 	two-character buffer
     89  *	full-length buffer
     90  *	large (uncacheable) name in path.
     91  *	deleted directory
     92  *	after rename of parent.
     93  *	permission failure.
     94  *	good pointer near end of address space
     95  *	really huge length
     96  *	really large (multi-block) directories
     97  *	chroot interactions:
     98  *		chroot, at / inside the directory.
     99  *		chroot, at some other inside directory.
    100  */
    101 
    102 /*
    103  * test cases not yet done:
    104  *		-o union mount
    105  *		chroot interactions:
    106  *			chroot to mounted directory.
    107  *			(i.e., proc a: chroot /foo; sleep;
    108  *		       		proc b: mount blort /foo)
    109  *		concurrent with force-unmounting of filesystem.
    110  */
    111 
    112 #define bigname "Funkelhausersteinweitz.SIPBADMIN.a" 	 /* don't ask */
    113 #define littlename "getcwdtest"
    114 #define othername "testgetcwd"
    115 
    116 static int verbose = 0;
    117 static int test = 1;
    118 static int fail = 0;
    119 static int pass = 0;
    120 static int sleepflag = 0;
    121 
    122 static uid_t altid = -1;
    123 
    124 static void
    125 check1 (dir, buf, calltext, actual, expected, experr)
    126 	char *dir;
    127 	char *buf;
    128 	char *calltext;
    129 	int actual, expected, experr;
    130 {
    131 	int ntest = test++;
    132 	if (actual != expected) {
    133 		fprintf(stderr,
    134 		    "test %d: in %s, %s failed; expected %d, got %d\n",
    135 		    ntest, dir, calltext, expected, actual);
    136 		if (actual < 0) perror("getcwd");
    137 		fail++;
    138 	} else if ((expected == -1) && (errno != (experr))) {
    139 		fprintf(stderr,
    140 		    "test %d: in %s, %s failed; expected error %d, got %d\n",
    141 		    ntest, dir, calltext, experr, errno);
    142 		if (actual < 0) perror("getcwd");
    143 		fail++;
    144 	} else if ((expected > 0) &&
    145 	    (buf != NULL) &&
    146 	    (strcmp (dir, buf) != 0)) {
    147 		fprintf(stderr,
    148 		    "test %d: in %s, %s got wrong dir %s\n",
    149 		    ntest, dir, calltext, buf);
    150 		fail++;
    151 	} else {
    152 		if (expected > 0) {
    153 			char newbuf[1024];
    154 			char *cp = old_getcwd(newbuf, sizeof(newbuf));
    155 			if (cp == NULL) {
    156 				fail++;
    157 				fprintf(stderr,
    158 				    "test %d: in %s, old getcwd failed!\n",
    159 				    ntest, dir);
    160 			} else if (strcmp(cp, buf)) {
    161 				fail++;
    162 				fprintf(stderr,
    163 				    "test %d: in %s, old_getcwd returned different dir %s\n",
    164 				    ntest, dir, cp);
    165 			}
    166 		}
    167 		pass++;
    168 		if (verbose)
    169 			printf("test %d: in %s, %s passed\n", ntest, dir, calltext);
    170 	}
    171 	if (sleepflag)
    172 		sleep(1);
    173 }
    174 
    175 int nloops = 100;
    176 
    177 void
    178 time_old_getcwd()
    179 {
    180 	char result_buf[1024];
    181 	if (old_getcwd(result_buf, 1024) == NULL) {
    182 		fprintf(stderr, "old_getcwd failed during timing test!\n");
    183 		perror("old_getcwd");
    184 		exit(1);
    185 	}
    186 
    187 }
    188 
    189 void
    190 time_kern_getcwd()
    191 {
    192 	char result_buf[1024];
    193 	if (__getcwd(result_buf, sizeof(result_buf)) < 0) {
    194 		fprintf(stderr, "getcwd failed during timing test!");
    195 		perror("getcwd");
    196 		exit(1);
    197 	}
    198 }
    199 
    200 static void
    201 time_func(name, func)
    202 	char *name;
    203 	void (*func) __P((void));
    204 {
    205 	struct timeval before, after;
    206 	double delta_t;
    207 
    208 	int i;
    209 	chdir ("/usr/share/examples/emul/ultrix/etc");
    210 
    211 	gettimeofday(&before, 0);
    212 	for (i=0; i<nloops; i++) {
    213 		(*func)();
    214 	}
    215 	gettimeofday(&after, 0);
    216 
    217 	delta_t = after.tv_sec - before.tv_sec;
    218 
    219 	delta_t += ((double)(after.tv_usec - before.tv_usec))/1000000.0;
    220 
    221 	printf("%s: %d calls in %10.3f seconds; ", name, nloops, delta_t);
    222 	printf("%10.6f ms/call\n", (delta_t*1000.0)/nloops);
    223 }
    224 
    225 void
    226 test_speed()
    227 {
    228 	int i;
    229 	for (i=0; i<5; i++)
    230 		time_func("kernel getcwd", time_kern_getcwd);
    231 
    232 	for (i=0; i<5; i++)
    233 		time_func("old user-space getcwd", time_old_getcwd);
    234 }
    235 
    236 #define CHECK(dir, call, ret, err) \
    237 	check1((dir), kbuf, #call, (call), (ret), (err))
    238 
    239 
    240 void
    241 test___getcwd_perms()
    242 {
    243 	char kbuf[1024];
    244 
    245 	if (geteuid() != 0)
    246 	  {
    247 	    fprintf(stderr, "Not root; skipping permission tests\n");
    248 	    return;
    249 	  }
    250 
    251 	mkdir ("/tmp/permdir", 0700);
    252 	mkdir ("/tmp/permdir/subdir", 0755);
    253 	chdir ("/tmp/permdir/subdir");
    254 
    255 	seteuid(altid);
    256 
    257 	CHECK("/tmp/permdir/subdir", __getcwd(kbuf, sizeof(kbuf)), -1, EACCES);
    258 
    259 	seteuid(0);
    260 	chdir ("/");
    261 	rmdir ("/tmp/permdir/subdir");
    262 	rmdir ("/tmp/permdir");
    263 }
    264 
    265 void
    266 test___getcwd_chroot()
    267 {
    268 	int pid, status;
    269 	char kbuf[1024];
    270 
    271 	if (geteuid() != 0)
    272 	  {
    273 	    fprintf(stderr, "Not root; skipping chroot tests\n");
    274 	    return;
    275 	  }
    276 
    277 	/* XXX we need fchroot to do this properly.. */
    278 	mkdir ("/tmp/chrootdir", 0755);
    279 	mkdir ("/tmp/chrootdir/subdir", 0755);
    280 
    281 	chdir ("/tmp/chrootdir");
    282 
    283 	CHECK ("/tmp/chrootdir", __getcwd(kbuf, sizeof(kbuf)), 15, 0);
    284 
    285 	fflush(NULL);
    286 
    287 	pid = fork();
    288 
    289 	if (pid < 0) {
    290 		perror("fork");
    291 		fail++;
    292 	} else if (pid == 0) {
    293 		fail = 0;
    294 		pass = 0;
    295 		/* chroot to root of filesystem (assuming MFS /tmp) */
    296 		chroot ("/tmp");
    297 		CHECK ("/chrootdir", __getcwd(kbuf, sizeof(kbuf)), 11, 0);
    298 		/* chroot to further down */
    299 		chroot ("/chrootdir");
    300 		CHECK ("/", __getcwd(kbuf, sizeof(kbuf)), 2, 0);
    301 		chdir("subdir");
    302 		CHECK ("/subdir", __getcwd(kbuf, sizeof(kbuf)), 8, 0);
    303 
    304 		if (fail)
    305 			exit(1);
    306 		else
    307 			exit(0);
    308 	} else {
    309 		waitpid(pid, &status, 0);
    310 
    311 		if (WIFEXITED(status) &&
    312 		    (WEXITSTATUS(status) == 0))
    313 			pass++;
    314 		else
    315 			fail++;
    316 
    317 	}
    318 
    319 	chdir ("/");
    320 	rmdir ("/tmp/chrootdir/subdir");
    321 	rmdir ("/tmp/chrootdir");
    322 }
    323 
    324 
    325 
    326 
    327 void
    328 test___getcwd()
    329 {
    330 	int i;
    331 	static char kbuf[1024];
    332 
    333 	chdir("/");
    334 
    335 	CHECK("/", __getcwd(0, 0), -1, ERANGE);
    336 	CHECK("/", __getcwd(0, -1), -1, ERANGE);
    337 	CHECK("/", __getcwd(kbuf, 0xdeadbeef), -1, ERANGE); /* large negative */
    338 	CHECK("/", __getcwd(kbuf, 0x7000beef), -1, ERANGE); /* large positive */
    339 	CHECK("/", __getcwd(kbuf, 0x10000), -1, ERANGE); /* outside address space */
    340 	CHECK("/", __getcwd(kbuf+0x100000, sizeof(kbuf)), -1, EFAULT); /* outside address space */
    341 	CHECK("/", __getcwd(0, 30), -1, EFAULT);
    342 	CHECK("/", __getcwd((void*)0xdeadbeef, 30), -1, EFAULT);
    343 	CHECK("/", __getcwd(kbuf, 2), 2, 0);
    344 	assert (strcmp(kbuf, "/") == 0);
    345 	CHECK("/", __getcwd(kbuf, sizeof(kbuf)), 2, 0);
    346 
    347 	CHECK("/", __getcwd(kbuf, 0), -1, ERANGE);
    348 	CHECK("/", __getcwd(kbuf, 1), -1, ERANGE);
    349 
    350 	chdir("/sbin");
    351 	CHECK("/sbin", __getcwd(kbuf, sizeof(kbuf)), 6, 0);
    352 	/* verify that cacheable path gets range check right.. */
    353 	CHECK("/sbin", __getcwd(kbuf, 3), -1, ERANGE);
    354 	chdir("/etc/mtree");
    355 	CHECK("/etc/mtree", __getcwd(kbuf, sizeof(kbuf)), 11, 0);
    356 	CHECK("/etc/mtree", __getcwd(kbuf, sizeof(kbuf)), 11, 0);
    357 	/* mount point */
    358 	chdir("/usr/bin");
    359 	CHECK("/usr/bin", __getcwd(kbuf, sizeof(kbuf)), 9, 0);
    360 
    361 	/* really large (non-cacheable) entry name */
    362 	chdir("/tmp");
    363 	(void) rmdir(bigname);
    364 	mkdir(bigname, 0755);
    365 	chdir(bigname);
    366 
    367 	/* verify that non-cachable path gets range check right.. */
    368 	CHECK("/tmp/" bigname, __getcwd(kbuf, 10), -1, ERANGE);
    369 	CHECK("/tmp/" bigname, __getcwd(kbuf, sizeof(kbuf)), 40, 0);
    370 
    371 	if (rmdir("/tmp/" bigname) < 0) {
    372 		perror("rmdir");
    373 	}
    374 	CHECK("deleted directory", __getcwd(kbuf, sizeof(kbuf)), -1, ENOENT);
    375 
    376 	chdir("/tmp");
    377 	(void) rmdir(littlename);
    378 	mkdir(littlename, 0755);
    379 	chdir(littlename);
    380 	CHECK("/tmp/" littlename, __getcwd(kbuf, sizeof(kbuf)), 16, 0);
    381 	if (rename("/tmp/" littlename, "/tmp/" othername) < 0) {
    382 		perror("rename");
    383 		fail++;
    384 	}
    385 	CHECK("/tmp/" othername, __getcwd(kbuf, sizeof(kbuf)), 16, 0);
    386 	if (rmdir("/tmp/" othername) < 0) {
    387 		perror("rmdir");
    388 		fail++;
    389 	}
    390 	CHECK("deleted directory", __getcwd(kbuf, sizeof(kbuf)), -1, ENOENT);
    391 
    392 	mkdir("/tmp/bigdir", 0755);
    393 	for (i=0; i<nloops; i++) {
    394 		char buf[MAXPATHLEN];
    395 		snprintf(buf, MAXPATHLEN, "/tmp/bigdir/bigsubdirwithanamewhichistoolongtocache%04d", i);
    396 		(void)rmdir(buf);
    397 		if (mkdir (buf, 0755) < 0) {
    398 			perror("mkdir");
    399 			fail++;
    400 			break;
    401 		}
    402 	}
    403 	for (i=0; i<nloops; i++) {
    404 		char buf[MAXPATHLEN];
    405 		snprintf(buf, MAXPATHLEN, "/tmp/bigdir/bigsubdirwithanamewhichistoolongtocache%04d", i);
    406 		if (chdir(buf) < 0) {
    407 			perror("chdir");
    408 			fail++;
    409 			break;
    410 		}
    411 		CHECK(buf, __getcwd(kbuf, sizeof(kbuf)), strlen(buf)+1, 0);
    412 	}
    413 	for (i=0; i<nloops; i++) {
    414 		char buf[MAXPATHLEN];
    415 		snprintf(buf, MAXPATHLEN, "/tmp/bigdir/bigsubdirwithanamewhichistoolongtocache%04d", i);
    416 		(void)rmdir(buf);
    417 	}
    418 	(void)rmdir("/tmp/bigdir");
    419 
    420 	test___getcwd_perms();
    421 	test___getcwd_chroot();
    422 }
    423 
    424 
    425 void
    426 stress_test_getcwd()
    427 {
    428 	char buf[MAXPATHLEN];
    429 	char ubuf[MAXPATHLEN];
    430 	char kbuf[MAXPATHLEN];
    431 	printf("reading directories from stdin..\n");
    432 	while (fgets(buf, MAXPATHLEN, stdin)) {
    433 		char *cp = strrchr(buf, '\n');
    434 		if (cp) *cp = '\0';
    435 
    436 		if (chdir (buf) < 0) {
    437 			warn("Can't change directory to %s", buf);
    438 			continue;
    439 		}
    440 
    441 
    442 		cp = old_getcwd (ubuf, MAXPATHLEN);
    443 		if (strcmp(buf, ubuf) != 0) {
    444 			warnx("In %s, old_getcwd says %s\n",
    445 			    buf, ubuf);
    446 		}
    447 
    448 
    449 		CHECK(buf, __getcwd (kbuf, MAXPATHLEN),
    450 		    strlen(ubuf)+1, 0);
    451 	}
    452 }
    453 
    454 
    455 /*
    456  *	- large directories.
    457  *
    458  *	- every single filesystem type
    459  *
    460  *	- walk filesystem, compare sys_getcwd with getcwd for each
    461  *	directory
    462  */
    463 
    464 void
    465 usage(progname)
    466 	char *progname;
    467 {
    468 	fprintf(stderr, "usage: %s [-srpvw] [-l nloops]\n", progname);
    469 	exit(1);
    470 }
    471 
    472 int run_stress = 0;
    473 int run_regression = 0;
    474 int run_performance = 0;
    475 
    476 int
    477 main(argc, argv)
    478 	int argc;
    479 	char **argv;
    480 {
    481 	int ch;
    482 	char *progname = argv[0];
    483 
    484 	uid_from_user("nobody", &altid);
    485 
    486 	while ((ch = getopt(argc, argv, "srpvwl:u:")) != -1)
    487 		switch (ch) {
    488 		case 's':
    489 			run_stress++;
    490 			break;
    491 		case 'r':
    492 			run_regression++;
    493 			break;
    494 		case 'p':
    495 			run_performance++;
    496 			break;
    497 		case 'v':
    498 			verbose++;
    499 			break;
    500 		case 'w':
    501 			sleepflag++;
    502 			break;
    503 		case 'l':
    504 			nloops = atoi(optarg);
    505 			if (nloops == 0)
    506 				nloops = 100;
    507 			break;
    508 		case 'u':
    509 			if (uid_from_user(optarg, &altid) != 0) {
    510 				fprintf(stderr, "unknown user %s\n", optarg);
    511 				usage(progname);
    512 				exit(1);
    513 			}
    514 			break;
    515 		case '?':
    516 		default:
    517 			usage(progname);
    518 		}
    519 	if (argc != optind)
    520 		usage(progname);
    521 
    522 	if (run_regression)
    523 		test___getcwd();
    524 
    525 	if (!fail && run_performance)
    526 		test_speed();
    527 
    528 	if (!fail && run_stress)
    529 		stress_test_getcwd();
    530 
    531 
    532 	if (verbose)
    533 		printf ("%d passes\n", pass);
    534 	if (!fail)
    535 		exit (0);
    536 	else {
    537 		printf("%d failures\n", fail);
    538 		exit(1);
    539 	}
    540 }
    541 
    542 
    543