| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is exec.def, from which is created exec.c. |
| 2 | It implements the builtin "exec" in Bash. |
| 3 | |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2019 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES exec.c |
| 22 | |
| 23 | $BUILTIN exec |
| 24 | $FUNCTION exec_builtin |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 25 | $SHORT_DOC exec [-cl] [-a name] [command [argument ...]] [redirection ...] |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Replace the shell with the given command. |
| 27 | |
| 28 | Execute COMMAND, replacing this shell with the specified program. |
| 29 | ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, |
| 30 | any redirections take effect in the current shell. |
| 31 | |
| 32 | Options: |
| 33 | -a name pass NAME as the zeroth argument to COMMAND |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 34 | -c execute COMMAND with an empty environment |
| 35 | -l place a dash in the zeroth argument to COMMAND |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 36 | |
| 37 | If the command cannot be executed, a non-interactive shell exits, unless |
| 38 | the shell option `execfail' is set. |
| 39 | |
| 40 | Exit Status: |
| 41 | Returns success unless COMMAND is not found or a redirection error occurs. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | $END |
| 43 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 44 | #include <config.h> |
| 45 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 46 | #include "../bashtypes.h" |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 47 | #include "posixstat.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 48 | #include <signal.h> |
| 49 | #include <errno.h> |
| 50 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | #if defined (HAVE_UNISTD_H) |
| 52 | # include <unistd.h> |
| 53 | #endif |
| 54 | |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 55 | #include <stdio.h> |
| 56 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | #include "../bashansi.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 58 | #include "../bashintl.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | |
| 60 | #include "../shell.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 61 | #include "../execute_cmd.h" |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 62 | #include "../findcmd.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 63 | #if defined (JOB_CONTROL) |
| 64 | # include "../jobs.h" |
| 65 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | #include "../flags.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | #include "../trap.h" |
| 68 | #if defined (HISTORY) |
| 69 | # include "../bashhist.h" |
| 70 | #endif |
| 71 | #include "common.h" |
| 72 | #include "bashgetopt.h" |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 73 | #include "input.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 74 | |
| 75 | /* Not all systems declare ERRNO in errno.h... and some systems #define it! */ |
| 76 | #if !defined (errno) |
| 77 | extern int errno; |
| 78 | #endif /* !errno */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 79 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | extern REDIRECT *redirection_undo_list; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 81 | extern char *exec_argv0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 83 | int no_exit_on_failed_exec; |
| 84 | |
| 85 | /* If the user wants this to look like a login shell, then |
| 86 | prepend a `-' onto NAME and return the new name. */ |
| 87 | static char * |
| 88 | mkdashname (name) |
| 89 | char *name; |
| 90 | { |
| 91 | char *ret; |
| 92 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 93 | ret = (char *)xmalloc (2 + strlen (name)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 94 | ret[0] = '-'; |
| 95 | strcpy (ret + 1, name); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 99 | int |
| 100 | exec_builtin (list) |
| 101 | WORD_LIST *list; |
| 102 | { |
| 103 | int exit_value = EXECUTION_FAILURE; |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 104 | int cleanenv, login, opt, orig_job_control; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 105 | char *argv0, *command, **args, **env, *newname, *com2; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 106 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 107 | cleanenv = login = 0; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 108 | exec_argv0 = argv0 = (char *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | |
| 110 | reset_internal_getopt (); |
| 111 | while ((opt = internal_getopt (list, "cla:")) != -1) |
| 112 | { |
| 113 | switch (opt) |
| 114 | { |
| 115 | case 'c': |
| 116 | cleanenv = 1; |
| 117 | break; |
| 118 | case 'l': |
| 119 | login = 1; |
| 120 | break; |
| 121 | case 'a': |
| 122 | argv0 = list_optarg; |
| 123 | break; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 124 | CASE_HELPOPT; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 125 | default: |
| 126 | builtin_usage (); |
| 127 | return (EX_USAGE); |
| 128 | } |
| 129 | } |
| 130 | list = loptend; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 131 | |
| 132 | /* First, let the redirections remain. */ |
| 133 | dispose_redirects (redirection_undo_list); |
| 134 | redirection_undo_list = (REDIRECT *)NULL; |
| 135 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | if (list == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 137 | return (EXECUTION_SUCCESS); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 138 | |
| 139 | #if defined (RESTRICTED_SHELL) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 140 | if (restricted) |
| 141 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 142 | sh_restricted ((char *)NULL); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 143 | return (EXECUTION_FAILURE); |
| 144 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 145 | #endif /* RESTRICTED_SHELL */ |
| 146 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 147 | args = strvec_from_word_list (list, 1, 0, (int *)NULL); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 148 | env = (char **)0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 149 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 150 | /* A command with a slash anywhere in its name is not looked up in $PATH. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 151 | command = absolute_program (args[0]) ? args[0] : search_for_command (args[0], 1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 152 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 153 | if (command == 0) |
| 154 | { |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 155 | if (file_isdir (args[0])) |
| 156 | { |
| 157 | #if defined (EISDIR) |
| 158 | builtin_error (_("%s: cannot execute: %s"), args[0], strerror (EISDIR)); |
| 159 | #else |
| 160 | builtin_error (_("%s: cannot execute: %s"), args[0], strerror (errno)); |
| 161 | #endif |
| 162 | exit_value = EX_NOEXEC; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | sh_notfound (args[0]); |
| 167 | exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */ |
| 168 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 169 | goto failed_exec; |
| 170 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 171 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 172 | com2 = full_pathname (command); |
| 173 | if (com2) |
| 174 | { |
| 175 | if (command != args[0]) |
| 176 | free (command); |
| 177 | command = com2; |
| 178 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 179 | |
| 180 | if (argv0) |
| 181 | { |
| 182 | free (args[0]); |
| 183 | args[0] = login ? mkdashname (argv0) : savestring (argv0); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 184 | exec_argv0 = savestring (args[0]); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 185 | } |
| 186 | else if (login) |
| 187 | { |
| 188 | newname = mkdashname (args[0]); |
| 189 | free (args[0]); |
| 190 | args[0] = newname; |
| 191 | } |
| 192 | |
| 193 | /* Decrement SHLVL by 1 so a new shell started here has the same value, |
| 194 | preserving the appearance. After we do that, we need to change the |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 195 | exported environment to include the new value. If we've already forked |
| 196 | and are in a subshell, we don't want to decrement the shell level, |
| 197 | since we are `increasing' the level */ |
| 198 | |
| 199 | if (cleanenv == 0 && (subshell_environment & SUBSHELL_PAREN) == 0) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 200 | adjust_shell_level (-1); |
| 201 | |
| 202 | if (cleanenv) |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 203 | { |
| 204 | env = strvec_create (1); |
| 205 | env[0] = (char *)0; |
| 206 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | else |
| 208 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 209 | maybe_make_export_env (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 210 | env = export_env; |
| 211 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 212 | |
| 213 | #if defined (HISTORY) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 214 | if (interactive_shell && subshell_environment == 0) |
| 215 | maybe_save_shell_history (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 216 | #endif /* HISTORY */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 217 | |
| 218 | restore_original_signals (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 219 | |
| 220 | #if defined (JOB_CONTROL) |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 221 | orig_job_control = job_control; /* XXX - was also interactive_shell */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 222 | if (subshell_environment == 0) |
| 223 | end_job_control (); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 224 | if (interactive || job_control) |
| 225 | default_tty_job_signals (); /* undo initialize_job_signals */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 226 | #endif /* JOB_CONTROL */ |
| 227 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 228 | #if defined (BUFFERED_INPUT) |
| 229 | if (default_buffered_input >= 0) |
| 230 | sync_buffered_stream (default_buffered_input); |
| 231 | #endif |
| 232 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 233 | exit_value = shell_execve (command, args, env); |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 234 | |
| 235 | /* We have to set this to NULL because shell_execve has called realloc() |
| 236 | to stuff more items at the front of the array, which may have caused |
| 237 | the memory to be freed by realloc(). We don't want to free it twice. */ |
| 238 | args = (char **)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 239 | if (cleanenv == 0) |
| 240 | adjust_shell_level (1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 241 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 242 | if (exit_value == EX_NOTFOUND) /* no duplicate error message */ |
| 243 | goto failed_exec; |
| 244 | else if (executable_file (command) == 0) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 245 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 246 | builtin_error (_("%s: cannot execute: %s"), command, strerror (errno)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 247 | exit_value = EX_NOEXEC; /* As per Posix.2, 3.14.6 */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 248 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 249 | else |
| 250 | file_error (command); |
| 251 | |
| 252 | failed_exec: |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 253 | FREE (command); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 254 | |
| 255 | if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0)) |
| 256 | exit_shell (exit_value); |
| 257 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 258 | if (args) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 259 | strvec_dispose (args); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 260 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 261 | if (env && env != export_env) |
| 262 | strvec_dispose (env); |
| 263 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 264 | initialize_traps (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 265 | initialize_signals (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 266 | |
| 267 | #if defined (JOB_CONTROL) |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 268 | if (orig_job_control) |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 269 | restart_job_control (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 270 | #endif /* JOB_CONTROL */ |
| 271 | |
| 272 | return (exit_value); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 273 | } |