Add hook to refresh config on SIGHUP.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 18 Sep 2008 16:15:37 +0000 (18:15 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 18 Sep 2008 16:15:37 +0000 (18:15 +0200)
Lock the pid file.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
common/common.c
common/server.c
common/server.h
pfix-srsd/main-srsd.c
postlicyd/main-postlicyd.c

index 3688c9d..758e869 100644 (file)
@@ -229,11 +229,20 @@ int drop_privileges(const char *user, const char *group)
 
 int pidfile_open(const char *name)
 {
+               struct flock lock;
+               p_clear(&lock, 1);
+               lock.l_type = F_WRLCK;
     if (name) {
         pidfile = fopen(name, "w");
         if (!pidfile)
             return -1;
-        fprintf(pidfile, "%d\n", getpid());
+                               if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) {
+                                               syslog(LOG_ERR, "program already started");
+                                               fclose(pidfile);
+                                               pidfile = NULL;
+                                               return -1;
+                               }
+                               fprintf(pidfile, "%d\n", getpid());
         return fflush(pidfile);
     }
     return 0;
@@ -252,10 +261,14 @@ int pidfile_refresh(void)
 
 static void pidfile_close(void)
 {
+               struct flock lock;
+               p_clear(&lock, 1);
+               lock.l_type = F_UNLCK;
     if (pidfile) {
         rewind(pidfile);
         ftruncate(fileno(pidfile), 0);
-        fclose(pidfile);
+        fcntl(fileno(pidfile), F_SETLK, &lock);
+                               fclose(pidfile);
         pidfile = NULL;
     }
 }
index 73c2888..6ea1a9a 100644 (file)
@@ -135,11 +135,18 @@ static int start_client(server_t *server, start_client_t starter,
 }
 
 int server_loop(start_client_t starter, delete_client_t deleter,
-                run_client_t runner, void* config) {
+                run_client_t runner, refresh_t refresh, void* config) {
     while (!sigint) {
         struct epoll_event evts[1024];
         int n;
 
+        if (sighup && refresh) {
+            if (!refresh(config)) {
+                syslog(LOG_ERR, "error while refreshing configuration");
+                return EXIT_FAILURE;
+            }
+        }
+
         n = epoll_select(evts, countof(evts), -1);
         if (n < 0) {
             if (errno != EAGAIN && errno != EINTR) {
index 9599b90..38289e6 100644 (file)
@@ -44,6 +44,7 @@ typedef void *(*start_listener_t)(void);
 typedef void  (*delete_client_t)(void*);
 typedef void *(*start_client_t)(server_t*);
 typedef int   (*run_client_t)(server_t*, void*);
+typedef bool   (*refresh_t)(void*);
 
 struct server_t {
     unsigned listener : 1;
@@ -57,6 +58,6 @@ struct server_t {
 int start_server(int port, start_listener_t starter, delete_client_t deleter);
 
 int server_loop(start_client_t starter, delete_client_t deleter,
-                run_client_t runner, void* config);
+                run_client_t runner, refresh_t refresh, void* config);
 
 #endif
index a8e6ce9..e7f4906 100644 (file)
@@ -321,5 +321,5 @@ int main(int argc, char *argv[])
         || start_listener(port_dec, true) < 0) {
         return EXIT_FAILURE;
     }
-    return server_loop(srsd_starter, NULL, process_srs, &config);
+    return server_loop(srsd_starter, NULL, process_srs, NULL, &config);
 }
index bc65920..5be638b 100644 (file)
@@ -284,7 +284,7 @@ int main(int argc, char *argv[])
     }
     {
         int res = server_loop(query_starter, (delete_client_t)query_delete,
-                              policy_run, config);
+                              policy_run, NULL, config);
         config_delete(&config);
         return res;
     }