From 000ca4806fe83b1c58044ebf7683ef80c904df59 Mon Sep 17 00:00:00 2001 From: David Scott Date: Thu, 18 Aug 2016 11:01:02 +0100 Subject: [PATCH] tap-vsockd: support reconnection If the server side crashes and is restarted, this patch makes tap-vsockd reconnect so the network is restored. Signed-off-by: David Scott --- alpine/packages/tap-vsockd/tap-vsockd.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/alpine/packages/tap-vsockd/tap-vsockd.c b/alpine/packages/tap-vsockd/tap-vsockd.c index d1f7db502..e678c72e3 100644 --- a/alpine/packages/tap-vsockd/tap-vsockd.c +++ b/alpine/packages/tap-vsockd/tap-vsockd.c @@ -22,6 +22,7 @@ #include #include +#include #include @@ -395,6 +396,13 @@ int main(int argc, char **argv) connection.tapfd = tapfd; int sock = -1; + int lsocket = -1; + if (listen_flag) { + syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap); + lsocket = create_listening_socket(sid); + } else { + syslog(LOG_INFO, "starting in connect mode with serviceid=%s and tap=%s", serviceid, tap); + } for (;;) { if (sock != -1) { @@ -402,12 +410,8 @@ int main(int argc, char **argv) sock = -1; } if (listen_flag) { - syslog(LOG_INFO, "starting in listening mode with serviceid=%s and tap=%s", serviceid, tap); - int lsocket = create_listening_socket(sid); sock = accept_socket(lsocket); - close(lsocket); } else { - syslog(LOG_INFO, "starting in connect mode with serviceid=%s and tap=%s", serviceid, tap); sock = connect_socket(sid); } @@ -427,6 +431,15 @@ int main(int argc, char **argv) daemon_flag = 0; daemonize(pidfile); } - handle(&connection); + /* Run the multithreaded part in a subprocess. On error the process will + exit() which tears down all the threads */ + pid_t child = fork(); + if (child == 0) { + handle(&connection); + /* should never happen but just in case of a logic bug in handle */ + exit(1); + } + int status; + while (waitpid(child, &status, 0) == -1) { } } }