autofs: register SIGTERM for gracefull exit
authorHans Dedecker <dedeckeh@gmail.com>
Wed, 18 Oct 2017 08:00:15 +0000 (10:00 +0200)
committerHans Dedecker <dedeckeh@gmail.com>
Wed, 18 Oct 2017 10:01:51 +0000 (12:01 +0200)
Register SIGTERM to gracefully terminate mountd.
At the same time don't handle the exit in signal handler context but
let the main bail out.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Signed-off-by: Rutger Lejeune <rutger-lejeune@hotmail.com>
autofs.c
include/signal.h
signal.c

index 8c69cbea5c763fe68e6f18364fc4a7e43aac2d6b..a898f499bef8cabc098fc01861af90d8b3ca402e 100644 (file)
--- a/autofs.c
+++ b/autofs.c
@@ -32,6 +32,7 @@
 
 static int fdin = 0; /* data coming out of the kernel */
 static int fdout = 0;/* data going into the kernel */
+static bool term = false;
 static dev_t dev;
 
 static time_t uci_timeout;
@@ -147,7 +148,7 @@ static int autofs_in(union autofs_v5_packet_union *pkt)
        fds[0].fd = fdout;
        fds[0].events = POLLIN;
 
-       while(1)
+       while(!term)
        {
                res = poll(fds, 1, -1);
 
@@ -163,6 +164,7 @@ static int autofs_in(union autofs_v5_packet_union *pkt)
                        return fullread(pkt, sizeof(*pkt));
                }
        }
+       return 1;
 }
 
 pid_t autofs_safe_fork(void)
@@ -171,24 +173,29 @@ pid_t autofs_safe_fork(void)
        if(!pid)
        {
                close(fdin);
-           close(fdout);
+               close(fdout);
        }
        return pid;
 }
 
-static void autofs_cleanup_handler(void)
+static void autofs_cleanup(void)
 {
        close(fdin);
        close(fdout);
        umount_autofs();
 }
 
+static void autofs_end_handler(int sig)
+{
+       term = true;
+}
+
 static void autofs_init(void)
 {
        int kproto_version;
        char *p;
        struct uci_context *ctx;
-       signal_init(autofs_cleanup_handler);
+       signal_init(autofs_end_handler);
        ctx = ucix_init("mountd");
        uci_timeout = ucix_get_option_int(ctx, "mountd", "mountd", "timeout", 60);
        p = ucix_get_option(ctx, "mountd", "mountd", "path");
@@ -226,7 +233,7 @@ int autofs_loop(void)
 {
        chdir("/");
        autofs_init();
-       while(1)
+       while(!term)
        {
                union autofs_v5_packet_union pkt;
                if(autofs_in(&pkt))
@@ -240,7 +247,7 @@ int autofs_loop(void)
                        log_printf("unknown packet type %d\n", pkt.hdr.type);
                poll(0, 0, 200);
        }
-       umount_autofs();
+       autofs_cleanup();
        log_printf("... quitting\n");
        closelog();
        return 0;
index 6ab31306a276cf5c2960e9b67c287927b686349e..b92036087ffbe28991cfea3728e4f549fb01cc2c 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef _SIGNAL_H__
 #define _SIGNAL_H__
 
-void signal_init(void (*_crtlc_cb)(void));
+void signal_init(void (*_crtlc_cb)(int));
 
 int signal_usr1(void);
 int signal_usr2(void);
index 6ab77da499765e449c5fbb2c405f82912b9c7697..10656418979b85c31fce45d01b82c45b762e23e1 100644 (file)
--- a/signal.c
+++ b/signal.c
@@ -7,21 +7,10 @@
 #include "include/led.h"
 #include "include/signal.h"
 
-static void (*crtlc_cb)(void) = 0;
-
-static void handlerINT(int s)
-{
-       log_printf("caught sig int ... cleaning up\n");
-       if(crtlc_cb)
-               crtlc_cb();
-       exit(0);
-}
-
-void signal_init(void (*_crtlc_cb)(void))
+void signal_init(void (*_crtlc_cb)(int))
 {
        struct sigaction s;
-       crtlc_cb = _crtlc_cb;
-       s.sa_handler = handlerINT;
+       s.sa_handler = _crtlc_cb;
        s.sa_flags = 0;
-       sigaction(SIGINT, &s, NULL);
+       sigaction(SIGTERM, &s, NULL);
 }