blob: fae91813c4ad4574b62445e0f2b3254fbaf5eb96 [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 *
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath545106f2002-11-11 06:21:00 +000011 */
12
Glenn L McGrath545106f2002-11-11 06:21:00 +000013
14#include <sys/types.h>
15#include <sys/wait.h>
16#include <stdlib.h>
17#include <dirent.h>
18#include <unistd.h>
19#include <ctype.h>
Glenn L McGrath39289b52002-11-15 22:18:01 +000020#include <errno.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000021
22#include "libbb.h"
23
24/* valid_name */
25/* True or false? Is this a valid filename (upper/lower alpha, digits,
26 * underscores, and hyphens only?)
27 */
28static int valid_name(const struct dirent *d)
29{
Bernhard Reutner-Fischer428a5cd2006-09-12 13:25:16 +000030 const char *c = d->d_name;
Glenn L McGrath545106f2002-11-11 06:21:00 +000031
32 while (*c) {
33 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
34 return 0;
35 }
36 ++c;
37 }
38 return 1;
39}
40
Eric Andersenaff114c2004-04-14 17:51:38 +000041/* test mode = 1 is the same as official run_parts
42 * test_mode = 2 means to fail silently on missing directories
Glenn L McGrath2e51a142003-01-20 23:50:59 +000043 */
44
Rob Landleydfba7412006-03-06 20:47:33 +000045int run_parts(char **args, const unsigned char test_mode, char **env)
Glenn L McGrath545106f2002-11-11 06:21:00 +000046{
47 struct dirent **namelist = 0;
48 struct stat st;
49 char *filename;
Glenn L McGrath157fea52002-11-21 22:17:11 +000050 char *arg0 = args[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +000051 int entries;
52 int i;
53 int exitstatus = 0;
54
Glenn L McGrath39289b52002-11-15 22:18:01 +000055#if __GNUC__
56 /* Avoid longjmp clobbering */
57 (void) &i;
58 (void) &exitstatus;
59#endif
Glenn L McGrath545106f2002-11-11 06:21:00 +000060 /* scandir() isn't POSIX, but it makes things easy. */
Glenn L McGrath157fea52002-11-21 22:17:11 +000061 entries = scandir(arg0, &namelist, valid_name, alphasort);
Glenn L McGrath545106f2002-11-11 06:21:00 +000062
63 if (entries == -1) {
Glenn L McGrath2e51a142003-01-20 23:50:59 +000064 if (test_mode & 2) {
65 return(2);
66 }
Denis Vlasenko89f0b342006-11-18 22:04:09 +000067 bb_perror_msg_and_die("cannot open '%s'", arg0);
Glenn L McGrath545106f2002-11-11 06:21:00 +000068 }
69
70 for (i = 0; i < entries; i++) {
71
Glenn L McGrath157fea52002-11-21 22:17:11 +000072 filename = concat_path_file(arg0, namelist[i]->d_name);
Glenn L McGrath545106f2002-11-11 06:21:00 +000073
Rob Landleyc5b1d4d2006-03-13 15:45:16 +000074 xstat(filename, &st);
Glenn L McGrath545106f2002-11-11 06:21:00 +000075 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
Glenn L McGrath502907f2003-08-30 12:27:36 +000076 if (test_mode) {
Glenn L McGrath2e51a142003-01-20 23:50:59 +000077 puts(filename);
Glenn L McGrathaad465e2003-01-20 23:34:12 +000078 } else {
Eric Andersend1309732003-05-27 20:45:59 +000079 /* exec_errno is common vfork variable */
80 volatile int exec_errno = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +000081 int result;
Eric Andersend1309732003-05-27 20:45:59 +000082 int pid;
Glenn L McGrath545106f2002-11-11 06:21:00 +000083
Glenn L McGrath39289b52002-11-15 22:18:01 +000084 if ((pid = vfork()) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 bb_perror_msg_and_die("failed to fork");
Eric Andersend1309732003-05-27 20:45:59 +000086 } else if (!pid) {
87 args[0] = filename;
Eric Andersen2a186892003-07-29 07:05:40 +000088 execve(filename, args, env);
Eric Andersend1309732003-05-27 20:45:59 +000089 exec_errno = errno;
Glenn L McGrath39289b52002-11-15 22:18:01 +000090 _exit(1);
Glenn L McGrath545106f2002-11-11 06:21:00 +000091 }
92
Eric Andersend1309732003-05-27 20:45:59 +000093 waitpid(pid, &result, 0);
94 if(exec_errno) {
95 errno = exec_errno;
Glenn L McGrath502907f2003-08-30 12:27:36 +000096 bb_perror_msg("failed to exec %s", filename);
97 exitstatus = 1;
Eric Andersend1309732003-05-27 20:45:59 +000098 }
99 if (WIFEXITED(result) && WEXITSTATUS(result)) {
100 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
101 exitstatus = 1;
102 } else if (WIFSIGNALED(result)) {
103 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
104 exitstatus = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000105 }
106 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000107 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000108 else if (!S_ISDIR(st.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_error_msg("component %s is not an executable plain file", filename);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000110 exitstatus = 1;
111 }
112
113 free(namelist[i]);
114 free(filename);
115 }
116 free(namelist);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117
Glenn L McGrath545106f2002-11-11 06:21:00 +0000118 return(exitstatus);
119}