diff --git a/include/exectd.h b/include/exectd.h index 67477c5..d1266aa 100644 --- a/include/exectd.h +++ b/include/exectd.h @@ -30,6 +30,7 @@ int ext_pipe(int fds[2]); int bi_popen(const char* const command, FILE** const in, FILE** const out); int bi_pclose(FILE** const in, FILE** const out, const int to_child[2], const int to_parent[2]); int execute_job(const char *path, char *const argv[], char *const envp[]); +int shell_job(const char *path, char *const argv[], char *const envp[],int outfd, int errfd); int ext_socket(int domain, int type, int protocol); int ext_bind(int sockfd, const SA *addr,socklen_t addrlen); diff --git a/src/exectd.c b/src/exectd.c index 840a2aa..13d2b1a 100644 --- a/src/exectd.c +++ b/src/exectd.c @@ -186,7 +186,35 @@ int execute_job( const char *path, char *const argv[], char *const envp[]){ return result; } +int shell_job(const char *path, char *const argv[], char *const envp[],int outfd, int errfd){ + int result = -1; + TRY{ + ext_dup2(outfd,STDOUT_FILENO); + ext_dup2(errfd,STDERR_FILENO); + ext_close(outfd); + ext_close(errfd); + pid_t pid = ext_fork(); + if(pid == 0){ // Child process + execve(path, argv, envp); + Exception e = { strerror(errno) }; + RAISE(e); + + } else { // Parent process + int wstatus; + wait(&wstatus); + + if(WIFEXITED(wstatus)){ // Normal exit + result = WEXITSTATUS(wstatus); + } + } + } ELSE { + exit(EXIT_FAILURE); + } END_TRY; + + return result; + +} /*---------- Networking Sockets --------*/ int ext_socket(int domain, int type, int protocol){ int status = socket(domain,type,protocol); diff --git a/tests/02_execute.c b/tests/02_execute.c index b9d9206..a56c760 100644 --- a/tests/02_execute.c +++ b/tests/02_execute.c @@ -1,6 +1,6 @@ #include "../include/exectd.h" int main() { - char *argv[] = {"/bin/ping", "-c","3","google.com", NULL}; // Example arguments + char *argv[] = {"/bin/ping", "-c","3","myrepos.dev", NULL}; // Example arguments char *envp[] = {NULL}; // Example environment variables int status = execute_job("/bin/ping", argv, envp); if (status != 0) { diff --git a/tests/03_shell.c b/tests/03_shell.c new file mode 100644 index 0000000..27b5d9d --- /dev/null +++ b/tests/03_shell.c @@ -0,0 +1,15 @@ +#include "../include/exectd.h" +int main() { + char *argv[] = {"/bin/ping", "-c","3","myrepos.dev", NULL}; // Example arguments + char *envp[] = {NULL}; // Example environment variables + int fd = ext_open("tests/bin/stdout_dup", O_WRONLY | O_CREAT); + int fd2 = ext_open("tests/bin/stderr_dup", O_WRONLY | O_CREAT); + int status = shell_job("/bin/ping", argv, envp, fd, fd2); + if (status != 0) { + // Handle error + printf("Shell job returned error code: %d\n", status); + } + + // Code here will run after the child process has finished + return 0; +}