]> arthur.barton.de Git - netdata.git/blob - src/popen.c
build: migrate to autotools
[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 = sysconf(_SC_OPEN_MAX); 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) fprintf(stderr, "Cannot fork again on pid %d\n", getpid());
97         if(pid != 0) {
98                 // the parent
99                 exit(0);
100         }
101
102         // set a new process group id for just this child
103         if( setpgid(0, 0) != 0 )
104                 fprintf(stderr, "Cannot set a new process group for pid %d (%s)\n", getpid(), strerror(errno));
105
106         if( getpgid(0) != getpid() )
107                 fprintf(stderr, "Process group set is incorrect. Expected %d, found %d\n", getpid(), getpgid(0));
108
109         if( setsid() != 0 )
110                 fprintf(stderr, "Cannot set session id for pid %d (%s)\n", getpid(), strerror(errno));
111
112         fprintf(stdout, "MYPID %d\n", getpid());
113         fflush(NULL);
114 #endif
115         
116         // reset all signals
117         for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV) signal(i, SIG_DFL);
118
119         fprintf(stderr, "executing command: '%s' on pid %d.\n", command, getpid());
120         execl("/bin/sh", "sh", "-c", command, NULL);
121         exit(1);
122 }
123
124 void mypclose(FILE *fp, pid_t pid) {
125         debug(D_EXIT, "Request to mypclose() on pid %d", pid);
126         
127         /*mypopen_del(fp);*/
128         fclose(fp);
129
130         siginfo_t info;
131         if(waitid(P_PID, pid, &info, WEXITED) != -1) {
132                 switch(info.si_code) {
133                         case CLD_EXITED:
134                                 error("pid %d exited with code %d.", info.si_pid, info.si_status);
135                                 break;
136
137                         case CLD_KILLED:
138                                 error("pid %d killed by signal %d.", info.si_pid, info.si_status);
139                                 break;
140
141                         case CLD_DUMPED: 
142                                 error("pid %d core dumped by signal %d.", info.si_pid, info.si_status);
143                                 break;
144
145                         case CLD_STOPPED:
146                                 error("pid %d stopped by signal %d.", info.si_pid, info.si_status);
147                                 break;
148
149                         case CLD_TRAPPED:
150                                 error("pid %d trapped by signal %d.", info.si_pid, info.si_status);
151                                 break;
152
153                         case CLD_CONTINUED:
154                                 error("pid %d continued by signal %d.", info.si_pid, info.si_status);
155                                 break;
156
157                         default:
158                                 error("pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
159                                 break;
160                 }
161         }
162         else error("Cannot waitid() for pid %d", pid);
163 }