Skip to content

Commit

Permalink
Introduce execute_command
Browse files Browse the repository at this point in the history
This is useful in situations where we do not have builtins avaiable.

Signed-off-by: Richard Yao <ryao@gentoo.org>
  • Loading branch information
ryao committed Nov 23, 2012
1 parent 8a72e9e commit 9b99681
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/udev/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4296,6 +4296,46 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
return endswith(de->d_name, suffix);
}

int execute_command(const char *command, char *const argv[])
{

pid_t pid;
int status;

if ((status = access(command, X_OK)) != 0)
return status;

if ((pid = fork()) < 0) {
log_error("Failed to fork: %m");
return pid;
}

if (pid == 0) {

execv(command, argv);

log_error("Failed to execute %s: %m", command);
_exit(EXIT_FAILURE);
}
else while (1)
{
siginfo_t si;

int r = waitid(P_PID, pid, &si, WEXITED);

if (!is_clean_exit(si.si_code, si.si_status, NULL)) {
if (si.si_code == CLD_EXITED)
log_error("%s exited with exit status %i.", command, si.si_status);
else
log_error("%s terminated by signal %s.", command, signal_to_string(si.si_status));
} else
log_debug("%s exited successfully.", command);

return si.si_status;

}
}

void execute_directory(const char *directory, DIR *d, char *argv[]) {
DIR *_d = NULL;
struct dirent *de;
Expand Down
1 change: 1 addition & 0 deletions src/udev/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ bool tty_is_console(const char *tty);
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);

int execute_command(const char *command, char *const argv[]);
void execute_directory(const char *directory, DIR *_d, char *argv[]);

int kill_and_sigcont(pid_t pid, int sig);
Expand Down

0 comments on commit 9b99681

Please sign in to comment.