blob: f2d90b64d9ae602ae13193bd11b3319dfd4af8c8 [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>
Eric Andersenc7bda1c2004-03-15 08:29:22 +000011 *
Eric Andersen7d682902001-10-31 10:59:29 +000012 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +000013 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Eric Andersen7d682902001-10-31 10:59:29 +000014 */
15
16/* This is my first attempt to write a program in C (well, this is my first
17 * attempt to write a program! :-) . */
18
19/* This piece of code is heavily based on the original version of run-parts,
Eric Andersenc7bda1c2004-03-15 08:29:22 +000020 * taken from debian-utils. I've only removed the long options and a the
Eric Andersen7d682902001-10-31 10:59:29 +000021 * report mode. As the original run-parts support only long options, I've
Eric Andersenc7bda1c2004-03-15 08:29:22 +000022 * broken compatibility because the BusyBox policy doesn't allow them.
23 * The supported options are:
Eric Andersen7d682902001-10-31 10:59:29 +000024 * -t test. Print the name of the files to be executed, without
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000025 * execute them.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000026 * -a ARG argument. Pass ARG as an argument the program executed. It can
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027 * be repeated to pass multiple arguments.
28 * -u MASK umask. Set the umask of the program executed to MASK. */
Eric Andersen7d682902001-10-31 10:59:29 +000029
Eric Andersenc7bda1c2004-03-15 08:29:22 +000030/* TODO
Eric Andersen7d682902001-10-31 10:59:29 +000031 * done - convert calls to error in perror... and remove error()
Eric Andersenc7bda1c2004-03-15 08:29:22 +000032 * done - convert malloc/realloc to their x... counterparts
Eric Andersen7d682902001-10-31 10:59:29 +000033 * done - remove catch_sigchld
Eric Andersenc7bda1c2004-03-15 08:29:22 +000034 * done - use bb's concat_path_file()
Eric Andersenfb74a452001-12-18 14:06:03 +000035 * done - declare run_parts_main() as extern and any other function as static?
36 */
Eric Andersen7d682902001-10-31 10:59:29 +000037
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000038#include "busybox.h"
Eric Andersen7d682902001-10-31 10:59:29 +000039#include <getopt.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000040#include <stdlib.h>
Eric Andersen7d682902001-10-31 10:59:29 +000041
Eric Andersen7d682902001-10-31 10:59:29 +000042
Eric Andersena1ed06b2003-07-26 09:16:00 +000043static const struct option runparts_long_options[] = {
44 { "test", 0, NULL, 't' },
45 { "umask", 1, NULL, 'u' },
46 { "arg", 1, NULL, 'a' },
47 { 0, 0, 0, 0 }
48};
49
Eric Andersen2a186892003-07-29 07:05:40 +000050extern char **environ;
51
Eric Andersen7d682902001-10-31 10:59:29 +000052/* run_parts_main */
53/* Process options */
Glenn L McGrath545106f2002-11-11 06:21:00 +000054int run_parts_main(int argc, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +000055{
Glenn L McGrath545106f2002-11-11 06:21:00 +000056 char **args = xmalloc(2 * sizeof(char *));
57 unsigned char test_mode = 0;
58 unsigned short argcount = 1;
59 int opt;
Eric Andersen7d682902001-10-31 10:59:29 +000060
Glenn L McGrath545106f2002-11-11 06:21:00 +000061 umask(022);
62
Denis Vlasenko13858992006-10-08 12:49:22 +000063 while ((opt = getopt_long(argc, argv, "tu:a:",
Eric Andersena1ed06b2003-07-26 09:16:00 +000064 runparts_long_options, NULL)) > 0)
65 {
Glenn L McGrath545106f2002-11-11 06:21:00 +000066 switch (opt) {
Denis Vlasenko13858992006-10-08 12:49:22 +000067 /* Enable test mode */
68 case 't':
69 test_mode++;
70 break;
71 /* Set the umask of the programs executed */
72 case 'u':
73 /* Check and set the umask of the program executed. As stated in the original
74 * run-parts, the octal conversion in libc is not foolproof; it will take the
75 * 8 and 9 digits under some circumstances. We'll just have to live with it.
76 */
77 umask(xstrtoul_range(optarg, 8, 0, 07777));
78 break;
79 /* Pass an argument to the programs */
80 case 'a':
81 /* Add an argument to the commands that we will call.
82 * Called once for every argument. */
83 args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
84 args[argcount++] = optarg;
85 break;
86 default:
87 bb_show_usage();
Glenn L McGrath545106f2002-11-11 06:21:00 +000088 }
89 }
90
91 /* We require exactly one argument: the directory name */
92 if (optind != (argc - 1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_show_usage();
Glenn L McGrath545106f2002-11-11 06:21:00 +000094 }
Eric Andersen7d682902001-10-31 10:59:29 +000095
Glenn L McGrath545106f2002-11-11 06:21:00 +000096 args[0] = argv[optind];
Glenn L McGratha69fd2e2003-01-08 03:26:47 +000097 args[argcount] = 0;
98
Eric Andersen2a186892003-07-29 07:05:40 +000099 return(run_parts(args, test_mode, environ));
Eric Andersen7d682902001-10-31 10:59:29 +0000100}