/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef V_ALPM_NATIVE_PROCESS_H #define V_ALPM_NATIVE_PROCESS_H #include #include #include #include #include #include #include /* typed access to socket and poll ABI; scheduling and process policy stay in V. */ static int v_alpm_process_pair(int32_t *result) { int pair[2], saved; if(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) != 0) { return -1; } result[0] = fcntl(pair[0], F_DUPFD_CLOEXEC, 3); result[1] = result[0] < 0 ? -1 : fcntl(pair[1], F_DUPFD_CLOEXEC, 3); saved = errno; close(pair[0]); close(pair[1]); if(result[0] < 0 || result[1] < 0) { if(result[0] >= 0) { close(result[0]); } result[0] = result[1] = -1; errno = saved; return -1; } #if defined(SO_NOSIGPIPE) { int enabled = 1; setsockopt(result[0], SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); setsockopt(result[1], SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); } #endif return 0; } static int v_alpm_process_nonblock(int fd) { int flags = fcntl(fd, F_GETFL); return flags < 0 ? -1 : fcntl(fd, F_SETFL, flags | O_NONBLOCK); } static int v_alpm_process_poll(int output, int input, int32_t *out_events, int32_t *in_events) { struct pollfd fds[2] = {{.fd = output, .events = POLLIN}, {.fd = input, .events = POLLOUT}}; int result = poll(fds, 2, -1); *out_events = fds[0].revents; *in_events = fds[1].revents; return result; } static int v_alpm_process_send(int fd, const void *data, uint32_t size) { #if defined(MSG_NOSIGNAL) return send(fd, data, size, MSG_NOSIGNAL); #else return send(fd, data, size, 0); #endif } static int v_alpm_process_exec(const char *command, char *const *args) { return execv(command, args); } #endif