22 static struct mypopen *mypopen_root = NULL;
24 static void mypopen_add(FILE *fp, pid_t *pid) {
25 struct mypopen *mp = malloc(sizeof(struct mypopen));
27 fatal("Cannot allocate %zu bytes", sizeof(struct mypopen))
33 mp->next = popen_root;
35 if(mypopen_root) mypopen_root->prev = mp;
39 static void mypopen_del(FILE *fp) {
42 for(mp = mypopen_root; mp; mp = mp->next)
43 if(mp->fd == fp) break;
45 if(!mp) error("Cannot find mypopen() file pointer in open childs.");
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;
57 FILE *mypopen(const char *command, pid_t *pidptr)
61 if(pipe(pipefd) == -1) return NULL;
65 close(pipefd[PIPE_READ]);
66 close(pipefd[PIPE_WRITE]);
72 close(pipefd[PIPE_WRITE]);
73 FILE *fp = fdopen(pipefd[PIPE_READ], "r");
74 /*mypopen_add(fp, pid);*/
81 for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i > 0; i--)
82 if(i != STDIN_FILENO && i != STDERR_FILENO && i != pipefd[PIPE_WRITE]) close(i);
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]);
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.
94 // fork again to become session leader
96 if(pid == -1) fprintf(stderr, "Cannot fork again on pid %d\n", getpid());
102 // set a new process group id for just this child
103 if( setpgid(0, 0) != 0 )
104 error("Cannot set a new process group for pid %d (%s)", getpid(), strerror(errno));
106 if( getpgid(0) != getpid() )
107 error("Process group set is incorrect. Expected %d, found %d", getpid(), getpgid(0));
110 error("Cannot set session id for pid %d (%s)", getpid(), strerror(errno));
112 fprintf(stdout, "MYPID %d\n", getpid());
121 if(pthread_sigmask(SIG_UNBLOCK, &sigset, NULL) == -1) {
122 error("Could not block signals for threads");
124 // We only need to reset ignored signals.
125 // Signals with signal handlers are reset by default.
127 sigemptyset(&sa.sa_mask);
128 sa.sa_handler = SIG_DFL;
129 if(sigaction(SIGPIPE, &sa, NULL) == -1) {
130 error("Failed to change signal handler for SIGTERM");
135 info("executing command: '%s' on pid %d.", command, getpid());
136 execl("/bin/sh", "sh", "-c", command, NULL);
140 int mypclose(FILE *fp, pid_t pid) {
141 debug(D_EXIT, "Request to mypclose() on pid %d", pid);
147 if(waitid(P_PID, (id_t) pid, &info, WEXITED) != -1) {
148 switch(info.si_code) {
150 error("pid %d exited with code %d.", info.si_pid, info.si_status);
151 return(info.si_status);
155 error("pid %d killed by signal %d.", info.si_pid, info.si_status);
160 error("pid %d core dumped by signal %d.", info.si_pid, info.si_status);
165 error("pid %d stopped by signal %d.", info.si_pid, info.si_status);
170 error("pid %d trapped by signal %d.", info.si_pid, info.si_status);
175 error("pid %d continued by signal %d.", info.si_pid, info.si_status);
180 error("pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
185 else error("Cannot waitid() for pid %d", pid);