-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexec_test.c
49 lines (41 loc) · 1.23 KB
/
exec_test.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
#include "apue.h"
#include <sys/wait.h>
#include <dirent.h>
void print_closeOnExec(int fd);
char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL };
int main(void)
{
pid_t pid;
int fd, flag;
DIR *dirp;
if ((dirp = opendir("/root")) == NULL)
err_sys("opendir error");
fd = dirfd(dirp);
print_closeOnExec(fd);
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) { /* specify pathname, specify environment */
if (execle("/home/chenzheng/Documents/github-apue/echoall", "echoall", "myarg1", "MY ARG2", (char *)0, env_init) < 0)
err_sys("execle error");
}
if (waitpid(pid, NULL, 0) < 0)
err_sys("wait error");
print_closeOnExec(fd);
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) { /* specify filename, inherit environment */
if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0)
err_sys("execlp error");
}
exit(0);
}
void print_closeOnExec(int fd)
{
int flag;
if ((flag = fcntl(fd, F_GETFD)) == 1)
printf("close_on_exec flag is on\n");
else if (flag == 0)
printf("close_on_exec flag is down\n");
else
printf("close_on_exec flag unknown\n");
}