-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute-command.c
executable file
·625 lines (511 loc) · 17.6 KB
/
execute-command.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// UCLA CS 111 Lab 1 command execution
#include "command.h"
#include "command-internals.h"
#include "alloc.h"
#include <sys/types.h>
#include <error.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
typedef struct node* node_t;
typedef struct child_node* child_node_t;
typedef struct command_node* command_node_t;
// A struct that holds the word and a pointer to the next word
struct node
{
char* word;
struct node* next;
};
//the child node which stores the word, a pointer to the enxt child and a pointer to the dependent
struct child_node
{
struct command_node* dependent;
struct child_node* next;
char* word;
};
// the main command node, stores the dependencies and the input outputs for a command. also has the next pointer.
// might be redundant but we did it cause it was easier
struct command_node
{
struct command* c;
struct node* inputs;
struct node* outputs;
int dependencies;
struct child_node* dependents;
int pid;
struct command_node* next;
char *word;
};
int
command_status (command_t c)
{
return c->status;
}
void execute_wrapper(command_t c);
void execute_simple_command(command_t command)
{
pid_t pid;
if((pid = fork()) < 0)
error(1,0,"Could not create new process");
if (pid == 0) {
if(command->input != NULL){
int fd0 = open(command->input,O_RDONLY,0666); //Open Input File
if(fd0 < 0)
error(1,0,"Input file does not exist");
dup2(fd0,0); //Copy File descriptor to STDIN
close(fd0);
}
if(command->output != NULL){
int fd1 = open(command->output,O_WRONLY | O_CREAT | O_TRUNC,0666); //Open Output File
if(fd1 < 0)
error(1,0,"Could not write to output file");
dup2(fd1,1); //Copy File descriptor to STDOUT
close(fd1);
}
if(execvp(command->u.word[0], command->u.word) < 0)
error(1,0,"Command execution failed");
}
else waitpid(pid,&command->status,0);
if(command->status == -1)
command->status = 1;
else command->status = 0;
}
void execute_and_or_command(command_t command)
{
if(command->type == AND_COMMAND){
execute_wrapper(command->u.command[0]);
command->status = command->u.command[0]->status;
if(command_status(command->u.command[0]) == 0){
execute_wrapper(command->u.command[1]);
command->status = command->u.command[1]->status;
}
else
command->status = 1;
}
if(command->type == OR_COMMAND){
execute_wrapper(command->u.command[0]);
command->status = command->u.command[0]->status;
//printf("%d\n",command->status);
if(command_status(command->u.command[0]) > 0){
execute_wrapper(command->u.command[1]);
command->status = command->u.command[1]->status;
}
else
command->status = 0;
}
}
void execute_pipe_command(command_t command)
{
int pipe_array[2];
pid_t pid1 , pid2;
if(pipe(pipe_array) == -1) error(1, 0, "pipe creation failed");
pid1 = fork();
if(pid1 > 0)
{
pid2 = fork();
if(pid2 >0 )
{
close(pipe_array[0]);
close(pipe_array[1]);
pid_t temp = waitpid(-1,&command->status,0);
if(temp == pid1)
{
waitpid(pid2,&command->status,0);
return;
}
if(temp == pid2)
{
waitpid(pid1,&command->status,0);
return;
}
}
else if(pid2 == 0)
{
close(pipe_array[0]);
dup2(pipe_array[1],1);
execute_wrapper(command->u.command[0]);
exit(command->u.command[0]->status);
}
}
else if (pid1 == 0)
{
close(pipe_array[1]);
dup2(pipe_array[0],0);
execute_wrapper(command->u.command[1]);
exit(command->u.command[1]->status);
}
else error(1,0,"Fork failure");
}
void execute_subshell_command(command_t command)
{
pid_t pid;
//if((pid = fork()) < 0)
// error(1,0,"Could not create new process");
//if (pid == 0) {
if(command->input != NULL){
int fd0 = open(command->input,O_RDONLY,0666); //Open Input File
if(fd0 < 0)
error(1,0,"Input file does not exist");
dup2(fd0,0); //Copy File descriptor to STDIN
close(fd0);
}
if(command->output != NULL){
int fd1 = open(command->output,O_WRONLY | O_CREAT | O_TRUNC,0666); //Open Output File
if(fd1 < 0)
error(1,0,"Could not write to output file");
dup2(fd1,1); //Copy File descriptor to STDOUT
close(fd1);
}
execute_wrapper(command->u.subshell_command);
command->status = command->u.subshell_command->status;
//error(1,0,"Command execution failed");
//}
//else waitpid(pid,&command->status,0);
}
void execute_wrapper(command_t command);
void execute_sequence_command(command_t command)
{
execute_wrapper(command->u.command[0]);
execute_wrapper(command->u.command[1]);
command->status = command->u.command[1]->status;
}
void execute_wrapper(command_t c)
{
switch(c->type){
case (SIMPLE_COMMAND):
execute_simple_command(c);
break;
case (AND_COMMAND):
case (OR_COMMAND):
execute_and_or_command(c);
break;
case (SEQUENCE_COMMAND):
execute_sequence_command(c);
break;
case (PIPE_COMMAND):
execute_pipe_command(c);
break;
case (SUBSHELL_COMMAND):
execute_subshell_command(c);
break;
}
}
//All the dependencies are added to the node and all the dependencies of the subcommand are added to the node.
void add_dependencies(command_node_t node, command_t command)
{
//We get the command node and the command here.
//First lets check if the input is not = 0, if not that means it is a command with an input and we need to take care of it.
if(command->input != 0)
{
if(node->inputs == NULL)
{
int size = sizeof(node_t);
node_t head = checked_malloc(size);
//We assign the head of our node_t struct to the input
head->word = command->input;
//Next is set to null
head->next = NULL;
node->inputs = head;
}
else // This means that input is not null
{
//We need to add the word to the list. We iterate through the noed struct till we reach the
//same input as the command input
while(strcmp(node->inputs->word, command->input) == 0)
{
if(node->inputs->next == NULL)
{
node->inputs->next = checked_malloc(sizeof(node_t));
node->inputs->next->word = command->input; // Word getting added
node->inputs->next->next = NULL;
}
else //continue to loop with the next word in the node
node->inputs=node->inputs->next;
}
}
}
//Time for output. check if the command has an output
if(command->output != 0)
{
if(node->outputs == NULL)
{
int size = sizeof(node_t);
node_t head = checked_malloc(size);
head->word = command->output;
head->next = NULL;
node->outputs = head;
}
else // This means that input is not null
{
//We need to add the word to the list. We iterate through the noed struct till we reach the
//same input as the command input
while(strcmp(node->outputs->word, command->output) == 0)
{
if(node->outputs->next == NULL)
{
node->outputs->next = checked_malloc(sizeof(node_t));
node->outputs->next->word = command->output; // Word getting added
node->outputs->next->next = NULL;
}
else //continue to loop with the next word in the node
node->outputs=node->outputs->next;
}
}
}
int temp = 0;
//At this point we are done adding the input and output of the command to the structs. Let's see what kind of command it is
//and do things accordingly
if(command->type == AND_COMMAND || command->type == OR_COMMAND || command->type == SEQUENCE_COMMAND || command->type == PIPE_COMMAND)
{
add_dependencies(node, command->u.command[0]); //Above mentioned commands could have input and out, so have to
//to add dependencies both for input and output. That is the left side of a pipe could itself be command.
//So we need to take care of those dependecies as well.
add_dependencies(node, command->u.command[1]);
}
//If the command is a simple command then it is a different case. we do not need to call the add dependencies function again
//Because simple command will not have a subcommand
if(command->type == SIMPLE_COMMAND )
{
temp = 1;
while(command->u.word[temp] != NULL)
{
if(node->inputs == NULL)
{
//Making the input of the node point to the head of node_t
int size = sizeof(node_t);
node_t head = checked_malloc(size);
head->word = command->u.word[temp];
head->next = NULL;
node->inputs = head;
}
else
{
//We need to add the word to the list. We iterate through the noed struct till we reach the
//same input as the command input
while(strcmp(node->inputs->word, command->u.word[temp]) == 0)
{
if(node->inputs->next == NULL)
{
node->inputs->next = checked_malloc(sizeof(node_t));
node->inputs->next->word = command->u.word[temp]; // Word getting added
node->inputs->next->next = NULL;
}
else
//traverse the list( node struct)
node->inputs = node->inputs->next;
}
if(node->outputs == NULL)
{
//Making the input of the node point to the head of node_t
int size = sizeof(node_t);
node_t head = checked_malloc(size);
head->word = command->u.word[temp];
head->next = NULL;
node->outputs = head;
}
else
{
//We need to add the word to the list. We iterate through the noed struct till we reach the
//same input as the command input
while(strcmp(node->outputs->word, command->u.word[temp]) == 0)
{
if(node->outputs->next == NULL)
{
node->outputs->next = checked_malloc(sizeof(node_t));
node->outputs->next->word = command->u.word[temp]; // Word getting added
node->outputs->next->next = NULL;
}
else
//traverse the list( node struct)
node->outputs = node->outputs->next;
}
}
}
temp++;
}
}
}
int helper(command_node_t previous, node_t outputs, node_t inputs, command_node_t next_dependent )
{
node_t current_output = outputs;
while(current_output != NULL)
{
node_t current_input = inputs;
for(;;)
{
if(current_input == NULL)
break;
if(strcmp(current_input->word, current_output->word) == 0)
{
//increamenting dependencies and making the waiting list know
next_dependent->dependencies += 1;
child_node_t dependecylist = previous->dependents;
child_node_t curr_node = dependecylist;
while(dependecylist != NULL)
{
curr_node = dependecylist;
dependecylist = dependecylist->next;
}
child_node_t new_node = checked_malloc(sizeof(struct child_node));
new_node->dependent = next_dependent;
new_node->next = NULL;
if(curr_node == NULL)
previous->dependents = new_node;
else
curr_node->next = new_node;
return -1;
}
current_input = current_input->next;
}
current_output = current_output->next;
}
return 1;
}
void initialize(command_node_t new_node,command_t command)
{
new_node->c = command;
new_node->inputs = NULL;
new_node->outputs = NULL;
new_node->dependencies = 0;
new_node->dependents = NULL;
new_node->pid = -1;
new_node->word = NULL;
}
command_t
forkingandwaiting(command_node_t dep_head, command_t final_command)
{
for(;;)
{
//Stop condition, we have executed everything
if(dep_head == NULL)
break;
command_node_t current_node = dep_head;
// For everyone on the list
for(;;)
{
if(current_node==NULL)
break;
//This means there are no dependencies
if(current_node->dependencies == 0 && current_node->pid < 1)
{
//We can fork now
int pid = fork();
if(pid == -1)
error(1,0,"Could not create new process");
else if( pid == 0)
{
execute_command(current_node->c,0); //Get the command and do the same thing we did for part b
exit(1); // If i do not exit here it goes into infinite loop
}
else if( pid > 0)
{
current_node->pid = pid;
}
else{int temp=0;} // Place holder
}
//Go to the next one,
current_node = current_node->next;
}
int status;
// Wait for someone to finish
pid_t pid1 = waitpid(-1, &status, 0);
// Use pid to determine who finished and remove them
command_node_t previous_node = NULL;
command_node_t curr_node = dep_head;
for(;;)
{
if(curr_node == NULL)
break;
// If they're not waiting on anyone
if(curr_node->pid == pid1)
{
child_node_t current_dependency = curr_node->dependents;
// for all on the list of dependents
while(current_dependency != NULL)
{
command_node_t temp_node = current_dependency->dependent;
// free that dependent (reduce dependencies)
temp_node->dependencies -= 1;
current_dependency = current_dependency->next;
}
// remove from the list
if(previous_node == NULL)
dep_head = curr_node->next;
else
previous_node->next = curr_node->next;
break;
}
previous_node = curr_node;
curr_node = curr_node->next;
}
}
return final_command;
}
command_t
execute_parallel_stream (command_stream_t com)
{
command_node_t dep_head = NULL;
command_t final_command = NULL;
command_t command;
//This function gets the command stream, we are reading the commands, one after the other.
for(;;)
{
command = read_command_stream (com);
//This means we finished reading the command
if(!command)
break;
command_node_t curr_node = NULL;
//We got a new command, so create a new command, initialize everything to null and new_node->c to the command read
command_node_t new_node = checked_malloc(sizeof(struct command_node));
//Initializae function will initialize everything!
initialize(new_node,command);
//Then we simply need to add the dependencies to our list
add_dependencies(new_node, command);
final_command = command;
//At this point the list has been populated and the dependencies have been added
command_node_t current_node = dep_head;
for(;;)
{
//This means there are no dependencie
if(current_node == NULL)
break;
//If ther are dependencies found, we do comparisons and assign the current node accordingly
helper(current_node, new_node->outputs, current_node->inputs, new_node);
helper(current_node, current_node->outputs, new_node->inputs, new_node);
curr_node = current_node;
//Traversing the list
current_node = current_node->next;
}
//This means means we can add to the waiting list
if( curr_node == NULL)
dep_head = new_node;
else
curr_node->next = new_node;
}
//dep head
// While there's someone on the waiting list
command_t retcommand = forkingandwaiting(dep_head,final_command);
return retcommand;
}
void
execute_command (command_t c, int time_travel)
{
/* FIXME: Replace this with your implementation. You may need to
add auxiliary functions and otherwise modify the source code.
You can also use external functions defined in the GNU C Library. */
//error (1, 0, "command execution not yet implemented");
execute_wrapper(c);
}