-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
381 lines (327 loc) · 11.4 KB
/
shell.c
File metadata and controls
381 lines (327 loc) · 11.4 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "shell.h"
/* Initialize shell process. */
void init_shell(char *argv[]);
/* Prints invite_string to STDOUT.
Return 1, if print was successful.
Or 0, if print failed. After fail shell will be closed. */
int get_invite();
/* Launch and wait, if necessary, new job. */
void launch_job(int foreground);
/* Used for executing command in forked process. */
void launch_process(process *p, pid_t pgid, int infile_local, int outfile_local, int errfile_local, int foreground);
char hostname[HOST_NAME_MAX]; /* name of host */
char username[LOGIN_NAME_MAX]; /* user name */
char invite_string[HOST_NAME_MAX + LOGIN_NAME_MAX + MAX_DIRECTORY_SIZE + 5]; /* invite string */
char line[READ_LINE_SIZE]; /* line reading buffer */
char varline[READ_LINE_SIZE * 4]; /* buffer line for values of variables from parsing input string */
char dir[MAX_DIRECTORY_SIZE]; /* directory string buffer for printing to invite_string */
int main(__attribute__((unused)) int argc, char *argv[])
{
/* INIT SHELL */
init_shell(argv);
int ncmds;
/* Main cycle. First, shell makes an invitation, then waits for the line to be entered. */
while (get_invite() && prompt_line(line, sizeof(line)) > 0)
{
/* End of waiting for input from the terminal. */
invite_mode = 0;
/* Parse line to cmds[] array. */
if ((ncmds = parse_line(line, varline)) <= 0)
continue;
/* Create new job. */
if (!(current_job = create_new_job(0, line)))
{
fprintf(stderr, "Can't create job!");
fflush(stderr);
shell_exit(EXIT_FAILURE);
}
/* Parse cmds[] data to current_job. */
fill_job(¤t_job, ncmds);
/* If parsing was failed. */
if(!current_job)
continue;
/* Add new current_job to job list. */
add_job(current_job);
/* Run current_job with set bkgrnd flag.
The bkgrnd value was obtained when parsing the input line. */
launch_job(!bkgrnd);
/* Current job may be removed, if job contains inner commands. */
if(current_job)
{
/* Check state of job after waiting. */
if(!bkgrnd && job_is_completed(current_job))
{
/* Notify all completed or stopped jobs after executing current_job. */
do_job_notification(0);
/* We no longer need this job. */
remove_job(current_job->pgid);
}else if(!bkgrnd && job_is_stopped(current_job))
{
/* Notify if current_job is stopped. */
current_job->notified = 1;
format_job_info(current_job, "stopped");
fprintf(stdout, "\n");
fflush(stdout);
}else if(bkgrnd)
{
/* Show index of new background task. */
current_job->notified = 1;
format_job_info(current_job, NULL);
fprintf(stdout, "\n");
fflush(stdout);
}
}
}
shell_exit(EXIT_SUCCESS);
return 0;
}
/* Prints invite_string to STDOUT.
Return 1, if print was successful.
Or 0, if print failed. After fail shell will be closed. */
int get_invite()
{
/* Begin to wait input.
At this moment SIGCHLD may print notifications of stopped or terminated processes. */
invite_mode = 1;
int flag = write(STDOUT_FILENO, invite_string, strlen(invite_string)) != -1;
return flag && (fflush(stdout) != EOF);
}
/* Initialize shell process. */
void init_shell(char *argv[])
{
shell_terminal = STDIN_FILENO;
/* See if we are running interactively. */
int shell_is_interactive = isatty(shell_terminal);
if (shell_is_interactive)
{
/* Loop until we are in the foreground. */
while(tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp()))
kill(-shell_pgid, SIGTTIN);
/* Ignore interactive and job-control signals. */
set_signal_handler(SIGINT, SIG_IGN);
set_signal_handler(SIGQUIT, SIG_IGN);
set_signal_handler(SIGTSTP, SIG_IGN);
set_signal_handler(SIGTTIN, SIG_IGN);
set_signal_handler(SIGTTOU, SIG_IGN);
set_signal_handler(SIGTERM, SIG_IGN);
set_signal_handler(SIGCHLD, notify_child);
/* Put ourselves in our own process group. */
shell_pgid = getpid();
if (setpgid(shell_pgid, shell_pgid) < 0)
{
perror("Couldn't put the shell in its own process group");
shell_exit(EXIT_FAILURE);
}
/* Grab control of the terminal. */
tcsetpgrp(shell_terminal, shell_pgid);
tcgetattr(shell_terminal, &shell_tmodes);
/* Init home location of shell. */
init_home(argv[0]);
/* Init invite_string. */
if(gethostname(hostname, HOST_NAME_MAX))
{
perror("gethostname");
strncpy(hostname, "unknown", strlen("unknown"));
hostname[strlen("unknown")] = '\0';
}
if(getlogin_r(username, LOGIN_NAME_MAX))
{
perror("getlogin_r");
strncpy(username, "unknown", strlen("unknown"));
username[strlen("unknown")] = '\0';
}
get_dir_prompt(dir);
sprintf(invite_string, "%s@%s:%s$ ", username, hostname, dir);
} else
{
fprintf(stderr, "Can't run shell: shell not in foreground!\n");
fflush(stderr);
shell_exit(EXIT_FAILURE);
}
}
/* Used for executing command in forked process. */
void launch_process(process *p, pid_t pgid, int infile_local, int outfile_local, int errfile_local, int foreground)
{
/* Put the process into the process group and give the process group
the terminal, if appropriate.
This has to be done both by the shell and in the individual
child processes because of potential race conditions. */
pid_t pid = getpid();
if (pgid == 0)
pgid = pid;
setpgid(pid, pgid);
if (foreground)
tcsetpgrp(shell_terminal, pgid);
/* Set the handling for job control signals back to the default. */
set_signal_handler(SIGINT, SIG_DFL);
set_signal_handler(SIGQUIT, SIG_DFL);
set_signal_handler(SIGTSTP, SIG_DFL);
set_signal_handler(SIGTTIN, SIG_DFL);
set_signal_handler(SIGTTOU, SIG_DFL);
set_signal_handler(SIGCHLD, SIG_DFL);
set_signal_handler(SIGPIPE, SIG_DFL);
set_signal_handler(SIGTERM, SIG_DFL);
/* Set the standard input/output channels of the new process. */
if (infile_local != STDIN_FILENO)
{
if(dup2(infile_local, STDIN_FILENO) == -1)
{
fprintf(stderr, "%s: dup", p->argv[0]);
perror(NULL);
clear_job_list(0);
exit(EXIT_FAILURE);
}
close(infile_local);
}
if (outfile_local != STDOUT_FILENO)
{
if(dup2(outfile_local, STDOUT_FILENO) == -1)
{
fprintf(stderr, "%s: dup", p->argv[0]);
perror(NULL);
clear_job_list(0);
exit(EXIT_FAILURE);
}
close(outfile_local);
}
if (errfile_local != STDERR_FILENO)
{
if(dup2(errfile_local, STDERR_FILENO) == -1)
{
fprintf(stderr, "%s: dup", p->argv[0]);
perror(NULL);
clear_job_list(0);
exit(EXIT_FAILURE);
}
close(errfile_local);
}
/* Save only argv for child. So copy it. */
unsigned size = 0;
while (p->argv[size] != NULL)
size++;
char** argv = malloc((size + 1) * sizeof(char*));
if(!argv)
{
perror("child malloc");
clear_job_list(0);
exit(EXIT_FAILURE);
}
for(unsigned j = 0; j < size; j++)
{
argv[j] = strdup(p->argv[j]);
if(errno == ENOMEM)
{
perror("child malloc");
for (unsigned k = 0; k < j; ++k)
free(argv[k]);
free(argv);
clear_job_list(0);
exit(EXIT_FAILURE);
}
}
argv[size] = NULL;
/* Clear extra memory in child process. */
clear_job_list(0);
free_dir();
/* Exec the new process. Make sure we exit. */
execvp(argv[0], argv);
fprintf(stderr, "\nexecvp: %s: ", argv[0]);
for(unsigned j = 0; j < size; j++)
free(argv[j]);
free(argv);
perror(NULL);
exit(EXIT_FAILURE);
}
/* Launch and wait, if necessary, new job. */
void launch_job(int foreground)
{
process *p, *p_next = NULL;
pid_t pid;
int mypipe[2], infile_local, outfile_local;
int inner_cmd_stat;
/* Flag for checking the job for inner commands only. */
int exec_only_inner = 1;
infile_local = current_job->stdin_file;
for (p = current_job->first_process;p;)
{
p_next = p->next;
/* Set up pipes, if necessary. */
if (p_next)
{
current_job->have_pipe = 1;
if (pipe(mypipe) < 0)
{
perror("pipe");
shell_exit(EXIT_FAILURE);
}
outfile_local = mypipe[1];
} else
outfile_local = current_job->stdout_file;
/* Check for the internal implementation of the command. */
if(command_is_inner(p->argv[0]))
{
/* Set flags that this inner process completed. */
p->stopped = 0;
p->completed = 1;
if ((inner_cmd_stat = exec_inner(p->argv[0], (const char **) p->argv, infile_local, outfile_local)) == MAY_EXIT)
/* We get MAY_EXIT code, so we can exit from shell. */
shell_exit(EXIT_SUCCESS);
else if (inner_cmd_stat != NOT_INNER_COMMAND)
{
/* Update invite_string, because of we can launch cd command. */
get_dir_prompt(dir);
sprintf(invite_string, "%s@%s:%s$ ", username, hostname, dir);
}
} else
{
/* We have non-internal command, so set exec_only_inner to 0. */
exec_only_inner = 0;
/* Fork the child processes. */
pid = fork();
if (pid == 0)
/* This is the child process. */
launch_process(p, current_job->pgid, infile_local, outfile_local, current_job->stderr_file, foreground);
else if (pid < 0)
{
/* The fork failed. */
perror("fork");
shell_exit(EXIT_FAILURE);
} else
{
/* This is the parent process. */
p->pid = pid;
if (!current_job->pgid)
current_job->pgid = pid;
setpgid(pid, current_job->pgid);
}
}
/* Current job may be removed, if job contains inner commands. */
if(current_job)
{
/* Clean up after pipes. */
if (infile_local != current_job->stdin_file)
close(infile_local);
if (outfile_local != current_job->stdout_file)
close(outfile_local);
}
infile_local = mypipe[0];
p = p_next;
}
/* Inner commands don't run in forked processes, so we don't have to wait for them. */
if(!exec_only_inner)
{
if(foreground)
put_job_in_foreground(current_job, 0);
else
put_job_in_background(current_job, 0);
}
}
/* Exit from shell. */
void shell_exit(int stat)
{
/* Free memory. */
clear_job_list(1);
free_dir();
/* Rest in peace, my victim of g***ocoding. */
exit(stat);
}