]> git.itanic.dy.fi Git - rrdd/blob - process.c
Remove debug output
[rrdd] / process.c
1 #include "process.h"
2
3 int child_count = 0;
4
5 int run(const char *cmd, char *const argv[])
6 {
7         int child, error;
8         child = fork();
9         if (child < 0) {
10                 error = errno;
11                 fprintf(stderr, "fork() failed: %s\n", strerror(error));
12                 return -1;
13         }
14
15         if (child) {
16                 printf("Forked child %d\n", child);
17                 child_count++;
18                 return child;
19         }
20
21         execvp(cmd, argv);
22         error = errno;
23         printf("Failed to execv command %s: %s\n", cmd, strerror(error));
24         exit(1);
25         return 0;
26 }
27
28 int harvest_zombies(int pid)
29 {
30         int status;
31
32         if (child_count == 0)
33                 return 0;
34
35         pid = waitpid(pid, &status, 0);
36         if (status == 0)
37                 printf("pid %d: terminated with exit code %d\n", pid, status);
38
39         child_count--;
40         return 1;
41 }
42
43 /*
44  * Runs a command cmd with params argv, connects stdin and stdout to
45  * readfd and writefd
46  *
47  * Returns the pid of the executed process
48  */
49 int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
50 {
51         int rfd[2], wfd[2], error, pid;
52
53         if (pipe(rfd)) {
54                 error = errno;
55                 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
56                 return -1;
57         }
58
59         if (pipe(wfd)) {
60                 error = errno;
61                 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
62                 return -1;
63         }
64
65         pid = fork();
66         if (pid < 0) {
67                 error = errno;
68                 fprintf(stderr, "fork() failed: %s\n", strerror(error));
69                 return -1;
70         }
71
72         if (pid) {
73                 close(rfd[1]);
74                 close(wfd[0]);
75                 *readfd = rfd[0];
76                 *writefd = wfd[1];
77                 return pid;
78         }
79
80         close(rfd[0]);
81         close(wfd[1]);
82         dup2(wfd[0], STDIN_FILENO);
83         dup2(rfd[1], STDOUT_FILENO);
84
85         /* Now we have redirected both stdin and stdout to parent process */
86         execvp(cmd, argv);
87         error = errno;
88         printf("Failed to execv command %s: %s\n", cmd, strerror(error));
89         exit(1);
90         return 0;
91 }
92
93 /*
94  * Runs a command cmd with params argv, connects stdin and stdout to
95  * readfd and writefd
96  *
97  * Returns the pid of the executed process
98  */
99 int run_piped_stream(const char *cmd, char *const argv[],
100                      FILE **readf, FILE **writef)
101 {
102         int rfd, wfd, pid, error;
103
104         pid = run_piped(cmd, argv, &rfd, &wfd);
105
106         if (readf) {
107                 *readf = fdopen(rfd, "r");
108                 if (*readf == NULL) {
109                         error = errno;
110                         fprintf(stderr,
111                                 "Error opening file stream for fd %d: %s\n",
112                                 rfd, strerror(error));
113                         return -1;
114                 }
115         }
116
117         if (writef) {
118                 *writef = fdopen(wfd, "w");
119                 if (*writef == NULL) {
120                         error = errno;
121                         fprintf(stderr,
122                                 "Error opening file stream for fd %d: %s\n",
123                                 wfd, strerror(error));
124                         return -1;
125                 }
126         }
127
128         return pid;
129 }