~emersion/mrsh

ref: 4b0435e60a86d710080ab7aac5b7ba8b23b29928 mrsh/shell/job.c -rw-r--r-- 4.8 KiB
4b0435e6emersion wip: job control 2 years 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#define _POSIX_C_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <mrsh/array.h>
#include <mrsh/shell.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "shell/job.h"
#include "shell/process.h"

static const int ignored_signals[] = {
	SIGINT,
	SIGQUIT,
	SIGTSTP,
	SIGTTIN,
	SIGTTOU,
};

static const size_t IGNORED_SIGNALS_LEN =
		sizeof(ignored_signals) / sizeof(ignored_signals[0]);

// TODO: these shouldn't be globals
struct mrsh_array jobs = { 0 };
static bool job_control_enabled = false;
static struct job *foreground_job = NULL;

bool mrsh_set_job_control(struct mrsh_state *state, bool enabled) {
	assert(state->fd >= 0);

	if (job_control_enabled == enabled) {
		return true;
	}

	if (enabled) {
		// Loop until we are in the foreground
		while (true) {
			pid_t pgid = getpgrp();
			if (tcgetpgrp(state->fd) == pgid) {
				break;
			}
			kill(-pgid, SIGTTIN);
		}

		// Ignore interactive and job-control signals
		struct sigaction sa = { .sa_handler = SIG_IGN };
		sigemptyset(&sa.sa_mask);
		for (size_t i = 0; i < IGNORED_SIGNALS_LEN; ++i) {
			sigaction(ignored_signals[i], &sa, NULL);
		}

		// Put ourselves in our own process group
		state->pgid = getpid();
		if (setpgid(state->pgid, state->pgid) < 0) {
			return false;
		}

		// Grab control of the terminal
		tcsetpgrp(state->fd, state->pgid);
		// Save default terminal attributes for shell
		tcgetattr(state->fd, &state->term_modes);
	} else {
		return false; // TODO
	}

	job_control_enabled = enabled;
	return true;
}

bool job_child_init(struct mrsh_state *state, bool foreground) {
	// TODO: don't always create a new process group
	pid_t pgid = getpid();
	if (setpgid(pgid, pgid) < 0) {
		return false;
	}

	if (foreground) {
		tcsetpgrp(state->fd, pgid);
	}

	struct sigaction sa = { .sa_handler = SIG_DFL };
	sigemptyset(&sa.sa_mask);
	for (size_t i = 0; i < IGNORED_SIGNALS_LEN; ++i) {
		sigaction(ignored_signals[i], &sa, NULL);
	}

	return true;
}

struct job *job_create(struct mrsh_state *state) {
	struct job *job = calloc(1, sizeof(struct job));
	job->state = state;
	job->pgid = 0;
	mrsh_array_add(&jobs, job);
	return job;
}

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;
}

static void job_remove(struct job *job) {
	assert(foreground_job != job);

	for (size_t i = 0; i < jobs.len; ++i) {
		if (jobs.data[i] == job) {
			array_remove(&jobs, i);
			break;
		}
	}
}

void job_destroy(struct job *job) {
	if (job == NULL) {
		return;
	}
	job_set_foreground(job, false);
	job_remove(job);
	free(job);
}

void job_add_process(struct job *job, pid_t pid) {
	if (job->pgid == 0) {
		job->pgid = pid;
	}

	setpgid(pid, job->pgid);
}

static struct job *job_from_pid(pid_t pid) {
	for (size_t i = 0; i < jobs.len; ++i) {
		struct job *job = jobs.data[i];
		// TODO: search in process list
		if (job->pgid == pid) {
			return job;
		}
	}
	return NULL;
}

bool job_set_foreground(struct job *job, bool foreground) {
	struct mrsh_state *state = job->state;

	bool is_foreground = foreground_job == job;
	if (is_foreground == foreground) {
		return true;
	}

	if (foreground) {
		if (foreground_job != NULL) {
			job_set_foreground(foreground_job, false);
		}

		tcsetpgrp(state->fd, job->pgid);

		// TODO: only do this if we want to continue this job
		//tcsetattr(state->fd, TCSADRAIN, &job->term_modes);
		foreground_job = job;
	} else {
		if (tcgetpgrp(state->fd) == job->pgid) {
			tcsetpgrp(state->fd, state->pgid);

			tcgetattr(state->fd, &job->term_modes);
			tcsetattr(state->fd, TCSADRAIN, &state->term_modes);
		}

		if (foreground_job == job) {
			foreground_job = NULL;
		}
	}

	return true;
}

bool job_continue(struct job *job) {
	// TODO: mark all processes as non-stopped
	return kill(-job->pgid, SIGCONT) >= 0;
}

struct job *job_foreground(void) {
	return foreground_job;
}

bool job_wait(struct job *job) {
	while (true) {
		int stat;
		pid_t pid;
		do {
			pid = waitpid(-1, &stat, WUNTRACED);
		} while (pid == -1 && errno == EINTR);

		if (pid == -1) {
			fprintf(stderr, "failed to waitpid(): %s\n", strerror(errno));
			return false;
		}
		printf("waitpid() = %d, %d, WIFSTOPPED=%d, WIFEXITED=%d, WIFSIGNALED=%d\n",
			pid, stat, WIFSTOPPED(stat), WIFEXITED(stat), WIFSIGNALED(stat)); // TODO: remove me

		process_notify(pid, stat);

		struct job *waited_job = job_from_pid(pid);
		if (waited_job == NULL) {
			continue;
		}

		// TODO: only works if job has only one process
		// TODO: figure out when to destroy jobs
		if (WIFSTOPPED(stat)) {
			job_set_foreground(job, false);
		} else if (WIFEXITED(stat) || WIFSIGNALED(stat)) {
			job_set_foreground(job, false);
			job_remove(job);
		} else {
			assert(false);
		}

		if (job == waited_job) {
			return true;
		}
	}
}