blob: a6ad81096d24f26771c173f2347aac045db0b3fe [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 *
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
Eric Andersen7d682902001-10-31 10:59:29 +000048 * done - use bb's concat_path_file()
Eric Andersenfb74a452001-12-18 14:06:03 +000049 * done - declare run_parts_main() as extern and any other function as static?
50 */
Eric Andersen7d682902001-10-31 10:59:29 +000051
Eric Andersen7d682902001-10-31 10:59:29 +000052#include <getopt.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000053#include <stdlib.h>
Eric Andersen7d682902001-10-31 10:59:29 +000054
Glenn L McGrath545106f2002-11-11 06:21:00 +000055#include "libbb.h"
Eric Andersen7d682902001-10-31 10:59:29 +000056
57/* run_parts_main */
58/* Process options */
Glenn L McGrath545106f2002-11-11 06:21:00 +000059int run_parts_main(int argc, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +000060{
Glenn L McGrath545106f2002-11-11 06:21:00 +000061 char **args = xmalloc(2 * sizeof(char *));
62 unsigned char test_mode = 0;
63 unsigned short argcount = 1;
64 int opt;
Eric Andersen7d682902001-10-31 10:59:29 +000065
Glenn L McGrath545106f2002-11-11 06:21:00 +000066 umask(022);
67
68 while ((opt = getopt(argc, argv, "tu:a:")) != -1) {
69 switch (opt) {
70 case 't': /* Enable test mode */
71 test_mode = 1;
72 break;
73 case 'u': /* Set the umask of the programs executed */
74 /* Check and set the umask of the program executed. As stated in the original
75 * run-parts, the octal conversion in libc is not foolproof; it will take the
76 * 8 and 9 digits under some circumstances. We'll just have to live with it.
77 */
78 {
79 const unsigned int mask = (unsigned int) strtol(optarg, NULL, 8);
80 if (mask > 07777) {
81 perror_msg_and_die("bad umask value");
82 }
83 umask(mask);
84 }
85 break;
86 case 'a': /* Pass an argument to the programs */
87 /* Add an argument to the commands that we will call.
88 * Called once for every argument. */
89 args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
90 args[argcount++] = optarg;
Glenn L McGrath545106f2002-11-11 06:21:00 +000091 break;
92 default:
93 show_usage();
94 }
95 }
96
97 /* We require exactly one argument: the directory name */
98 if (optind != (argc - 1)) {
Eric Andersen7d682902001-10-31 10:59:29 +000099 show_usage();
Glenn L McGrath545106f2002-11-11 06:21:00 +0000100 }
Eric Andersen7d682902001-10-31 10:59:29 +0000101
Glenn L McGrath545106f2002-11-11 06:21:00 +0000102 args[0] = argv[optind];
Glenn L McGratha69fd2e2003-01-08 03:26:47 +0000103 args[argcount] = 0;
104
Glenn L McGrath545106f2002-11-11 06:21:00 +0000105 return(run_parts(args, test_mode));
Eric Andersen7d682902001-10-31 10:59:29 +0000106}