xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <libc-lock.h> |
| 26 | #include <sysdep-cancel.h> |
| 27 | |
| 28 | |
| 29 | #define SHELL_PATH "/bin/sh" /* Path of the shell. */ |
| 30 | #define SHELL_NAME "sh" /* Name to give it. */ |
| 31 | |
| 32 | |
| 33 | #ifdef _LIBC_REENTRANT |
| 34 | static struct sigaction intr, quit; |
| 35 | static int sa_refcntr; |
| 36 | __libc_lock_define_initialized (static, lock); |
| 37 | |
| 38 | # define DO_LOCK() __libc_lock_lock (lock) |
| 39 | # define DO_UNLOCK() __libc_lock_unlock (lock) |
| 40 | # define INIT_LOCK() ({ __libc_lock_init (lock); sa_refcntr = 0; }) |
| 41 | # define ADD_REF() sa_refcntr++ |
| 42 | # define SUB_REF() --sa_refcntr |
| 43 | #else |
| 44 | # define DO_LOCK() |
| 45 | # define DO_UNLOCK() |
| 46 | # define INIT_LOCK() |
| 47 | # define ADD_REF() 0 |
| 48 | # define SUB_REF() 0 |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | /* Execute LINE as a shell command, returning its status. */ |
| 53 | static int |
| 54 | do_system (const char *line) |
| 55 | { |
| 56 | int status, save; |
| 57 | pid_t pid; |
| 58 | struct sigaction sa; |
| 59 | #ifndef _LIBC_REENTRANT |
| 60 | struct sigaction intr, quit; |
| 61 | #endif |
| 62 | sigset_t omask; |
| 63 | |
| 64 | sa.sa_handler = SIG_IGN; |
| 65 | sa.sa_flags = 0; |
| 66 | __sigemptyset (&sa.sa_mask); |
| 67 | |
| 68 | DO_LOCK (); |
| 69 | if (ADD_REF () == 0) |
| 70 | { |
| 71 | if (__sigaction (SIGINT, &sa, &intr) < 0) |
| 72 | { |
| 73 | (void) SUB_REF (); |
| 74 | goto out; |
| 75 | } |
| 76 | if (__sigaction (SIGQUIT, &sa, &quit) < 0) |
| 77 | { |
| 78 | save = errno; |
| 79 | (void) SUB_REF (); |
| 80 | goto out_restore_sigint; |
| 81 | } |
| 82 | } |
| 83 | DO_UNLOCK (); |
| 84 | |
| 85 | /* We reuse the bitmap in the 'sa' structure. */ |
| 86 | __sigaddset (&sa.sa_mask, SIGCHLD); |
| 87 | save = errno; |
| 88 | if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0) |
| 89 | { |
| 90 | #ifndef _LIBC |
| 91 | if (errno == ENOSYS) |
| 92 | __set_errno (save); |
| 93 | else |
| 94 | #endif |
| 95 | { |
| 96 | DO_LOCK (); |
| 97 | if (SUB_REF () == 0) |
| 98 | { |
| 99 | save = errno; |
| 100 | (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); |
| 101 | out_restore_sigint: |
| 102 | (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); |
| 103 | __set_errno (save); |
| 104 | } |
| 105 | out: |
| 106 | DO_UNLOCK (); |
| 107 | return -1; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #ifdef CLEANUP_HANDLER |
| 112 | CLEANUP_HANDLER; |
| 113 | #endif |
| 114 | |
| 115 | #ifdef FORK |
| 116 | pid = FORK (); |
| 117 | #else |
| 118 | pid = __fork (); |
| 119 | #endif |
| 120 | if (pid == (pid_t) 0) |
| 121 | { |
| 122 | /* Child side. */ |
| 123 | const char *new_argv[4]; |
| 124 | new_argv[0] = SHELL_NAME; |
| 125 | new_argv[1] = "-c"; |
| 126 | new_argv[2] = line; |
| 127 | new_argv[3] = NULL; |
| 128 | |
| 129 | /* Restore the signals. */ |
| 130 | (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); |
| 131 | (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); |
| 132 | (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL); |
| 133 | INIT_LOCK (); |
| 134 | |
| 135 | /* Exec the shell. */ |
| 136 | (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ); |
| 137 | _exit (127); |
| 138 | } |
| 139 | else if (pid < (pid_t) 0) |
| 140 | /* The fork failed. */ |
| 141 | status = -1; |
| 142 | else |
| 143 | /* Parent side. */ |
| 144 | { |
| 145 | /* Note the system() is a cancellation point. But since we call |
| 146 | waitpid() which itself is a cancellation point we do not |
| 147 | have to do anything here. */ |
| 148 | if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid) |
| 149 | status = -1; |
| 150 | } |
| 151 | |
| 152 | #ifdef CLEANUP_HANDLER |
| 153 | CLEANUP_RESET; |
| 154 | #endif |
| 155 | |
| 156 | save = errno; |
| 157 | DO_LOCK (); |
| 158 | if ((SUB_REF () == 0 |
| 159 | && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL) |
| 160 | | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0) |
| 161 | || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0) |
| 162 | { |
| 163 | #ifndef _LIBC |
| 164 | /* glibc cannot be used on systems without waitpid. */ |
| 165 | if (errno == ENOSYS) |
| 166 | __set_errno (save); |
| 167 | else |
| 168 | #endif |
| 169 | status = -1; |
| 170 | } |
| 171 | DO_UNLOCK (); |
| 172 | |
| 173 | return status; |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | __libc_system (const char *line) |
| 178 | { |
| 179 | if (line == NULL) |
| 180 | /* Check that we have a command processor available. It might |
| 181 | not be available after a chroot(), for example. */ |
| 182 | return do_system ("exit 0") == 0; |
| 183 | |
| 184 | return do_system (line); |
| 185 | } |
| 186 | weak_alias (__libc_system, system) |