CI / test (push) Failing after 16s
- shell: the module under the pointer gets PointerMove, the one it left gets PointerLeave (hover feedback, e.g. the dock's magnifying tiles) - sys: reset SIGCHLD to SIG_DFL in the child before exec (SIG_IGN is inherited and breaks apps that wait() on helpers, e.g. terminals); flatpak run keeps the real session bus - inspector: drop the redundant 'scroll for more' hint next to the scrollbar Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
472 lines
13 KiB
C
472 lines
13 KiB
C
#define _GNU_SOURCE
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/un.h>
|
|
#include <time.h>
|
|
#include <signal.h>
|
|
#include <sys/prctl.h>
|
|
#include <dirent.h>
|
|
#include <unistd.h>
|
|
#include <limits.h>
|
|
|
|
#include "moonbit.h"
|
|
|
|
static void bytes_to_cstr(moonbit_bytes_t b, char *out, size_t cap) {
|
|
int32_t len = Moonbit_array_length(b);
|
|
size_t n = (size_t)len < cap - 1 ? (size_t)len : cap - 1;
|
|
memcpy(out, b, n);
|
|
out[n] = '\0';
|
|
}
|
|
|
|
static int set_nonblock_cloexec(int fd) {
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
if (flags >= 0) {
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
}
|
|
int fdflags = fcntl(fd, F_GETFD, 0);
|
|
if (fdflags >= 0) {
|
|
fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_unix_listen(moonbit_bytes_t path_bytes) {
|
|
char path[108];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
unlink(path);
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
if (fd < 0) return -1;
|
|
set_nonblock_cloexec(fd);
|
|
|
|
struct sockaddr_un addr;
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sun_family = AF_UNIX;
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
|
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
if (listen(fd, 16) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_unix_accept(int32_t listen_fd) {
|
|
int fd = accept(listen_fd, NULL, NULL);
|
|
if (fd < 0) {
|
|
return -1;
|
|
}
|
|
set_nonblock_cloexec(fd);
|
|
return fd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_unix_connect(moonbit_bytes_t path_bytes) {
|
|
char path[108];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
if (fd < 0) return -1;
|
|
|
|
struct sockaddr_un addr;
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sun_family = AF_UNIX;
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_close_fd(int32_t fd) {
|
|
if (fd < 0) return 0;
|
|
return close(fd);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_unlink(moonbit_bytes_t path_bytes) {
|
|
char path[512];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
return unlink(path);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_poll_read(int32_t fd, int32_t timeout_ms) {
|
|
struct pollfd pfd;
|
|
pfd.fd = fd;
|
|
pfd.events = POLLIN;
|
|
pfd.revents = 0;
|
|
int r = poll(&pfd, 1, timeout_ms);
|
|
if (r < 0) return -1;
|
|
if (r == 0) return 0;
|
|
return (pfd.revents & (POLLIN | POLLHUP | POLLERR)) ? 1 : 0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t *out_fd) {
|
|
out_fd[0] = -1;
|
|
int32_t cap = Moonbit_array_length(buf);
|
|
int32_t n = max_len < cap ? max_len : cap;
|
|
if (n <= 0) return 0;
|
|
|
|
struct iovec iov;
|
|
iov.iov_base = buf;
|
|
iov.iov_len = (size_t)n;
|
|
|
|
char cmsg_buf[CMSG_SPACE(sizeof(int) * 4)];
|
|
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
|
|
|
struct msghdr msg;
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.msg_iov = &iov;
|
|
msg.msg_iovlen = 1;
|
|
msg.msg_control = cmsg_buf;
|
|
msg.msg_controllen = sizeof(cmsg_buf);
|
|
|
|
ssize_t recvd = recvmsg(fd, &msg, 0);
|
|
if (recvd < 0) {
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) return -1;
|
|
return -2;
|
|
}
|
|
|
|
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
|
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
|
int *fds = (int *)CMSG_DATA(cmsg);
|
|
size_t num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
|
if (num_fds > 0) {
|
|
out_fd[0] = fds[0];
|
|
for (size_t i = 1; i < num_fds; i++) {
|
|
close(fds[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return (int32_t)recvd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_sendmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t send_fd) {
|
|
int32_t cap = Moonbit_array_length(buf);
|
|
int32_t n = len < cap ? len : cap;
|
|
if (n <= 0) return 0;
|
|
|
|
struct iovec iov;
|
|
iov.iov_base = buf;
|
|
iov.iov_len = (size_t)n;
|
|
|
|
char cmsg_buf[CMSG_SPACE(sizeof(int))];
|
|
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
|
|
|
struct msghdr msg;
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.msg_iov = &iov;
|
|
msg.msg_iovlen = 1;
|
|
|
|
if (send_fd >= 0) {
|
|
msg.msg_control = cmsg_buf;
|
|
msg.msg_controllen = sizeof(cmsg_buf);
|
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
|
memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(int));
|
|
}
|
|
|
|
size_t total = 0;
|
|
while (total < (size_t)n) {
|
|
iov.iov_base = buf + total;
|
|
iov.iov_len = (size_t)n - total;
|
|
ssize_t sent = sendmsg(fd, &msg, MSG_NOSIGNAL);
|
|
if (sent < 0) {
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
struct pollfd pfd = { .fd = fd, .events = POLLOUT, .revents = 0 };
|
|
poll(&pfd, 1, 50);
|
|
continue;
|
|
}
|
|
return -1;
|
|
}
|
|
total += (size_t)sent;
|
|
// Only send ancillary data on the first sendmsg call
|
|
msg.msg_control = NULL;
|
|
msg.msg_controllen = 0;
|
|
}
|
|
return (int32_t)total;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_memfd_create(moonbit_bytes_t name_bytes, int32_t size) {
|
|
char name[64];
|
|
bytes_to_cstr(name_bytes, name, sizeof(name));
|
|
int fd = memfd_create(name, MFD_CLOEXEC);
|
|
if (fd < 0) return -1;
|
|
if (size > 0 && ftruncate(fd, (off_t)size) < 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_pwrite_bytes(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t offset) {
|
|
int32_t cap = Moonbit_array_length(buf);
|
|
int32_t n = len < cap ? len : cap;
|
|
ssize_t w = pwrite(fd, buf, (size_t)n, (off_t)offset);
|
|
return w < 0 ? -1 : (int32_t)w;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
moonbit_bytes_t mp_mmap_read(int32_t fd, int32_t size) {
|
|
if (fd < 0 || size <= 0) {
|
|
return moonbit_make_bytes_raw(0);
|
|
}
|
|
struct stat st;
|
|
if (fstat(fd, &st) < 0 || st.st_size < (off_t)size) {
|
|
return moonbit_make_bytes_raw(0);
|
|
}
|
|
void *ptr = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED, fd, 0);
|
|
if (ptr == MAP_FAILED) {
|
|
return moonbit_make_bytes_raw(0);
|
|
}
|
|
moonbit_bytes_t out = moonbit_make_bytes_raw(size);
|
|
memcpy(out, ptr, (size_t)size);
|
|
munmap(ptr, (size_t)size);
|
|
return out;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
moonbit_bytes_t mp_read_file(moonbit_bytes_t path_bytes) {
|
|
char path[512];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) return moonbit_make_bytes_raw(0);
|
|
fseek(f, 0, SEEK_END);
|
|
long sz = ftell(f);
|
|
if (sz <= 0) {
|
|
fclose(f);
|
|
return moonbit_make_bytes_raw(0);
|
|
}
|
|
fseek(f, 0, SEEK_SET);
|
|
moonbit_bytes_t out = moonbit_make_bytes_raw((int32_t)sz);
|
|
size_t rd = fread(out, 1, (size_t)sz, f);
|
|
fclose(f);
|
|
if (rd != (size_t)sz) {
|
|
return moonbit_make_bytes_raw(0);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_write_file(moonbit_bytes_t path_bytes, moonbit_bytes_t buf, int32_t len) {
|
|
char path[512];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
int32_t cap = Moonbit_array_length(buf);
|
|
int32_t n = len < cap ? len : cap;
|
|
FILE *f = fopen(path, "wb");
|
|
if (!f) return -1;
|
|
size_t wr = fwrite(buf, 1, (size_t)n, f);
|
|
fclose(f);
|
|
return wr == (size_t)n ? 0 : -1;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
void mp_sleep_ms(int32_t ms) {
|
|
if (ms > 0) {
|
|
usleep((useconds_t)ms * 1000);
|
|
}
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_getpid(void) {
|
|
return (int32_t)getpid();
|
|
}
|
|
|
|
/* Local wall-clock field: 0 year, 1 month(1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday(0=Sun). */
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_wall_time_field(int32_t which) {
|
|
time_t t = time(NULL);
|
|
struct tm tmv;
|
|
localtime_r(&t, &tmv);
|
|
switch (which) {
|
|
case 0: return tmv.tm_year + 1900;
|
|
case 1: return tmv.tm_mon + 1;
|
|
case 2: return tmv.tm_mday;
|
|
case 3: return tmv.tm_hour;
|
|
case 4: return tmv.tm_min;
|
|
case 5: return tmv.tm_sec;
|
|
case 6: return tmv.tm_wday;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
/* True when `name` resolves to an executable via PATH (or is an executable path). */
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_has_exe(moonbit_bytes_t name_bytes) {
|
|
char name[256];
|
|
bytes_to_cstr(name_bytes, name, sizeof(name));
|
|
if (strchr(name, '/')) return access(name, X_OK) == 0;
|
|
const char *path = getenv("PATH");
|
|
if (!path) return 0;
|
|
char *copy = strdup(path);
|
|
char *save = NULL;
|
|
int found = 0;
|
|
for (char *dir = strtok_r(copy, ":", &save); dir && !found; dir = strtok_r(NULL, ":", &save)) {
|
|
char full[1024];
|
|
snprintf(full, sizeof(full), "%s/%s", dir, name);
|
|
if (access(full, X_OK) == 0) found = 1;
|
|
}
|
|
free(copy);
|
|
return found;
|
|
}
|
|
|
|
/* Private session bus for launched apps, so single-instance apps (Firefox, Kate,
|
|
* Dolphin...) don't forward the launch to an instance on the host session.
|
|
* Started lazily; dies with the compositor. */
|
|
static char private_bus[256];
|
|
static int private_bus_tried = 0;
|
|
|
|
static void start_private_bus(void) {
|
|
if (private_bus_tried) return;
|
|
private_bus_tried = 1;
|
|
int fds[2];
|
|
if (pipe(fds) != 0) return;
|
|
pid_t pid = fork();
|
|
if (pid < 0) { close(fds[0]); close(fds[1]); return; }
|
|
if (pid == 0) {
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
|
close(fds[0]);
|
|
char arg[64];
|
|
snprintf(arg, sizeof(arg), "--print-address=%d", fds[1]);
|
|
execlp("dbus-daemon", "dbus-daemon", "--session", "--nofork", arg, (char *)NULL);
|
|
_exit(127);
|
|
}
|
|
close(fds[1]);
|
|
ssize_t n = read(fds[0], private_bus, sizeof(private_bus) - 1);
|
|
close(fds[0]);
|
|
if (n > 0) {
|
|
private_bus[n] = 0;
|
|
char *nl = strchr(private_bus, '\n');
|
|
if (nl) *nl = 0;
|
|
} else {
|
|
private_bus[0] = 0;
|
|
}
|
|
}
|
|
|
|
/* Launch `cmd` detached, with WAYLAND_DISPLAY pointing at our socket. Returns 0 on fork success. */
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_spawn(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes) {
|
|
char cmd[2048];
|
|
char display[512];
|
|
bytes_to_cstr(cmd_bytes, cmd, sizeof(cmd));
|
|
bytes_to_cstr(display_bytes, display, sizeof(display));
|
|
signal(SIGCHLD, SIG_IGN); /* no zombies */
|
|
start_private_bus();
|
|
pid_t pid = fork();
|
|
if (pid < 0) return -1;
|
|
if (pid == 0) {
|
|
setsid();
|
|
/* SIG_IGN survives exec; apps (e.g. terminals that wait() on a helper) need the default. */
|
|
signal(SIGCHLD, SIG_DFL);
|
|
/* Flatpak apps reach the host (shell lookup, portals) through org.freedesktop.Flatpak
|
|
* on the real session bus, so they keep it; the sandbox isolates their instances anyway. */
|
|
if (private_bus[0] && strstr(cmd, "flatpak run") == NULL) setenv("DBUS_SESSION_BUS_ADDRESS", private_bus, 1);
|
|
setenv("WAYLAND_DISPLAY", display, 1);
|
|
unsetenv("DISPLAY");
|
|
/* Make toolkits pick our Wayland socket, not X11 or the host session. */
|
|
setenv("XDG_SESSION_TYPE", "wayland", 1);
|
|
setenv("QT_QPA_PLATFORM", "wayland", 1);
|
|
setenv("GDK_BACKEND", "wayland", 1);
|
|
setenv("SDL_VIDEODRIVER", "wayland", 1);
|
|
setenv("MOZ_ENABLE_WAYLAND", "1", 1);
|
|
setenv("ELECTRON_OZONE_PLATFORM_HINT", "wayland", 1);
|
|
setenv("CLUTTER_BACKEND", "wayland", 1);
|
|
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
|
|
_exit(127);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Names of "*.desktop" files in `dir`, newline-separated. */
|
|
MOONBIT_FFI_EXPORT
|
|
moonbit_bytes_t mp_list_desktop(moonbit_bytes_t dir_bytes) {
|
|
char dir[512];
|
|
bytes_to_cstr(dir_bytes, dir, sizeof(dir));
|
|
DIR *d = opendir(dir);
|
|
if (!d) return moonbit_make_bytes_raw(0);
|
|
size_t cap = 4096, len = 0;
|
|
char *buf = malloc(cap);
|
|
struct dirent *e;
|
|
while ((e = readdir(d)) != NULL) {
|
|
size_t n = strlen(e->d_name);
|
|
if (n < 9 || strcmp(e->d_name + n - 8, ".desktop") != 0) continue;
|
|
if (len + n + 2 > cap) { cap = (len + n + 2) * 2; buf = realloc(buf, cap); }
|
|
memcpy(buf + len, e->d_name, n);
|
|
len += n;
|
|
buf[len++] = '\n';
|
|
}
|
|
closedir(d);
|
|
moonbit_bytes_t out = moonbit_make_bytes_raw((int32_t)len);
|
|
memcpy(out, buf, len);
|
|
free(buf);
|
|
return out;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
moonbit_bytes_t mp_getenv(moonbit_bytes_t name_bytes) {
|
|
char name[128];
|
|
bytes_to_cstr(name_bytes, name, sizeof(name));
|
|
const char *v = getenv(name);
|
|
if (!v) return moonbit_make_bytes_raw(0);
|
|
size_t n = strlen(v);
|
|
moonbit_bytes_t out = moonbit_make_bytes_raw((int32_t)n);
|
|
memcpy(out, v, n);
|
|
return out;
|
|
}
|
|
|
|
/* mkdir -p; returns 0 when the directory exists afterwards. */
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t mp_mkdir_p(moonbit_bytes_t path_bytes) {
|
|
char path[512];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
for (char *p = path + 1; *p; p++) {
|
|
if (*p == '/') {
|
|
*p = 0;
|
|
mkdir(path, 0755);
|
|
*p = '/';
|
|
}
|
|
}
|
|
if (mkdir(path, 0755) != 0 && errno != EEXIST) return -1;
|
|
return 0;
|
|
}
|
|
|
|
/* Read a small /proc or /sys file (they report size 0, so read until EOF; capped at 64 KiB). */
|
|
MOONBIT_FFI_EXPORT
|
|
moonbit_bytes_t mp_read_proc(moonbit_bytes_t path_bytes) {
|
|
char path[512];
|
|
bytes_to_cstr(path_bytes, path, sizeof(path));
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) return moonbit_make_bytes_raw(0);
|
|
static char buf[65536];
|
|
size_t n = fread(buf, 1, sizeof(buf), f);
|
|
fclose(f);
|
|
moonbit_bytes_t out = moonbit_make_bytes_raw((int32_t)n);
|
|
memcpy(out, buf, n);
|
|
return out;
|
|
}
|