From: Jo-Philipp Wich Date: Wed, 4 Sep 2013 15:09:51 +0000 (+0200) Subject: main: store session data to disk on receipt of SIGUSR1 or SIGHUP. On HUP terminate... X-Git-Url: http://git.archive.openwrt.org/?p=project%2Frpcd.git;a=commitdiff_plain;h=524032c291fcb4d8e4b160f4c9c4ec8b5de92db3 main: store session data to disk on receipt of SIGUSR1 or SIGHUP. On HUP terminate self and re-exec --- diff --git a/exec.c b/exec.c index a5f6561..3d2ec71 100644 --- a/exec.c +++ b/exec.c @@ -50,7 +50,7 @@ rpc_errno_status(void) } } -static const char * +const char * rpc_exec_lookup(const char *cmd) { struct stat s; diff --git a/include/rpcd/exec.h b/include/rpcd/exec.h index 05ba3ca..dca1bad 100644 --- a/include/rpcd/exec.h +++ b/include/rpcd/exec.h @@ -78,6 +78,8 @@ struct rpc_exec_context { rpc_exec_done_cb_t finish_cb; }; +const char *rpc_exec_lookup(const char *cmd); + int rpc_exec(const char **args, rpc_exec_write_cb_t in, rpc_exec_read_cb_t out, rpc_exec_read_cb_t err, rpc_exec_done_cb_t end, void *priv, struct ubus_context *ctx, diff --git a/main.c b/main.c index 7335780..9bebd3e 100644 --- a/main.c +++ b/main.c @@ -26,8 +26,34 @@ #include #include #include +#include static struct ubus_context *ctx; +static bool respawn = false; + +static void +handle_signal(int sig) +{ + rpc_session_freeze(); + uloop_cancelled = true; + respawn = (sig == SIGHUP); +} + +static void +exec_self(int argc, char **argv) +{ + int i; + const char *cmd = rpc_exec_lookup(argv[0]); + char **args = calloc(argc + 1, sizeof(char *)); + + if (!cmd || !args) + return; + + for (i = 0; i < argc; i++) + args[i] = argv[i]; + + execv(cmd, (char * const *)args); +} int main(int argc, char **argv) { @@ -45,9 +71,8 @@ int main(int argc, char **argv) } signal(SIGPIPE, SIG_IGN); - - argc -= optind; - argv += optind; + signal(SIGHUP, handle_signal); + signal(SIGUSR1, handle_signal); uloop_init(); @@ -63,9 +88,14 @@ int main(int argc, char **argv) rpc_uci_api_init(ctx); rpc_plugin_api_init(ctx); + rpc_session_thaw(); + uloop_run(); ubus_free(ctx); uloop_done(); + if (respawn) + exec_self(argc, argv); + return 0; }