sched: rework file handling API

Replace SCH_*InputFileHandler() functions with more general
SCH_*FileHandler(), where events are specified as a new parameter and
which will later support other file events, e.g. file ready for ouput
and exception.

The file handlers have two new parameters: file descriptor and event.
This commit is contained in:
Miroslav Lichvar 2016-06-16 16:47:40 +02:00
parent aeb57a36b2
commit 0a10545314
7 changed files with 49 additions and 38 deletions

View file

@ -143,7 +143,7 @@ static ADF_AuthTable access_auth_table;
/* ================================================== */ /* ================================================== */
/* Forward prototypes */ /* Forward prototypes */
static void read_from_cmd_socket(void *anything); static void read_from_cmd_socket(int sock_fd, int event, void *anything);
/* ================================================== */ /* ================================================== */
@ -242,7 +242,7 @@ prepare_socket(int family, int port_number)
} }
/* Register handler for read events on the socket */ /* Register handler for read events on the socket */
SCH_AddInputFileHandler(sock_fd, read_from_cmd_socket, (void *)(long)sock_fd); SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_from_cmd_socket, NULL);
return sock_fd; return sock_fd;
} }
@ -325,19 +325,19 @@ void
CAM_Finalise(void) CAM_Finalise(void)
{ {
if (sock_fdu >= 0) { if (sock_fdu >= 0) {
SCH_RemoveInputFileHandler(sock_fdu); SCH_RemoveFileHandler(sock_fdu);
close(sock_fdu); close(sock_fdu);
unlink(CNF_GetBindCommandPath()); unlink(CNF_GetBindCommandPath());
} }
sock_fdu = -1; sock_fdu = -1;
if (sock_fd4 >= 0) { if (sock_fd4 >= 0) {
SCH_RemoveInputFileHandler(sock_fd4); SCH_RemoveFileHandler(sock_fd4);
close(sock_fd4); close(sock_fd4);
} }
sock_fd4 = -1; sock_fd4 = -1;
#ifdef FEAT_IPV6 #ifdef FEAT_IPV6
if (sock_fd6 >= 0) { if (sock_fd6 >= 0) {
SCH_RemoveInputFileHandler(sock_fd6); SCH_RemoveFileHandler(sock_fd6);
close(sock_fd6); close(sock_fd6);
} }
sock_fd6 = -1; sock_fd6 = -1;
@ -1189,12 +1189,12 @@ handle_server_stats(CMD_Request *rx_message, CMD_Reply *tx_message)
/* Read a packet and process it */ /* Read a packet and process it */
static void static void
read_from_cmd_socket(void *anything) read_from_cmd_socket(int sock_fd, int event, void *anything)
{ {
CMD_Request rx_message; CMD_Request rx_message;
CMD_Reply tx_message; CMD_Reply tx_message;
int status, read_length, expected_length, rx_message_length; int status, read_length, expected_length, rx_message_length;
int localhost, allowed, sock_fd, log_index; int localhost, allowed, log_index;
union sockaddr_all where_from; union sockaddr_all where_from;
socklen_t from_length; socklen_t from_length;
IPAddr remote_ip; IPAddr remote_ip;
@ -1204,7 +1204,6 @@ read_from_cmd_socket(void *anything)
rx_message_length = sizeof(rx_message); rx_message_length = sizeof(rx_message);
from_length = sizeof(where_from); from_length = sizeof(where_from);
sock_fd = (long)anything;
status = recvfrom(sock_fd, (char *)&rx_message, rx_message_length, 0, status = recvfrom(sock_fd, (char *)&rx_message, rx_message_length, 0,
&where_from.sa, &from_length); &where_from.sa, &from_length);

View file

@ -72,7 +72,7 @@ start_resolving(void *anything)
/* ================================================== */ /* ================================================== */
static void static void
end_resolving(void *anything) end_resolving(int fd, int event, void *anything)
{ {
struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything; struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything;
int i; int i;
@ -83,7 +83,7 @@ end_resolving(void *anything)
resolving_threads--; resolving_threads--;
SCH_RemoveInputFileHandler(inst->pipe[0]); SCH_RemoveFileHandler(inst->pipe[0]);
close(inst->pipe[0]); close(inst->pipe[0]);
close(inst->pipe[1]); close(inst->pipe[1]);
@ -120,7 +120,7 @@ DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *
LOG_FATAL(LOGF_Nameserv, "pthread_create() failed"); LOG_FATAL(LOGF_Nameserv, "pthread_create() failed");
} }
SCH_AddInputFileHandler(inst->pipe[0], end_resolving, inst); SCH_AddFileHandler(inst->pipe[0], SCH_FILE_INPUT, end_resolving, inst);
} }
/* ================================================== */ /* ================================================== */

View file

@ -80,7 +80,7 @@ static int initialised=0;
/* ================================================== */ /* ================================================== */
/* Forward prototypes */ /* Forward prototypes */
static void read_from_socket(void *anything); static void read_from_socket(int sock_fd, int event, void *anything);
/* ================================================== */ /* ================================================== */
@ -230,7 +230,7 @@ prepare_socket(int family, int port_number, int client_only)
} }
/* Register handler for read events on the socket */ /* Register handler for read events on the socket */
SCH_AddInputFileHandler(sock_fd, read_from_socket, (void *)(long)sock_fd); SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_from_socket, NULL);
return sock_fd; return sock_fd;
} }
@ -282,7 +282,7 @@ close_socket(int sock_fd)
if (sock_fd == INVALID_SOCK_FD) if (sock_fd == INVALID_SOCK_FD)
return; return;
SCH_RemoveInputFileHandler(sock_fd); SCH_RemoveFileHandler(sock_fd);
close(sock_fd); close(sock_fd);
} }
@ -482,12 +482,12 @@ NIO_IsServerSocket(int sock_fd)
/* ================================================== */ /* ================================================== */
static void static void
read_from_socket(void *anything) read_from_socket(int sock_fd, int event, void *anything)
{ {
/* This should only be called when there is something /* This should only be called when there is something
to read, otherwise it will block. */ to read, otherwise it will block. */
int status, sock_fd; int status;
NTP_Receive_Buffer message; NTP_Receive_Buffer message;
union sockaddr_in46 where_from; union sockaddr_in46 where_from;
unsigned int flags = 0; unsigned int flags = 0;
@ -514,7 +514,6 @@ read_from_socket(void *anything)
msg.msg_controllen = sizeof(cmsgbuf); msg.msg_controllen = sizeof(cmsgbuf);
msg.msg_flags = 0; msg.msg_flags = 0;
sock_fd = (long)anything;
status = recvmsg(sock_fd, &msg, flags); status = recvmsg(sock_fd, &msg, flags);
/* Don't bother checking if read failed or why if it did. More /* Don't bother checking if read failed or why if it did. More

View file

@ -57,14 +57,13 @@ struct sock_sample {
int magic; int magic;
}; };
static void read_sample(void *anything) static void read_sample(int sockfd, int event, void *anything)
{ {
struct sock_sample sample; struct sock_sample sample;
RCL_Instance instance; RCL_Instance instance;
int sockfd, s; int s;
instance = (RCL_Instance)anything; instance = (RCL_Instance)anything;
sockfd = (long)RCL_GetDriverData(instance);
s = recv(sockfd, &sample, sizeof (sample), 0); s = recv(sockfd, &sample, sizeof (sample), 0);
@ -122,7 +121,7 @@ static int sock_initialise(RCL_Instance instance)
} }
RCL_SetDriverData(instance, (void *)(long)sockfd); RCL_SetDriverData(instance, (void *)(long)sockfd);
SCH_AddInputFileHandler(sockfd, read_sample, instance); SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
return 1; return 1;
} }
@ -131,7 +130,7 @@ static void sock_finalise(RCL_Instance instance)
int sockfd; int sockfd;
sockfd = (long)RCL_GetDriverData(instance); sockfd = (long)RCL_GetDriverData(instance);
SCH_RemoveInputFileHandler(sockfd); SCH_RemoveFileHandler(sockfd);
close(sockfd); close(sockfd);
} }

View file

@ -50,7 +50,7 @@
static void measurement_timeout(void *any); static void measurement_timeout(void *any);
static void read_from_device(void *any); static void read_from_device(int fd_, int event, void *any);
/* ================================================== */ /* ================================================== */
@ -564,7 +564,7 @@ RTC_Linux_Initialise(void)
operating_mode = OM_NORMAL; operating_mode = OM_NORMAL;
/* Register file handler */ /* Register file handler */
SCH_AddInputFileHandler(fd, read_from_device, NULL); SCH_AddFileHandler(fd, SCH_FILE_INPUT, read_from_device, NULL);
/* Register slew handler */ /* Register slew handler */
LCL_AddParameterChangeHandler(slew_samples, NULL); LCL_AddParameterChangeHandler(slew_samples, NULL);
@ -585,7 +585,7 @@ RTC_Linux_Finalise(void)
/* Remove input file handler */ /* Remove input file handler */
if (fd >= 0) { if (fd >= 0) {
SCH_RemoveInputFileHandler(fd); SCH_RemoveFileHandler(fd);
close(fd); close(fd);
/* Save the RTC data */ /* Save the RTC data */
@ -805,7 +805,7 @@ process_reading(time_t rtc_time, struct timeval *system_time)
/* ================================================== */ /* ================================================== */
static void static void
read_from_device(void *any) read_from_device(int fd_, int event, void *any)
{ {
int status; int status;
unsigned long data; unsigned long data;
@ -821,7 +821,7 @@ read_from_device(void *any)
/* This looks like a bad error : the file descriptor was indicating it was /* This looks like a bad error : the file descriptor was indicating it was
* ready to read but we couldn't read anything. Give up. */ * ready to read but we couldn't read anything. Give up. */
LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read flags %s : %s", CNF_GetRtcDevice(), strerror(errno)); LOG(LOGS_ERR, LOGF_RtcLinux, "Could not read flags %s : %s", CNF_GetRtcDevice(), strerror(errno));
SCH_RemoveInputFileHandler(fd); SCH_RemoveFileHandler(fd);
switch_interrupts(0); /* Likely to raise error too, but just to be sure... */ switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
close(fd); close(fd);
fd = -1; fd = -1;

22
sched.c
View file

@ -166,12 +166,14 @@ SCH_Finalise(void) {
/* ================================================== */ /* ================================================== */
void void
SCH_AddInputFileHandler SCH_AddFileHandler
(int fd, SCH_FileHandler handler, SCH_ArbitraryArgument arg) (int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg)
{ {
FileHandlerEntry *ptr; FileHandlerEntry *ptr;
assert(initialised); assert(initialised);
assert(events);
assert(fd >= 0);
if (fd >= FD_SETSIZE) if (fd >= FD_SETSIZE)
LOG_FATAL(LOGF_Scheduler, "Too many file descriptors"); LOG_FATAL(LOGF_Scheduler, "Too many file descriptors");
@ -202,7 +204,7 @@ SCH_AddInputFileHandler
/* ================================================== */ /* ================================================== */
void void
SCH_RemoveInputFileHandler(int fd) SCH_RemoveFileHandler(int fd)
{ {
int fds_left, fd_to_check; int fds_left, fd_to_check;
@ -231,6 +233,18 @@ SCH_RemoveInputFileHandler(int fd)
/* ================================================== */ /* ================================================== */
void
SCH_SetFileHandlerEvents(int fd, int events)
{
FileHandlerEntry *ptr;
assert(events);
ptr = ARR_GetElement(file_handlers, fd);
ptr->events = events;
}
/* ================================================== */
void void
SCH_GetLastEventTime(struct timeval *cooked, double *err, struct timeval *raw) SCH_GetLastEventTime(struct timeval *cooked, double *err, struct timeval *raw)
{ {
@ -533,7 +547,7 @@ dispatch_filehandlers(int nfh, fd_set *fhs)
/* This descriptor can be read from, dispatch its handler */ /* This descriptor can be read from, dispatch its handler */
ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fh); ptr = (FileHandlerEntry *)ARR_GetElement(file_handlers, fh);
(ptr->handler)(ptr->arg); (ptr->handler)(fh, SCH_FILE_INPUT, ptr->arg);
/* Decrement number of readable files still to find */ /* Decrement number of readable files still to find */
--nfh; --nfh;

14
sched.h
View file

@ -40,7 +40,7 @@ typedef enum {
} SCH_TimeoutClass; } SCH_TimeoutClass;
typedef void* SCH_ArbitraryArgument; typedef void* SCH_ArbitraryArgument;
typedef void (*SCH_FileHandler)(SCH_ArbitraryArgument); typedef void (*SCH_FileHandler)(int fd, int event, SCH_ArbitraryArgument);
typedef void (*SCH_TimeoutHandler)(SCH_ArbitraryArgument); typedef void (*SCH_TimeoutHandler)(SCH_ArbitraryArgument);
/* Exported functions */ /* Exported functions */
@ -51,13 +51,13 @@ extern void SCH_Initialise(void);
/* Finalisation function for the module */ /* Finalisation function for the module */
extern void SCH_Finalise(void); extern void SCH_Finalise(void);
/* File events */
#define SCH_FILE_INPUT 1
/* Register a handler for when select goes true on a file descriptor */ /* Register a handler for when select goes true on a file descriptor */
extern void SCH_AddInputFileHandler extern void SCH_AddFileHandler(int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg);
(int fd, /* The file descriptor */ extern void SCH_RemoveFileHandler(int fd);
SCH_FileHandler, /* The handler routine */ extern void SCH_SetFileHandlerEvents(int fd, int events);
SCH_ArbitraryArgument /* An arbitrary passthrough argument to the handler */
);
extern void SCH_RemoveInputFileHandler(int fd);
/* Get the time stamp taken after a file descriptor became ready or a timeout expired */ /* Get the time stamp taken after a file descriptor became ready or a timeout expired */
extern void SCH_GetLastEventTime(struct timeval *cooked, double *err, struct timeval *raw); extern void SCH_GetLastEventTime(struct timeval *cooked, double *err, struct timeval *raw);