2013-01-26 15:29:09 +01:00
|
|
|
/*
|
2013-09-02 17:09:57 +02:00
|
|
|
* rpcd - UBUS RPC server
|
2013-01-26 15:29:09 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
|
2014-01-12 13:48:58 +01:00
|
|
|
* Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
|
2013-01-26 15:29:09 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2018-11-22 14:04:45 +01:00
|
|
|
#include <stdlib.h>
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
#include <libubox/blobmsg_json.h>
|
|
|
|
#include <libubus.h>
|
2013-01-31 22:19:22 +01:00
|
|
|
#include <signal.h>
|
2013-09-05 15:14:00 +02:00
|
|
|
#include <sys/stat.h>
|
2013-01-26 15:29:09 +01:00
|
|
|
|
rc: new ubus object for handling /etc/init.d/ scripts
This commit adds "rc" ubus object with methods "list" and "exec" for
listing and calling init.d script appropriately. It's useful for all
kind of UIs (e.g. LuCI) and custom apps.
Example:
root@OpenWrt:~# ubus call rc list
{
"blockd": {
"start": 80,
"enabled": true,
"running": true
},
"dnsmasq": {
"start": 19,
"enabled": true,
"running": true
}
}
root@OpenWrt:~# ubus call rc init '{ "name": "blockd", "action": "disable" }'
root@OpenWrt:~# ubus call rc init '{ "name": "dnsmasq", "action": "stop" }'
root@OpenWrt:~# ubus call rc list
{
"blockd": {
"start": 80,
"enabled": false,
"running": true
},
"dnsmasq": {
"start": 19,
"enabled": true,
"running": false
}
}
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: Jo-Philipp Wich <jo@mein.io>
2020-06-23 20:46:19 +02:00
|
|
|
#include <rpcd/plugin.h>
|
2013-01-26 15:29:09 +01:00
|
|
|
static struct ubus_context *ctx;
|
2013-09-04 17:09:51 +02:00
|
|
|
static bool respawn = false;
|
|
|
|
|
2018-11-28 12:07:58 +01:00
|
|
|
int rpc_exec_timeout = RPC_EXEC_DEFAULT_TIMEOUT;
|
2018-11-22 14:04:45 +01:00
|
|
|
|
2013-09-04 17:09:51 +02:00
|
|
|
static void
|
|
|
|
handle_signal(int sig)
|
|
|
|
{
|
|
|
|
uloop_cancelled = true;
|
|
|
|
respawn = (sig == SIGHUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exec_self(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
2019-10-21 14:59:24 +02:00
|
|
|
const char *cmd;
|
|
|
|
char **args;
|
2013-09-04 17:09:51 +02:00
|
|
|
|
2019-10-21 14:59:24 +02:00
|
|
|
cmd = rpc_exec_lookup(argv[0]);
|
|
|
|
if (!cmd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
args = calloc(argc + 1, sizeof(char *));
|
|
|
|
if (!args)
|
2013-09-04 17:09:51 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
args[i] = argv[i];
|
|
|
|
|
2013-09-05 14:26:14 +02:00
|
|
|
setenv("RPC_HANGUP", "1", 1);
|
2013-09-04 17:09:51 +02:00
|
|
|
execv(cmd, (char * const *)args);
|
|
|
|
}
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-09-05 15:14:00 +02:00
|
|
|
struct stat s;
|
2013-09-05 14:26:14 +02:00
|
|
|
const char *hangup;
|
2013-01-26 15:29:09 +01:00
|
|
|
const char *ubus_socket = NULL;
|
|
|
|
int ch;
|
|
|
|
|
2018-11-22 14:04:45 +01:00
|
|
|
while ((ch = getopt(argc, argv, "s:t:")) != -1) {
|
2013-01-26 15:29:09 +01:00
|
|
|
switch (ch) {
|
|
|
|
case 's':
|
|
|
|
ubus_socket = optarg;
|
|
|
|
break;
|
2018-11-22 14:04:45 +01:00
|
|
|
|
|
|
|
case 't':
|
2018-11-28 12:07:58 +01:00
|
|
|
rpc_exec_timeout = 1000 * strtol(optarg, NULL, 0);
|
2018-11-22 14:04:45 +01:00
|
|
|
break;
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 12:07:58 +01:00
|
|
|
if (rpc_exec_timeout < 1000 || rpc_exec_timeout > 600000) {
|
2018-11-22 14:04:45 +01:00
|
|
|
fprintf(stderr, "Invalid execution timeout specified\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:14:30 +02:00
|
|
|
umask(0077);
|
|
|
|
|
2013-01-31 22:19:22 +01:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2013-09-04 17:09:51 +02:00
|
|
|
signal(SIGHUP, handle_signal);
|
|
|
|
signal(SIGUSR1, handle_signal);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
uloop_init();
|
|
|
|
|
|
|
|
ctx = ubus_connect(ubus_socket);
|
|
|
|
if (!ctx) {
|
|
|
|
fprintf(stderr, "Failed to connect to ubus\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ubus_add_uloop(ctx);
|
|
|
|
|
2013-08-30 18:31:15 +02:00
|
|
|
rpc_plugin_api_init(ctx);
|
2013-09-05 14:26:14 +02:00
|
|
|
hangup = getenv("RPC_HANGUP");
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
uloop_run();
|
|
|
|
ubus_free(ctx);
|
|
|
|
uloop_done();
|
|
|
|
|
2013-09-04 17:09:51 +02:00
|
|
|
if (respawn)
|
|
|
|
exec_self(argc, argv);
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|