/
exec.c
145 lines (124 loc) · 2.78 KB
/
exec.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
/* CMPSC 311, Spring 2013, Project 7
*
* Author: Jacob Jones
* Email: jaj5333@psu.edu
*
* Author: Scott Cheloha
* Email: ssc5145@psu.edu
*/
/* Here, we implement:
* new_child -- forks a new child and puts it in fg or bg pgid
* exec -- needs to be completed with flags, should check which exec to use (ve,vp,lp)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "exec.h"
#include "pr7.h"
#include "wrapper.h"
#include "linked.h"
//------------------------------------------------------------------------------
int new_child(char *Argv[], int background, int status)
{
pid_t pid;
if ((pid = Fork(__func__, __LINE__)) == (pid_t) -1)
{
return 1; // fork() failed, return to prompt
}
/* If we're in the child process,
* install default signal handlers, then
* call the exec() family and replace the child
*/
if (pid == 0)
{
if (!background)
{
Signal(SIGINT, SIG_DFL, __func__, __LINE__);
}
else
{
Signal(SIGINT, SIG_IGN, __func__, __LINE__);
}
Signal(SIGCHLD, SIG_DFL, __func__, __LINE__);
exec(Argv, status); // should never return to this point
}
/* If we're in the parent process, we either:
* wait (foreground process)
* return to shell (background process)
*/
/* Parent forked a foreground process */
if (!background)
{
// set up global variables for foreground process
fg_pid = pid;
setpgid(pid, pid);
fg_pgid = getpgid(pid);
waitpid(-1, &status, 0);
}
/* Parent forked a background process */
else
{
// if bg_pgid has not been set, set it to the child's pid
if (bg_pgid == 0)
{
setpgid(pid, pid);
bg_pgid = getpgid(pid);
}
else
{
setpgid(pid, bg_pgid); // add the child to the background PID group
}
// modify background process list
process_list_append(bg_processes, Argv[0], pid, getpgid(pid));
if (v_flag)
{
fprintf(stderr, "-%s: %s added to background process list.\n",
prog, Argv[0]);
}
}
return 0;
}
void exec(char *Argv[], int status)
{
// We use execvp()
if (exec_flag == 0)
{
if ((status = execvp(Argv[0], Argv)) == -1)
{
fprintf(stderr, "-%s: ", Argv[0]);
if (d_flag)
{
fprintf(stderr, "execvp() ");
}
fprintf(stderr, "failed: %s\n", strerror(errno));
_exit(EXIT_FAILURE);
}
}
// We use execve()
if (exec_flag == 1)
{
if ((status = execve(Argv[0], Argv, environ)) == -1)
{
fprintf(stderr, "-%s: ", Argv[0]);
if (d_flag)
{
fprintf(stderr, "execve() ");
}
fprintf(stderr, "failed: %s\n", strerror(errno));
_exit(EXIT_FAILURE);
}
}
// use execlp()
if (exec_flag == 2)
{
if ((status = execlp(Argv[0], Argv[0])) == -1)
{
fprintf(stderr, "-%s: execlp() failed: %s\n", prog, strerror(errno));
_exit(EXIT_FAILURE);
}
}
}