From 355040f5769e68cd397b0917240cc9f44bc6f9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Fri, 10 Feb 2017 11:54:19 +0000 Subject: [PATCH] Don't destroy tun on SIGHUP --- src/main.c | 12 +++++++++++- src/tun.c | 7 +++++++ src/tun.h | 9 +++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index c7f2264..88824c3 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ static struct { volatile sig_atomic_t quit; + volatile sig_atomic_t reload; char *dev; char *keyfile; char *host; @@ -87,6 +88,7 @@ fd_set_nonblock(int fd) static void gt_quit_handler(int sig) { + gt.reload = (sig == SIGHUP); gt.quit = 1; } @@ -103,9 +105,9 @@ gt_set_signal(void) sigaction(SIGINT, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); + sigaction(SIGHUP, &sa, NULL); sa.sa_handler = SIG_IGN; - sigaction(SIGHUP, &sa, NULL); sigaction(SIGPIPE, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); sigaction(SIGUSR2, &sa, NULL); @@ -292,6 +294,9 @@ main(int argc, char **argv) return 1; } + if (tun_set_persist(tun_fd, 0) == -1) + perror("tun_set_persist"); + struct mud *mud = mud_create(gt.bind.port, gt.ipv4, gt.ipv6, !gt.chacha20, GT_MTU(gt.mtu)); @@ -482,5 +487,10 @@ main(int argc, char **argv) } } + if (gt.reload && tun_fd >= 0) { + if (tun_set_persist(tun_fd, 1) == -1) + perror("tun_set_persist"); + } + return 0; } diff --git a/src/tun.c b/src/tun.c index 75e905b..eaceea8 100644 --- a/src/tun.c +++ b/src/tun.c @@ -17,6 +17,7 @@ #define IFF_TUN 0x0001 #define IFF_NO_PI 0x1000 #define TUNSETIFF _IOW('T', 202, int) +#define TUNSETPERSIST _IOW('T', 203, int) #endif #ifdef __APPLE__ @@ -260,3 +261,9 @@ tun_set_mtu(char *dev_name, int mtu) return ret; } + +int +tun_set_persist(int fd, int on) +{ + return ioctl(fd, TUNSETPERSIST, on); +} diff --git a/src/tun.h b/src/tun.h index f99d92a..df99d74 100644 --- a/src/tun.h +++ b/src/tun.h @@ -1,6 +1,7 @@ #pragma once -int tun_create (char *, char **); -int tun_read (int, void *, size_t); -int tun_write (int, const void *, size_t); -int tun_set_mtu (char *, int); +int tun_create (char *, char **); +int tun_read (int, void *, size_t); +int tun_write (int, const void *, size_t); +int tun_set_mtu (char *, int); +int tun_set_persist (int, int);