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