From patchwork Tue Jan 9 00:38:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 857183 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-88961-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="AmnPjKyI"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zFtYG3PXSz9sP9 for ; Tue, 9 Jan 2018 11:38:42 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; q=dns; s= default; b=ggsWdV35T/6ITiUfGj2Mner6CTdQwj9ieyNPfLEpxSgZf00OBG0nc 0wUU/s2y1gNJT/Yio2oIgM3eglsjNrMUitPWwN/lmzPo0GxS4r5OjfBZmvrHsykL iyUFGHRq+FmOFI01bJQsggPRF+5YM2MOdcaNOpR2+q/ws4ZVPlk4mk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; s=default; bh=NgpTftN7+SRi6H6nTcR+vsOaJmA=; b=AmnPjKyIujTzCBJEzONlUjeTnjFQ lSB11z9OwSryULLjhysZ3rjsmwVsfWn+KTUdP9kf8nnRFD7BfS5rVXawGvvJdmhD 8eSEv/s0dCvwXw7U62Nwm2L8QqLAz0tzbqrqKlWC+LrXbW60tf4vzrPaA+x1jLVi K2Iv9WQdlHhZT7Q= Received: (qmail 45537 invoked by alias); 9 Jan 2018 00:38:36 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 45527 invoked by uid 89); 9 Jan 2018 00:38:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, SPF_NEUTRAL autolearn=ham version=3.3.2 spammy=Inform X-HELO: hera.aquilenet.fr From: Samuel Thibault To: libc-alpha@sourceware.org Cc: Samuel Thibault Subject: [hurd,commited] hurd: Use the new file_exec_paths RPC Date: Tue, 9 Jan 2018 01:38:23 +0100 Message-Id: <20180109003823.13294-1-samuel.thibault@ens-lyon.org> From: Emilio Pozuelo Monfort From: Svante Signell Pass the file paths of executable to the exec server, both relative and absolute, which exec needs to properly execute and avertise #!-scripts. Previously, the exec server tried to guess the name from argv[0] but argv[0] only contains the executable name by convention. * hurd/hurdexec.c (_hurd_exec): Deprecate function. (_hurd_exec_paths): New function. * hurd/hurd.h (_hurd_exec): Deprecate function. (_hurd_exec_paths): Declare function. * hurd/Versions: Export _hurd_exec_paths. * sysdeps/mach/hurd/execve.c: Include and (__execve): Use __getcwd to build absolute path, and use _hurd_exec_paths instead of _hurd_exec. * sysdeps/mach/hurd/spawni.c: Likewise. * sysdeps/mach/hurd/fexecve.c: Use _hurd_exec_paths instead of _hurd_exec. --- ChangeLog | 15 +++++++++ hurd/Versions | 4 +++ hurd/hurd.h | 13 ++++++-- hurd/hurdexec.c | 52 +++++++++++++++++++++++++------ sysdeps/mach/hurd/execve.c | 36 ++++++++++++++++++++-- sysdeps/mach/hurd/fexecve.c | 5 +-- sysdeps/mach/hurd/spawni.c | 74 ++++++++++++++++++++++++++++++++++----------- 7 files changed, 167 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8890b0b6f4..60058a0ca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2010-08-04 Emilio Pozuelo Monfort +2017-09-26 Svante Signell + + * hurd/hurdexec.c (_hurd_exec): Deprecate function. + (_hurd_exec_paths): New function. + * hurd/hurd.h (_hurd_exec): Deprecate function. + (_hurd_exec_paths): Declare function. + * hurd/Versions: Export _hurd_exec_paths. + * sysdeps/mach/hurd/execve.c: Include and + (__execve): Use __getcwd to build absolute path, and use + _hurd_exec_paths instead of _hurd_exec. + * sysdeps/mach/hurd/spawni.c: Likewise. + * sysdeps/mach/hurd/fexecve.c: Use _hurd_exec_paths instead of + _hurd_exec. + 2018-01-08 Dmitry V. Levin * sysdeps/unix/sysv/linux/tst-ttyname.c (do_in_chroot_1): Skip the diff --git a/hurd/Versions b/hurd/Versions index 77f5b4271e..b059405a50 100644 --- a/hurd/Versions +++ b/hurd/Versions @@ -129,6 +129,10 @@ libc { # functions used in macros & inline functions __errno_location; } + GLIBC_2.27 { + # "quasi-internal" functions + _hurd_exec_paths; + } HURD_CTHREADS_0.3 { # weak refs to libthreads functions that libc calls iff libthreads in use diff --git a/hurd/hurd.h b/hurd/hurd.h index a3d534a35a..7cf23b5598 100644 --- a/hurd/hurd.h +++ b/hurd/hurd.h @@ -240,12 +240,21 @@ extern FILE *fopenport (io_t port, const char *mode); extern FILE *__fopenport (io_t port, const char *mode); -/* Execute a file, replacing TASK's current program image. */ +/* Deprecated: use _hurd_exec_paths instead. */ extern error_t _hurd_exec (task_t task, file_t file, char *const argv[], - char *const envp[]); + char *const envp[]) __attribute_deprecated__; + +/* Execute a file, replacing TASK's current program image. */ + +extern error_t _hurd_exec_paths (task_t task, + file_t file, + const char *path, + const char *abspath, + char *const argv[], + char *const envp[]); /* Inform the proc server we have exited with STATUS, and kill the diff --git a/hurd/hurdexec.c b/hurd/hurdexec.c index 6fa9cf5c1d..027fa5a3ee 100644 --- a/hurd/hurdexec.c +++ b/hurd/hurdexec.c @@ -30,10 +30,29 @@ /* Overlay TASK, executing FILE with arguments ARGV and environment ENVP. If TASK == mach_task_self (), some ports are dealloc'd by the exec server. - ARGV and ENVP are terminated by NULL pointers. */ + ARGV and ENVP are terminated by NULL pointers. + Deprecated: use _hurd_exec_paths instead. */ error_t _hurd_exec (task_t task, file_t file, char *const argv[], char *const envp[]) +{ + return _hurd_exec_paths (task, file, NULL, NULL, argv, envp); +} + +link_warning (_hurd_exec, + "_hurd_exec is deprecated, use _hurd_exec_paths instead"); + +/* Overlay TASK, executing FILE with arguments ARGV and environment ENVP. + If TASK == mach_task_self (), some ports are dealloc'd by the exec server. + ARGV and ENVP are terminated by NULL pointers. PATH is the relative path to + FILE and ABSPATH is the absolute path to FILE. Passing NULL, though possible, + should be avoided, since then the exec server may not know the path to + FILE if FILE is a script, and will then pass /dev/fd/N to the + interpreter. */ +error_t +_hurd_exec_paths (task_t task, file_t file, + const char *path, const char *abspath, + char *const argv[], char *const envp[]) { error_t err; char *args, *env; @@ -216,7 +235,7 @@ _hurd_exec (task_t task, file_t file, /* We have euid != svuid or egid != svgid. POSIX.1 says that exec sets svuid = euid and svgid = egid. So we must get a new auth port and reauthenticate everything with it. We'll pass the new - ports in file_exec instead of our own ports. */ + ports in file_exec_paths instead of our own ports. */ auth_t newauth; @@ -360,13 +379,28 @@ _hurd_exec (task_t task, file_t file, if (__sigismember (&_hurdsig_traced, SIGKILL)) flags |= EXEC_SIGTRAP; #endif - err = __file_exec (file, task, flags, - args, argslen, env, envlen, - dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, - ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, - ints, INIT_INT_MAX, - please_dealloc, pdp - please_dealloc, - &_hurd_msgport, task == __mach_task_self () ? 1 : 0); + err = __file_exec_paths (file, task, flags, + path ? path : "", + abspath ? abspath : "", + args, argslen, env, envlen, + dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, + ports, MACH_MSG_TYPE_COPY_SEND, + _hurd_nports, + ints, INIT_INT_MAX, + please_dealloc, pdp - please_dealloc, + &_hurd_msgport, + task == __mach_task_self () ? 1 : 0); + /* Fall back for backwards compatibility. This can just be removed + when __file_exec goes away. */ + if (err == MIG_BAD_ID) + err = __file_exec (file, task, flags, + args, argslen, env, envlen, + dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, + ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, + ints, INIT_INT_MAX, + please_dealloc, pdp - please_dealloc, + &_hurd_msgport, + task == __mach_task_self () ? 1 : 0); } /* Release references to the standard ports. */ diff --git a/sysdeps/mach/hurd/execve.c b/sysdeps/mach/hurd/execve.c index 04d3bd0d3e..1c5b681d89 100644 --- a/sysdeps/mach/hurd/execve.c +++ b/sysdeps/mach/hurd/execve.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include /* Replace the current process, executing FILE_NAME with arguments ARGV and environment ENVP. ARGV and ENVP are terminated by NULL pointers. */ @@ -25,16 +27,46 @@ int __execve (const char *file_name, char *const argv[], char *const envp[]) { error_t err; - file_t file = __file_name_lookup (file_name, O_EXEC, 0); + char *concat_name = NULL; + const char *abs_path; + file_t file = __file_name_lookup (file_name, O_EXEC, 0); if (file == MACH_PORT_NULL) return -1; + if (file_name[0] == '/') + { + /* Already an absolute path */ + abs_path = file_name; + } + else + { + /* Relative path */ + char *cwd = __getcwd (NULL, 0); + if (cwd == NULL) + { + __mach_port_deallocate (__mach_task_self (), file); + return -1; + } + + int res = __asprintf (&concat_name, "%s/%s", cwd, file_name); + free (cwd); + if (res == -1) + { + __mach_port_deallocate (__mach_task_self (), file); + return -1; + } + + abs_path = concat_name; + } + /* Hopefully this will not return. */ - err = _hurd_exec (__mach_task_self (), file, argv, envp); + err = _hurd_exec_paths (__mach_task_self (), file, + file_name, abs_path, argv, envp); /* Oh well. Might as well be tidy. */ __mach_port_deallocate (__mach_task_self (), file); + free (concat_name); return __hurd_fail (err); } diff --git a/sysdeps/mach/hurd/fexecve.c b/sysdeps/mach/hurd/fexecve.c index a56a51bba1..c5679f197e 100644 --- a/sysdeps/mach/hurd/fexecve.c +++ b/sysdeps/mach/hurd/fexecve.c @@ -25,8 +25,9 @@ int fexecve (int fd, char *const argv[], char *const envp[]) { - error_t err = HURD_DPORT_USE (fd, _hurd_exec (__mach_task_self (), port, - argv, envp)); + error_t err = HURD_DPORT_USE (fd, _hurd_exec_paths (__mach_task_self (), + port, NULL, NULL, + argv, envp)); if (! err) err = EGRATUITOUS; return __hurd_fail (err); diff --git a/sysdeps/mach/hurd/spawni.c b/sysdeps/mach/hurd/spawni.c index 24c1e5cfb2..aa3b0c4660 100644 --- a/sysdeps/mach/hurd/spawni.c +++ b/sysdeps/mach/hurd/spawni.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,8 @@ __spawni (pid_t *pid, const char *file, { pid_t new_pid; char *path, *p, *name; + char *concat_name = NULL, *relpath, *abspath; + int res; size_t len; size_t pathlen; short int flags; @@ -59,14 +62,14 @@ __spawni (pid_t *pid, const char *file, that remains visible after an exec is registration with the proc server, and the inheritance of various values and ports. All those inherited values and ports are what get collected up and passed in the - file_exec RPC by an exec call. So we do the proc server registration - here, following the model of fork (see fork.c). We then collect up - the inherited values and ports from this (parent) process following - the model of exec (see hurd/hurdexec.c), modify or replace each value - that fork would (plus the specific changes demanded by ATTRP and - FILE_ACTIONS), and make the file_exec RPC on the requested executable - file with the child process's task port rather than our own. This - should be indistinguishable from the fork + exec implementation, + file_exec_paths RPC by an exec call. So we do the proc server + registration here, following the model of fork (see fork.c). We then + collect up the inherited values and ports from this (parent) process + following the model of exec (see hurd/hurdexec.c), modify or replace each + value that fork would (plus the specific changes demanded by ATTRP and + FILE_ACTIONS), and make the file_exec_paths RPC on the requested + executable file with the child process's task port rather than our own. + This should be indistinguishable from the fork + exec implementation, except that all errors will be detected here (in the parent process) and return proper errno codes rather than the child dying with 127. @@ -547,7 +550,7 @@ __spawni (pid_t *pid, const char *file, if ((xflags & SPAWN_XFLAGS_USE_PATH) == 0 || strchr (file, '/') != NULL) /* The FILE parameter is actually a path. */ - err = child_lookup (file, O_EXEC, 0, &execfile); + err = child_lookup (relpath = file, O_EXEC, 0, &execfile); else { /* We have to search for FILE on the path. */ @@ -608,6 +611,7 @@ __spawni (pid_t *pid, const char *file, } // We only get here when we are done looking for the file. + relpath = startp; break; } while (*p++ != '\0'); @@ -615,6 +619,26 @@ __spawni (pid_t *pid, const char *file, if (err) goto out; + if (relpath[0] == '/') + { + /* Already an absolute path */ + abspath = relpath; + } + else + { + /* Relative path */ + char *cwd = __getcwd (NULL, 0); + if (cwd == NULL) + goto out; + + res = __asprintf (&concat_name, "%s/%s", cwd, relpath); + free (cwd); + if (res == -1) + goto out; + + abspath = concat_name; + } + /* Almost there! */ { mach_port_t ports[_hurd_nports]; @@ -624,14 +648,28 @@ __spawni (pid_t *pid, const char *file, inline error_t exec (file_t file) { - return __file_exec (file, task, - (__sigismember (&_hurdsig_traced, SIGKILL) - ? EXEC_SIGTRAP : 0), - args, argslen, env, envlen, - dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, - ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, - ints, INIT_INT_MAX, - NULL, 0, NULL, 0); + error_t err = __file_exec_paths + (file, task, + __sigismember (&_hurdsig_traced, SIGKILL) ? EXEC_SIGTRAP : 0, + relpath, abspath, args, argslen, env, envlen, + dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, + ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, + ints, INIT_INT_MAX, + NULL, 0, NULL, 0); + + /* Fallback for backwards compatibility. This can just be removed + when __file_exec goes away. */ + if (err == MIG_BAD_ID) + return __file_exec (file, task, + (__sigismember (&_hurdsig_traced, SIGKILL) + ? EXEC_SIGTRAP : 0), + args, argslen, env, envlen, + dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, + ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, + ints, INIT_INT_MAX, + NULL, 0, NULL, 0); + + return err; } /* Now we are out of things that can fail before the file_exec RPC, @@ -750,6 +788,8 @@ __spawni (pid_t *pid, const char *file, _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]); } + free (concat_name); + if (err) /* This hack canonicalizes the error code that we return. */ err = (__hurd_fail (err), errno);