| 1 | /* Minimal /bin/sh for in-container use. |
| 2 | Copyright (C) 2018-2021 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #define _FILE_OFFSET_BITS 64 |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sched.h> |
| 25 | #include <sys/syscall.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <dirent.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/fcntl.h> |
| 32 | #include <sys/file.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <stdarg.h> |
| 35 | #include <sys/sysmacros.h> |
| 36 | #include <ctype.h> |
| 37 | #include <utime.h> |
| 38 | #include <errno.h> |
| 39 | #include <error.h> |
| 40 | |
| 41 | #include <support/support.h> |
| 42 | |
| 43 | /* Design considerations |
| 44 | |
| 45 | General rule: optimize for developer time, not run time. |
| 46 | |
| 47 | Specifically: |
| 48 | |
| 49 | * Don't worry about slow algorithms |
| 50 | * Don't worry about free'ing memory |
| 51 | * Don't implement anything the testsuite doesn't need. |
| 52 | * Line and argument counts are limited, see below. |
| 53 | |
| 54 | */ |
| 55 | |
| 56 | #define MAX_ARG_COUNT 100 |
| 57 | #define MAX_LINE_LENGTH 1000 |
| 58 | |
| 59 | /* Debugging is enabled via --debug, which must be the first argument. */ |
| 60 | static int debug_mode = 0; |
| 61 | #define dprintf if (debug_mode) fprintf |
| 62 | |
| 63 | /* Emulate the "/bin/true" command. Arguments are ignored. */ |
| 64 | static int |
| 65 | true_func (char **argv) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* Emulate the "/bin/echo" command. Options are ignored, arguments |
| 71 | are printed to stdout. */ |
| 72 | static int |
| 73 | echo_func (char **argv) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | for (i = 0; argv[i]; i++) |
| 78 | { |
| 79 | if (i > 0) |
| 80 | putchar (' '); |
| 81 | fputs (argv[i], stdout); |
| 82 | } |
| 83 | putchar ('\n'); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* Emulate the "/bin/cp" command. Options are ignored. Only copies |
| 89 | one source file to one destination file. Directory destinations |
| 90 | are not supported. */ |
| 91 | static int |
| 92 | copy_func (char **argv) |
| 93 | { |
| 94 | char *sname = argv[0]; |
| 95 | char *dname = argv[1]; |
| 96 | int sfd, dfd; |
| 97 | struct stat st; |
| 98 | |
| 99 | sfd = open (sname, O_RDONLY); |
| 100 | if (sfd < 0) |
| 101 | { |
| 102 | fprintf (stderr, "cp: unable to open %s for reading: %s\n" , |
| 103 | sname, strerror (errno)); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | if (fstat (sfd, &st) < 0) |
| 108 | { |
| 109 | fprintf (stderr, "cp: unable to fstat %s: %s\n" , |
| 110 | sname, strerror (errno)); |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600); |
| 115 | if (dfd < 0) |
| 116 | { |
| 117 | fprintf (stderr, "cp: unable to open %s for writing: %s\n" , |
| 118 | dname, strerror (errno)); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | if (support_copy_file_range (sfd, 0, dfd, 0, st.st_size, 0) != st.st_size) |
| 123 | { |
| 124 | fprintf (stderr, "cp: cannot copy file %s to %s: %s\n" , |
| 125 | sname, dname, strerror (errno)); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | close (sfd); |
| 130 | close (dfd); |
| 131 | |
| 132 | chmod (dname, st.st_mode & 0777); |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /* Emulate the 'exit' builtin. The exit value is optional. */ |
| 139 | static int |
| 140 | exit_func (char **argv) |
| 141 | { |
| 142 | int exit_val = 0; |
| 143 | |
| 144 | if (argv[0] != 0) |
| 145 | exit_val = atoi (argv[0]) & 0xff; |
| 146 | exit (exit_val); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* Emulate the "/bin/kill" command. Options are ignored. */ |
| 151 | static int |
| 152 | kill_func (char **argv) |
| 153 | { |
| 154 | int signum = SIGTERM; |
| 155 | int i; |
| 156 | |
| 157 | for (i = 0; argv[i]; i++) |
| 158 | { |
| 159 | pid_t pid; |
| 160 | if (strcmp (argv[i], "$$" ) == 0) |
| 161 | pid = getpid (); |
| 162 | else |
| 163 | pid = atoi (argv[i]); |
| 164 | kill (pid, signum); |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* This is a list of all the built-in commands we understand. */ |
| 170 | static struct { |
| 171 | const char *name; |
| 172 | int (*func) (char **argv); |
| 173 | } builtin_funcs[] = { |
| 174 | { "true" , true_func }, |
| 175 | { "echo" , echo_func }, |
| 176 | { "cp" , copy_func }, |
| 177 | { "exit" , exit_func }, |
| 178 | { "kill" , kill_func }, |
| 179 | { NULL, NULL } |
| 180 | }; |
| 181 | |
| 182 | /* Run one tokenized command. argv[0] is the command. argv is |
| 183 | NULL-terminated. */ |
| 184 | static void |
| 185 | run_command_array (char **argv) |
| 186 | { |
| 187 | int i, j; |
| 188 | pid_t pid; |
| 189 | int status; |
| 190 | int (*builtin_func) (char **args); |
| 191 | |
| 192 | if (argv[0] == NULL) |
| 193 | return; |
| 194 | |
| 195 | builtin_func = NULL; |
| 196 | |
| 197 | int new_stdin = 0; |
| 198 | int new_stdout = 1; |
| 199 | int new_stderr = 2; |
| 200 | |
| 201 | dprintf (stderr, "run_command_array starting\n" ); |
| 202 | for (i = 0; argv[i]; i++) |
| 203 | dprintf (stderr, " argv [%d] `%s'\n" , i, argv[i]); |
| 204 | |
| 205 | for (j = i = 0; argv[i]; i++) |
| 206 | { |
| 207 | if (strcmp (argv[i], "<" ) == 0 && argv[i + 1]) |
| 208 | { |
| 209 | new_stdin = open (argv[i + 1], O_WRONLY|O_CREAT|O_TRUNC, 0777); |
| 210 | ++i; |
| 211 | continue; |
| 212 | } |
| 213 | if (strcmp (argv[i], ">" ) == 0 && argv[i + 1]) |
| 214 | { |
| 215 | new_stdout = open (argv[i + 1], O_WRONLY|O_CREAT|O_TRUNC, 0777); |
| 216 | ++i; |
| 217 | continue; |
| 218 | } |
| 219 | if (strcmp (argv[i], ">>" ) == 0 && argv[i + 1]) |
| 220 | { |
| 221 | new_stdout = open (argv[i + 1], O_WRONLY|O_CREAT|O_APPEND, 0777); |
| 222 | ++i; |
| 223 | continue; |
| 224 | } |
| 225 | if (strcmp (argv[i], "2>" ) == 0 && argv[i + 1]) |
| 226 | { |
| 227 | new_stderr = open (argv[i + 1], O_WRONLY|O_CREAT|O_TRUNC, 0777); |
| 228 | ++i; |
| 229 | continue; |
| 230 | } |
| 231 | argv[j++] = argv[i]; |
| 232 | } |
| 233 | argv[j] = NULL; |
| 234 | |
| 235 | |
| 236 | for (i = 0; builtin_funcs[i].name != NULL; i++) |
| 237 | if (strcmp (argv[0], builtin_funcs[i].name) == 0) |
| 238 | builtin_func = builtin_funcs[i].func; |
| 239 | |
| 240 | dprintf (stderr, "builtin %p argv0 `%s'\n" , builtin_func, argv[0]); |
| 241 | |
| 242 | pid = fork (); |
| 243 | if (pid < 0) |
| 244 | { |
| 245 | fprintf (stderr, "sh: fork failed\n" ); |
| 246 | exit (1); |
| 247 | } |
| 248 | |
| 249 | if (pid == 0) |
| 250 | { |
| 251 | if (new_stdin != 0) |
| 252 | { |
| 253 | dup2 (new_stdin, 0); |
| 254 | close (new_stdin); |
| 255 | } |
| 256 | if (new_stdout != 1) |
| 257 | { |
| 258 | dup2 (new_stdout, 1); |
| 259 | close (new_stdout); |
| 260 | } |
| 261 | if (new_stderr != 2) |
| 262 | { |
| 263 | dup2 (new_stderr, 2); |
| 264 | close (new_stderr); |
| 265 | } |
| 266 | |
| 267 | if (builtin_func != NULL) |
| 268 | exit (builtin_func (argv + 1)); |
| 269 | |
| 270 | execvp (argv[0], argv); |
| 271 | |
| 272 | fprintf (stderr, "sh: execing %s failed: %s" , |
| 273 | argv[0], strerror (errno)); |
| 274 | exit (127); |
| 275 | } |
| 276 | |
| 277 | waitpid (pid, &status, 0); |
| 278 | |
| 279 | dprintf (stderr, "exiting run_command_array\n" ); |
| 280 | |
| 281 | if (WIFEXITED (status)) |
| 282 | { |
| 283 | int rv = WEXITSTATUS (status); |
| 284 | if (rv) |
| 285 | exit (rv); |
| 286 | } |
| 287 | else if (WIFSIGNALED (status)) |
| 288 | { |
| 289 | int sig = WTERMSIG (status); |
| 290 | raise (sig); |
| 291 | } |
| 292 | else |
| 293 | exit (1); |
| 294 | } |
| 295 | |
| 296 | /* Run one command-as-a-string, by tokenizing it. Limited to |
| 297 | MAX_ARG_COUNT arguments. Simple substitution is done of $1 to $9 |
| 298 | (as whole separate tokens) from iargs[]. Quoted strings work if |
| 299 | the quotes wrap whole tokens; i.e. "foo bar" but not foo" bar". */ |
| 300 | static void |
| 301 | run_command_string (const char *cmdline, const char **iargs) |
| 302 | { |
| 303 | char *args[MAX_ARG_COUNT+1]; |
| 304 | int ap = 0; |
| 305 | const char *start, *end; |
| 306 | int nargs; |
| 307 | |
| 308 | for (nargs = 0; iargs[nargs] != NULL; ++nargs) |
| 309 | ; |
| 310 | |
| 311 | dprintf (stderr, "run_command_string starting: '%s'\n" , cmdline); |
| 312 | |
| 313 | while (ap < MAX_ARG_COUNT) |
| 314 | { |
| 315 | /* If the argument is quoted, this is the quote character, else NUL. */ |
| 316 | int in_quote = 0; |
| 317 | |
| 318 | /* Skip whitespace up to the next token. */ |
| 319 | while (*cmdline && isspace (*cmdline)) |
| 320 | cmdline ++; |
| 321 | if (*cmdline == 0) |
| 322 | break; |
| 323 | |
| 324 | start = cmdline; |
| 325 | /* Check for quoted argument. */ |
| 326 | in_quote = (*cmdline == '\'' || *cmdline == '"') ? *cmdline : 0; |
| 327 | |
| 328 | /* Skip to end of token; either by whitespace or matching quote. */ |
| 329 | dprintf (stderr, "in_quote %d\n" , in_quote); |
| 330 | while (*cmdline |
| 331 | && (!isspace (*cmdline) || in_quote)) |
| 332 | { |
| 333 | if (*cmdline == in_quote |
| 334 | && cmdline != start) |
| 335 | in_quote = 0; |
| 336 | dprintf (stderr, "[%c]%d " , *cmdline, in_quote); |
| 337 | cmdline ++; |
| 338 | } |
| 339 | dprintf (stderr, "\n" ); |
| 340 | |
| 341 | /* Allocate space for this token and store it in args[]. */ |
| 342 | end = cmdline; |
| 343 | dprintf (stderr, "start<%s> end<%s>\n" , start, end); |
| 344 | args[ap] = (char *) xmalloc (end - start + 1); |
| 345 | memcpy (args[ap], start, end - start); |
| 346 | args[ap][end - start] = 0; |
| 347 | |
| 348 | /* Strip off quotes, if found. */ |
| 349 | dprintf (stderr, "args[%d] = <%s>\n" , ap, args[ap]); |
| 350 | if (args[ap][0] == '\'' |
| 351 | && args[ap][strlen (args[ap])-1] == '\'') |
| 352 | { |
| 353 | args[ap][strlen (args[ap])-1] = 0; |
| 354 | args[ap] ++; |
| 355 | } |
| 356 | |
| 357 | else if (args[ap][0] == '"' |
| 358 | && args[ap][strlen (args[ap])-1] == '"') |
| 359 | { |
| 360 | args[ap][strlen (args[ap])-1] = 0; |
| 361 | args[ap] ++; |
| 362 | } |
| 363 | |
| 364 | /* Replace positional parameters like $4. */ |
| 365 | else if (args[ap][0] == '$' |
| 366 | && isdigit (args[ap][1]) |
| 367 | && args[ap][2] == 0) |
| 368 | { |
| 369 | int a = args[ap][1] - '1'; |
| 370 | if (0 <= a && a < nargs) |
| 371 | args[ap] = strdup (iargs[a]); |
| 372 | } |
| 373 | |
| 374 | ap ++; |
| 375 | |
| 376 | if (*cmdline == 0) |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | /* Lastly, NULL terminate the array and run it. */ |
| 381 | args[ap] = NULL; |
| 382 | run_command_array (args); |
| 383 | } |
| 384 | |
| 385 | /* Run a script by reading lines and passing them to the above |
| 386 | function. */ |
| 387 | static void |
| 388 | run_script (const char *filename, const char **args) |
| 389 | { |
| 390 | char line[MAX_LINE_LENGTH + 1]; |
| 391 | dprintf (stderr, "run_script starting: '%s'\n" , filename); |
| 392 | FILE *f = fopen (filename, "r" ); |
| 393 | if (f == NULL) |
| 394 | { |
| 395 | fprintf (stderr, "sh: %s: %s\n" , filename, strerror (errno)); |
| 396 | exit (1); |
| 397 | } |
| 398 | while (fgets (line, sizeof (line), f) != NULL) |
| 399 | { |
| 400 | if (line[0] == '#') |
| 401 | { |
| 402 | dprintf (stderr, "comment: %s\n" , line); |
| 403 | continue; |
| 404 | } |
| 405 | run_command_string (line, args); |
| 406 | } |
| 407 | fclose (f); |
| 408 | } |
| 409 | |
| 410 | int |
| 411 | main (int argc, const char **argv) |
| 412 | { |
| 413 | int i; |
| 414 | |
| 415 | if (strcmp (argv[1], "--debug" ) == 0) |
| 416 | { |
| 417 | debug_mode = 1; |
| 418 | --argc; |
| 419 | ++argv; |
| 420 | } |
| 421 | |
| 422 | dprintf (stderr, "container-sh starting:\n" ); |
| 423 | for (i = 0; i < argc; i++) |
| 424 | dprintf (stderr, " argv[%d] is `%s'\n" , i, argv[i]); |
| 425 | |
| 426 | if (strcmp (argv[1], "-c" ) == 0) |
| 427 | run_command_string (argv[2], argv+3); |
| 428 | else |
| 429 | run_script (argv[1], argv+2); |
| 430 | |
| 431 | dprintf (stderr, "normal exit 0\n" ); |
| 432 | return 0; |
| 433 | } |
| 434 | |