~emersion/mrsh

ref: 41ead0f046b94515afa805ed67da4e82d38b028f mrsh/shell/process.c -rw-r--r-- 1.8 KiB
41ead0f0Simon Ser build: disable -Wformat-overflow 3 months ago
                                                                                
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
#define _POSIX_C_SOURCE 1
#include <assert.h>
#include <mrsh/array.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "shell/process.h"
#include "shell/task.h"

struct mrsh_process *process_create(struct mrsh_state *state, pid_t pid) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	struct mrsh_process *proc = calloc(1, sizeof(struct mrsh_process));
	proc->pid = pid;
	proc->state = state;
	mrsh_array_add(&priv->processes, proc);
	return proc;
}

static void array_remove(struct mrsh_array *array, size_t i) {
	memmove(&array->data[i], &array->data[i + 1],
		(array->len - i - 1) * sizeof(void *));
	--array->len;
}

void process_destroy(struct mrsh_process *proc) {
	struct mrsh_state_priv *priv = state_get_priv(proc->state);

	for (size_t i = 0; i < priv->processes.len; ++i) {
		if (priv->processes.data[i] == proc) {
			array_remove(&priv->processes, i);
			break;
		}
	}

	free(proc);
}

int process_poll(struct mrsh_process *proc) {
	if (proc->stopped) {
		return TASK_STATUS_STOPPED;
	} else if (!proc->terminated) {
		return TASK_STATUS_WAIT;
	}

	if (WIFEXITED(proc->stat)) {
		return WEXITSTATUS(proc->stat);
	} else if (WIFSIGNALED(proc->stat)) {
		return 129; // POSIX requires >128
	} else {
		assert(false);
	}
}

void update_process(struct mrsh_state *state, pid_t pid, int stat) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	struct mrsh_process *proc = NULL;
	bool found = false;
	for (size_t i = 0; i < priv->processes.len; ++i) {
		proc = priv->processes.data[i];
		if (proc->pid == pid) {
			found = true;
			break;
		}
	}
	if (!found) {
		return;
	}

	if (WIFEXITED(stat) || WIFSIGNALED(stat)) {
		proc->terminated = true;
		proc->stat = stat;
	} else if (WIFSTOPPED(stat)) {
		proc->stopped = true;
		proc->signal = WSTOPSIG(stat);
	} else {
		assert(false);
	}
}