Added shell job that dups stdout and err
This commit is contained in:
parent
04faf2790d
commit
28799e4770
@ -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);
|
||||
|
28
src/exectd.c
28
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);
|
||||
|
@ -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) {
|
||||
|
15
tests/03_shell.c
Normal file
15
tests/03_shell.c
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user