Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 3 | * run command from specified directory |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * |
| 6 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 7 | * rewrite to vfork usage by |
| 8 | * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <dirent.h> |
| 23 | #include <unistd.h> |
| 24 | #include <ctype.h> |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 26 | |
| 27 | #include "libbb.h" |
| 28 | |
| 29 | /* valid_name */ |
| 30 | /* True or false? Is this a valid filename (upper/lower alpha, digits, |
| 31 | * underscores, and hyphens only?) |
| 32 | */ |
| 33 | static int valid_name(const struct dirent *d) |
| 34 | { |
| 35 | char *c = d->d_name; |
| 36 | |
| 37 | while (*c) { |
| 38 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { |
| 39 | return 0; |
| 40 | } |
| 41 | ++c; |
| 42 | } |
| 43 | return 1; |
| 44 | } |
| 45 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 46 | /* test mode = 1 is the same as official run_parts |
| 47 | * test_mode = 2 means to fail silently on missing directories |
Glenn L McGrath | 2e51a14 | 2003-01-20 23:50:59 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 50 | int run_parts(char **args, const unsigned char test_mode, char **env) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 51 | { |
| 52 | struct dirent **namelist = 0; |
| 53 | struct stat st; |
| 54 | char *filename; |
Glenn L McGrath | 157fea5 | 2002-11-21 22:17:11 +0000 | [diff] [blame] | 55 | char *arg0 = args[0]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 56 | int entries; |
| 57 | int i; |
| 58 | int exitstatus = 0; |
| 59 | |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 60 | #if __GNUC__ |
| 61 | /* Avoid longjmp clobbering */ |
| 62 | (void) &i; |
| 63 | (void) &exitstatus; |
| 64 | #endif |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 65 | /* scandir() isn't POSIX, but it makes things easy. */ |
Glenn L McGrath | 157fea5 | 2002-11-21 22:17:11 +0000 | [diff] [blame] | 66 | entries = scandir(arg0, &namelist, valid_name, alphasort); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 67 | |
| 68 | if (entries == -1) { |
Glenn L McGrath | 2e51a14 | 2003-01-20 23:50:59 +0000 | [diff] [blame] | 69 | if (test_mode & 2) { |
| 70 | return(2); |
| 71 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_perror_msg_and_die("failed to open directory %s", arg0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | for (i = 0; i < entries; i++) { |
| 76 | |
Glenn L McGrath | 157fea5 | 2002-11-21 22:17:11 +0000 | [diff] [blame] | 77 | filename = concat_path_file(arg0, namelist[i]->d_name); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 78 | |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 79 | xstat(filename, &st); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 80 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { |
Glenn L McGrath | 502907f | 2003-08-30 12:27:36 +0000 | [diff] [blame] | 81 | if (test_mode) { |
Glenn L McGrath | 2e51a14 | 2003-01-20 23:50:59 +0000 | [diff] [blame] | 82 | puts(filename); |
Glenn L McGrath | aad465e | 2003-01-20 23:34:12 +0000 | [diff] [blame] | 83 | } else { |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 84 | /* exec_errno is common vfork variable */ |
| 85 | volatile int exec_errno = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 86 | int result; |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 87 | int pid; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 88 | |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 89 | if ((pid = vfork()) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | bb_perror_msg_and_die("failed to fork"); |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 91 | } else if (!pid) { |
| 92 | args[0] = filename; |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 93 | execve(filename, args, env); |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 94 | exec_errno = errno; |
Glenn L McGrath | 39289b5 | 2002-11-15 22:18:01 +0000 | [diff] [blame] | 95 | _exit(1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 98 | waitpid(pid, &result, 0); |
| 99 | if(exec_errno) { |
| 100 | errno = exec_errno; |
Glenn L McGrath | 502907f | 2003-08-30 12:27:36 +0000 | [diff] [blame] | 101 | bb_perror_msg("failed to exec %s", filename); |
| 102 | exitstatus = 1; |
Eric Andersen | d130973 | 2003-05-27 20:45:59 +0000 | [diff] [blame] | 103 | } |
| 104 | if (WIFEXITED(result) && WEXITSTATUS(result)) { |
| 105 | bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); |
| 106 | exitstatus = 1; |
| 107 | } else if (WIFSIGNALED(result)) { |
| 108 | bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); |
| 109 | exitstatus = 1; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 112 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 113 | else if (!S_ISDIR(st.st_mode)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | bb_error_msg("component %s is not an executable plain file", filename); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 115 | exitstatus = 1; |
| 116 | } |
| 117 | |
| 118 | free(namelist[i]); |
| 119 | free(filename); |
| 120 | } |
| 121 | free(namelist); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 122 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 123 | return(exitstatus); |
| 124 | } |