moused(8): Use KQUEUE_CPONFORK flag to pass kqueue fd through fork

and use libc daemon() instead of private rfork-based copy.
This commit is contained in:
Vladimir Kondratyev
2026-02-11 01:59:06 +03:00
parent 3d85dcd1a9
commit 20a3c611f5
+2 -56
View File
@@ -492,7 +492,6 @@ static void usage(void);
static void log_or_warn(int log_pri, int errnum, const char *fmt, ...)
__printflike(3, 4);
static int r_daemon(void);
static enum device_if r_identify_if(int fd);
static enum device_type r_identify_evdev(int fd);
static enum device_type r_identify_sysmouse(int fd);
@@ -761,7 +760,7 @@ main(int argc, char *argv[])
if ((cfd = open("/dev/consolectl", O_RDWR, 0)) == -1)
logerr(1, "cannot open /dev/consolectl");
if ((kfd = kqueue()) == -1)
if ((kfd = kqueuex(KQUEUE_CPONFORK)) == -1)
logerr(1, "cannot create kqueue");
if (portname == NULL && (dfd = connect_devd()) == -1)
logwarnx("cannot open devd socket");
@@ -827,7 +826,7 @@ main(int argc, char *argv[])
logerrx(1, "moused already running, pid: %d", mpid);
logwarn("cannot open pid file");
}
if (r_daemon()) {
if (daemon(0, 0)) {
int saved_errno = errno;
pidfile_remove(pfh);
errno = saved_errno;
@@ -1284,59 +1283,6 @@ log_or_warn(int log_pri, int errnum, const char *fmt, ...)
va_end(ap);
}
static int
r_daemon(void)
{
struct sigaction osa, sa;
pid_t newgrp;
int oerrno;
int osa_ok;
int nullfd;
/* A SIGHUP may be thrown when the parent exits below. */
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
osa_ok = sigaction(SIGHUP, &sa, &osa);
/* Keep kqueue fd alive */
switch (rfork(RFPROC)) {
case -1:
return (-1);
case 0:
break;
default:
/*
* A fine point: _exit(0), not exit(0), to avoid triggering
* atexit(3) processing
*/
_exit(0);
}
newgrp = setsid();
oerrno = errno;
if (osa_ok != -1)
sigaction(SIGHUP, &osa, NULL);
if (newgrp == -1) {
errno = oerrno;
return (-1);
}
(void)chdir("/");
nullfd = open("/dev/null", O_RDWR, 0);
if (nullfd != -1) {
(void)dup2(nullfd, STDIN_FILENO);
(void)dup2(nullfd, STDOUT_FILENO);
(void)dup2(nullfd, STDERR_FILENO);
}
if (nullfd > 2)
close(nullfd);
return (0);
}
static inline int
bit_find(bitstr_t *array, int start, int stop)
{