/* ================================================== */
/* Forward prototypes */
-static void read_from_cmd_socket(void *anything);
+static void read_from_cmd_socket(int sock_fd, int event, void *anything);
/* ================================================== */
}
/* 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;
}
CAM_Finalise(void)
{
if (sock_fdu >= 0) {
- SCH_RemoveInputFileHandler(sock_fdu);
+ SCH_RemoveFileHandler(sock_fdu);
close(sock_fdu);
unlink(CNF_GetBindCommandPath());
}
sock_fdu = -1;
if (sock_fd4 >= 0) {
- SCH_RemoveInputFileHandler(sock_fd4);
+ SCH_RemoveFileHandler(sock_fd4);
close(sock_fd4);
}
sock_fd4 = -1;
#ifdef FEAT_IPV6
if (sock_fd6 >= 0) {
- SCH_RemoveInputFileHandler(sock_fd6);
+ SCH_RemoveFileHandler(sock_fd6);
close(sock_fd6);
}
sock_fd6 = -1;
/* Read a packet and process it */
static void
-read_from_cmd_socket(void *anything)
+read_from_cmd_socket(int sock_fd, int event, void *anything)
{
CMD_Request rx_message;
CMD_Reply tx_message;
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;
socklen_t from_length;
IPAddr remote_ip;
rx_message_length = sizeof(rx_message);
from_length = sizeof(where_from);
- sock_fd = (long)anything;
status = recvfrom(sock_fd, (char *)&rx_message, rx_message_length, 0,
&where_from.sa, &from_length);
/* ================================================== */
static void
-end_resolving(void *anything)
+end_resolving(int fd, int event, void *anything)
{
struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything;
int i;
resolving_threads--;
- SCH_RemoveInputFileHandler(inst->pipe[0]);
+ SCH_RemoveFileHandler(inst->pipe[0]);
close(inst->pipe[0]);
close(inst->pipe[1]);
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);
}
/* ================================================== */
/* ================================================== */
/* Forward prototypes */
-static void read_from_socket(void *anything);
+static void read_from_socket(int sock_fd, int event, void *anything);
/* ================================================== */
}
/* 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;
}
if (sock_fd == INVALID_SOCK_FD)
return;
- SCH_RemoveInputFileHandler(sock_fd);
+ SCH_RemoveFileHandler(sock_fd);
close(sock_fd);
}
/* ================================================== */
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
to read, otherwise it will block. */
- int status, sock_fd;
+ int status;
NTP_Receive_Buffer message;
union sockaddr_in46 where_from;
unsigned int flags = 0;
msg.msg_controllen = sizeof(cmsgbuf);
msg.msg_flags = 0;
- sock_fd = (long)anything;
status = recvmsg(sock_fd, &msg, flags);
/* Don't bother checking if read failed or why if it did. More
int magic;
};
-static void read_sample(void *anything)
+static void read_sample(int sockfd, int event, void *anything)
{
struct sock_sample sample;
RCL_Instance instance;
- int sockfd, s;
+ int s;
instance = (RCL_Instance)anything;
- sockfd = (long)RCL_GetDriverData(instance);
s = recv(sockfd, &sample, sizeof (sample), 0);
}
RCL_SetDriverData(instance, (void *)(long)sockfd);
- SCH_AddInputFileHandler(sockfd, read_sample, instance);
+ SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
return 1;
}
int sockfd;
sockfd = (long)RCL_GetDriverData(instance);
- SCH_RemoveInputFileHandler(sockfd);
+ SCH_RemoveFileHandler(sockfd);
close(sockfd);
}
static void measurement_timeout(void *any);
-static void read_from_device(void *any);
+static void read_from_device(int fd_, int event, void *any);
/* ================================================== */
operating_mode = OM_NORMAL;
/* Register file handler */
- SCH_AddInputFileHandler(fd, read_from_device, NULL);
+ SCH_AddFileHandler(fd, SCH_FILE_INPUT, read_from_device, NULL);
/* Register slew handler */
LCL_AddParameterChangeHandler(slew_samples, NULL);
/* Remove input file handler */
if (fd >= 0) {
- SCH_RemoveInputFileHandler(fd);
+ SCH_RemoveFileHandler(fd);
close(fd);
/* Save the RTC data */
/* ================================================== */
static void
-read_from_device(void *any)
+read_from_device(int fd_, int event, void *any)
{
int status;
unsigned long data;
/* This looks like a bad error : the file descriptor was indicating it was
* 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));
- SCH_RemoveInputFileHandler(fd);
+ SCH_RemoveFileHandler(fd);
switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
close(fd);
fd = -1;
/* ================================================== */
void
-SCH_AddInputFileHandler
-(int fd, SCH_FileHandler handler, SCH_ArbitraryArgument arg)
+SCH_AddFileHandler
+(int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg)
{
FileHandlerEntry *ptr;
assert(initialised);
+ assert(events);
+ assert(fd >= 0);
if (fd >= FD_SETSIZE)
LOG_FATAL(LOGF_Scheduler, "Too many file descriptors");
/* ================================================== */
void
-SCH_RemoveInputFileHandler(int fd)
+SCH_RemoveFileHandler(int fd)
{
int fds_left, fd_to_check;
/* ================================================== */
+void
+SCH_SetFileHandlerEvents(int fd, int events)
+{
+ FileHandlerEntry *ptr;
+
+ assert(events);
+ ptr = ARR_GetElement(file_handlers, fd);
+ ptr->events = events;
+}
+
+/* ================================================== */
+
void
SCH_GetLastEventTime(struct timeval *cooked, double *err, struct timeval *raw)
{
/* This descriptor can be read from, dispatch its handler */
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 */
--nfh;
} SCH_TimeoutClass;
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);
/* Exported functions */
/* Finalisation function for the module */
extern void SCH_Finalise(void);
+/* File events */
+#define SCH_FILE_INPUT 1
+
/* Register a handler for when select goes true on a file descriptor */
-extern void SCH_AddInputFileHandler
-(int fd, /* The file descriptor */
- SCH_FileHandler, /* The handler routine */
- SCH_ArbitraryArgument /* An arbitrary passthrough argument to the handler */
-);
-extern void SCH_RemoveInputFileHandler(int fd);
+extern void SCH_AddFileHandler(int fd, int events, SCH_FileHandler handler, SCH_ArbitraryArgument arg);
+extern void SCH_RemoveFileHandler(int fd);
+extern void SCH_SetFileHandlerEvents(int fd, int events);
/* 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);