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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int
main (int argc, char **argv)
{
int fd, pid, n;
int fds[2];
static char buf[4096];
close (1);
if ((fd = open ("/dev/null", O_WRONLY)) != 1)
{
fprintf (stderr, "couldn't redirect stdout to /dev/null, fd %d - %s\n", fd, strerror ());
exit (1);
}
if (pipe (fds))
{
fprintf (stderr, "pipe call failed - %s\n", strerror ());
exit (1);
}
if ((pid = fork ()) == 0)
{
close (fds[0]);
if (dup2 (fds[1], 2) != 2)
{
fprintf (stderr, "couldn't redirect stderr to pipe - %s\n", strerror ());
exit (1);
}
exit (system ("ls"));
}
else if (pid < 0)
{
perror ("couldn't fork");
exit (1);
}
close (fds[1]);
if (read (fds[0], buf, 4096) != 0)
{
fprintf (stderr, "system call failed?\n%s\n", buf);
exit (1);
}
if (waitpid (pid, &n, 0) < 0)
{
perror ("waitpid failed");
exit (1);
}
if (n != 0)
{
fprintf (stderr, "system() call returend %p\n", n);
exit (1);
}
exit (0);
}
|