about summary refs log tree commit diff
path: root/src/unistd
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-11-01 20:34:05 -0400
committerRich Felker <dalias@aerifal.cx>2013-11-01 20:34:05 -0400
commitf9fb20b42da0e755d93de229a5a737d79a0e8f60 (patch)
treea4a617b75888d3a96c26aba40fa5ce47304ee55b /src/unistd
parent984af5c99e2efaf17c0c764d66a275da764f94d2 (diff)
downloadmusl-f9fb20b42da0e755d93de229a5a737d79a0e8f60.tar.gz
musl-f9fb20b42da0e755d93de229a5a737d79a0e8f60.tar.xz
musl-f9fb20b42da0e755d93de229a5a737d79a0e8f60.zip
simplify faccessat AT_EACCESS path and eliminate resource dependence
now that we're waiting for the exit status of the child process, the
result can be conveyed in the exit status rather than via a pipe.
since the error value might not fit in 7 bits, a table is used to
translate possible meaningful error values to small integers.
Diffstat (limited to 'src/unistd')
-rw-r--r--src/unistd/faccessat.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/unistd/faccessat.c b/src/unistd/faccessat.c
index 76bbd4c7..33478959 100644
--- a/src/unistd/faccessat.c
+++ b/src/unistd/faccessat.c
@@ -1,5 +1,6 @@
 #include <unistd.h>
 #include <fcntl.h>
+#include <errno.h>
 #include <sys/wait.h>
 #include "syscall.h"
 #include "pthread_impl.h"
@@ -8,19 +9,26 @@ struct ctx {
 	int fd;
 	const char *filename;
 	int amode;
-	int p;
+};
+
+static const int errors[] = {
+	0, -EACCES, -ELOOP, -ENAMETOOLONG, -ENOENT, -ENOTDIR,
+	-EROFS, -EBADF, -EINVAL, -ETXTBSY,
+	-EFAULT, -EIO, -ENOMEM,
+	-EBUSY
 };
 
 static int checker(void *p)
 {
 	struct ctx *c = p;
 	int ret;
+	int i;
 	if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
 	    || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
 		__syscall(SYS_exit, 1);
 	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
-	__syscall(SYS_write, c->p, &ret, sizeof ret);
-	return 0;
+	for (i=0; i < sizeof errors/sizeof *errors - 1 && ret!=errors[i]; i++);
+	return i;
 }
 
 int faccessat(int fd, const char *filename, int amode, int flag)
@@ -34,21 +42,20 @@ int faccessat(int fd, const char *filename, int amode, int flag)
 	char stack[1024];
 	sigset_t set;
 	pid_t pid;
-	int status;
-	int ret, p[2];
-
-	if (pipe2(p, O_CLOEXEC)) return __syscall_ret(-EBUSY);
-	struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1] };
+	int ret = -EBUSY;
+	struct ctx c = { .fd = fd, .filename = filename, .amode = amode };
 
 	__block_all_sigs(&set);
 	
 	pid = __clone(checker, stack+sizeof stack, 0, &c);
-	__syscall(SYS_close, p[1]);
-
-	if (pid<0 || __syscall(SYS_read, p[0], &ret, sizeof ret) != sizeof(ret))
-		ret = -EBUSY;
-	__syscall(SYS_close, p[0]);
-	__syscall(SYS_wait4, pid, &status, __WCLONE, 0);
+	if (pid > 0) {
+		int status;
+		do {
+			__syscall(SYS_wait4, pid, &status, __WCLONE, 0);
+		} while (!WIFEXITED(status) && !WIFSIGNALED(status));
+		if (WIFEXITED(status))
+			ret = errors[WEXITSTATUS(status)];
+	}
 
 	__restore_sigs(&set);