X-Git-Url: http://git.archive.openwrt.org/?p=project%2Frpcd.git;a=blobdiff_plain;f=main.c;h=3b6841baca861bd4772feb9f825fe54231098f26;hp=efaa114aded0f8aa6d9fab6852f56fc5fd7316a5;hb=ec21f653b936b54e4d788d77bfce18634255adb7;hpb=07c2f0a9b1c7f79e81fa8a51cca5ecbe9eaf7293 diff --git a/main.c b/main.c index efaa114..3b6841b 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,7 @@ * rpcd - UBUS RPC server * * Copyright (C) 2013 Felix Fietkau - * Copyright (C) 2013 Jo-Philipp Wich + * Copyright (C) 2013-2014 Jo-Philipp Wich * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,15 +22,45 @@ #include #include #include +#include -#include "session.h" -#include "uci.h" -#include "plugin.h" +#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; }