summaryrefslogtreecommitdiffstats
path: root/02_exercise/builtins.c
blob: 9f2392715d78fe17416aa77ba673219f8d4724de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <stdlib.h>


#include "array.h"
#include "builtins.h"

void handle(int sig) {
    /* Do nothing. */
}

typedef struct {
    pid_t pid;
    bool done;
} hack_struct;

void builtin_wait(process p, bool bind) {
    struct sigaction handler;
    handler.sa_handler = handle;
    sigemptyset(&handler.sa_mask);
    handler.sa_flags = 0;

    hack_struct *pcs;
    arrayInit(pcs);

    size_t i = 1;
    bool             error = false;
    while (!error && p.argv[i] != NULL) {
        char        *end;
        hack_struct pc;
        pc.pid  = strtol(p.argv[i], &end, 10);
        pc.done = false;
        if (*end != '\0') {
            fprintf(stderr, "Not a valid pid: %s\n", p.argv[i]);
            error = true;
            break;
        }
        arrayPush(pcs) = pc;
        ++i;
    }

    if (error || arrayLen(pcs) == 0) {
        printf("plz giv pid\n");
        arrayRelease(pcs);
        return;
    }

    int options = WUNTRACED;
    if (!bind) {
        options |= WNOHANG;
    }

    int done_count = 0;
    while (done_count != arrayLen(pcs)) {
        for (int j = 0; j < arrayLen(pcs); ++j) {
            int         status;
            hack_struct *current_pc = &j[pcs];
            if (current_pc->done) {
                continue;
            }

            if (bind) {
                printf("Resuming %ld...\n", (long) current_pc->pid);
            }/* else {
                printf("Waiting for %ld...\n", (long) current_pc->pid);
            }*/
            // install signal handler without SA_RESTART, so waitpid gets interrupted
            sigaction(SIGINT, &handler, NULL);
            pid_t res;
            if ((res = waitpid(current_pc->pid, &status, options)) < 0) {
                if (EINTR == errno) {
                    // cancelled by ctrl-c
                    if (bind) {
                        kill(current_pc->pid, SIGKILL);
                        waitpid(current_pc->pid, &status, options);
                        printf("Killed [%ld]\n", (long) current_pc->pid);
                    } else {
                        kill(current_pc->pid, SIGCONT);
                    }
                } else {
                    perror("Could not wait for process");
                    ++done_count;
                    current_pc->done = true;
                }
            } else if (res == 0) {
                // wait terminated because nohang
            } else {
                current_pc->done = true;
                ++done_count;
                if (bind) {
                    printf("[%i] ", WEXITSTATUS(status));
                } else {
                    printf("[%i] TERMINATED\n", current_pc->pid);
                    if (WIFEXITED(status)) {
                        printf("[%i] exited normally with status: %i\n", current_pc->pid,
                               WEXITSTATUS(status));
                    } else if (WIFSTOPPED(status)) {
                        printf("[%i] was stopped by signal: %s\n", current_pc->pid,
                               strsignal(WSTOPSIG(status)));
                    } else if (WIFSIGNALED(status)) {
                        printf("[%i] was terminated by signal: %s\n", current_pc->pid,
                               strsignal(WTERMSIG(status)));
                    } else {
                        printf("[%i] exited (not sure why), exit status: %i\n", current_pc->pid,
                               WEXITSTATUS(status));
                    }
                }
            }
            signal(SIGINT, SIG_IGN);
        }
    }

    arrayRelease(pcs);
}