summaryrefslogtreecommitdiffstats
path: root/02_exercise/process.c
blob: 00f876206a5c99603799596ab355a02cdfea2182 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Created by stefan on 21.05.20.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>

#include "array.h"
#include "process.h"

// Given a substring of a line tries to parse out as much information as possible
int parse_command(char const *line, char const *end, process *p) {

    char *part;
    char **local_parts;
    if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) {
        fprintf(stderr, "Failed to prepare new part / parts array whilst parsing line");
        return -1;
    }

    for (size_t i = 0; line + i < end; ++i) {
        char c = line[i];
        if (c == ' ' || c == '\0') {
            if (arrayLen(part) == 0)
                continue;
            arrayPush(part) = '\0';
            arrayPush(local_parts) = part;
            if (c == '\0') {
                arrayPush(local_parts) = NULL;
                break;
            } else {
                arrayInit(part);
            }
        } else {
            arrayPush(part) = c;
        }
    }
    if (arrayLen(part) == 0) {
        arrayRelease(part);
    } else {
        arrayPush(local_parts) = part;
    }

    arrayPush(local_parts) = NULL;
    p->argc = arrayLen(local_parts) - 1;
    p->argv = local_parts;

    return 0;
}

int parse_line(char const *const line, process **processes) {
    // Splits the line at | and then parses the commands
    int ret_code = 0;

    if (arrayInit(*processes) != 0) {
        perror("Failed to initialize processes array");
        return -1;
    }
    bool done = false;
    char const *cursor = line;
    while (!done) {
        process p = {.in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true};
        char const *end = strchr(cursor, '|');
        if (end == NULL) {
            done = true;
            end = line + strlen(line);
        }
        if ((ret_code = parse_command(cursor, end, &p)) != 0) {
            return ret_code;
        }

        arrayPush(*processes) = p;
        cursor = end + 1;
    }
    size_t p_len = arrayLen(*processes);
    process *last = *processes + (p_len - 1);

    // Linking up all processes as we currently only have pipes for multiple commands

    for (size_t i = 0; i < p_len - 1; ++i) {
        int fds[2];
        if (pipe(fds) != 0) {
            perror("Failed to create pipe");
            return -1;
        }
        (*processes)[i].out_fd = fds[1];
        (*processes)[i + 1].in_fd = fds[0];
    }
    // Setting all processes to non blocking when
    if (strcmp(last->argv[last->argc - 1], "&") == 0) {
        arrayPop(last->argv);
        last->argc = last->argc - 1;
        last->argv[last->argc] = NULL;

        for (size_t i = 0; i < p_len; ++i) {
            (*processes)[i].blocking = false;
        }
    }
    return ret_code;
}

int exec_command(process p, unsigned timeout) {
    timeout = timeout ^ timeout;
    int pid;
    int status;

    if ((pid = fork()) == 0) {
        if (p.in_fd != 0) {
            dup2(p.in_fd, 0);
        }
        if (p.out_fd != 0) {
            dup2(p.out_fd, 1);
        }
        execvp(p.argv[0], p.argv);
        fprintf(stderr, "could not execute \"%s\"\n", p.argv[0]);
        fflush(stderr);
        exit(-1);
    }

    if (pid < 0) {
        fprintf(stderr, "no fork\n");
        exit(-2);
    }

    waitpid(pid, &status, 0);

    return WEXITSTATUS(status);
}

int free_processes(process **pr) {
    process *processes = *pr;
    while (!arrayIsEmpty(processes)) {
        process p = arrayPop(processes);
        while (!arrayIsEmpty(p.argv)) {
            char *tmp = arrayPop(p.argv);
            if (tmp)
                arrayRelease(tmp);
        }
        arrayRelease(p.argv);
    }
    arrayRelease(processes);
    *pr = NULL;
}