blob: 38b6e1144ffef45eaa2858995ad6c7a95342aa7d [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <sys/socket.h>
#include "base.h"
#include "handle.h"
#define STATUS_SOCK_SNDBUF 4096
#define MAX_STATUS_BATCH 256
int handle_pair_create(bool nonblocking_client, struct handle_pair *pair)
{
const int sndbuf = STATUS_SOCK_SNDBUF;
int fds[2], ret;
if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK,
0, fds) < 0)
return -errno;
if (!nonblocking_client) {
int flags = fcntl(fds[1], F_GETFL);
if (flags >= 0)
fcntl(fds[1], F_SETFL, flags & ~O_NONBLOCK);
}
setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
ret = file_identity_of_fd(fds[1], &pair->client_id);
if (ret < 0) {
close_fd(fds[1]);
close_fd(fds[0]);
return ret;
}
pair->supervisor_fd = fds[0];
return fds[1];
}
bool handle_push_exit_status(const struct handle_pair *pair, int32_t status)
{
for (int i = 0; i < MAX_STATUS_BATCH; i++) {
if (send(pair->supervisor_fd, &status, sizeof(status),
MSG_DONTWAIT | MSG_NOSIGNAL) < 0)
return errno == EAGAIN;
}
return false;
}