Ameausoone / howdoi-action

Github Action which respond to issue with github.com/gleitz/howdoi (awesome) tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do i Bury a corpse !?

mathela59 opened this issue · comments

How do i Bury a corpse !?

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

static
int prepare_log(void)
{
int fd = open("error.log",
O_CREAT | O_RDWR | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
return fd;
}

static
void log_stderr(int pipe_ends[], int outfile)
{
char buffer[512];
int nbytes;
close(pipe_ends[1]);
while ((nbytes = read(pipe_ends[0], buffer, sizeof(buffer))) > 0)
{
write(STDOUT_FILENO, buffer, nbytes);
write(outfile, buffer, nbytes);
}
close(pipe_ends[0]);
close(outfile);
}

static
void child(int pipe_ends[], char *argv[])
{
dup2(pipe_ends[1], 1);
close(pipe_ends[0]);
close(pipe_ends[1]);
execvp(argv[0], argv);
fprintf(stderr, "failed to execute %s\n", argv[0]);
exit(EXIT_FAILURE);
}

static
void bury(void)
{
int status;
int corpse;
while ((corpse = wait(&status)) > 0)
printf("%d: child %d exited with status 0x%.4X\n", (int)getpid(), corpse, status);
}

int main(void)
{
int fd = prepare_log();
char *argv[] = {"seq", "10", NULL};

int pipe1[2];
pipe(pipe1);

pid_t pid = fork();

if (pid > 0)
{
    log_stderr(pipe1, fd);
    bury();
}
else if (pid == 0)
{
    child(pipe1, argv);
}
return 0;

}