]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sched: rework file handling API
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 16 Jun 2016 14:47:40 +0000 (16:47 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 23 Jun 2016 09:33:54 +0000 (11:33 +0200)
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.

cmdmon.c
nameserv_async.c
ntp_io.c
refclock_sock.c
rtc_linux.c
sched.c
sched.h

index 26622d2887e7ad517c848c63d44b3ae6cd2f6e0b..fde8154585e7c75690c50dc1635c7f0aa617aad9 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -143,7 +143,7 @@ static ADF_AuthTable access_auth_table;
 
 /* ================================================== */
 /* 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 */
-  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;
 }
@@ -325,19 +325,19 @@ void
 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;
@@ -1189,12 +1189,12 @@ handle_server_stats(CMD_Request *rx_message, CMD_Reply *tx_message)
 /* 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;
@@ -1204,7 +1204,6 @@ read_from_cmd_socket(void *anything)
   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);
 
index 0175cc259e669b5bee55ff9d9939ec3c377d558f..e9932353e09c3297d03eff443ead21551b363eaf 100644 (file)
@@ -72,7 +72,7 @@ start_resolving(void *anything)
 /* ================================================== */
 
 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;
@@ -83,7 +83,7 @@ end_resolving(void *anything)
 
   resolving_threads--;
 
-  SCH_RemoveInputFileHandler(inst->pipe[0]);
+  SCH_RemoveFileHandler(inst->pipe[0]);
   close(inst->pipe[0]);
   close(inst->pipe[1]);
 
@@ -120,7 +120,7 @@ DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *
     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);
 }
 
 /* ================================================== */
index cf087deb224e93c11b6b41c85f1512dd3075047c..5efc2a2f1259d4c5013fbe2ca1acee2fe5b8ba5e 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -80,7 +80,7 @@ static int initialised=0;
 /* ================================================== */
 
 /* 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 */
-  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;
 }
@@ -282,7 +282,7 @@ close_socket(int sock_fd)
   if (sock_fd == INVALID_SOCK_FD)
     return;
 
-  SCH_RemoveInputFileHandler(sock_fd);
+  SCH_RemoveFileHandler(sock_fd);
   close(sock_fd);
 }
 
@@ -482,12 +482,12 @@ NIO_IsServerSocket(int 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;
@@ -514,7 +514,6 @@ read_from_socket(void *anything)
   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
index 8ab4d527d29920d3814076de93b245532c68058a..f42c6c8fba73076b323a3b92996276dfce820aa4 100644 (file)
@@ -57,14 +57,13 @@ struct sock_sample {
   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);
 
@@ -122,7 +121,7 @@ static int sock_initialise(RCL_Instance instance)
   }
 
   RCL_SetDriverData(instance, (void *)(long)sockfd);
-  SCH_AddInputFileHandler(sockfd, read_sample, instance);
+  SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
   return 1;
 }
 
@@ -131,7 +130,7 @@ static void sock_finalise(RCL_Instance instance)
   int sockfd;
 
   sockfd = (long)RCL_GetDriverData(instance);
-  SCH_RemoveInputFileHandler(sockfd);
+  SCH_RemoveFileHandler(sockfd);
   close(sockfd);
 }
 
index ee915d3acf6c6b9be8bdb18b6fb90de03d242f77..27094820f72043fd9af41828b4130b3b46a31461 100644 (file)
@@ -50,7 +50,7 @@
 
 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;
 
   /* 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);
@@ -585,7 +585,7 @@ RTC_Linux_Finalise(void)
 
   /* Remove input file handler */
   if (fd >= 0) {
-    SCH_RemoveInputFileHandler(fd);
+    SCH_RemoveFileHandler(fd);
     close(fd);
 
     /* Save the RTC data */
@@ -805,7 +805,7 @@ process_reading(time_t rtc_time, struct timeval *system_time)
 /* ================================================== */
 
 static void
-read_from_device(void *any)
+read_from_device(int fd_, int event, void *any)
 {
   int status;
   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
      * 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;
diff --git a/sched.c b/sched.c
index bab926aaef66ef944ee72bd2071a91d43b8efdb8..5879d10008a1ddc7d9f7b73854bee2503eb93c37 100644 (file)
--- a/sched.c
+++ b/sched.c
@@ -166,12 +166,14 @@ SCH_Finalise(void) {
 /* ================================================== */
 
 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");
@@ -202,7 +204,7 @@ SCH_AddInputFileHandler
 /* ================================================== */
 
 void
-SCH_RemoveInputFileHandler(int fd)
+SCH_RemoveFileHandler(int fd)
 {
   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
 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 */
       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;
diff --git a/sched.h b/sched.h
index 6d297d30ec3f354f04802beb73cb8159c5a99bf7..e519d38e18db8df10f62f2760bbe9d48de4dad3b 100644 (file)
--- a/sched.h
+++ b/sched.h
@@ -40,7 +40,7 @@ typedef enum {
 } 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 */
@@ -51,13 +51,13 @@ extern void SCH_Initialise(void);
 /* 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);