-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.c
346 lines (298 loc) · 7.61 KB
/
tasks.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// tasks.c
// yxsh
//
// Created by Kirill on 19.04.2018.
// Copyright © 2018 Kirill. All rights reserved.
//
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <readline/readline.h>
#include "tasks.h"
#define STATUS_RUNNING 0
#define STATUS_STOPPED 1
#define STATUS_SIGNAL 2
#define STATUS_DONE 3
#define STATUS_CRASHED 4
static bool update_task_status(task_t*);
static void print_task(task_t*);
static char* get_status_description(int);
static int translate_status(int);
static void remove_task_by_index(size_t, tasks_env_t*);
void tasks_create_env(tasks_env_t* env) {
memset(env, 0, sizeof(tasks_env_t));
}
bool tasks_run_task(tasks_env_t* env, pid_t pgid, bool bg, char* display) {
pid_t* pids = (pid_t*) malloc(sizeof(pid_t));
pids[0] = pgid;
return tasks_run_pipeline(env, pgid, pids, bg, 1, display);
}
bool tasks_run_pipeline(tasks_env_t* env, pid_t pgid, pid_t* pids,
bool bg, size_t num, char* display) {
size_t index = 0;
bool finished;
task_t* task;
if (env->tasks_size >= MAXTSKS) { // No free space to create new task
// Try to collect zombies
tasks_collect_zombies(env);
// Check free space again
if (env->tasks_size >= MAXTSKS)
return false;
}
// Search for free index to insert new task
while (index < MAXTSKS && env->tasks[index++]);
// Free index not found
if (index >= MAXTSKS)
return false;
if (!(env->tasks[index - 1] = (task_t*) malloc(sizeof(task_t))))
return false;
task = env->tasks[index - 1];
task->display_name = display;
task->pgid = pgid;
task->id = index;
task->status = STATUS_RUNNING;
task->count = num;
task->pids = pids;
task->pids_amount = num;
env->tasks_size++;
finished = bg ? !update_task_status(task) : task_wait(task);
if (bg || !finished) {
if (!bg)
rl_crlf();
print_task(task);
}
if (finished)
remove_task_by_index(index - 1, env);
return true;
}
bool tasks_has_free(tasks_env_t* env) {
if (env->tasks_size >= MAXTSKS)
tasks_collect_zombies(env);
return env->tasks_size < MAXTSKS;
}
void tasks_release_env(tasks_env_t* env) {
size_t i = 0;
size_t task_count = 0;
int status;
signal(SIGCHLD, SIG_DFL);
while (task_count < env->tasks_size && i < MAXTSKS) {
task_t* task = env->tasks[i++];
if (!task)
continue;
task_count++;
killpg(task->pgid, SIGHUP);
if (waitpid(task->pgid, &status, WNOHANG | WUNTRACED) != -1)
task->status = translate_status(status);
remove_task_by_index(i - 1, env);
task_count--;
}
}
void tasks_collect_zombies(tasks_env_t* env) {
size_t i = 0;
size_t task_count = 0;
while (task_count < env->tasks_size && i < MAXTSKS) {
task_t* task = env->tasks[i++];
if (!task)
continue;
task_count++;
if ((task->status != STATUS_RUNNING &&
task->status != STATUS_STOPPED) ||
!update_task_status(task)) {
print_task(task);
remove_task_by_index(i - 1, env);
task_count--;
}
}
}
bool tasks_update_status(tasks_env_t* env) {
int status;
pid_t pid;
size_t i;
size_t task_count;
bool print = false;
while ((pid = wait(&status)) > 0) {
task_count = 0;
i = 0;
// Search for task with received pid
while (task_count < env->tasks_size && i < MAXTSKS) {
task_t* task = env->tasks[i++];
if (!task)
continue;
task_count++;
// Search for pid in this task
for (size_t j = 0; j < task->pids_amount; j++) {
if (pid == task->pids[j]) {
if (--task->count == 0) {
task->status = translate_status(status);
rl_crlf();
print_task(task);
print = true;
if (task->status != STATUS_STOPPED && task->status != STATUS_RUNNING)
remove_task_by_index(i - 1, env);
}
break;
}
}
}
}
return print;
}
void tasks_dump_list(tasks_env_t* env) {
size_t i = 0;
size_t task_count = 0;
while (task_count < env->tasks_size && i < MAXTSKS) {
task_t* task = env->tasks[i++];
if (!task)
continue;
task_count++;
print_task(task);
}
}
task_t* task_by_id(tasks_env_t* env, size_t id) {
return id - 1 < MAXTSKS ? env->tasks[id - 1] : NULL;
}
void task_resume_background(task_t* task) {
task->status = STATUS_RUNNING;
print_task(task);
if (killpg(task->pgid, SIGCONT)) {
perror("yxsh: Cannot resume task");
return;
}
}
void task_resume_foreground(tasks_env_t* env, task_t* task) {
signal(SIGCHLD, SIG_DFL);
if (!setup_terminal(task->pgid))
return;
if (killpg(task->pgid, SIGCONT)) {
perror("yxsh: Cannot resume task");
setup_terminal(getpgrp());
return;
}
signal(SIGINT, SIG_DFL);
if (task_wait(task))
remove_task_by_index(task->id - 1, env);
else {
rl_crlf();
print_task(task);
}
setup_terminal(getpgrp());
}
bool task_wait(task_t* task) {
int status;
bool repeat = true;
while (repeat) {
pid_t result = waitpid(-task->pgid, &status, WUNTRACED);
if (result != -1) {
if (WIFSTOPPED(status)) {
task->status = STATUS_STOPPED;
return false;
} else {
task->status = translate_status(status);
repeat = --task->count != 0;
}
} else {
perror("yxsh: Cannot wait for process");
return true;
}
}
return true;
}
bool setup_terminal(pid_t pid) {
signal(SIGTTOU, SIG_IGN);
if (tcsetpgrp(STDIN_FILENO, pid)) {
perror("yxsh: Cannot setup terminal foreground");
signal(SIGTTOU, SIG_DFL);
return false;
}
signal(SIGTTOU, SIG_DFL);
return true;
}
/**
* Updates task status.
*
* @param task Task.
*
* @return true if task is running or stopped and pipeline is not finished.
*/
static bool update_task_status(task_t* task) {
pid_t result;
int status;
if (!(result = waitpid(-task->pgid, &status, WNOHANG | WUNTRACED)))
return true;
if (result == -1) {
perror("yxsh: Cannot wait for process");
return false;
}
task->status = translate_status(status);
return task->status == STATUS_STOPPED
|| task->status == STATUS_RUNNING
|| --task->count != 0;
}
/**
* Prints task to stderr.
*
* @param task Task.
*/
static void print_task(task_t* task) {
char* status = get_status_description(task->status);
fprintf(stderr, "[%lu] (%d | %s): %s\n", (unsigned long) task->id,
(int) task->pgid, status, task->display_name);
}
/**
* Translates status from system status returned from waitpid to
* local status defines.
*
* @param status Input status.
*
* @return Result status.
*/
static int translate_status(int status) {
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
return STATUS_CRASHED;
return STATUS_DONE;
}
if (WIFSIGNALED(status))
return STATUS_SIGNAL;
if (WIFSTOPPED(status))
return STATUS_STOPPED;
return STATUS_RUNNING;
}
/**
* Looks for a suitable description string for process status.
*
* @param status Status.
*
* @return Description string.
*/
static char* get_status_description(int status) {
switch (status) {
case STATUS_CRASHED:
return "Crashed";
case STATUS_DONE:
return "Done";
case STATUS_SIGNAL:
return "Terminated";
case STATUS_STOPPED:
return "Stopped";
}
return "Running";
}
/**
* Removes task from environment by index and free memory.
*
* @param pos Position of the task.
* @param env Current environment.
*/
static void remove_task_by_index(size_t pos, tasks_env_t* env) {
free(env->tasks[pos]->display_name);
free(env->tasks[pos]->pids);
free(env->tasks[pos]);
env->tasks_size--;
env->tasks[pos] = NULL;
}