Added shell job that dups stdout and err

This commit is contained in:
Randy Jordan 2024-11-22 19:21:06 -06:00
parent 04faf2790d
commit 28799e4770
Signed by: Randy-Jordan
GPG Key ID: D57FA29E3B54663E
4 changed files with 45 additions and 1 deletions

View File

@ -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_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 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 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_socket(int domain, int type, int protocol);
int ext_bind(int sockfd, const SA *addr,socklen_t addrlen); int ext_bind(int sockfd, const SA *addr,socklen_t addrlen);

View File

@ -186,7 +186,35 @@ int execute_job( const char *path, char *const argv[], char *const envp[]){
return result; 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 --------*/ /*---------- Networking Sockets --------*/
int ext_socket(int domain, int type, int protocol){ int ext_socket(int domain, int type, int protocol){
int status = socket(domain,type,protocol); int status = socket(domain,type,protocol);

View File

@ -1,6 +1,6 @@
#include "../include/exectd.h" #include "../include/exectd.h"
int main() { 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 char *envp[] = {NULL}; // Example environment variables
int status = execute_job("/bin/ping", argv, envp); int status = execute_job("/bin/ping", argv, envp);
if (status != 0) { if (status != 0) {

15
tests/03_shell.c Normal file
View 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;
}