blob: 58645660b0a68650d418a36fe42387626df48659 [file] [log] [blame]
Glenn L McGrath545106f2002-11-11 06:21:00 +00001/* vi: set sw=4 ts=4: */
2/*
Glenn L McGrath39289b52002-11-15 22:18:01 +00003 * run command from specified directory
Glenn L McGrath545106f2002-11-11 06:21:00 +00004 *
5 *
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
Glenn L McGrath39289b52002-11-15 22:18:01 +00007 * rewrite to vfork usage by
8 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath545106f2002-11-11 06:21:00 +00009 *
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 *
Glenn L McGrath545106f2002-11-11 06:21:00 +000015 *
16 */
17
Glenn L McGrath545106f2002-11-11 06:21:00 +000018
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>
Glenn L McGrath39289b52002-11-15 22:18:01 +000025#include <errno.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000026
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 */
33static 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
Glenn L McGrath2e51a142003-01-20 23:50:59 +000046/* test mode = 1 is the same as offical run_parts
47 * test_mode = 2 means to fail siliently on missing directories
48 */
49
Glenn L McGrath545106f2002-11-11 06:21:00 +000050extern int run_parts(char **args, const unsigned char test_mode)
51{
52 struct dirent **namelist = 0;
53 struct stat st;
54 char *filename;
Glenn L McGrath157fea52002-11-21 22:17:11 +000055 char *arg0 = args[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +000056 int entries;
57 int i;
58 int exitstatus = 0;
59
Glenn L McGrath39289b52002-11-15 22:18:01 +000060#if __GNUC__
61 /* Avoid longjmp clobbering */
62 (void) &i;
63 (void) &exitstatus;
64#endif
Glenn L McGrath545106f2002-11-11 06:21:00 +000065 /* scandir() isn't POSIX, but it makes things easy. */
Glenn L McGrath157fea52002-11-21 22:17:11 +000066 entries = scandir(arg0, &namelist, valid_name, alphasort);
Glenn L McGrath545106f2002-11-11 06:21:00 +000067
68 if (entries == -1) {
Glenn L McGrath2e51a142003-01-20 23:50:59 +000069 if (test_mode & 2) {
70 return(2);
71 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 bb_perror_msg_and_die("failed to open directory %s", arg0);
Glenn L McGrath545106f2002-11-11 06:21:00 +000073 }
74
75 for (i = 0; i < entries; i++) {
76
Glenn L McGrath157fea52002-11-21 22:17:11 +000077 filename = concat_path_file(arg0, namelist[i]->d_name);
Glenn L McGrath545106f2002-11-11 06:21:00 +000078
79 if (stat(filename, &st) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_perror_msg_and_die("failed to stat component %s", filename);
Glenn L McGrath545106f2002-11-11 06:21:00 +000081 }
82 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
Glenn L McGrath2e51a142003-01-20 23:50:59 +000083 if (test_mode & 1) {
84 puts(filename);
Glenn L McGrathaad465e2003-01-20 23:34:12 +000085 } else {
Glenn L McGrath39289b52002-11-15 22:18:01 +000086 /* exec_errno is common vfork variable */
87 volatile int exec_errno = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +000088 int result;
89 int pid;
90
Glenn L McGrath39289b52002-11-15 22:18:01 +000091 if ((pid = vfork()) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_perror_msg_and_die("failed to fork");
Glenn L McGrath545106f2002-11-11 06:21:00 +000093 } else if (!pid) {
Glenn L McGrath39289b52002-11-15 22:18:01 +000094 args[0] = filename;
95 execv(filename, args);
96 exec_errno = errno;
97 _exit(1);
Glenn L McGrath545106f2002-11-11 06:21:00 +000098 }
99
100 waitpid(pid, &result, 0);
Glenn L McGrath39289b52002-11-15 22:18:01 +0000101 if(exec_errno) {
102 errno = exec_errno;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 bb_perror_msg_and_die("failed to exec %s", filename);
Glenn L McGrath39289b52002-11-15 22:18:01 +0000104 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000105 if (WIFEXITED(result) && WEXITSTATUS(result)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000107 exitstatus = 1;
108 } else if (WIFSIGNALED(result)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000110 exitstatus = 1;
111 }
112 }
113 }
114 else if (!S_ISDIR(st.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_error_msg("component %s is not an executable plain file", filename);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000116 exitstatus = 1;
117 }
118
119 free(namelist[i]);
120 free(filename);
121 }
122 free(namelist);
123
124 return(exitstatus);
125}