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