-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.c
139 lines (131 loc) · 3.15 KB
/
worker.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
#include "header.h"
/**
* worker_execute_core - this is the core execution function
* @arginv: arg inventory
*
* Return: 0
*/
pid_t worker_execute_core(arg_inventory_t *arginv)
{
unsigned int i;
/* int p[2]; Set of pipes' descriptors */
ptree_t *ptree;
/**
* int fd_input = 0; Input file descriptor; soon to be changed with
* something if there is < command somewhere
*/
for (i = 0; i < arginv->pipeline.processesN; i++)
{
ptree = arginv->pipeline.processes[i].ptree;
arginv->commands = ptree->strings;
ptree->stringsN += expand_alias(arginv);
ptree->strings = arginv->commands;
/* pipe(p); Create the two-way pipe */
/**
* arginv->pipein = fd_input;
* arginv->pipeout = (i + 1 < arginv->pipeline.processesN) ? p[1] : 0;
*
* if (arginv->pipeline.processes[i].io_redir)
* {
* arginv->filename = arginv->pipeline.processes[i].filename;
* arginv->io_redir = arginv->pipeline.processes[i].io_redir;
* }
* else
* {
* arginv->filename = NULL;
* arginv->io_redir = 0;
* }
*/
arginv->pipeline.processes[i].pid = execute(arginv);
/**
* close(p[1]); Close non-needed descriptor
* fd_input = p[0]; The input should be saved for the next comman
*/
}
return (arginv->pipeline.processes[arginv->pipeline.processesN - 1].pid);
}
/**
* worker_execute_tree - this executes ptree
* @arginv: arg inventory
* @ptree: the parsing tree
* @depth: the depth of parsing tree
*
* Return: the last pid
*/
pid_t worker_execute_tree(arg_inventory_t *arginv, ptree_t *ptree,
unsigned int depth)
{
int status = 0, id, execute = 1;
pid_t last_pid = -1;
if (!ptree)
return (last_pid);
id = ptree->token_id;
if (id == TOKEN_STRING || id == TOKEN_PIPE || is_redirection(id))
{ /* execute pipeline */
init_pipeline(&arginv->pipeline, ptree);
last_pid = worker_execute_core(arginv);
delete_pipeline(&arginv->pipeline);
return (last_pid);
} /* recursive call on each child */
if (ptree->left)
{
last_pid = worker_execute_tree(arginv, ptree->left, depth + 1);
if (id != TOKEN_BACKGROUND)
{ /* wait for the child */
waitpid(last_pid, &status, 0);
status = WEXITSTATUS(status);
}
else
arginv->n_bg_jobs++, arginv->last_bg_pid = last_pid;
switch (status)
{
case 1:
arginv->exit_status = 127;
break;
default:
arginv->exit_status = status;
break;
}
if ((id == TOKEN_AND && status) || (id == TOKEN_OR && !status))
execute = 0;
if (execute)
last_pid = worker_execute_tree(arginv, ptree->right, depth + 1);
}
return (last_pid);
}
/**
* worker_execute - executes background process
* @arginv: arginv
*
* Return: 0 or exit
*/
int worker_execute(arg_inventory_t *arginv)
{
pid_t last_pid;
int status = 0;
arginv->n_bg_jobs = 0;
last_pid = worker_execute_tree(arginv, arginv->parser.tree, 0);
if (last_pid != -1)
{
if (arginv->parser.tree->token_id != TOKEN_BACKGROUND)
{
waitpid(last_pid, &status, 0);
status = WEXITSTATUS(status);
}
else
{
arginv->n_bg_jobs++;
arginv->last_bg_pid = last_pid;
}
switch (status)
{
case 1:
arginv->exit_status = 127;
break;
default:
arginv->exit_status = status;
break;
}
}
return (0);
}