blob: ea019c27c3aebb0a2e8090435ca7939e3bdf9ac6 [file] [log] [blame]
Eric Andersen7d682902001-10-31 10:59:29 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini run-parts implementation for busybox
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2007 Bernhard Reutner-Fischer
Eric Andersen7d682902001-10-31 10:59:29 +00006 *
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +00007 * 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 Andersen7d682902001-10-31 10:59:29 +00009 *
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 Andersenc7bda1c2004-03-15 08:29:22 +000013 *
Eric Andersen7d682902001-10-31 10:59:29 +000014 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +000015 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Eric Andersen7d682902001-10-31 10:59:29 +000016 */
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 Andersenc7bda1c2004-03-15 08:29:22 +000022 * taken from debian-utils. I've only removed the long options and a the
Eric Andersen7d682902001-10-31 10:59:29 +000023 * report mode. As the original run-parts support only long options, I've
Eric Andersenc7bda1c2004-03-15 08:29:22 +000024 * broken compatibility because the BusyBox policy doesn't allow them.
25 * The supported options are:
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000026 * -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 Andersenfb74a452001-12-18 14:06:03 +000031 */
Eric Andersen7d682902001-10-31 10:59:29 +000032
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
34
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000035struct globals {
36 char **names;
37 int cur;
38 char *cmd[1];
39};
40#define G (*(struct globals*)&bb_common_bufsiz1)
41#define names (G.names)
42#define cur (G.cur )
43#define cmd (G.cmd )
44
Denis Vlasenkod50dda82008-06-15 05:40:56 +000045enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000046
47enum {
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000048 OPT_r = (1 << 0),
49 OPT_a = (1 << 1),
50 OPT_u = (1 << 2),
51 OPT_t = (1 << 3),
52 OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000053};
54
55#if ENABLE_FEATURE_RUN_PARTS_FANCY
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000056#define list_mode (option_mask32 & OPT_l)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000057#else
58#define list_mode 0
59#endif
60
61/* Is this a valid filename (upper/lower alpha, digits,
62 * underscores, and hyphens only?)
63 */
64static bool invalid_name(const char *c)
65{
66 c = bb_basename(c);
67
68 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
69 c++;
70
71 return *c; /* TRUE (!0) if terminating NUL is not reached */
72}
73
74static int bb_alphasort(const void *p1, const void *p2)
75{
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000076 int r = strcmp(*(char **) p1, *(char **) p2);
77 return (option_mask32 & OPT_r) ? -r : r;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000078}
79
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000080static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000081{
82 if (depth == 1)
83 return TRUE;
84
85 if (depth == 2
86 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
87 || invalid_name(file)
88 || (!list_mode && access(file, X_OK) != 0))
89 ) {
90 return SKIP;
91 }
92
Denis Vlasenkodeeed592008-07-08 05:14:36 +000093 names = xrealloc_vector(names, 4, cur);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000094 names[cur++] = xstrdup(file);
Denis Vlasenko27842282008-08-04 13:20:36 +000095 /*names[cur] = NULL; - xrealloc_vector did it */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000096
97 return TRUE;
98}
99
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000100#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000101static const char runparts_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000102 "arg\0" Required_argument "a"
103 "umask\0" Required_argument "u"
104 "test\0" No_argument "t"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000105#if ENABLE_FEATURE_RUN_PARTS_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000106 "list\0" No_argument "l"
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000107 "reverse\0" No_argument "r"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000108//TODO: "verbose\0" No_argument "v"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000109#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000110 ;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000111#endif
112
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000113int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000114int run_parts_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +0000115{
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000116 const char *umask_p = "22";
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000117 llist_t *arg_list = NULL;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000118 unsigned n;
119 int ret;
Eric Andersen7d682902001-10-31 10:59:29 +0000120
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000121#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000122 applet_long_options = runparts_longopts;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000123#endif
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000124 /* We require exactly one argument: the directory name */
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000125 /* We require exactly one argument: the directory name */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000126 opt_complementary = "=1:a::";
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000127 getopt32(argv, "ra:u:t"IF_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000128
129 umask(xstrtou_range(umask_p, 8, 0, 07777));
130
131 n = 1;
132 while (arg_list && n < NUM_CMD) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000133 cmd[n++] = llist_pop(&arg_list);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000134 }
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000135 /* cmd[n] = NULL; - is already zeroed out */
136
137 /* run-parts has to sort executables by name before running them */
138
139 recursive_action(argv[optind],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000140 ACTION_RECURSE|ACTION_FOLLOWLINKS,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000141 act, /* file action */
142 act, /* dir action */
143 NULL, /* user data */
144 1 /* depth */
145 );
146
147 if (!names)
148 return 0;
149
150 qsort(names, cur, sizeof(char *), bb_alphasort);
151
152 n = 0;
153 while (1) {
154 char *name = *names++;
155 if (!name)
156 break;
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000157 if (option_mask32 & (OPT_t | OPT_l)) {
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000158 puts(name);
159 continue;
160 }
161 cmd[0] = name;
162 ret = wait4pid(spawn(cmd));
163 if (ret == 0)
164 continue;
165 n = 1;
166 if (ret < 0)
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000167 bb_perror_msg("can't execute '%s'", name);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000168 else /* ret > 0 */
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000169 bb_error_msg("%s exited with code %d", name, ret);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000170 }
171
172 return n;
Eric Andersen7d682902001-10-31 10:59:29 +0000173}