| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | #ifndef V_ALPM_NATIVE_PROCESS_H |
| 3 | #define V_ALPM_NATIVE_PROCESS_H |
| 4 | #include <errno.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <poll.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | /* typed access to socket and poll ABI; scheduling and process policy stay in V. */ |
| 13 | static int v_alpm_process_pair(int32_t *result) { |
| 14 | int pair[2], saved; |
| 15 | if(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) != 0) { return -1; } |
| 16 | result[0] = fcntl(pair[0], F_DUPFD_CLOEXEC, 3); |
| 17 | result[1] = result[0] < 0 ? -1 : fcntl(pair[1], F_DUPFD_CLOEXEC, 3); |
| 18 | saved = errno; |
| 19 | close(pair[0]); |
| 20 | close(pair[1]); |
| 21 | if(result[0] < 0 || result[1] < 0) { |
| 22 | if(result[0] >= 0) { close(result[0]); } |
| 23 | result[0] = result[1] = -1; |
| 24 | errno = saved; |
| 25 | return -1; |
| 26 | } |
| 27 | #if defined(SO_NOSIGPIPE) |
| 28 | { int enabled = 1; |
| 29 | setsockopt(result[0], SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); |
| 30 | setsockopt(result[1], SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)); } |
| 31 | #endif |
| 32 | return 0; |
| 33 | } |
| 34 | static int v_alpm_process_nonblock(int fd) { |
| 35 | int flags = fcntl(fd, F_GETFL); |
| 36 | return flags < 0 ? -1 : fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 37 | } |
| 38 | static int v_alpm_process_poll(int output, int input, int32_t *out_events, int32_t *in_events) { |
| 39 | struct pollfd fds[2] = {{.fd = output, .events = POLLIN}, {.fd = input, .events = POLLOUT}}; |
| 40 | int result = poll(fds, 2, -1); |
| 41 | *out_events = fds[0].revents; |
| 42 | *in_events = fds[1].revents; |
| 43 | return result; |
| 44 | } |
| 45 | static int v_alpm_process_send(int fd, const void *data, uint32_t size) { |
| 46 | #if defined(MSG_NOSIGNAL) |
| 47 | return send(fd, data, size, MSG_NOSIGNAL); |
| 48 | #else |
| 49 | return send(fd, data, size, 0); |
| 50 | #endif |
| 51 | } |
| 52 | static int v_alpm_process_exec(const char *command, char *const *args) { return execv(command, args); } |
| 53 | #endif |
| 54 | |