11 static struct mypopen *mypopen_root = NULL;
13 static void mypopen_add(FILE *fp, pid_t *pid) {
14 struct mypopen *mp = malloc(sizeof(struct mypopen));
16 fatal("Cannot allocate %zu bytes", sizeof(struct mypopen))
22 mp->next = popen_root;
24 if(mypopen_root) mypopen_root->prev = mp;
28 static void mypopen_del(FILE *fp) {
31 for(mp = mypopen_root; mp; mp = mp->next)
32 if(mp->fd == fp) break;
34 if(!mp) error("Cannot find mypopen() file pointer in open childs.");
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;
46 FILE *mypopen(const char *command, pid_t *pidptr)
50 if(pipe(pipefd) == -1) return NULL;
54 close(pipefd[PIPE_READ]);
55 close(pipefd[PIPE_WRITE]);
61 close(pipefd[PIPE_WRITE]);
62 FILE *fp = fdopen(pipefd[PIPE_READ], "r");
63 /*mypopen_add(fp, pid);*/
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);
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]);
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.
83 // fork again to become session leader
86 error("pre-execution of command '%s' on pid %d: Cannot fork 2nd time.", command, getpid());
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());
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));
101 error("pre-execution of command '%s' on pid %d: Cannot set session id.", command, getpid());
103 fprintf(stdout, "MYPID %d\n", getpid());
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());
115 // We only need to reset ignored signals.
116 // Signals with signal handlers are reset by default.
118 sigemptyset(&sa.sa_mask);
119 sa.sa_handler = SIG_DFL;
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());
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());
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());
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());
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());
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());
141 debug(D_CHILDS, "executing command: '%s' on pid %d.", command, getpid());
142 execl("/bin/sh", "sh", "-c", command, NULL);
146 int mypclose(FILE *fp, pid_t pid) {
147 debug(D_EXIT, "Request to mypclose() on pid %d", pid);
152 // this is required in musl
153 // without it the childs do not exit
156 // close the pipe file pointer
160 if(waitid(P_PID, (id_t) pid, &info, WEXITED) != -1) {
161 switch(info.si_code) {
164 error("child pid %d exited with code %d.", info.si_pid, info.si_status);
165 return(info.si_status);
169 error("child pid %d killed by signal %d.", info.si_pid, info.si_status);
174 error("child pid %d core dumped by signal %d.", info.si_pid, info.si_status);
179 error("child pid %d stopped by signal %d.", info.si_pid, info.si_status);
184 error("child pid %d trapped by signal %d.", info.si_pid, info.si_status);
189 error("child pid %d continued by signal %d.", info.si_pid, info.si_status);
194 error("child pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
200 error("Cannot waitid() for pid %d", pid);