Blame axfer/waiter-select.c

Packit Service a9274b
// SPDX-License-Identifier: GPL-2.0
Packit Service a9274b
//
Packit Service a9274b
// waiter-select.c - Waiter for event notification by select(2).
Packit Service a9274b
//
Packit Service a9274b
// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
Packit Service a9274b
//
Packit Service a9274b
// Licensed under the terms of the GNU General Public License, version 2.
Packit Service a9274b
Packit Service a9274b
#include "waiter.h"
Packit Service a9274b
Packit Service a9274b
#include <stdlib.h>
Packit Service a9274b
#include <unistd.h>
Packit Service a9274b
#include <string.h>
Packit Service a9274b
#include <errno.h>
Packit Service a9274b
#include <sys/select.h>
Packit Service a9274b
Packit Service a9274b
// Except for POLLERR.
Packit Service a9274b
#ifdef POLLRDNORM
Packit Service a9274b
// This program is for userspace compliant to POSIX 2008 (IEEE 1003.1:2008).
Packit Service a9274b
// This is the default compliance level since glibc-2.12 or later.
Packit Service a9274b
# define POLLIN_SET	(POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP)
Packit Service a9274b
# define POLLOUT_SET	(POLLWRBAND | POLLWRNORM | POLLOUT)
Packit Service a9274b
#else
Packit Service a9274b
// However it's allowed to be for old compliance levels.
Packit Service a9274b
# define POLLIN_SET	(POLLIN | POLLHUP)
Packit Service a9274b
# define POLLOUT_SET	(POLLOUT)
Packit Service a9274b
#endif
Packit Service a9274b
#define POLLEX_SET	(POLLPRI)
Packit Service a9274b
Packit Service a9274b
Packit Service a9274b
struct select_state {
Packit Service a9274b
	fd_set rfds_rd;
Packit Service a9274b
	fd_set rfds_wr;
Packit Service a9274b
	fd_set rfds_ex;
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
static int select_prepare(struct waiter_context *waiter)
Packit Service a9274b
{
Packit Service a9274b
	return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int select_wait_event(struct waiter_context *waiter, int timeout_msec)
Packit Service a9274b
{
Packit Service a9274b
	struct select_state *state = waiter->private_data;
Packit Service a9274b
	struct pollfd *pfd;
Packit Service a9274b
	int fd_max;
Packit Service a9274b
	struct timeval tv, *tv_ptr;
Packit Service a9274b
	int i;
Packit Service a9274b
	int err;
Packit Service a9274b
Packit Service a9274b
	FD_ZERO(&state->rfds_rd);
Packit Service a9274b
	FD_ZERO(&state->rfds_wr);
Packit Service a9274b
	FD_ZERO(&state->rfds_ex);
Packit Service a9274b
Packit Service a9274b
	fd_max = 0;
Packit Service a9274b
	for (i = 0; i < waiter->pfd_count; ++i) {
Packit Service a9274b
		pfd = &waiter->pfds[i];
Packit Service a9274b
Packit Service a9274b
		if (pfd->events & POLLIN_SET)
Packit Service a9274b
			FD_SET(pfd->fd, &state->rfds_rd);
Packit Service a9274b
		if (pfd->events & POLLOUT_SET)
Packit Service a9274b
			FD_SET(pfd->fd, &state->rfds_wr);
Packit Service a9274b
		if (pfd->events & POLLEX_SET)
Packit Service a9274b
			FD_SET(pfd->fd, &state->rfds_ex);
Packit Service a9274b
		if (pfd->fd > fd_max)
Packit Service a9274b
			fd_max = pfd->fd;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	if (timeout_msec < 0) {
Packit Service a9274b
		tv_ptr = NULL;
Packit Service a9274b
	} else {
Packit Service a9274b
		tv.tv_sec = 0;
Packit Service a9274b
		tv.tv_usec = timeout_msec * 1000;
Packit Service a9274b
		tv_ptr = &tv;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	err = select(fd_max + 1, &state->rfds_rd, &state->rfds_wr,
Packit Service a9274b
		     &state->rfds_ex, tv_ptr);
Packit Service a9274b
	if (err < 0)
Packit Service a9274b
		return -errno;
Packit Service a9274b
Packit Service a9274b
	for (i = 0; i < waiter->pfd_count; ++i) {
Packit Service a9274b
		pfd = &waiter->pfds[i];
Packit Service a9274b
Packit Service a9274b
		pfd->revents = 0;
Packit Service a9274b
		if (FD_ISSET(pfd->fd, &state->rfds_rd))
Packit Service a9274b
			pfd->revents |= POLLIN;
Packit Service a9274b
		if (FD_ISSET(pfd->fd, &state->rfds_wr))
Packit Service a9274b
			pfd->revents |= POLLOUT;
Packit Service a9274b
		if (FD_ISSET(pfd->fd, &state->rfds_ex))
Packit Service a9274b
			pfd->revents |= POLLHUP;
Packit Service a9274b
	}
Packit Service a9274b
Packit Service a9274b
	return err;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void select_release(struct waiter_context *waiter)
Packit Service a9274b
{
Packit Service a9274b
	return;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
const struct waiter_data waiter_select = {
Packit Service a9274b
	.ops = {
Packit Service a9274b
		.prepare	= select_prepare,
Packit Service a9274b
		.wait_event	= select_wait_event,
Packit Service a9274b
		.release	= select_release,
Packit Service a9274b
	},
Packit Service a9274b
	.private_size = sizeof(struct select_state),
Packit Service a9274b
};