int ni_process_run_and_wait(ni_process_t *pi) { int rv; rv = __ni_process_run(pi, NULL); if (rv < 0) return rv; while (waitpid(pi->pid, &pi->status, 0) < 0) { if (errno == EINTR) continue; ni_error("%s: waitpid returns error (%m)", __func__); return -1; } pi->pid = 0; if (pi->notify_callback) pi->notify_callback(pi); if (!ni_process_exit_status_okay(pi)) { ni_error("subprocesses exited with error"); return -1; } return rv; }
/* * Run a subprocess. */ int ni_process_run(ni_process_t *pi) { int pfd[2], rv; /* Our code in socket.c is only able to deal with sockets for now; */ if (socketpair(AF_UNIX, SOCK_STREAM, 0, pfd) < 0) { ni_error("%s: unable to create pipe: %m", __func__); return -1; } rv = __ni_process_run(pi, pfd); if (rv >= 0) { /* Set up a socket to receive the redirected output of the * subprocess. */ pi->socket = __ni_process_get_output(pi, pfd[0]); ni_socket_activate(pi->socket); close(pfd[1]); } else { if (pfd[0] >= 0) close(pfd[0]); if (pfd[1] >= 0) close(pfd[1]); } return rv; }
int ni_process_run_and_capture_output(ni_process_t *pi, ni_buffer_t *out_buffer) { int pfd[2], rv; if (pipe(pfd) < 0) { ni_error("%s: unable to create pipe: %m", __func__); return -1; } rv = __ni_process_run(pi, pfd); if (rv < 0) { close(pfd[0]); close(pfd[1]); return rv; } close(pfd[1]); while (1) { int cnt; if (ni_buffer_tailroom(out_buffer) < 256) ni_buffer_ensure_tailroom(out_buffer, 4096); cnt = read(pfd[0], ni_buffer_tail(out_buffer), ni_buffer_tailroom(out_buffer)); if (cnt == 0) { break; } else if (cnt > 0) { out_buffer->tail += cnt; } else if (errno != EINTR) { ni_error("read error on subprocess pipe: %m"); return -1; } } while (waitpid(pi->pid, &pi->status, 0) < 0) { if (errno == EINTR) continue; ni_error("%s: waitpid returns error (%m)", __func__); return -1; } pi->pid = 0; if (pi->notify_callback) pi->notify_callback(pi); if (!ni_process_exit_status_okay(pi)) { ni_error("subprocesses exited with error"); return -1; } return rv; }
int ni_process_run_and_capture_output(ni_process_t *pi, ni_buffer_t *out_buffer) { int pfd[2], rv; if (pipe(pfd) < 0) { ni_error("%s: unable to create pipe: %m", __func__); return NI_PROCESS_FAILURE; } rv = __ni_process_run(pi, pfd); if (rv < NI_PROCESS_SUCCESS) { close(pfd[0]); close(pfd[1]); return rv; } rv = NI_PROCESS_SUCCESS; close(pfd[1]); while (1) { int cnt; if (ni_buffer_tailroom(out_buffer) < 256) ni_buffer_ensure_tailroom(out_buffer, 4096); cnt = read(pfd[0], ni_buffer_tail(out_buffer), ni_buffer_tailroom(out_buffer)); if (cnt == 0) { break; } else if (cnt > 0) { out_buffer->tail += cnt; } else if (errno != EINTR) { ni_error("read error on subprocess pipe: %m"); rv = NI_PROCESS_IOERROR; break; } } close(pfd[0]); while (waitpid(pi->pid, &pi->status, 0) < 0) { if (errno == EINTR) continue; ni_error("%s: waitpid returns error (%m)", __func__); rv = NI_PROCESS_WAITPID; } if (pi->notify_callback) pi->notify_callback(pi); if (rv != NI_PROCESS_SUCCESS) return rv; return __ni_process_run_info(pi); }
int ni_process_run_and_wait(ni_process_t *pi) { int rv; rv = __ni_process_run(pi, NULL); if (rv < NI_PROCESS_SUCCESS) return rv; rv = NI_PROCESS_SUCCESS; while (waitpid(pi->pid, &pi->status, 0) < 0) { if (errno == EINTR) continue; ni_error("%s: waitpid returned error (%m)", __func__); rv = NI_PROCESS_WAITPID; } if (pi->notify_callback) pi->notify_callback(pi); if (rv != NI_PROCESS_SUCCESS) return rv; return __ni_process_run_info(pi); }