]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
guardianctrl: Add command "reload".
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 20 Oct 2014 19:03:48 +0000 (21:03 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 21 Oct 2014 19:26:59 +0000 (21:26 +0200)
This command is used to send a SIGHUP to the guardian process, to perform
a reload of the configuration.

src/misc-progs/guardianctrl.c

index cde38f7da3c709a55db9dab722ee32b0b645e011..e71c46b2dcaba084cbf0005fd805c0ad4870cee0 100644 (file)
@@ -5,6 +5,8 @@
  *
  */
 
+#include <errno.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -16,6 +18,7 @@
 #include "netutil.h"
 
 const char *chain = "GUARDIAN";
+const char *pidfile = "/run/guardian.pid";
 
 int main(int argc, char *argv[]) {
        char cmd[STRING_SIZE];
@@ -24,7 +27,7 @@ int main(int argc, char *argv[]) {
                 exit(1);
 
         if (argc < 2) {
-                fprintf(stderr, "\nNo argument given.\n\nguardianctrl (start|stop|restart|get-chain|flush-chain|block|unblock)\n\n");
+                fprintf(stderr, "\nNo argument given.\n\nguardianctrl (start|stop|restart|reload|get-chain|flush-chain|block|unblock)\n\n");
                 exit(1);
         }
        if (strcmp(argv[1], "start") == 0) {
@@ -36,6 +39,9 @@ int main(int argc, char *argv[]) {
        } else if (strcmp(argv[1], "restart") == 0) {
                safe_system("/etc/rc.d/init.d/guardian restart");
 
+       } else if (strcmp(argv[1], "reload") == 0) {
+               reloadDaemon();
+
        } else if (strcmp(argv[1], "get-chain") == 0) {
                snprintf(cmd, sizeof(cmd), "/sbin/iptables -n -v -L %s", chain);
                 safe_system(cmd);
@@ -73,9 +79,43 @@ int main(int argc, char *argv[]) {
                        exit(1);
                }
         } else {
-                fprintf(stderr, "\nBad argument given.\n\nguardianctrl (start|stop|restart|get-chain|flush-chain|block|unblock)\n\n");
+                fprintf(stderr, "\nBad argument given.\n\nguardianctrl (start|stop|restart|reload|get-chain|flush-chain|block|unblock)\n\n");
                 exit(1);
         }
 
         return 0;
 }
+
+/* Function to perfom a reload of guardian, by sending a SIGHUP signal to the process.
+ * The process id directly will be read from the defined pidfile. */
+void reloadDaemon(void) {
+       FILE *file = NULL;
+
+       // Open the pidfile.
+       file = fopen(pidfile, "r");
+
+       // Exit if the file could not opened.
+       if (file == NULL) {
+               fprintf(stderr, "Could not open %s for reading.\n", pidfile);
+               exit(1);
+       }
+
+       int pid = 0;
+
+       // Read the process id from the file.
+       if(fscanf(file, "%d", &pid) <= 0) {
+               fprintf(stderr, "Invalid data from pidfile (%s).\n", pidfile);
+               exit(1);
+       }
+
+       // Close the pidfile.
+       fclose(file);
+
+       // Send a SIGHUP to the process.
+       if(kill(pid, SIGHUP) != 0) {
+               fprintf(stderr, "Could not execute kill(): %s\n", strerror(errno));
+               exit(1);
+       }
+
+       return 0;
+}