|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
@ -7,18 +9,65 @@
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
pid_t cmd_execute(const char *path, const char *const argv[]) {
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
enum process_result cmd_execute(const char *path, const char *const argv[], pid_t *pid) {
|
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
|
|
if (pipe(fd) == -1) {
|
|
|
|
|
perror("pipe");
|
|
|
|
|
return PROCESS_ERROR_GENERIC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum process_result ret = PROCESS_SUCCESS;
|
|
|
|
|
|
|
|
|
|
*pid = fork();
|
|
|
|
|
if (*pid == -1) {
|
|
|
|
|
perror("fork");
|
|
|
|
|
return -1;
|
|
|
|
|
ret = PROCESS_ERROR_GENERIC;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
if (pid == 0) {
|
|
|
|
|
execvp(path, (char *const *)argv);
|
|
|
|
|
perror("exec");
|
|
|
|
|
|
|
|
|
|
if (*pid > 0) {
|
|
|
|
|
// parent close write side
|
|
|
|
|
close(fd[1]);
|
|
|
|
|
fd[1] = -1;
|
|
|
|
|
// wait for EOF or receive errno from child
|
|
|
|
|
if (read(fd[0], &ret, sizeof(ret)) == -1) {
|
|
|
|
|
perror("read");
|
|
|
|
|
ret = PROCESS_ERROR_GENERIC;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
} else if (*pid == 0) {
|
|
|
|
|
// child close read side
|
|
|
|
|
close(fd[0]);
|
|
|
|
|
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) {
|
|
|
|
|
execvp(path, (char *const *)argv);
|
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
|
ret = PROCESS_ERROR_MISSING_BINARY;
|
|
|
|
|
} else {
|
|
|
|
|
ret = PROCESS_ERROR_GENERIC;
|
|
|
|
|
}
|
|
|
|
|
perror("exec");
|
|
|
|
|
} else {
|
|
|
|
|
perror("fcntl");
|
|
|
|
|
ret = PROCESS_ERROR_GENERIC;
|
|
|
|
|
}
|
|
|
|
|
// send ret to the parent
|
|
|
|
|
if (write(fd[1], &ret, sizeof(ret)) == -1) {
|
|
|
|
|
perror("write");
|
|
|
|
|
}
|
|
|
|
|
// close write side before exiting
|
|
|
|
|
close(fd[1]);
|
|
|
|
|
_exit(1);
|
|
|
|
|
}
|
|
|
|
|
return pid;
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (fd[0] != -1) {
|
|
|
|
|
close(fd[0]);
|
|
|
|
|
}
|
|
|
|
|
if (fd[1] != -1) {
|
|
|
|
|
close(fd[1]);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_bool cmd_terminate(pid_t pid) {
|
|
|
|
|