]> arthur.barton.de Git - netdata.git/blob - src/popen.c
fix for defunct childs
[netdata.git] / src / popen.c
1 #include <signal.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6
7 #include "log.h"
8 #include "popen.h"
9 /*
10 struct mypopen {
11         pid_t pid;
12         FILE *fp;
13         struct mypopen *next;
14         struct mypopen *prev;
15 };
16
17 static struct mypopen *mypopen_root = NULL;
18
19 static void mypopen_add(FILE *fp, pid_t *pid) {
20         struct mypopen *mp = malloc(sizeof(struct mypopen));
21         if(!mp) {
22                 fatal("Cannot allocate %zu bytes", sizeof(struct mypopen))
23                 return;
24         }
25
26         mp->fp = fp;
27         mp->pid = pid;
28         mp->next = popen_root;
29         mp->prev = NULL;
30         if(mypopen_root) mypopen_root->prev = mp;
31         mypopen_root = mp;
32 }
33
34 static void mypopen_del(FILE *fp) {
35         struct mypopen *mp;
36
37         for(mp = mypopen_root; mp; mp = mp->next)
38                 if(mp->fd == fp) break;
39
40         if(!mp) error("Cannot find mypopen() file pointer in open childs.");
41         else {
42                 if(mp->next) mp->next->prev = mp->prev;
43                 if(mp->prev) mp->prev->next = mp->next;
44                 if(mypopen_root == mp) mypopen_root = mp->next;
45                 free(mp);
46         }
47 }
48 */
49 #define PIPE_READ 0
50 #define PIPE_WRITE 1
51
52 FILE *mypopen(const char *command, pid_t *pidptr)
53 {
54         int pipefd[2];
55
56         if(pipe(pipefd) == -1) return NULL;
57
58         int pid = fork();
59         if(pid == -1) {
60                 close(pipefd[PIPE_READ]);
61                 close(pipefd[PIPE_WRITE]);
62                 return NULL;
63         }
64         if(pid != 0) {
65                 // the parent
66                 *pidptr = pid;
67                 close(pipefd[PIPE_WRITE]);
68                 FILE *fp = fdopen(pipefd[PIPE_READ], "r");
69                 /*mypopen_add(fp, pid);*/
70                 return(fp);
71         }
72         // the child
73
74         // close all files
75         int i;
76         for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
77                 if(i != STDIN_FILENO && i != STDERR_FILENO && i != pipefd[PIPE_WRITE]) close(i);
78
79         // move the pipe to stdout
80         if(pipefd[PIPE_WRITE] != STDOUT_FILENO) {
81                 dup2(pipefd[PIPE_WRITE], STDOUT_FILENO);
82                 close(pipefd[PIPE_WRITE]);
83         }
84
85 #ifdef DETACH_PLUGINS_FROM_NETDATA
86         // this was an attempt to detach the child and use the suspend mode charts.d
87         // unfortunatelly it does not work as expected.
88
89         // fork again to become session leader
90         pid = fork();
91         if(pid == -1) fprintf(stderr, "Cannot fork again on pid %d\n", getpid());
92         if(pid != 0) {
93                 // the parent
94                 exit(0);
95         }
96
97         // set a new process group id for just this child
98         if( setpgid(0, 0) != 0 )
99                 fprintf(stderr, "Cannot set a new process group for pid %d (%s)\n", getpid(), strerror(errno));
100
101         if( getpgid(0) != getpid() )
102                 fprintf(stderr, "Process group set is incorrect. Expected %d, found %d\n", getpid(), getpgid(0));
103
104         if( setsid() != 0 )
105                 fprintf(stderr, "Cannot set session id for pid %d (%s)\n", getpid(), strerror(errno));
106
107         fprintf(stdout, "MYPID %d\n", getpid());
108         fflush(NULL);
109 #endif
110         
111         // reset all signals
112         for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV) signal(i, SIG_DFL);
113
114         fprintf(stderr, "executing command: '%s' on pid %d.\n", command, getpid());
115         execl("/bin/sh", "sh", "-c", command, NULL);
116         exit(1);
117 }
118
119 void mypclose(FILE *fp, pid_t pid) {
120         /*mypopen_del(fp);*/
121         fclose(fp);
122
123         siginfo_t info;
124         if(waitid(P_PID, pid, &info, WEXITED) != -1) {
125                 switch(info.si_code) {
126                         case CLD_EXITED:
127                                 error("pid %d exited with code %d.", info.si_pid, info.si_status);
128                                 break;
129
130                         case CLD_KILLED:
131                                 error("pid %d killed by signal %d.", info.si_pid, info.si_status);
132                                 break;
133
134                         case CLD_DUMPED: 
135                                 error("pid %d core dumped by signal %d.", info.si_pid, info.si_status);
136                                 break;
137
138                         case CLD_STOPPED:
139                                 error("pid %d stopped by signal %d.", info.si_pid, info.si_status);
140                                 break;
141
142                         case CLD_TRAPPED:
143                                 error("pid %d trapped by signal %d.", info.si_pid, info.si_status);
144                                 break;
145
146                         case CLD_CONTINUED:
147                                 error("pid %d continued by signal %d.", info.si_pid, info.si_status);
148                                 break;
149
150                         default:
151                                 error("pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
152                                 break;
153                 }
154         }
155         else error("Cannot waitid() for pid %d", pid);
156 }