17 static struct mypopen *mypopen_root = NULL;
19 static void mypopen_add(FILE *fp, pid_t *pid) {
20 struct mypopen *mp = malloc(sizeof(struct mypopen));
22 fatal("Cannot allocate %zu bytes", sizeof(struct mypopen))
28 mp->next = popen_root;
30 if(mypopen_root) mypopen_root->prev = mp;
34 static void mypopen_del(FILE *fp) {
37 for(mp = mypopen_root; mp; mp = mp->next)
38 if(mp->fd == fp) break;
40 if(!mp) error("Cannot find mypopen() file pointer in open childs.");
42 if(mp->next) mp->next->prev = mp->prev;
43 if(mp->prev) mp->prev->next = mp->next;
44 if(mypopen_root == mp) mypopen_root = mp->next;
52 FILE *mypopen(const char *command, pid_t *pidptr)
56 if(pipe(pipefd) == -1) return NULL;
60 close(pipefd[PIPE_READ]);
61 close(pipefd[PIPE_WRITE]);
67 close(pipefd[PIPE_WRITE]);
68 FILE *fp = fdopen(pipefd[PIPE_READ], "r");
69 /*mypopen_add(fp, pid);*/
76 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
77 if(i != STDIN_FILENO && i != STDERR_FILENO && i != pipefd[PIPE_WRITE]) close(i);
79 // move the pipe to stdout
80 if(pipefd[PIPE_WRITE] != STDOUT_FILENO) {
81 dup2(pipefd[PIPE_WRITE], STDOUT_FILENO);
82 close(pipefd[PIPE_WRITE]);
85 #ifdef DETACH_PLUGINS_FROM_NETDATA
86 // this was an attempt to detach the child and use the suspend mode charts.d
87 // unfortunatelly it does not work as expected.
89 // fork again to become session leader
91 if(pid == -1) fprintf(stderr, "Cannot fork again on pid %d\n", getpid());
97 // set a new process group id for just this child
98 if( setpgid(0, 0) != 0 )
99 fprintf(stderr, "Cannot set a new process group for pid %d (%s)\n", getpid(), strerror(errno));
101 if( getpgid(0) != getpid() )
102 fprintf(stderr, "Process group set is incorrect. Expected %d, found %d\n", getpid(), getpgid(0));
105 fprintf(stderr, "Cannot set session id for pid %d (%s)\n", getpid(), strerror(errno));
107 fprintf(stdout, "MYPID %d\n", getpid());
112 for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV) signal(i, SIG_DFL);
114 fprintf(stderr, "executing command: '%s' on pid %d.\n", command, getpid());
115 execl("/bin/sh", "sh", "-c", command, NULL);
119 void mypclose(FILE *fp, pid_t pid) {
124 if(waitid(P_PID, pid, &info, WEXITED) != -1) {
125 switch(info.si_code) {
127 error("pid %d exited with code %d.", info.si_pid, info.si_status);
131 error("pid %d killed by signal %d.", info.si_pid, info.si_status);
135 error("pid %d core dumped by signal %d.", info.si_pid, info.si_status);
139 error("pid %d stopped by signal %d.", info.si_pid, info.si_status);
143 error("pid %d trapped by signal %d.", info.si_pid, info.si_status);
147 error("pid %d continued by signal %d.", info.si_pid, info.si_status);
151 error("pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
155 else error("Cannot waitid() for pid %d", pid);