blob: bf906a399ac2d65163187becae7c8b8a64e25f2a [file] [log] [blame]
Glenn L McGrath545106f2002-11-11 06:21:00 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini run-parts implementation for busybox
4 *
5 *
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
7 *
8 * Based on the Debian run-parts program, version 1.15
9 * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
10 * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 * 02111-1307 USA
27 *
28 */
29
30/* This is my first attempt to write a program in C (well, this is my first
31 * attempt to write a program! :-) . */
32
33/* This piece of code is heavily based on the original version of run-parts,
34 * taken from debian-utils. I've only removed the long options and a the
35 * report mode. As the original run-parts support only long options, I've
36 * broken compatibility because the BusyBox policy doesn't allow them.
37 * The supported options are:
38 * -t test. Print the name of the files to be executed, without
39 * execute them.
40 * -a ARG argument. Pass ARG as an argument the program executed. It can
41 * be repeated to pass multiple arguments.
42 * -u MASK umask. Set the umask of the program executed to MASK. */
43
44/* TODO
45 * done - convert calls to error in perror... and remove error()
46 * done - convert malloc/realloc to their x... counterparts
47 * done - remove catch_sigchld
48 * done - use bb's concat_path_file()
49 * done - declare run_parts_main() as extern and any other function as static?
50 */
51
52#include <sys/types.h>
53#include <sys/wait.h>
54#include <stdlib.h>
55#include <dirent.h>
56#include <unistd.h>
57#include <ctype.h>
58
59#include "libbb.h"
60
61/* valid_name */
62/* True or false? Is this a valid filename (upper/lower alpha, digits,
63 * underscores, and hyphens only?)
64 */
65static int valid_name(const struct dirent *d)
66{
67 char *c = d->d_name;
68
69 while (*c) {
70 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
71 return 0;
72 }
73 ++c;
74 }
75 return 1;
76}
77
78/* run_parts */
79/* Find the parts to run & call run_part() */
80extern int run_parts(char **args, const unsigned char test_mode)
81{
82 struct dirent **namelist = 0;
83 struct stat st;
84 char *filename;
85 int entries;
86 int i;
87 int exitstatus = 0;
88
89 /* scandir() isn't POSIX, but it makes things easy. */
90 entries = scandir(args[0], &namelist, valid_name, alphasort);
91
92 if (entries == -1) {
93 perror_msg_and_die("failed to open directory %s", args[0]);
94 }
95
96 for (i = 0; i < entries; i++) {
97
98 filename = concat_path_file(args[0], namelist[i]->d_name);
99
100 if (stat(filename, &st) < 0) {
101 perror_msg_and_die("failed to stat component %s", filename);
102 }
103 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
104 if (test_mode)
105 printf("run-parts would run %s\n", filename);
106 else {
107 int result;
108 int pid;
109
110 if ((pid = fork()) < 0) {
111 perror_msg_and_die("failed to fork");
112 } else if (!pid) {
113 execv(args[0], args);
114 perror_msg_and_die("failed to exec %s", args[0]);
115 }
116
117 waitpid(pid, &result, 0);
118
119 if (WIFEXITED(result) && WEXITSTATUS(result)) {
120 perror_msg("%s exited with return code %d", args[0], WEXITSTATUS(result));
121 exitstatus = 1;
122 } else if (WIFSIGNALED(result)) {
123 perror_msg("%s exited because of uncaught signal %d", args[0], WTERMSIG(result));
124 exitstatus = 1;
125 }
126 }
127 }
128 else if (!S_ISDIR(st.st_mode)) {
129 error_msg("component %s is not an executable plain file", filename);
130 exitstatus = 1;
131 }
132
133 free(namelist[i]);
134 free(filename);
135 }
136 free(namelist);
137
138 return(exitstatus);
139}