uci: reset uci_ptr flags when merging options during section add
[oweals/rpcd.git] / main.c
1 /*
2  * rpcd - UBUS RPC server
3  *
4  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
5  *   Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #include <unistd.h>
21 #include <stdlib.h>
22
23 #include <libubox/blobmsg_json.h>
24 #include <libubus.h>
25 #include <signal.h>
26 #include <sys/stat.h>
27
28 #include <rpcd/session.h>
29 #include <rpcd/uci.h>
30 #include <rpcd/plugin.h>
31 #include <rpcd/exec.h>
32
33 static struct ubus_context *ctx;
34 static bool respawn = false;
35
36 int rpc_exec_timeout = RPC_EXEC_DEFAULT_TIMEOUT;
37
38 static void
39 handle_signal(int sig)
40 {
41         rpc_session_freeze();
42         uloop_cancelled = true;
43         respawn = (sig == SIGHUP);
44 }
45
46 static void
47 exec_self(int argc, char **argv)
48 {
49         int i;
50         const char *cmd;
51         char **args;
52
53         cmd = rpc_exec_lookup(argv[0]);
54         if (!cmd)
55                 return;
56
57         args = calloc(argc + 1, sizeof(char *));
58         if (!args)
59                 return;
60
61         for (i = 0; i < argc; i++)
62                 args[i] = argv[i];
63
64         setenv("RPC_HANGUP", "1", 1);
65         execv(cmd, (char * const *)args);
66 }
67
68 int main(int argc, char **argv)
69 {
70         struct stat s;
71         const char *hangup;
72         const char *ubus_socket = NULL;
73         int ch;
74
75         while ((ch = getopt(argc, argv, "s:t:")) != -1) {
76                 switch (ch) {
77                 case 's':
78                         ubus_socket = optarg;
79                         break;
80
81                 case 't':
82                         rpc_exec_timeout = 1000 * strtol(optarg, NULL, 0);
83                         break;
84
85                 default:
86                         break;
87                 }
88         }
89
90         if (rpc_exec_timeout < 1000 || rpc_exec_timeout > 600000) {
91                 fprintf(stderr, "Invalid execution timeout specified\n");
92                 return -1;
93         }
94
95         if (stat(RPC_UCI_DIR_PREFIX, &s))
96                 mkdir(RPC_UCI_DIR_PREFIX, 0700);
97
98         umask(0077);
99
100         signal(SIGPIPE, SIG_IGN);
101         signal(SIGHUP,  handle_signal);
102         signal(SIGUSR1, handle_signal);
103
104         uloop_init();
105
106         ctx = ubus_connect(ubus_socket);
107         if (!ctx) {
108                 fprintf(stderr, "Failed to connect to ubus\n");
109                 return -1;
110         }
111
112         ubus_add_uloop(ctx);
113
114         rpc_session_api_init(ctx);
115         rpc_uci_api_init(ctx);
116         rpc_plugin_api_init(ctx);
117
118         hangup = getenv("RPC_HANGUP");
119
120         if (!hangup || strcmp(hangup, "1"))
121                 rpc_uci_purge_savedirs();
122         else
123                 rpc_session_thaw();
124
125         uloop_run();
126         ubus_free(ctx);
127         uloop_done();
128
129         if (respawn)
130                 exec_self(argc, argv);
131
132         return 0;
133 }