-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_control.c
288 lines (266 loc) · 6.2 KB
/
process_control.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
/**
* Definitions for process control functions.
*/
#define _POSIX_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include "llist.h"
#include "process_control.h"
#include "signal_handlers.h"
/**
* Forks a child to try and execute the user input as a foreground process.
*
* @param input The full user command
*/
int
fork_child_fg(struct Input *input)
{
// Block SIGTSTP until fg process finishes
sigset_t block_set;
sigaddset(&block_set, SIGTSTP);
sigprocmask(SIG_BLOCK, &block_set, NULL); // block SIGTSTP
// Fork a child
pid_t childPid = fork();
if (childPid == -1)
{ // Fork error
fprintf(stderr, "fork(): Fork failed\n");
fflush(stderr);
exit(EXIT_FAILURE);
}
else if (childPid == 0)
{ // Child Process
// Unblock SIGINT
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
// Setup SIGINT action
struct sigaction SIGINT_action;
SIGINT_action.sa_handler = catch_SIGINT; // custom handler
sigemptyset(&SIGINT_action.sa_mask);
SIGINT_action.sa_flags = 0;
sigaction(SIGINT, &SIGINT_action, NULL); // child will catch SIGINT
// Redirect I/O if needed and try to execute the command
if (!redirect_input(input->infile) && !redirect_output(input->outfile))
{
exec_input(input->args);
}
// Error during execution
cleanup_input(input);
exit(EXIT_FAILURE);
}
else
{ // Parent Process
// Wait for the child to finish
int childStatus;
int exitStatus;
childPid = waitpid(childPid, &childStatus, 0);
// Report its status
if (WIFEXITED(childStatus))
{
exitStatus = WEXITSTATUS(childStatus);
}
else
{
exitStatus = -WTERMSIG(childStatus);
printf("terminated by signal %d\n", -exitStatus);
fflush(stdout);
}
sigprocmask(SIG_UNBLOCK, &block_set, NULL); // unblock SIGTSTP
return exitStatus;
}
}
/**
* Forks a child to try and execute the user input as a background
* process.
*
* @param input The full user command
* @return A pointer to the new child Node
*/
struct Node *
fork_child_bg(struct Input *input)
{
// Fork a child
pid_t childPid = fork();
if (childPid == -1)
{ // Fork error
fprintf(stderr, "fork(): Fork failed\n");
fflush(stderr);
exit(EXIT_FAILURE);
}
else if (childPid == 0)
{ // Child Process
struct sigaction ignore_action = {0};
ignore_action.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &ignore_action, NULL); // child will ignore SIGTSTP
// Define the path for I/O redirection
char *in = "/dev/null";
char *out = "/dev/null";
if (input->infile != NULL)
{
in = input->infile;
}
if (input->outfile != NULL)
{
out = input->outfile;
}
// Redirect I/O and try to execute the input
if (!redirect_input(in) && !redirect_output(out))
{
exec_input(input->args);
}
// Error while executing
cleanup_input(input);
exit(EXIT_FAILURE);
}
else
{ // Parent Process
printf("background PID is %d\n", childPid);
fflush(stdout);
struct Node *newChild = init_node(childPid);
return newChild;
}
}
/**
* (adapted from "Processes and I/O" Exploration)
* Redirects stdin to the file with the given filename.
*
* @param infile The name of the input file
* @return -1 for failure, 0 for success
*/
int
redirect_input(char *filename)
{
if (filename != NULL)
{
int in = open(filename, O_RDONLY);
fcntl(in, F_SETFD, FD_CLOEXEC);
if (in == -1)
{
perror("open()");
fflush(stderr);
return -1;
}
if (dup2(in, 0) == -1)
{
perror("dup2()");
fflush(stderr);
return -1;
}
}
return 0;
}
/**
* (adapted from "Processes and I/O" Exploration)
* Redirects stdout to the file with the given filename.
*
* @param outfile The name of the output file
* @return -1 for failure, 0 for success
*/
int
redirect_output(char *filename)
{
if (filename != NULL)
{
int out = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
fcntl(out, F_SETFD, FD_CLOEXEC);
if (out == -1)
{
perror("open()");
fflush(stderr);
return -1;
}
if (dup2(out, 1) == -1)
{
perror("dup2()");
fflush(stderr);
return -1;
}
}
return 0;
}
/**
* Attempts to execute the given argument(s), printing an error and
* returning if not.
*
* @param args The array of arguments
* @return -1 to signify failure
*/
int
exec_input(char **args)
{
// Attempt to exec the command
execvp(args[0], args);
// Error during execution
fprintf(stderr, "execvp(): Bad argument(s) '%s", args[0]);
for (int i = 1; args[i] != NULL; ++i)
{
fprintf(stderr, " %s", args[i]);
}
fprintf(stderr, "'\n");
fflush(stderr);
return -1;
}
/**
* Attempts to reap any will child processes, returning the PID of the
* reaped process if successful or 0 otherwise.
*
* @param bgLlist The pointer to the list of current background processes
*/
void
reap(struct Llist *bgLlist)
{
if (bgLlist == NULL || bgLlist->size == 0)
{
return; // No processes to reap
}
// Attempt to reap a process and report its exit status
int reapedPid;
int childStatus;
int exitStatus;
do
{
reapedPid = waitpid(-1, &childStatus, WNOHANG);
if (reapedPid > 0)
{
// Process has finished
printf("background process %d finished: ", reapedPid);
if WIFEXITED(childStatus)
{
exitStatus = WEXITSTATUS(childStatus);
printf("exit value %d\n", exitStatus);
}
else if (WIFSIGNALED(childStatus))
{
exitStatus = WTERMSIG(childStatus);
printf("terminated by signal %d\n", exitStatus);
}
fflush(stdout);
delete_node(bgLlist, reapedPid);
}
}
while (reapedPid > 0);
}
/**
* Iterates over the list of background processes to kill them.
*
* @param bgLlist The pointer to the list of background processes
*/
void
kill_bg(struct Llist *bgLlist)
{
struct Node *current = bgLlist->head;
while (current != NULL)
{
if (kill(current->value, SIGTERM))
{
perror("kill()");
}
current = current->next;
}
}