]> arthur.barton.de Git - netdata.git/blob - src/popen.c
all required system headers in common.h; some progress on health variables
[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(SIGUSR1, &sa, NULL) == -1)
123                         error("pre-execution of command '%s' on pid %d: failed to set default signal handler for SIGUSR1.", command, getpid());
124
125                 if(sigaction(SIGPIPE, &sa, NULL) == -1)
126                         error("pre-execution of command '%s' on pid %d: failed to set default signal handler for SIGPIPE.", command, getpid());
127         }
128
129
130         info("executing command: '%s' on pid %d.", command, getpid());
131         execl("/bin/sh", "sh", "-c", command, NULL);
132         exit(1);
133 }
134
135 int mypclose(FILE *fp, pid_t pid) {
136         debug(D_EXIT, "Request to mypclose() on pid %d", pid);
137
138         /*mypopen_del(fp);*/
139         fclose(fp);
140
141         siginfo_t info;
142         if(waitid(P_PID, (id_t) pid, &info, WEXITED) != -1) {
143                 switch(info.si_code) {
144                         case CLD_EXITED:
145                                 if(info.si_status)
146                                         error("child pid %d exited with code %d.", info.si_pid, info.si_status);
147                                 return(info.si_status);
148                                 break;
149
150                         case CLD_KILLED:
151                                 error("child pid %d killed by signal %d.", info.si_pid, info.si_status);
152                                 return(-1);
153                                 break;
154
155                         case CLD_DUMPED:
156                                 error("child pid %d core dumped by signal %d.", info.si_pid, info.si_status);
157                                 return(-2);
158                                 break;
159
160                         case CLD_STOPPED:
161                                 error("child pid %d stopped by signal %d.", info.si_pid, info.si_status);
162                                 return(0);
163                                 break;
164
165                         case CLD_TRAPPED:
166                                 error("child pid %d trapped by signal %d.", info.si_pid, info.si_status);
167                                 return(-4);
168                                 break;
169
170                         case CLD_CONTINUED:
171                                 error("child pid %d continued by signal %d.", info.si_pid, info.si_status);
172                                 return(0);
173                                 break;
174
175                         default:
176                                 error("child pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
177                                 return(-5);
178                                 break;
179                 }
180         }
181         else
182                 error("Cannot waitid() for pid %d", pid);
183         
184         return 0;
185 }