audit: Add poll / select support

It was previously not possible to poll() or select() on the trigger
device, which made implementing proper signal handling in auditd
difficult.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans, markj
Differential Revision:	https://reviews.freebsd.org/D57457
This commit is contained in:
Dag-Erling Smørgrav
2026-06-05 23:50:38 +02:00
parent 520e0f576f
commit 0620c99d27
+21 -2
View File
@@ -32,8 +32,10 @@
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/selinfo.h>
#include <sys/systm.h>
#include <sys/uio.h>
@@ -45,8 +47,6 @@
* used to communicate with userland. /dev/audit reliably delivers one-byte
* messages to a listening application (or discards them if there is no
* listening application).
*
* Currently, select/poll are not supported on the trigger device.
*/
struct trigger_info {
unsigned int trigger;
@@ -58,6 +58,7 @@ static struct cdev *audit_dev;
static int audit_isopen = 0;
static TAILQ_HEAD(, trigger_info) trigger_list;
static struct mtx audit_trigger_mtx;
static struct selinfo audit_trigger_rsel;
static int
audit_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
@@ -127,6 +128,22 @@ audit_write(struct cdev *dev, struct uio *uio, int ioflag)
return (EOPNOTSUPP);
}
static int
audit_poll(struct cdev *dev, int events, struct thread *td)
{
int revents = 0;
if (events & (POLLIN | POLLRDNORM)) {
mtx_lock(&audit_trigger_mtx);
if (!TAILQ_EMPTY(&trigger_list))
revents = events & (POLLIN | POLLRDNORM);
else
selrecord(td, &audit_trigger_rsel);
mtx_unlock(&audit_trigger_mtx);
}
return (revents);
}
int
audit_send_trigger(unsigned int trigger)
{
@@ -142,6 +159,7 @@ audit_send_trigger(unsigned int trigger)
}
ti->trigger = trigger;
TAILQ_INSERT_TAIL(&trigger_list, ti, list);
selwakeup(&audit_trigger_rsel);
wakeup(&trigger_list);
mtx_unlock(&audit_trigger_mtx);
return (0);
@@ -153,6 +171,7 @@ static struct cdevsw audit_cdevsw = {
.d_close = audit_close,
.d_read = audit_read,
.d_write = audit_write,
.d_poll = audit_poll,
.d_name = "audit"
};