17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
568a94df1Scf * Common Development and Distribution License (the "License").
668a94df1Scf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
284a38094cSToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * _xftw - file tree walk the uses expanded stat structure
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * int _xftw(path, fn, depth) char *path; int (*fn)(); int depth;
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * Given a path name, _xftw starts from the file given by that path
367c478bd9Sstevel@tonic-gate * name and visits each file and directory in the tree beneath
377c478bd9Sstevel@tonic-gate * that file. If a single file has multiple links within the
387c478bd9Sstevel@tonic-gate * structure, it will be visited once for each such link.
397c478bd9Sstevel@tonic-gate * For each object visited, fn is called with three arguments.
407c478bd9Sstevel@tonic-gate * (*fn) (pathname, statp, ftwflag)
417c478bd9Sstevel@tonic-gate * The first contains the path name of the object, the second
427c478bd9Sstevel@tonic-gate * contains a pointer to a stat buffer which will usually hold
437c478bd9Sstevel@tonic-gate * appropriate information for the object and the third will
447c478bd9Sstevel@tonic-gate * contain an integer value giving additional information about
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * FTW_F The object is a file for which stat was
477c478bd9Sstevel@tonic-gate * successful. It does not guarantee that the
487c478bd9Sstevel@tonic-gate * file can actually be read.
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * FTW_D The object is a directory for which stat and
517c478bd9Sstevel@tonic-gate * open for read were both successful.
527c478bd9Sstevel@tonic-gate *
537c478bd9Sstevel@tonic-gate * FTW_DNR The object is a directory for which stat
547c478bd9Sstevel@tonic-gate * succeeded, but which cannot be read. Because
557c478bd9Sstevel@tonic-gate * the directory cannot be read, fn will not be
567c478bd9Sstevel@tonic-gate * called for any descendants of this directory.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * FTW_NS Stat failed on the object because of lack of
597c478bd9Sstevel@tonic-gate * appropriate permission. This indication will
607c478bd9Sstevel@tonic-gate * be given for example for each file in a
617c478bd9Sstevel@tonic-gate * directory with read but no execute permission.
627c478bd9Sstevel@tonic-gate * Because stat failed, it is not possible to
637c478bd9Sstevel@tonic-gate * determine whether this object is a file or a
647c478bd9Sstevel@tonic-gate * directory. The stat buffer passed to fn will
657c478bd9Sstevel@tonic-gate * contain garbage. Stat failure for any reason
667c478bd9Sstevel@tonic-gate * other than lack of permission will be
677c478bd9Sstevel@tonic-gate * considered an error and will cause _xftw to stop
687c478bd9Sstevel@tonic-gate * and return -1 to its caller.
697c478bd9Sstevel@tonic-gate *
707c478bd9Sstevel@tonic-gate * If fn returns nonzero, _xftw stops and returns the same value
717c478bd9Sstevel@tonic-gate * to its caller. If _xftw gets into other trouble along the way,
727c478bd9Sstevel@tonic-gate * it returns -1 and leaves an indication of the cause in errno.
737c478bd9Sstevel@tonic-gate *
747c478bd9Sstevel@tonic-gate * The third argument to _xftw does not limit the depth to which
757c478bd9Sstevel@tonic-gate * _xftw will go. Rather, it limits the depth to which _xftw will
767c478bd9Sstevel@tonic-gate * go before it starts recycling file descriptors. In general,
777c478bd9Sstevel@tonic-gate * it is necessary to use a file descriptor for each level of the
787c478bd9Sstevel@tonic-gate * tree, but they can be recycled for deep trees by saving the
7968a94df1Scf * position, closing, re-opening, and seeking. In order to descend
8068a94df1Scf * to arbitrary depths, _xftw requires 2 file descriptors to be open
8168a94df1Scf * during the call to openat(), therefore if the depth argument
8268a94df1Scf * is less than 2 _xftw will not use openat(), and it will fail with
8368a94df1Scf * ENAMETOOLONG if it descends to a directory that exceeds PATH_MAX.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * this interface uses the expanded stat structure and therefore
887c478bd9Sstevel@tonic-gate * must have EFT enabled.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate #ifdef _STYPES
917c478bd9Sstevel@tonic-gate #undef _STYPES
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #include "lint.h"
957c478bd9Sstevel@tonic-gate #include <sys/types.h>
967c478bd9Sstevel@tonic-gate #include <sys/stat.h>
9768a94df1Scf #include <fcntl.h>
987c478bd9Sstevel@tonic-gate #include <sys/param.h>
997c478bd9Sstevel@tonic-gate #include <dirent.h>
1007c478bd9Sstevel@tonic-gate #include <errno.h>
1017c478bd9Sstevel@tonic-gate #include <ftw.h>
1027c478bd9Sstevel@tonic-gate #include <string.h>
1037c478bd9Sstevel@tonic-gate #include <stdlib.h>
10468a94df1Scf #include <unistd.h>
10568a94df1Scf
10668a94df1Scf struct Var {
10768a94df1Scf int level;
10868a94df1Scf int odepth;
10968a94df1Scf };
11068a94df1Scf
11168a94df1Scf static DIR *nocdopendir(const char *, struct Var *);
11268a94df1Scf static int nocdstat(const char *, struct stat *, struct Var *, int);
11368a94df1Scf static const char *get_unrooted(const char *);
11468a94df1Scf static int fwalk(const char *, int (*)(const char *, const struct stat *, int),
11568a94df1Scf int, struct Var *);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate int
_xftw(int ver __unused,const char * path,int (* fn)(const char *,const struct stat *,int),int depth)1184a38094cSToomas Soome _xftw(int ver __unused, const char *path,
1194a38094cSToomas Soome int (*fn)(const char *, const struct stat *, int), int depth)
12068a94df1Scf {
12168a94df1Scf struct Var var;
12268a94df1Scf int rc;
12368a94df1Scf
12468a94df1Scf var.level = 0;
12568a94df1Scf var.odepth = depth;
12668a94df1Scf rc = fwalk(path, fn, depth, &var);
12768a94df1Scf return (rc);
12868a94df1Scf }
12968a94df1Scf
13068a94df1Scf /*
13168a94df1Scf * This is the recursive walker.
13268a94df1Scf */
13368a94df1Scf static int
fwalk(const char * path,int (* fn)(const char *,const struct stat *,int),int depth,struct Var * vp)13468a94df1Scf fwalk(const char *path, int (*fn)(const char *, const struct stat *, int),
1354a38094cSToomas Soome int depth, struct Var *vp)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate size_t n;
1387c478bd9Sstevel@tonic-gate int rc;
1397c478bd9Sstevel@tonic-gate int save_errno;
1407c478bd9Sstevel@tonic-gate DIR *dirp;
1417c478bd9Sstevel@tonic-gate char *subpath;
1427c478bd9Sstevel@tonic-gate struct stat sb;
1437c478bd9Sstevel@tonic-gate struct dirent *direntp;
1447c478bd9Sstevel@tonic-gate
14568a94df1Scf vp->level++;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * Try to get file status.
1497c478bd9Sstevel@tonic-gate * If unsuccessful, errno will say why.
1507c478bd9Sstevel@tonic-gate * It's ok to have a symbolic link that points to
1517c478bd9Sstevel@tonic-gate * non-existing file. In this case, pass FTW_NS
15268a94df1Scf * to a function instead of aborting fwalk() right away.
1537c478bd9Sstevel@tonic-gate */
15468a94df1Scf if (nocdstat(path, &sb, vp, 0) < 0) {
1557c478bd9Sstevel@tonic-gate #ifdef S_IFLNK
1567c478bd9Sstevel@tonic-gate save_errno = errno;
15768a94df1Scf if ((nocdstat(path, &sb, vp, AT_SYMLINK_NOFOLLOW) != -1) &&
15868a94df1Scf ((sb.st_mode & S_IFMT) == S_IFLNK)) {
1597c478bd9Sstevel@tonic-gate errno = save_errno;
1607c478bd9Sstevel@tonic-gate return (*fn)(path, &sb, FTW_NS);
1617c478bd9Sstevel@tonic-gate } else {
1627c478bd9Sstevel@tonic-gate errno = save_errno;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * The stat succeeded, so we know the object exists.
1707c478bd9Sstevel@tonic-gate * If not a directory, call the user function and return.
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFDIR)
1737c478bd9Sstevel@tonic-gate return ((*fn)(path, &sb, FTW_F));
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * The object was a directory.
1777c478bd9Sstevel@tonic-gate *
1787c478bd9Sstevel@tonic-gate * Open a file to read the directory
1797c478bd9Sstevel@tonic-gate */
18068a94df1Scf dirp = nocdopendir(path, vp);
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate * Call the user function, telling it whether
1847c478bd9Sstevel@tonic-gate * the directory can be read. If it can't be read
1857c478bd9Sstevel@tonic-gate * call the user function or indicate an error,
1867c478bd9Sstevel@tonic-gate * depending on the reason it couldn't be read.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate if (dirp == NULL)
1897c478bd9Sstevel@tonic-gate return (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* We could read the directory. Call user function. */
1927c478bd9Sstevel@tonic-gate rc = (*fn)(path, &sb, FTW_D);
1937c478bd9Sstevel@tonic-gate if (rc != 0) {
1947c478bd9Sstevel@tonic-gate (void) closedir(dirp);
1957c478bd9Sstevel@tonic-gate return (rc);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * Read the directory one component at a time.
2007c478bd9Sstevel@tonic-gate * We must ignore "." and "..", but other than that,
2017c478bd9Sstevel@tonic-gate * just create a path name and call self to check it out.
2027c478bd9Sstevel@tonic-gate */
203*00ae5933SToomas Soome while ((direntp = readdir(dirp)) != NULL) {
2047c478bd9Sstevel@tonic-gate long here;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if (strcmp(direntp->d_name, ".") == 0 ||
2077c478bd9Sstevel@tonic-gate strcmp(direntp->d_name, "..") == 0)
2087c478bd9Sstevel@tonic-gate continue;
2097c478bd9Sstevel@tonic-gate
21068a94df1Scf /* Create a prefix to which we will append component names */
21168a94df1Scf n = strlen(path);
21268a94df1Scf subpath = malloc(n + strlen(direntp->d_name) + 2);
21368a94df1Scf if (subpath == 0) {
21468a94df1Scf (void) closedir(dirp);
21568a94df1Scf errno = ENOMEM;
21668a94df1Scf return (-1);
21768a94df1Scf }
21868a94df1Scf (void) strcpy(subpath, path);
21968a94df1Scf if (subpath[0] != '\0' && subpath[n-1] != '/')
22068a94df1Scf subpath[n++] = '/';
22168a94df1Scf
2227c478bd9Sstevel@tonic-gate /* Append component name to the working path */
2237c478bd9Sstevel@tonic-gate (void) strlcpy(&subpath[n], direntp->d_name, MAXNAMELEN);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * If we are about to exceed our depth,
2277c478bd9Sstevel@tonic-gate * remember where we are and close a file.
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate if (depth <= 1) {
2307c478bd9Sstevel@tonic-gate here = telldir(dirp);
23168a94df1Scf if (closedir(dirp) < 0) {
23268a94df1Scf free(subpath);
2337c478bd9Sstevel@tonic-gate return (-1);
23468a94df1Scf }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * Do a recursive call to process the file.
2397c478bd9Sstevel@tonic-gate * (watch this, sports fans)
2407c478bd9Sstevel@tonic-gate */
24168a94df1Scf rc = fwalk(subpath, fn, depth-1, vp);
2427c478bd9Sstevel@tonic-gate if (rc != 0) {
24368a94df1Scf free(subpath);
2447c478bd9Sstevel@tonic-gate if (depth > 1)
2457c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2467c478bd9Sstevel@tonic-gate return (rc);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * If we closed the file, try to reopen it.
2517c478bd9Sstevel@tonic-gate */
2527c478bd9Sstevel@tonic-gate if (depth <= 1) {
25368a94df1Scf dirp = nocdopendir(path, vp);
25468a94df1Scf if (dirp == NULL) {
25568a94df1Scf free(subpath);
2567c478bd9Sstevel@tonic-gate return (-1);
25768a94df1Scf }
2587c478bd9Sstevel@tonic-gate seekdir(dirp, here);
2597c478bd9Sstevel@tonic-gate }
26068a94df1Scf free(subpath);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate }
26568a94df1Scf
26668a94df1Scf /*
26768a94df1Scf * Open a directory with an arbitrarily long path name. If the original
26868a94df1Scf * depth arg >= 2, use openat() to make sure that it doesn't fail with
26968a94df1Scf * ENAMETOOLONG.
27068a94df1Scf */
27168a94df1Scf static DIR *
nocdopendir(const char * path,struct Var * vp)27268a94df1Scf nocdopendir(const char *path, struct Var *vp)
27368a94df1Scf {
27468a94df1Scf int fd, cfd;
27568a94df1Scf DIR *fdd;
27668a94df1Scf char *dirp, *token, *ptr;
27768a94df1Scf
27868a94df1Scf fdd = opendir(path);
27968a94df1Scf if ((vp->odepth > 1) && (fdd == NULL) && (errno == ENAMETOOLONG)) {
28068a94df1Scf /*
28168a94df1Scf * Traverse the path using openat() to get the fd for
28268a94df1Scf * fdopendir().
28368a94df1Scf */
28468a94df1Scf if ((dirp = strdup(path)) == NULL) {
28568a94df1Scf errno = ENAMETOOLONG;
28668a94df1Scf return (NULL);
28768a94df1Scf }
28868a94df1Scf if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
2894a38094cSToomas Soome if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
2904a38094cSToomas Soome (void) free(dirp);
2914a38094cSToomas Soome errno = ENAMETOOLONG;
2924a38094cSToomas Soome return (NULL);
29368a94df1Scf }
2944a38094cSToomas Soome while ((token = strtok_r(NULL, "/", &ptr)) != NULL) {
2954a38094cSToomas Soome if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
2964a38094cSToomas Soome (void) close(fd);
2974a38094cSToomas Soome (void) free(dirp);
2984a38094cSToomas Soome errno = ENAMETOOLONG;
2994a38094cSToomas Soome return (NULL);
3004a38094cSToomas Soome }
3014a38094cSToomas Soome (void) close(fd);
3024a38094cSToomas Soome fd = cfd;
3034a38094cSToomas Soome }
3044a38094cSToomas Soome (void) free(dirp);
3054a38094cSToomas Soome return (fdopendir(fd));
30668a94df1Scf }
30768a94df1Scf (void) free(dirp);
30868a94df1Scf errno = ENAMETOOLONG;
30968a94df1Scf }
31068a94df1Scf return (fdd);
31168a94df1Scf }
31268a94df1Scf
31368a94df1Scf /*
31468a94df1Scf * Stat a file with an arbitrarily long path name. If we aren't doing a
31568a94df1Scf * stat on the arg passed to _xftw() and if the original depth arg >= 2,
31668a94df1Scf * use openat() to make sure that it doesn't fail with ENAMETOOLONG.
31768a94df1Scf */
31868a94df1Scf static int
nocdstat(const char * path,struct stat * statp,struct Var * vp,int sym)31968a94df1Scf nocdstat(const char *path, struct stat *statp, struct Var *vp, int sym)
32068a94df1Scf {
32168a94df1Scf int fd, cfd;
32268a94df1Scf char *dirp, *token, *ptr;
32368a94df1Scf int rc;
32468a94df1Scf const char *unrootp;
32568a94df1Scf int save_err;
32668a94df1Scf
32768a94df1Scf rc = fstatat(AT_FDCWD, path, statp, sym);
32868a94df1Scf if ((vp->level > 1) && (vp->odepth >= 2) && (rc < 0) &&
32968a94df1Scf (errno == ENAMETOOLONG)) {
33068a94df1Scf /* Traverse path using openat() to get fd for fstatat(). */
33168a94df1Scf if ((dirp = strdup(path)) == NULL) {
33268a94df1Scf errno = ENAMETOOLONG;
33368a94df1Scf return (-1);
33468a94df1Scf }
33568a94df1Scf if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
3364a38094cSToomas Soome if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
33768a94df1Scf (void) free(dirp);
33868a94df1Scf errno = ENAMETOOLONG;
3394a38094cSToomas Soome return (-1);
3404a38094cSToomas Soome }
3414a38094cSToomas Soome unrootp = get_unrooted(path);
3424a38094cSToomas Soome while (((token = strtok_r(NULL, "/", &ptr)) != NULL) &&
3434a38094cSToomas Soome (strcmp(token, unrootp) != 0)) {
3444a38094cSToomas Soome if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
3454a38094cSToomas Soome (void) close(fd);
3464a38094cSToomas Soome (void) free(dirp);
3474a38094cSToomas Soome errno = ENAMETOOLONG;
3484a38094cSToomas Soome return (0);
3494a38094cSToomas Soome }
3504a38094cSToomas Soome (void) close(fd);
3514a38094cSToomas Soome fd = cfd;
3524a38094cSToomas Soome }
3534a38094cSToomas Soome (void) free(dirp);
3544a38094cSToomas Soome rc = fstatat(fd, unrootp, statp, sym);
3554a38094cSToomas Soome save_err = errno;
3564a38094cSToomas Soome (void) close(fd);
3574a38094cSToomas Soome errno = save_err;
3584a38094cSToomas Soome return (rc);
35968a94df1Scf }
36068a94df1Scf (void) free(dirp);
36168a94df1Scf errno = ENAMETOOLONG;
36268a94df1Scf }
36368a94df1Scf return (rc);
36468a94df1Scf }
36568a94df1Scf
36668a94df1Scf /*
36768a94df1Scf * Return pointer basename of path. This routine doesn't remove
36868a94df1Scf * trailing slashes, but there won't be any.
36968a94df1Scf */
37068a94df1Scf static const char *
get_unrooted(const char * path)37168a94df1Scf get_unrooted(const char *path)
37268a94df1Scf {
37368a94df1Scf const char *ptr;
37468a94df1Scf
37568a94df1Scf if (!path || !*path)
37668a94df1Scf return (NULL);
37768a94df1Scf
37868a94df1Scf ptr = path + strlen(path);
37968a94df1Scf /* find last char in path before any trailing slashes */
38068a94df1Scf while (ptr != path && *--ptr == '/')
38168a94df1Scf ;
38268a94df1Scf
38368a94df1Scf if (ptr == path) /* all slashes */
38468a94df1Scf return (ptr);
38568a94df1Scf
38668a94df1Scf while (ptr != path)
38768a94df1Scf if (*--ptr == '/')
38868a94df1Scf return (++ptr);
38968a94df1Scf
39068a94df1Scf return (ptr);
39168a94df1Scf }
392