mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ismael Luceno <ismael@iodev.co.uk>
To: musl@lists.openwall.com
Cc: Rich Felker <dalias@libc.org>, Ismael Luceno <ismael@iodev.co.uk>
Subject: [musl] [PATCH v3 1/2] nftw: implement FTW_CHDIR
Date: Sun, 23 Jan 2022 16:59:54 +0100	[thread overview]
Message-ID: <20220123155955.16484-2-ismael@iodev.co.uk> (raw)
In-Reply-To: <20220123155955.16484-1-ismael@iodev.co.uk>

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 src/misc/nftw.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 5b233b2b8e77..7569a657e54e 100644
--- a/src/misc/nftw.c
+++ b/src/misc/nftw.c
@@ -87,6 +87,14 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 		DIR *d = fdopendir(dfd);
 		if (d) {
 			struct dirent *de;
+			if (flags & FTW_CHDIR) {
+				if (!fchdir(dfd)) {
+					err = errno;
+					closedir(d);
+					errno = err;
+					return -1;
+				}
+			}
 			while ((de = readdir(d))) {
 				if (de->d_name[0] == '.'
 				 && (!de->d_name[1]
@@ -123,6 +131,7 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
 	int r, cs;
 	size_t l;
 	char pathbuf[PATH_MAX+1];
+	int orig_dfd;
 
 	if (fd_limit <= 0) return 0;
 
@@ -133,9 +142,22 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
 	}
 	memcpy(pathbuf, path, l+1);
 	
+	if (flags & FTW_CHDIR) {
+		orig_dfd = open(".", O_CLOEXEC | O_PATH);
+		if (orig_dfd < 0)
+			return -1;
+	}
+
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
 	pthread_setcancelstate(cs, 0);
+	if (flags & FTW_CHDIR) {
+		if (!fchdir(orig_dfd))
+			r = -1;
+		int err = errno;
+		close(orig_dfd);
+		errno = err;
+	}
 	return r;
 }
 
-- 
2.33.0


  reply	other threads:[~2022-01-23 15:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-23 15:59 [musl] [PATCH v3 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Ismael Luceno
2022-01-23 15:59 ` Ismael Luceno [this message]
2022-05-12 13:00   ` [musl] [PATCH v3 1/2] nftw: implement FTW_CHDIR Rich Felker
2022-01-23 15:59 ` [musl] [PATCH v3 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) Ismael Luceno
2022-01-24  0:47   ` Dominique MARTINET
2022-01-24 19:53     ` Ismael Luceno
2022-03-09  4:37       ` Dominique MARTINET
2022-03-09 11:41         ` Rich Felker
2022-04-28  2:02           ` Dominique MARTINET
2022-01-24 22:13 ` [musl] [PATCH v3 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220123155955.16484-2-ismael@iodev.co.uk \
    --to=ismael@iodev.co.uk \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --subject='Re: [musl] [PATCH v3 1/2] nftw: implement FTW_CHDIR' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).