From patchwork Wed Sep 1 17:39:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44829 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 07550385AC25 for ; Wed, 1 Sep 2021 17:40:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 07550385AC25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630518021; bh=Gd9KRYFmk6f1Ns7zbW3FPqjjpu/CsBFIOSU5/8BOIL0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=p+3k8A2WKTGZ7WqCUnTUfu7nOVoK/EGBYRBL2WQYlhOLz1FmllP7pTLwzqiCfCocU Vy0Yl3FT6LXAygoPDq4p5GEd/AkQXtc9QhfIJhKR5cd2frzwjZzchXtvvFr3OIiPGj xEAz1jm3lR7lf+2rSRBctnZrj/6ONX1sN6iFHFK0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id 56611385743C for ; Wed, 1 Sep 2021 17:39:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 56611385743C Received: by mail-qk1-x729.google.com with SMTP id f22so261573qkm.5 for ; Wed, 01 Sep 2021 10:39:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Gd9KRYFmk6f1Ns7zbW3FPqjjpu/CsBFIOSU5/8BOIL0=; b=EOiKslL4JIkSDAuUx/kVF86TMinCYMzTRNqoYcIMGAlZYC8qiucEv3Lu19h5adOvTt oDkHJrDNRCILxG5rPpv73GBhzFneXUmdqHYko5rnWNYvIF92V1MATyE4fWn7vuMjtCp2 lMhlwrXzTwowhxcAhfu3ZdvZd5bXK69jZ3tBJPUuwX37Yr23d8u4+vN6ck1LzxVRW+cR iZhBGibxwiTHeW+TnXZtl0QJEAgfNFpZceXQ/KFWjOyoKgBEBeF8HCKQvWbM07LIeKbN Wx97RTXaXl5pon/hV/rtAfX26BlwIpvTziBnLJLGenm19RMl7M+7ENqgwjoTDevZbWsR 5Fqw== X-Gm-Message-State: AOAM530NcEzO9UJXHeh3gqkz96Ia/BoJtiByfWFN8KMXpFHSo0UYm4LT V9ziB3ADqNzmguAXYYQ2wALomfXDsfrm9A== X-Google-Smtp-Source: ABdhPJy36vswmui2UCtgg8zALgO7DTF3TMvIbZaslynuG1C13uslzm0amHBx7J9B1n6WtkFe5ruzTg== X-Received: by 2002:a05:620a:8cb:: with SMTP id z11mr814780qkz.328.1630517989585; Wed, 01 Sep 2021 10:39:49 -0700 (PDT) Received: from birita.. ([2804:18:8b5:79f6:6068:def9:ee5b:1e9]) by smtp.gmail.com with ESMTPSA id t194sm125847qka.72.2021.09.01.10.39.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Sep 2021 10:39:49 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4 1/2] posix: Remove spawni.c Date: Wed, 1 Sep 2021 14:39:37 -0300 Message-Id: <20210901173938.1048607-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Godmar Back Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Although it provide an alternate implementation that communicates using pipe() instead of shared memory, no port uses and it adds extra burden for posix_spawn() extensions. Reviewed-by: Florian Weimer --- sysdeps/posix/spawni.c | 343 ----------------------------------------- 1 file changed, 343 deletions(-) delete mode 100644 sysdeps/posix/spawni.c diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c deleted file mode 100644 index ad1858fe4e..0000000000 --- a/sysdeps/posix/spawni.c +++ /dev/null @@ -1,343 +0,0 @@ -/* Guts of POSIX spawn interface. Generic POSIX.1 version. - Copyright (C) 2000-2021 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "spawn_int.h" - - -/* The Unix standard contains a long explanation of the way to signal - an error after the fork() was successful. Since no new wait status - was wanted there is no way to signal an error using one of the - available methods. The committee chose to signal an error by a - normal program exit with the exit code 127. */ -#define SPAWN_ERROR 127 - -struct posix_spawn_args -{ - sigset_t oldmask; - const char *file; - int (*exec) (const char *, char *const *, char *const *); - const posix_spawn_file_actions_t *fa; - const posix_spawnattr_t *restrict attr; - char *const *argv; - ptrdiff_t argc; - char *const *envp; - int xflags; - int pipe[2]; -}; - -/* Older version requires that shell script without shebang definition - to be called explicitly using /bin/sh (_PATH_BSHELL). */ -static void -maybe_script_execute (struct posix_spawn_args *args) -{ - if (SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_15) - && (args->xflags & SPAWN_XFLAGS_TRY_SHELL) && errno == ENOEXEC) - { - char *const *argv = args->argv; - ptrdiff_t argc = args->argc; - - /* Construct an argument list for the shell. */ - char *new_argv[argc + 2]; - new_argv[0] = (char *) _PATH_BSHELL; - new_argv[1] = (char *) args->file; - if (argc > 1) - memcpy (new_argv + 2, argv + 1, argc * sizeof (char *)); - else - new_argv[2] = NULL; - - /* Execute the shell. */ - args->exec (new_argv[0], new_argv, args->envp); - } -} - -/* Function used in the clone call to setup the signals mask, posix_spawn - attributes, and file actions. */ -static int -__spawni_child (void *arguments) -{ - struct posix_spawn_args *args = arguments; - const posix_spawnattr_t *restrict attr = args->attr; - const posix_spawn_file_actions_t *file_actions = args->fa; - int ret; - - __close (args->pipe[0]); - - /* Set signal default action. */ - if ((attr->__flags & POSIX_SPAWN_SETSIGDEF) != 0) - { - /* We have to iterate over all signals. This could possibly be - done better but it requires system specific solutions since - the sigset_t data type can be very different on different - architectures. */ - int sig; - struct sigaction sa; - - memset (&sa, '\0', sizeof (sa)); - sa.sa_handler = SIG_DFL; - - for (sig = 1; sig <= _NSIG; ++sig) - if (__sigismember (&attr->__sd, sig) != 0 - && __sigaction (sig, &sa, NULL) != 0) - goto fail; - } - -#ifdef _POSIX_PRIORITY_SCHEDULING - /* Set the scheduling algorithm and parameters. */ - if ((attr->__flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER)) - == POSIX_SPAWN_SETSCHEDPARAM) - { - if (__sched_setparam (0, &attr->__sp) == -1) - goto fail; - } - else if ((attr->__flags & POSIX_SPAWN_SETSCHEDULER) != 0) - { - if (__sched_setscheduler (0, attr->__policy, &attr->__sp) == -1) - goto fail; - } -#endif - - /* Set the process session ID. */ - if ((attr->__flags & POSIX_SPAWN_SETSID) != 0 - && __setsid () < 0) - goto fail; - - /* Set the process group ID. */ - if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0 - && __setpgid (0, attr->__pgrp) != 0) - goto fail; - - /* Set the effective user and group IDs. */ - if ((attr->__flags & POSIX_SPAWN_RESETIDS) != 0 - && (local_seteuid (__getuid ()) != 0 - || local_setegid (__getgid ())) != 0) - goto fail; - - /* Execute the file actions. */ - if (file_actions != NULL) - { - int cnt; - struct rlimit64 fdlimit; - bool have_fdlimit = false; - - for (cnt = 0; cnt < file_actions->__used; ++cnt) - { - struct __spawn_action *action = &file_actions->__actions[cnt]; - - switch (action->tag) - { - case spawn_do_close: - if (__close_nocancel (action->action.close_action.fd) != 0) - { - if (have_fdlimit == 0) - { - __getrlimit64 (RLIMIT_NOFILE, &fdlimit); - have_fdlimit = true; - } - - /* Only signal errors for file descriptors out of range. */ - if (action->action.close_action.fd < 0 - || action->action.close_action.fd >= fdlimit.rlim_cur) - goto fail; - } - break; - - case spawn_do_open: - { - /* POSIX states that if fildes was already an open file descriptor, - it shall be closed before the new file is opened. This avoid - pontential issues when posix_spawn plus addopen action is called - with the process already at maximum number of file descriptor - opened and also for multiple actions on single-open special - paths (like /dev/watchdog). */ - __close_nocancel (action->action.open_action.fd); - - int new_fd = __open_nocancel (action->action.open_action.path, - action->action.open_action.oflag - | O_LARGEFILE, - action->action.open_action.mode); - - if (new_fd == -1) - goto fail; - - /* Make sure the desired file descriptor is used. */ - if (new_fd != action->action.open_action.fd) - { - if (__dup2 (new_fd, action->action.open_action.fd) - != action->action.open_action.fd) - goto fail; - - if (__close_nocancel (new_fd) != 0) - goto fail; - } - } - break; - - case spawn_do_dup2: - /* Austin Group issue #411 requires adddup2 action with source - and destination being equal to remove close-on-exec flag. */ - if (action->action.dup2_action.fd - == action->action.dup2_action.newfd) - { - int fd = action->action.dup2_action.newfd; - int flags = __fcntl (fd, F_GETFD, 0); - if (flags == -1) - goto fail; - if (__fcntl (fd, F_SETFD, flags & ~FD_CLOEXEC) == -1) - goto fail; - } - else if (__dup2 (action->action.dup2_action.fd, - action->action.dup2_action.newfd) - != action->action.dup2_action.newfd) - goto fail; - break; - - case spawn_do_chdir: - if (__chdir (action->action.chdir_action.path) != 0) - goto fail; - break; - - case spawn_do_fchdir: - if (__fchdir (action->action.fchdir_action.fd) != 0) - goto fail; - break; - - case spawn_do_closefrom: - __set_errno (EINVAL); - goto fail; - } - } - } - - /* Set the initial signal mask of the child if POSIX_SPAWN_SETSIGMASK - is set, otherwise restore the previous one. */ - __sigprocmask (SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK) - ? &attr->__ss : &args->oldmask, 0); - - args->exec (args->file, args->argv, args->envp); - - /* This is compatibility function required to enable posix_spawn run - script without shebang definition for older posix_spawn versions - (2.15). */ - maybe_script_execute (args); - -fail: - /* errno should have an appropriate non-zero value; otherwise, - there's a bug in glibc or the kernel. For lack of an error code - (EINTERNALBUG) describing that, use ECHILD. Another option would - be to set args->err to some negative sentinel and have the parent - abort(), but that seems needlessly harsh. */ - ret = errno ? : ECHILD; - if (ret) - /* Since sizeof errno < PIPE_BUF, the write is atomic. */ - while (__write_nocancel (args->pipe[1], &ret, sizeof (ret)) < 0); - - _exit (SPAWN_ERROR); -} - -/* Spawn a new process executing PATH with the attributes describes in *ATTRP. - Before running the process perform the actions described in FILE-ACTIONS. */ -int -__spawnix (pid_t *pid, const char *file, - const posix_spawn_file_actions_t *file_actions, - const posix_spawnattr_t *attrp, char *const argv[], - char *const envp[], int xflags, - int (*exec) (const char *, char *const *, char *const *)) -{ - struct posix_spawn_args args; - int ec; - - if (__pipe2 (args.pipe, O_CLOEXEC)) - return errno; - - /* Disable asynchronous cancellation. */ - int state; - __libc_ptf_call (__pthread_setcancelstate, - (PTHREAD_CANCEL_DISABLE, &state), 0); - - ptrdiff_t argc = 0; - ptrdiff_t limit = INT_MAX - 1; - while (argv[argc++] != NULL) - if (argc == limit) - { - errno = E2BIG; - return errno; - } - - args.file = file; - args.exec = exec; - args.fa = file_actions; - args.attr = attrp ? attrp : &(const posix_spawnattr_t) { 0 }; - args.argv = argv; - args.argc = argc; - args.envp = envp; - args.xflags = xflags; - - /* Generate the new process. */ - pid_t new_pid = __fork (); - - if (new_pid == 0) - __spawni_child (&args); - else if (new_pid > 0) - { - __close (args.pipe[1]); - - if (__read (args.pipe[0], &ec, sizeof ec) != sizeof ec) - ec = 0; - else - __waitpid (new_pid, &(int) { 0 }, 0); - } - else - ec = errno; - - __close (args.pipe[0]); - - if ((ec == 0) && (pid != NULL)) - *pid = new_pid; - - __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); - - return ec; -} - -int -__spawni (pid_t * pid, const char *file, - const posix_spawn_file_actions_t * acts, - const posix_spawnattr_t * attrp, char *const argv[], - char *const envp[], int xflags) -{ - /* It uses __execvpex to avoid run ENOEXEC in non compatibility mode (it - will be handled by maybe_script_execute). */ - return __spawnix (pid, file, acts, attrp, argv, envp, xflags, - xflags & SPAWN_XFLAGS_USE_PATH ? __execvpex : __execve); -}