#ifndef SUBPROCESS_H #define SUBPROCESS_H #include #include #include #include #include #include pid_t pid = NULL; int subprocess(char *command) { int sp[2] = {0}; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) == -1) { perror("socketpair"); exit(EXIT_FAILURE); } pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (!pid) { close(sp[0]); dup2(sp[1], STDIN_FILENO); dup2(sp[1], STDOUT_FILENO); dup2(sp[1], STDERR_FILENO); close(sp[1]); execlp(command, command, NULL); perror("execlp"); exit(EXIT_FAILURE); } else { close(sp[1]); return sp[0]; } } #endif