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