-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
297 lines (248 loc) · 6.97 KB
/
jobs.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
#include "./jobs.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct job_element {
int jid;
pid_t pid;
process_state_t state;
char *command;
struct job_element *next;
};
typedef struct job_element job_element_t;
// head is the head of the list
// current is the current element being iterated over
struct job_list {
job_element_t *head;
job_element_t *current;
pid_t shell_pid;
};
/* initializes job list, returns pointer */
job_list_t *init_job_list() {
job_list_t *job_list = (job_list_t *)malloc(sizeof(job_list_t));
job_list->head = NULL;
job_list->current = NULL;
job_list->shell_pid = getpid();
return job_list;
}
/*
* cleans up jobs list
* Note: this function will free the job_list pointer
* DO NOT use the pointer after this function is called
*/
void cleanup_job_list(job_list_t *job_list) {
if (job_list == NULL) {
return;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
job_element_t *nextElement = cur->next;
// if we are cleaning up the shell's job list and not a child's
if (getpid() == job_list->shell_pid) {
/* kill process */
if (kill(-cur->pid, SIGKILL) < 0) {
perror("kill");
}
}
if (cur->command != NULL) {
free(cur->command);
cur->command = NULL;
}
free(cur);
cur = nextElement;
}
job_list->head = NULL;
job_list->current = NULL;
job_list->shell_pid = 0;
free(job_list);
}
/* adds new job to list, returns 0 on success, -1 on failure */
int add_job(job_list_t *job_list, int jid, pid_t pid, process_state_t state,
char *command) {
if (job_list == NULL || (state != RUNNING && state != STOPPED) ||
command == NULL) {
return -1;
}
job_element_t *new = (job_element_t *)malloc(sizeof(job_element_t));
new->jid = jid;
new->pid = pid;
// allocate new char*'s and copy buffers in to protect our code
new->state = state;
size_t cmdlen = strlen(command);
new->command = (char *)malloc(sizeof(char) * (cmdlen + 1));
memcpy(new->command, command, cmdlen);
new->command[cmdlen] = 0;
new->next = NULL;
if (job_list->head == NULL) {
// add to head
job_list->head = new;
job_list->current = new;
} else {
// add to tail
job_element_t *cur = job_list->head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new;
}
return 0;
}
/* removes job from list, given job's JID,
returns 0 on success, -1 on failure */
int remove_job_jid(job_list_t *job_list, int jid) {
if (job_list == NULL) {
return -1;
}
job_element_t *prev = NULL;
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->jid == jid) {
if (prev != NULL) {
prev->next = cur->next;
}
if (job_list->head == cur) {
job_list->head = cur->next;
}
if (job_list->current == cur) {
job_list->current = cur->next;
}
if (cur->command != NULL) {
free(cur->command);
cur->command = NULL;
}
free(cur);
cur = NULL;
return 0;
}
prev = cur;
cur = cur->next;
}
return -1;
}
/* removes job from list, given job's PID,
returns 0 on success, -1 on failure */
int remove_job_pid(job_list_t *job_list, pid_t pid) {
if (job_list == NULL) {
return -1;
}
job_element_t *prev = NULL;
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->pid == pid) {
if (prev != NULL) {
prev->next = cur->next;
}
if (job_list->head == cur) {
job_list->head = cur->next;
}
if (job_list->current == cur) {
job_list->current = cur->next;
}
if (cur->command != NULL) {
free(cur->command);
cur->command = NULL;
}
free(cur);
cur = NULL;
return 0;
}
prev = cur;
cur = cur->next;
}
return -1;
}
/* updates job's state, given job's JID, returns 0 on success, -1 on failure */
int update_job_jid(job_list_t *job_list, int jid, process_state_t state) {
if (job_list == NULL) {
return -1;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->jid == jid) {
cur->state = state;
return 0;
}
cur = cur->next;
}
return -1;
}
/* updates job's state, given job's PID, returns 0 on success, -1 on failure */
int update_job_pid(job_list_t *job_list, pid_t pid, process_state_t state) {
if (job_list == NULL) {
return -1;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->pid == pid) {
cur->state = state;
return 0;
}
cur = cur->next;
}
return -1;
}
/* gets PID of job, given job's JID, returns PID on success, -1 on failure */
pid_t get_job_pid(job_list_t *job_list, int jid) {
if (job_list == NULL) {
return -1;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->jid == jid) {
return cur->pid;
}
cur = cur->next;
}
return -1;
}
/* gets JID of job, given job's PID, returns JID on success, -1 on failure */
int get_job_jid(job_list_t *job_list, pid_t pid) {
if (job_list == NULL) {
return -1;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
if (cur->pid == pid) {
return cur->jid;
}
cur = cur->next;
}
return -1;
}
/*
* gets next PID in list
* call this in a loop to get the PID of the next job in the list
* returns the PID if there is one, -1 if the end of the list has been reached,
* after which it will start at the head of the list again
*/
pid_t get_next_pid(job_list_t *job_list) { // circular iterator
if (job_list == NULL) {
return -1;
}
if (job_list->current == NULL) {
job_list->current = job_list->head;
return -1;
} else {
pid_t pid = job_list->current->pid;
job_list->current = job_list->current->next;
return pid;
}
}
/* jobs command, prints out the jobs list */
void jobs(job_list_t *job_list) {
if (job_list == NULL) {
return;
}
job_element_t *cur = job_list->head;
while (cur != NULL) {
char *state_string = cur->state == RUNNING ? "Running" : "Stopped";
if (printf("[%d] (%d) %s %s\n", cur->jid, cur->pid, state_string,
cur->command) < 0) {
fprintf(stderr, "error printing jobs list\n");
cleanup_job_list(job_list);
exit(1);
}
cur = cur->next;
}
}