X-Git-Url: http://git.archive.openwrt.org/?p=project%2Frpcd.git;a=blobdiff_plain;f=main.c;h=75f5cee0fa15195f243f5c3e41394389b5516f38;hp=7335780bba067f383166ab871be6150058d66cca;hb=64633f313638b8afc179a55d56fd46e22d582146;hpb=e0afdb10ae879d26d7f81d1dba8d47978a9a2aa9 diff --git a/main.c b/main.c index 7335780..75f5cee 100644 --- a/main.c +++ b/main.c @@ -22,15 +22,45 @@ #include #include #include +#include #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]; + + setenv("RPC_HANGUP", "1", 1); + execv(cmd, (char * const *)args); +} int main(int argc, char **argv) { + struct stat s; + const char *hangup; const char *ubus_socket = NULL; int ch; @@ -44,10 +74,14 @@ int main(int argc, char **argv) } } - signal(SIGPIPE, SIG_IGN); + if (stat("/var/run/rpcd", &s)) + mkdir("/var/run/rpcd", 0700); - argc -= optind; - argv += optind; + umask(0077); + + signal(SIGPIPE, SIG_IGN); + signal(SIGHUP, handle_signal); + signal(SIGUSR1, handle_signal); uloop_init(); @@ -63,9 +97,19 @@ int main(int argc, char **argv) rpc_uci_api_init(ctx); rpc_plugin_api_init(ctx); + hangup = getenv("RPC_HANGUP"); + + if (!hangup || strcmp(hangup, "1")) + rpc_uci_purge_savedirs(); + else + rpc_session_thaw(); + uloop_run(); ubus_free(ctx); uloop_done(); + if (respawn) + exec_self(argc, argv); + return 0; }