last_patch_69, 8 bit clean and other fixes from Vladimir N. Oleynik
[oweals/busybox.git] / libbb / run_parts.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * run command from specified directory
4  *
5  *
6  * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
7  * rewrite to vfork usage by
8  * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  *
16  */
17
18
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <stdlib.h>
22 #include <dirent.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <errno.h>
26
27 #include "libbb.h"
28
29 /* valid_name */
30 /* True or false? Is this a valid filename (upper/lower alpha, digits,
31  * underscores, and hyphens only?)
32  */
33 static int valid_name(const struct dirent *d)
34 {
35         char *c = d->d_name;
36
37         while (*c) {
38                 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
39                         return 0;
40                 }
41                 ++c;
42         }
43         return 1;
44 }
45
46 /* run_parts */
47 /* Find the parts to run & call run_part() */
48 extern int run_parts(char **args, const unsigned char test_mode)
49 {
50         struct dirent **namelist = 0;
51         struct stat st;
52         char *filename;
53         char *arg0 = args[0];
54         int entries;
55         int i;
56         int exitstatus = 0;
57
58 #if __GNUC__
59         /* Avoid longjmp clobbering */
60         (void) &i;
61         (void) &exitstatus;
62 #endif
63         /* scandir() isn't POSIX, but it makes things easy. */
64         entries = scandir(arg0, &namelist, valid_name, alphasort);
65
66         if (entries == -1) {
67                 perror_msg_and_die("failed to open directory %s", arg0);
68         }
69
70         for (i = 0; i < entries; i++) {
71
72                 filename = concat_path_file(arg0, namelist[i]->d_name);
73
74                 if (stat(filename, &st) < 0) {
75                         perror_msg_and_die("failed to stat component %s", filename);
76                 }
77                 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
78                         if (test_mode)
79                                 printf("run-parts would run %s\n", filename);
80                         else {
81                                 /* exec_errno is common vfork variable */
82                                 volatile int exec_errno = 0;
83                                 int result;
84                                 int pid;
85
86                                 if ((pid = vfork()) < 0) {
87                                         perror_msg_and_die("failed to fork");
88                                 } else if (!pid) {
89                                         args[0] = filename;
90                                         execv(filename, args);
91                                         exec_errno = errno;
92                                         _exit(1);
93                                 }
94
95                                 waitpid(pid, &result, 0);
96                                 if(exec_errno) {
97                                         errno = exec_errno;
98                                         perror_msg_and_die("failed to exec %s", filename);
99                                 }
100                                 if (WIFEXITED(result) && WEXITSTATUS(result)) {
101                                         perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
102                                         exitstatus = 1;
103                                 } else if (WIFSIGNALED(result)) {
104                                         perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
105                                         exitstatus = 1;
106                                 }
107                         }
108                 } 
109                 else if (!S_ISDIR(st.st_mode)) {
110                         error_msg("component %s is not an executable plain file", filename);
111                         exitstatus = 1;
112                 }
113
114                 free(namelist[i]);
115                 free(filename);
116         }
117         free(namelist);
118         
119         return(exitstatus);
120 }