aboutsummaryrefslogtreecommitdiff
path: root/src/Program.c
blob: 2509f6f30e71af5d6be46f0db892c2c472e86ab6 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "../include/Program.h"
#include <unistd.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define READ_END 0
#define WRITE_END 1

int exec_program(const char **args, ProgramOutputCallback output_callback, void *userdata) {
    /* 1 arguments */
    if(args[0] == NULL)
        return -1;
    
    int fd[2];
    if(pipe(fd) == -1) {
        perror("Failed to open pipe");
        return -2;
    }

    pid_t parent_pid = getpid();

    pid_t pid = fork();
    if(pid == -1) {
        perror("Failed to fork");
        return -3;
    } else if(pid == 0) { /* child */
        if(prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
            perror("prctl(PR_SET_PDEATHSIG, SIGTERM) failed");
            exit(127);
        }

        /* Test if the parent died before the above call to prctl */
        if(getppid() != parent_pid)
            exit(127);

        dup2(fd[WRITE_END], STDOUT_FILENO);
        close(fd[READ_END]);
        close(fd[WRITE_END]);

        execvp(args[0], args);
        return 0;
    } else { /* parent */
        close(fd[WRITE_END]);

        int result = 0;
        int status;

        char buffer[2048];

        for(;;) {
            ssize_t bytes_read = read(fd[READ_END], buffer, sizeof(buffer) - 1);
            if(bytes_read == 0) {
                break;
            } else if(bytes_read == -1) {
                int err = errno;
                fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err));
                result = -err;
                goto cleanup;
            }

            buffer[bytes_read] = '\0';
            if(output_callback && output_callback(buffer, bytes_read, userdata) != 0)
                break;
        }

        if(waitpid(pid, &status, 0) == -1) {
            perror("waitpid failed");
            result = -5;
            goto cleanup;
        }

        if(!WIFEXITED(status)) {
            result = -4;
            goto cleanup;
        }

        int exit_status = WEXITSTATUS(status);
        if(exit_status != 0) {
            fprintf(stderr, "Failed to execute program (");
            const char **arg = args;
            while(*arg) {
                if(arg != args)
                    fputc(' ', stderr);
                fprintf(stderr, "'%s'", *arg);
                ++arg;
            }
            fprintf(stderr, "), exit status %d\n", exit_status);
            result = -exit_status;
            goto cleanup;
        }

        cleanup:
        close(fd[READ_END]);
        return result;
    }
}

int wait_program(pid_t process_id) {
    int status;
    if(waitpid(process_id, &status, 0) == -1) {
        perror("waitpid failed");
        return -errno;
    }

    if(!WIFEXITED(status))
        return -4;

    return WEXITSTATUS(status);
}

int exec_program_async(const char **args, pid_t *result_process_id) {
    /* 1 arguments */
    if(args[0] == NULL)
        return -1;

    pid_t parent_pid = getpid();

    pid_t pid = fork();
    if(pid == -1) {
        int err = errno;
        perror("Failed to fork");
        return -err;
    } else if(pid == 0) { /* child */
        if(prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
            perror("prctl(PR_SET_PDEATHSIG, SIGTERM) failed");
            exit(127);
        }

        /* Test if the parent died before the above call to prctl */
        if(getppid() != parent_pid)
            exit(127);

        execvp(args[0], args);
    } else { /* parent */
        if(result_process_id)
            *result_process_id = pid;
    }
    return 0;
}

#if 0
int program_pipe_write(ProgramPipe *self, const char *data, size_t size) {
    ssize_t bytes_written = write(self->write_fd, data, size);
    if(bytes_written == -1) {
        int err = errno;
        perror("Failed to write to pipe to program");
        return -err;
    }
    return 0;
}

int program_pipe_read(ProgramPipe *self, ProgramOutputCallback output_callback, void *userdata) {
    char buffer[2048];

    for(;;) {
        ssize_t bytes_read = read(self->read_fd, buffer, sizeof(buffer) - 1);
        if(bytes_read == 0) {
            break;
        } else if(bytes_read == -1) {
            int err = errno;
            perror("Failed to read from pipe to program");
            return -err;
        }

        buffer[bytes_read] = '\0';
        if(output_callback && output_callback(buffer, bytes_read, userdata) != 0)
            break;
    }

    return 0;
}

void program_pipe_close(ProgramPipe *self) {
    close(self->read_fd);
    close(self->write_fd);
    self->read_fd = -1;
    self->write_fd = -1;
}
#endif