12 pr_err("fork() failed: %s\n", strerror(error));
17 pr_info("Forked child %d\n", child);
22 /* reset child's child count */
27 int run(const char *cmd, char *const argv[])
30 if ((child = do_fork()))
35 pr_err("Failed to execv command %s: %s\n", cmd, strerror(error));
40 int harvest_zombies(int pid)
47 pid = waitpid(pid, &status, 0);
50 pr_err("Error on wait(): %s\n", strerror(error));
53 pr_info("pid %d: exit code %d. Children left: %d\n", pid,
54 status, --child_count);
60 * Runs a command cmd with params argv, connects stdin and stdout to
63 * Returns the pid of the executed process
65 int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
67 int rfd[2], wfd[2], error, pid;
71 pr_err("pipe() failed: %s\n", strerror(error));
77 pr_err("pipe() failed: %s\n", strerror(error));
93 dup2(wfd[0], STDIN_FILENO);
94 dup2(rfd[1], STDOUT_FILENO);
96 /* Now we have redirected both stdin and stdout to parent process */
99 pr_err("Failed to execv command %s: %s\n", cmd, strerror(error));
105 * Runs a command cmd with params argv, connects stdin and stdout to
108 * Returns the pid of the executed process
110 int run_piped_stream(const char *cmd, char *const argv[],
111 FILE **readf, FILE **writef)
113 int rfd, wfd, pid, error;
115 pid = run_piped(cmd, argv, &rfd, &wfd);
118 *readf = fdopen(rfd, "r");
119 if (*readf == NULL) {
121 pr_err("Error opening file stream for fd %d: %s\n",
122 rfd, strerror(error));
128 *writef = fdopen(wfd, "w");
129 if (*writef == NULL) {
131 pr_err("Error opening file stream for fd %d: %s\n",
132 wfd, strerror(error));