Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini run-parts implementation for busybox |
| 4 | * |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Bernhard Fischer |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 7 | * Based on a older version that was in busybox which was 1k big.. |
| 8 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 9 | * |
| 10 | * Based on the Debian run-parts program, version 1.15 |
| 11 | * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>, |
| 12 | * Copyright (C) 1996-1999 Guy Maor <maor@debian.org> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 14 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 15 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* This is my first attempt to write a program in C (well, this is my first |
| 19 | * attempt to write a program! :-) . */ |
| 20 | |
| 21 | /* This piece of code is heavily based on the original version of run-parts, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | * taken from debian-utils. I've only removed the long options and a the |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 23 | * report mode. As the original run-parts support only long options, I've |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 24 | * broken compatibility because the BusyBox policy doesn't allow them. |
| 25 | * The supported options are: |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 26 | * -t test. Print the name of the files to be executed, without |
| 27 | * execute them. |
| 28 | * -a ARG argument. Pass ARG as an argument the program executed. It can |
| 29 | * be repeated to pass multiple arguments. |
| 30 | * -u MASK umask. Set the umask of the program executed to MASK. |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 31 | */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 32 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
| 36 | |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 37 | struct globals { |
| 38 | char **names; |
| 39 | int cur; |
| 40 | char *cmd[1]; |
| 41 | }; |
| 42 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 43 | #define names (G.names) |
| 44 | #define cur (G.cur ) |
| 45 | #define cmd (G.cmd ) |
| 46 | |
| 47 | enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(struct globals)) / sizeof(cmd[0]) }; |
| 48 | |
| 49 | enum { |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 50 | OPT_r = (1 << 0), |
| 51 | OPT_a = (1 << 1), |
| 52 | OPT_u = (1 << 2), |
| 53 | OPT_t = (1 << 3), |
| 54 | OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | #if ENABLE_FEATURE_RUN_PARTS_FANCY |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 58 | #define list_mode (option_mask32 & OPT_l) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 59 | #else |
| 60 | #define list_mode 0 |
| 61 | #endif |
| 62 | |
| 63 | /* Is this a valid filename (upper/lower alpha, digits, |
| 64 | * underscores, and hyphens only?) |
| 65 | */ |
| 66 | static bool invalid_name(const char *c) |
| 67 | { |
| 68 | c = bb_basename(c); |
| 69 | |
| 70 | while (*c && (isalnum(*c) || *c == '_' || *c == '-')) |
| 71 | c++; |
| 72 | |
| 73 | return *c; /* TRUE (!0) if terminating NUL is not reached */ |
| 74 | } |
| 75 | |
| 76 | static int bb_alphasort(const void *p1, const void *p2) |
| 77 | { |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 78 | int r = strcmp(*(char **) p1, *(char **) p2); |
| 79 | return (option_mask32 & OPT_r) ? -r : r; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 82 | static int act(const char *file, struct stat *statbuf, void *args ATTRIBUTE_UNUSED, int depth) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 83 | { |
| 84 | if (depth == 1) |
| 85 | return TRUE; |
| 86 | |
| 87 | if (depth == 2 |
| 88 | && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK)) |
| 89 | || invalid_name(file) |
| 90 | || (!list_mode && access(file, X_OK) != 0)) |
| 91 | ) { |
| 92 | return SKIP; |
| 93 | } |
| 94 | |
| 95 | names = xrealloc(names, (cur + 2) * sizeof(names[0])); |
| 96 | names[cur++] = xstrdup(file); |
| 97 | names[cur] = NULL; |
| 98 | |
| 99 | return TRUE; |
| 100 | } |
| 101 | |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 102 | #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 103 | static const char runparts_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 104 | "arg\0" Required_argument "a" |
| 105 | "umask\0" Required_argument "u" |
| 106 | "test\0" No_argument "t" |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_RUN_PARTS_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 108 | "list\0" No_argument "l" |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 109 | "reverse\0" No_argument "r" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 110 | //TODO: "verbose\0" No_argument "v" |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 111 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 112 | ; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 115 | int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 116 | int run_parts_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 117 | { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 118 | const char *umask_p = "22"; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 119 | llist_t *arg_list = NULL; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 120 | unsigned n; |
| 121 | int ret; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 122 | |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 123 | #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 124 | applet_long_options = runparts_longopts; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 125 | #endif |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 126 | /* We require exactly one argument: the directory name */ |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 127 | /* We require exactly one argument: the directory name */ |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 128 | opt_complementary = "=1:a::"; |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 129 | getopt32(argv, "ra:u:t"USE_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 130 | |
| 131 | umask(xstrtou_range(umask_p, 8, 0, 07777)); |
| 132 | |
| 133 | n = 1; |
| 134 | while (arg_list && n < NUM_CMD) { |
| 135 | cmd[n] = arg_list->data; |
| 136 | arg_list = arg_list->link; |
| 137 | n++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 138 | } |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 139 | /* cmd[n] = NULL; - is already zeroed out */ |
| 140 | |
| 141 | /* run-parts has to sort executables by name before running them */ |
| 142 | |
| 143 | recursive_action(argv[optind], |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 144 | ACTION_RECURSE|ACTION_FOLLOWLINKS, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 145 | act, /* file action */ |
| 146 | act, /* dir action */ |
| 147 | NULL, /* user data */ |
| 148 | 1 /* depth */ |
| 149 | ); |
| 150 | |
| 151 | if (!names) |
| 152 | return 0; |
| 153 | |
| 154 | qsort(names, cur, sizeof(char *), bb_alphasort); |
| 155 | |
| 156 | n = 0; |
| 157 | while (1) { |
| 158 | char *name = *names++; |
| 159 | if (!name) |
| 160 | break; |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 161 | if (option_mask32 & (OPT_t | OPT_l)) { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 162 | puts(name); |
| 163 | continue; |
| 164 | } |
| 165 | cmd[0] = name; |
| 166 | ret = wait4pid(spawn(cmd)); |
| 167 | if (ret == 0) |
| 168 | continue; |
| 169 | n = 1; |
| 170 | if (ret < 0) |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 171 | bb_perror_msg("can't exec %s", name); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 172 | else /* ret > 0 */ |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 173 | bb_error_msg("%s exited with code %d", name, ret); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return n; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 177 | } |