1 /* vi: set sw=4 ts=4: */
3 * run command from specified directory
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>
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.
19 #include <sys/types.h>
30 /* True or false? Is this a valid filename (upper/lower alpha, digits,
31 * underscores, and hyphens only?)
33 static int valid_name(const struct dirent *d)
38 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
47 /* Find the parts to run & call run_part() */
48 extern int run_parts(char **args, const unsigned char test_mode)
50 struct dirent **namelist = 0;
58 /* Avoid longjmp clobbering */
62 /* scandir() isn't POSIX, but it makes things easy. */
63 entries = scandir(args[0], &namelist, valid_name, alphasort);
66 perror_msg_and_die("failed to open directory %s", args[0]);
69 for (i = 0; i < entries; i++) {
71 filename = concat_path_file(args[0], namelist[i]->d_name);
73 if (stat(filename, &st) < 0) {
74 perror_msg_and_die("failed to stat component %s", filename);
76 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
78 printf("run-parts would run %s\n", filename);
80 /* exec_errno is common vfork variable */
81 volatile int exec_errno = 0;
85 if ((pid = vfork()) < 0) {
86 perror_msg_and_die("failed to fork");
89 execv(filename, args);
94 waitpid(pid, &result, 0);
97 perror_msg_and_die("failed to exec %s", filename);
99 if (WIFEXITED(result) && WEXITSTATUS(result)) {
100 perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
102 } else if (WIFSIGNALED(result)) {
103 perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
108 else if (!S_ISDIR(st.st_mode)) {
109 error_msg("component %s is not an executable plain file", filename);