]> git.pld-linux.org Git - projects/rc-scripts.git/blame - src/run-parts.c
netfs: respect VSERVER_ISOLATION_NET here as well
[projects/rc-scripts.git] / src / run-parts.c
CommitLineData
868bbbe0
ER
1/* run-parts: run a bunch of scripts in a directory
2 *
3 * Debian run-parts program
4 * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
5 * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
6 * Copyright (C) 2002-2012 Clint Adams <clint@debian.org>
7 *
8 * This is free software; see the GNU General Public License version 2
9 * or later for copying conditions. There is NO warranty.
10 *
11 * Based on run-parts.pl version 0.2, Copyright (C) 1994 Ian Jackson.
12 *
13 */
14
15#include <stdio.h>
16#include <stdarg.h>
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/wait.h>
20#include <dirent.h>
21#include <sys/stat.h>
22#include <unistd.h>
23#include <fcntl.h>
24#ifdef HAVE_GETOPT_H
25#include <getopt.h>
26#endif /* HAVE_GETOPT_H */
27#include <string.h>
28#include <errno.h>
29#include <ctype.h>
30#include <signal.h>
31#include <sys/time.h>
32#include <regex.h>
33
34#define RUNPARTS_NORMAL 0
35#define RUNPARTS_ERE 1
36#define RUNPARTS_LSBSYSINIT 100
37
38int test_mode = 0;
39int list_mode = 0;
40int verbose_mode = 0;
41int report_mode = 0;
42int reverse_mode = 0;
43int exitstatus = 0;
44int regex_mode = 0;
45int exit_on_error_mode = 0;
46int new_session_mode = 0;
47
48int argcount = 0, argsize = 0;
49char **args = 0;
50
51char *custom_ere;
52regex_t hierre, tradre, excsre, classicalre, customre;
53
54static void catch_signals();
55static void restore_signals();
56
57static char* regex_get_error(int errcode, regex_t *compiled);
58static void regex_compile_pattern(void);
59static void regex_clean(void);
60
61void error(char *format, ...)
62{
63 va_list ap;
64
65 fprintf(stderr, "run-parts: ");
66
67 va_start(ap, format);
68 vfprintf(stderr, format, ap);
69 va_end(ap);
70
71 fprintf(stderr, "\n");
72}
73
74
75void version()
76{
77 fprintf(stderr, "Debian run-parts program, version " PACKAGE_VERSION
78 "\nCopyright (C) 1994 Ian Jackson, Copyright (C) 1996 Jeff Noxon.\n"
79 "Copyright (C) 1996,1997,1998,1999 Guy Maor\n"
80 "Copyright (C) 2002-2012 Clint Adams\n"
81 "This is free software; see the GNU General Public License version 2\n"
82 "or later for copying conditions. There is NO warranty.\n");
83 exit(0);
84}
85
86
87void usage()
88{
89 fprintf(stderr, "Usage: run-parts [OPTION]... DIRECTORY\n"
90 " --test print script names which would run, but don't run them.\n"
91 " --list print names of all valid files (can not be used with\n"
92 " --test)\n"
93 " -v, --verbose print script names before running them.\n"
94 " --report print script names if they produce output.\n"
95 " --reverse reverse execution order of scripts.\n"
96 " --exit-on-error exit as soon as a script returns with a non-zero exit\n"
97 " code.\n"
98 " --lsbsysinit validate filenames based on LSB sysinit specs.\n"
99 " --new-session run each script in a separate process session\n"
100 " --regex=PATTERN validate filenames based on POSIX ERE pattern PATTERN.\n"
101 " -u, --umask=UMASK sets umask to UMASK (octal), default is 022.\n"
102 " -a, --arg=ARGUMENT pass ARGUMENT to scripts, use once for each argument.\n"
103 " -V, --version output version information and exit.\n"
104 " -h, --help display this help and exit.\n");
105 exit(0);
106}
107
108
109/* The octal conversion in libc is not foolproof; it will take the 8 and 9
110 * digits under some circumstances. We'll just have to live with it.
111 */
112void set_umask()
113{
114 int mask, result;
115
116 result = sscanf(optarg, "%o", &mask);
117 if ((result != 1) || (mask > 07777) || (mask < 0)) {
118 error("bad umask value");
119 exit(1);
120 }
121
122 umask(mask);
123}
124
125/* Add an argument to the commands that we will call. Called once for
126 every argument. */
127void add_argument(char *newarg)
128{
129 if (argcount + 1 >= argsize) {
130 argsize = argsize ? argsize * 2 : 4;
131 args = realloc(args, argsize * (sizeof(char *)));
132 if (!args) {
133 error("failed to reallocate memory for arguments: %s", strerror(errno));
134 exit(1);
135 }
136 }
137 args[argcount++] = newarg;
138 args[argcount] = 0;
139}
140
141/* True or false? Is this a valid filename? */
142int valid_name(const struct dirent *d)
143{
144 char *s;
145 unsigned int retval;
146
147 s = (char *)&(d->d_name);
148
149 if (regex_mode == RUNPARTS_ERE)
150 retval = !regexec(&customre, s, 0, NULL, 0);
151
152 else if (regex_mode == RUNPARTS_LSBSYSINIT) {
153
154 if (!regexec(&hierre, s, 0, NULL, 0))
155 retval = regexec(&excsre, s, 0, NULL, 0);
156
157 else
158 retval = !regexec(&tradre, s, 0, NULL, 0);
159
bf15f595
ER
160 } else {
161 if (!regexec(&classicalre, s, 0, NULL, 0)) {
162 retval = regexec(&excsre, s, 0, NULL, 0);
163 }
164 }
868bbbe0
ER
165
166 return retval;
167}
168
169/* Execute a file */
170void run_part(char *progname)
171{
172 int result, waited;
173 int pid, r;
174 int pout[2], perr[2];
175
176 waited = 0;
177
178 if (report_mode && (pipe(pout) || pipe(perr))) {
179 error("pipe: %s", strerror(errno));
180 exit(1);
181 }
182 if ((pid = fork()) < 0) {
183 error("failed to fork: %s", strerror(errno));
184 exit(1);
185 }
186 else if (!pid) {
187 restore_signals();
188 if (new_session_mode)
189 setsid();
190 if (report_mode) {
191 if (dup2(pout[1], STDOUT_FILENO) == -1 ||
192 dup2(perr[1], STDERR_FILENO) == -1) {
193 error("dup2: %s", strerror(errno));
194 exit(1);
195 }
196 close(pout[0]);
197 close(perr[0]);
198 close(pout[1]);
199 close(perr[1]);
200 }
201 args[0] = progname;
202 execv(progname, args);
203 error("failed to exec %s: %s", progname, strerror(errno));
204 exit(1);
205 }
206
207 if (report_mode) {
208 fd_set set;
209 sigset_t tempmask;
210 struct timespec zero_timeout;
211 struct timespec *the_timeout;
212 int max, printflag;
213 ssize_t c;
214 char buf[4096];
215
216 sigemptyset(&tempmask);
217 sigprocmask(0, NULL, &tempmask);
218 sigdelset(&tempmask, SIGCHLD);
219
220 memset(&zero_timeout, 0, sizeof(zero_timeout));
221 the_timeout = NULL;
222
223 close(pout[1]);
224 close(perr[1]);
225 max = pout[0] > perr[0] ? pout[0] + 1 : perr[0] + 1;
226 printflag = 0;
227
228 while (pout[0] >= 0 || perr[0] >= 0) {
229 if (!waited) {
230 r = waitpid(pid, &result, WNOHANG);
231 if (r == -1) {
232 error("waitpid: %s", strerror(errno));
233 exit(1);
234 }
235 if (r != 0 && (WIFEXITED(result) || WIFSIGNALED(result))) {
236 /* If the process dies, set a zero timeout. Rarely, some processes
237 * leak file descriptors (e.g., by starting a naughty daemon).
238 * select() would wait forever since the pipes wouldn't close.
239 * We loop, with a zero timeout, until there's no data left, then
240 * give up. This shouldn't affect non-leaky processes. */
241 waited = 1;
242 the_timeout = &zero_timeout;
243 }
244 }
245
246 FD_ZERO(&set);
247 if (pout[0] >= 0)
248 FD_SET(pout[0], &set);
249 if (perr[0] >= 0)
250 FD_SET(perr[0], &set);
251 r = pselect(max, &set, 0, 0, the_timeout, &tempmask);
252
253 if (r < 0) {
254 if (errno == EINTR)
255 continue;
256
257 error("select: %s", strerror(errno));
258 exit(1);
259 }
260 else if (r > 0) {
261 /* If STDOUT or STDERR get closed / full, we still run to completion
262 * (and just ignore that we can't output process output any more).
263 * Perhaps we should instead kill the child process we are running
264 * if that happens.
265 * For now partial writes are not retried to complete - that can
266 * and should be done, but needs care to ensure that we don't hang
267 * if the fd doesn't accept more data ever - or we need to decide that
268 * waiting is the appropriate thing to do.
269 */
270 int ignored;
271 if (pout[0] >= 0 && FD_ISSET(pout[0], &set)) {
272 c = read(pout[0], buf, sizeof(buf));
273 if (c > 0) {
274 if (!printflag) {
275 printf("%s:\n", progname);
276 fflush(stdout);
277 printflag = 1;
278 }
279 ignored = write(STDOUT_FILENO, buf, c);
280 }
281 else if (c == 0) {
282 close(pout[0]);
283 pout[0] = -1;
284 }
285 else if (c < 0) {
286 close(pout[0]);
287 pout[0] = -1;
288 error("failed to read from stdout pipe: %s", strerror (errno));
289 }
290 }
291 if (perr[0] >= 0 && FD_ISSET(perr[0], &set)) {
292 c = read(perr[0], buf, sizeof(buf));
293 if (c > 0) {
294 if (!printflag) {
295 fprintf(stderr, "%s:\n", progname);
296 fflush(stderr);
297 printflag = 1;
298 }
299 ignored = write(STDERR_FILENO, buf, c);
300 }
301 else if (c == 0) {
302 close(perr[0]);
303 perr[0] = -1;
304 }
305 else if (c < 0) {
306 close(perr[0]);
307 perr[0] = -1;
308 error("failed to read from error pipe: %s", strerror (errno));
309 }
310 }
311 }
312 else if (r == 0 && waited) {
313 /* Zero timeout, no data left. */
314 close(perr[0]);
315 perr[0] = -1;
316 close(pout[0]);
317 pout[0] = -1;
318 }
319 else {
320 /* assert(FALSE): select was called with infinite timeout, so
321 it either returns successfully or is interrupted */
322 } /*if */
323 } /*while */
324 }
325
326 if (!waited) {
327 r = waitpid(pid, &result, 0);
328
329 if (r == -1) {
330 error("waitpid: %s", strerror(errno));
331 exit(1);
332 }
333 }
334
335 if (WIFEXITED(result) && WEXITSTATUS(result)) {
336 error("%s exited with return code %d", progname, WEXITSTATUS(result));
337 exitstatus = 1;
338 }
339 else if (WIFSIGNALED(result)) {
340 error("%s exited because of uncaught signal %d", progname,
341 WTERMSIG(result));
342 exitstatus = 1;
343 }
344}
345
346static void handle_signal(int s)
347{
348 /* Do nothing */
349}
350
351/* Catch SIGCHLD with an empty function to interrupt select() */
352static void catch_signals()
353{
354 struct sigaction act;
355 sigset_t set;
356
357 memset(&act, 0, sizeof(act));
358 act.sa_handler = handle_signal;
359 act.sa_flags = SA_NOCLDSTOP;
360 sigaction(SIGCHLD, &act, NULL);
361
362 sigemptyset(&set);
363 sigaddset(&set, SIGCHLD);
364 sigprocmask(SIG_BLOCK, &set, NULL);
365}
366
367/* Unblock signals before execing a child */
368static void restore_signals()
369{
370 sigset_t set;
371 sigemptyset(&set);
372 sigaddset(&set, SIGCHLD);
373 sigprocmask(SIG_UNBLOCK, &set, NULL);
374}
375
376/* Find the parts to run & call run_part() */
377void run_parts(char *dirname)
378{
379 struct dirent **namelist;
380 char *filename;
381 size_t filename_length, dirname_length;
382 int entries, i, result;
383 struct stat st;
384
385 /* dirname + "/" */
386 dirname_length = strlen(dirname) + 1;
387 /* dirname + "/" + ".." + "\0" (This will save one realloc.) */
388 filename_length = dirname_length + 2 + 1;
389 if (!(filename = malloc(filename_length))) {
390 error("failed to allocate memory for path: %s", strerror(errno));
391 exit(1);
392 }
393 strcpy(filename, dirname);
394 strcat(filename, "/");
395
396 /* scandir() isn't POSIX, but it makes things easy. */
397 entries = scandir(dirname, &namelist, valid_name, alphasort);
398 if (entries < 0) {
399 error("failed to open directory %s: %s", dirname, strerror(errno));
400 exit(1);
401 }
402
403 i = reverse_mode ? 0 : entries;
404 for (i = reverse_mode ? (entries - 1) : 0;
405 reverse_mode ? (i >= 0) : (i < entries); reverse_mode ? i-- : i++) {
406 if (filename_length < dirname_length + strlen(namelist[i]->d_name) + 1) {
407 filename_length = dirname_length + strlen(namelist[i]->d_name) + 1;
408 if (!(filename = realloc(filename, filename_length))) {
409 error("failed to reallocate memory for path: %s", strerror(errno));
410 exit(1);
411 }
412 }
413 strcpy(filename + dirname_length, namelist[i]->d_name);
414
415 strcpy(filename, dirname);
416 strcat(filename, "/");
417 strcat(filename, namelist[i]->d_name);
418
419 result = stat(filename, &st);
420 if (result < 0) {
421 error("failed to stat component %s: %s", filename, strerror(errno));
422 if (exit_on_error_mode) {
423 exit(1);
424 }
425 else
426 continue;
427 }
428
429 if (S_ISREG(st.st_mode)) {
430 if (!access(filename, X_OK)) {
431 if (test_mode) {
432 printf("%s\n", filename);
433 }
434 else if (list_mode) {
435 if (!access(filename, R_OK))
436 printf("%s\n", filename);
437 }
438 else {
439 if (verbose_mode)
440 if (argcount) {
441 char **a = args;
442
443 fprintf(stderr, "run-parts: executing %s", filename);
444 while(*(++a))
445 fprintf(stderr, " %s", *a);
446 fprintf(stderr, "\n");
447 } else {
448 fprintf(stderr, "run-parts: executing %s\n", filename);
449 }
450 run_part(filename);
451 if (exitstatus != 0 && exit_on_error_mode) return;
452 }
453 }
454 else if (!access(filename, R_OK)) {
455 if (list_mode) {
456 printf("%s\n", filename);
457 }
458 }
459 else if (S_ISLNK(st.st_mode)) {
460 if (!list_mode) {
461 error("run-parts: component %s is a broken symbolic link\n",filename);
462 exitstatus = 1;
463 }
464 }
465 }
466 else if (!S_ISDIR(st.st_mode)) {
467 if (!list_mode) {
468 error("run-parts: component %s is not an executable plain file\n",
469 filename);
470 exitstatus = 1;
471 }
472 }
473
474 free(namelist[i]);
475 }
476 free(namelist);
477 free(filename);
478}
479
480/* Process options */
481int main(int argc, char *argv[])
482{
483 custom_ere = NULL;
484 umask(022);
485 add_argument(0);
486
487 for (;;) {
488 int c;
489 int option_index = 0;
490
491 static struct option long_options[] = {
492 {"test", 0, &test_mode, 1},
493 {"list", 0, &list_mode, 1},
494 {"verbose", 0, 0, 'v'},
495 {"report", 0, &report_mode, 1},
496 {"reverse", 0, &reverse_mode, 1},
497 {"umask", 1, 0, 'u'},
498 {"arg", 1, 0, 'a'},
499 {"help", 0, 0, 'h'},
500 {"version", 0, 0, 'V'},
501 {"lsbsysinit", 0, &regex_mode, RUNPARTS_LSBSYSINIT},
502 {"regex", 1, &regex_mode, RUNPARTS_ERE},
503 {"exit-on-error", 0, &exit_on_error_mode, 1},
504 {"new-session", 0, &new_session_mode, 1},
505 {0, 0, 0, 0}
506 };
507
508 c = getopt_long(argc, argv, "u:ha:vV", long_options, &option_index);
509 if (c == EOF)
510 break;
511 switch (c) {
512 case 0:
513 if(option_index==10) { /* hardcoding this will lead to trouble */
514 custom_ere = strdup(optarg);
515 }
516 break;
517 case 'u':
518 set_umask();
519 break;
520 case 'a':
521 add_argument(optarg);
522 break;
523 case 'h':
524 usage();
525 break;
526 case 'v':
527 verbose_mode = 1;
528 break;
529 case 'V':
530 version();
531 break;
532 default:
533 fprintf(stderr, "Try `run-parts --help' for more information.\n");
534 exit(1);
535 }
536 }
537
538 /* We require exactly one argument: the directory name */
539 if (optind != (argc - 1)) {
540 error("missing operand");
541 fprintf(stderr, "Try `run-parts --help' for more information.\n");
542 exit(1);
543 } else if (list_mode && test_mode) {
544 error("--list and --test can not be used together");
545 fprintf(stderr, "Try `run-parts --help' for more information.\n");
546 exit(1);
547 } else {
548 catch_signals();
549 regex_compile_pattern();
550 run_parts(argv[optind]);
551 regex_clean();
552
553 free(args);
554 free(custom_ere);
555
556 return exitstatus;
557 }
558}
559
560/*
561 * Compile patterns used by the application
562 *
563 * In order for a string to be matched by a pattern, this pattern must be
564 * compiled with the regcomp function. If an error occurs, the application
565 * exits and displays the error.
566 */
567static void
568regex_compile_pattern (void)
569{
570 int err;
571 regex_t *pt_regex;
572
573 if (regex_mode == RUNPARTS_ERE) {
574
575 if ((err = regcomp(&customre, custom_ere,
576 REG_EXTENDED | REG_NOSUB)) != 0)
577 pt_regex = &customre;
578
579 } else if (regex_mode == RUNPARTS_LSBSYSINIT) {
580
581 if ( (err = regcomp(&hierre, "^_?([a-z0-9_.]+-)+[a-z0-9]+$",
582 REG_EXTENDED | REG_NOSUB)) != 0)
583 pt_regex = &hierre;
584
bf15f595 585 else if ( (err = regcomp(&excsre, "^[a-z0-9-].*(\.rpm(save|new|orig)|~|,v)$",
868bbbe0
ER
586 REG_EXTENDED | REG_NOSUB)) != 0)
587 pt_regex = &excsre;
588
589 else if ( (err = regcomp(&tradre, "^[a-z0-9][a-z0-9-]*$", REG_NOSUB))
590 != 0)
591 pt_regex = &tradre;
592
bf15f595
ER
593 } else if ( (err = regcomp(&classicalre, "^.+$",
594 REG_EXTENDED | REG_NOSUB)) != 0)
595 pt_regex = &classicalre;
596 else if ( (err = regcomp(&excsre, "^[.]|(\.rpm(save|new|orig)|~|,v)$",
868bbbe0 597 REG_EXTENDED | REG_NOSUB)) != 0)
bf15f595 598 pt_regex = &excsre;
868bbbe0
ER
599
600 if (err != 0) {
601 fprintf(stderr, "Unable to build regexp: %s", \
602 regex_get_error(err, pt_regex));
603 exit(1);
604 }
605}
606
607/*
608 * Get a regex error.
609 *
610 * This function allocates a buffer to store the regex error description.
611 * If a buffer cannot be allocated, then the use of xmalloc will end the
612 * program.
613 *
614 * @errcode: return error code from a one of the regex functions
615 * @compiled: compile pattern which causes the failure
616 *
617 * It returns a pointer on the current regex error description.
618 */
619static char *
620regex_get_error(int errcode, regex_t *compiled)
621{
622 size_t length;
623 char *buf;
624
625 length = regerror(errcode, compiled, NULL, 0);
626 buf = malloc(length);
627 if (buf == 0) {
628 error("Virtual memory exhausted\n");
629 exit(1);
630 }
631
632 regerror(errcode, compiled, buf, length);
633
634 return buf;
635}
636
637/*
638 * Clean the compiled patterns according to the current regex_mode
639 */
640static void
641regex_clean(void)
642{
643 if (regex_mode == RUNPARTS_ERE)
644 regfree(&customre);
645
646 else if (regex_mode == RUNPARTS_LSBSYSINIT) {
647 regfree(&hierre);
648 regfree(&excsre);
649 regfree(&tradre);
650
bf15f595 651 } else {
868bbbe0 652 regfree(&classicalre);
bf15f595
ER
653 regfree(&excsre);
654 }
868bbbe0 655}
This page took 0.128129 seconds and 4 git commands to generate.