| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <signal.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <sys/signalfd.h> | |
| #include "base.h" | |
| #include "fdpass.h" | |
| #include "notify.h" | |
| #include "supervisor.h" | |
| #include "tracee.h" | |
| static void print_error(int err) | |
| { | |
| if (ucells_error) | |
| fprintf(stderr, "ucells: %s: %s\n", ucells_error, | |
| strerror(-err)); | |
| else | |
| fprintf(stderr, "ucells: %s\n", strerror(-err)); | |
| } | |
| [[noreturn]] static void exec_tracee(int socket, const sigset_t *original_mask, | |
| char *const command[]) | |
| { | |
| int listener, ret; | |
| if (sigprocmask(SIG_SETMASK, original_mask, NULL) < 0) { | |
| ret = err_ctx(-errno, "restore tracee signal mask"); | |
| goto fail; | |
| } | |
| listener = install_cells_notif_filter(); | |
| if (listener < 0) { | |
| ret = listener; | |
| goto fail; | |
| } | |
| ret = send_fd(socket, listener); | |
| if (ret < 0) | |
| goto fail; | |
| close_fd(listener); | |
| close_fd(socket); | |
| execvp(command[0], command); | |
| fprintf(stderr, "ucells: exec %s: %s\n", command[0], strerror(errno)); | |
| _exit(127); | |
| fail: | |
| print_error(ret); | |
| _exit(126); | |
| } | |
| static int run(char *const command[]) | |
| { | |
| struct supervisor *supervisor; | |
| struct tracee tracee; | |
| sigset_t mask, original_mask; | |
| int listener, signals, sockets[2]; | |
| pid_t pid; | |
| int ret; | |
| ret = supervisor_create(&supervisor); | |
| if (ret < 0) | |
| return ret; | |
| sigemptyset(&mask); | |
| sigaddset(&mask, SIGINT); | |
| sigaddset(&mask, SIGTERM); | |
| sigaddset(&mask, SIGCHLD); | |
| if (sigprocmask(SIG_BLOCK, &mask, &original_mask) < 0) { | |
| ret = err_ctx(-errno, "block supervisor signals"); | |
| goto destroy_supervisor; | |
| } | |
| signals = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK); | |
| if (signals < 0) { | |
| ret = err_ctx(-errno, "create signalfd"); | |
| goto destroy_supervisor; | |
| } | |
| ret = fd_pair(sockets); | |
| if (ret < 0) | |
| goto close_signals; | |
| pid = fork(); | |
| if (pid < 0) { | |
| ret = err_ctx(-errno, "fork tracee"); | |
| close_fd(sockets[1]); | |
| goto close_parent; | |
| } | |
| if (!pid) { | |
| close_fd(sockets[0]); | |
| close_fd(signals); | |
| /* Only the parent tears down the supervisor's BPF links. */ | |
| exec_tracee(sockets[1], &original_mask, command); | |
| } | |
| tracee.pid = pid; | |
| close_fd(sockets[1]); | |
| listener = recv_fd(sockets[0]); | |
| if (listener < 0) { | |
| ret = listener; | |
| goto destroy_tracee; | |
| } | |
| close_fd(take_fd(&sockets[0])); | |
| ret = supervisor_run(supervisor, &tracee, listener, signals); | |
| close_fd(listener); | |
| destroy_tracee: | |
| tracee_destroy(&tracee); | |
| close_parent: | |
| close_fd(sockets[0]); | |
| close_signals: | |
| close_fd(signals); | |
| destroy_supervisor: | |
| supervisor_destroy(supervisor); | |
| return ret; | |
| } | |
| static const char usage[] = "Usage: ucells [OPTIONS] [--] <COMMAND>...\n"; | |
| int main(int argc, char *argv[]) | |
| { | |
| int command = 1; | |
| int ret; | |
| if (argc > 1) { | |
| const char *option = argv[1]; | |
| if (!strcmp(option, "-h") || !strcmp(option, "--help")) { | |
| printf("Run Cells v3 programs on Linux\n\n%s\n" | |
| "Options:\n -h, --help Print help\n" | |
| " -V, --version Print version\n", | |
| usage); | |
| return 0; | |
| } | |
| if (!strcmp(option, "-V") || !strcmp(option, "--version")) { | |
| puts("ucells " UCELLS_VERSION); | |
| return 0; | |
| } | |
| if (!strcmp(option, "--")) { | |
| command++; | |
| } else if (option[0] == '-' && option[1]) { | |
| fprintf(stderr, "ucells: unexpected option '%s'\n%s", | |
| option, usage); | |
| return 2; | |
| } | |
| } | |
| if (command >= argc) { | |
| fprintf(stderr, "ucells: missing command\n%s", usage); | |
| return 2; | |
| } | |
| ret = run(argv + command); | |
| if (ret < 0) { | |
| print_error(ret); | |
| return 1; | |
| } | |
| return ret; | |
| } |