Let's be friendly in what we accept: whenever we define a JSON structure, let's also allow decimal strings where we want an integer. This patch purely replaces JSON_VARIANT_UNSIGNED by _JSON_VARIANT_TYPE_INVALID in the various JsonDispatch[] tables, so that we'll happily accept any type in json_dispatch(), so that json_dispatch_uint64() and related tools can do their thing. This does not switch over OCI (as a JSON structure not defined by us).
71 lines
2.2 KiB
C
71 lines
2.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "varlink-io.systemd.service.h"
|
|
|
|
static VARLINK_DEFINE_METHOD(Ping);
|
|
|
|
static VARLINK_DEFINE_METHOD(Reload);
|
|
|
|
static VARLINK_DEFINE_METHOD(
|
|
SetLogLevel,
|
|
VARLINK_DEFINE_INPUT(level, VARLINK_INT, 0));
|
|
|
|
VARLINK_DEFINE_INTERFACE(
|
|
io_systemd_service,
|
|
"io.systemd.service",
|
|
&vl_method_Ping,
|
|
&vl_method_Reload,
|
|
&vl_method_SetLogLevel);
|
|
|
|
int varlink_method_ping(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
|
|
assert(link);
|
|
|
|
if (json_variant_elements(parameters) > 0)
|
|
return varlink_error_invalid_parameter(link, parameters);
|
|
|
|
log_debug("Received io.systemd.service.Ping");
|
|
|
|
return varlink_reply(link, NULL);
|
|
}
|
|
|
|
int varlink_method_set_log_level(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
|
|
static const JsonDispatch dispatch_table[] = {
|
|
{ "level", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int64, 0, JSON_MANDATORY },
|
|
{}
|
|
};
|
|
|
|
int64_t level;
|
|
uid_t uid;
|
|
int r;
|
|
|
|
assert(link);
|
|
assert(parameters);
|
|
|
|
/* NOTE: The method does have 1 parameter, but we must compare to 2 here, because
|
|
* json_variant_elements() breaks abstraction and exposes internal structure of JsonObject. */
|
|
if (json_variant_elements(parameters) != 2)
|
|
return varlink_error_invalid_parameter(link, parameters);
|
|
|
|
r = varlink_dispatch(link, parameters, dispatch_table, &level);
|
|
if (r != 0)
|
|
return r;
|
|
|
|
if (LOG_PRI(level) != level)
|
|
return varlink_error_invalid_parameter(link, parameters);
|
|
|
|
r = varlink_get_peer_uid(link, &uid);
|
|
if (r < 0)
|
|
return r;
|
|
|
|
if (uid != getuid() && uid != 0)
|
|
return varlink_error(link, VARLINK_ERROR_PERMISSION_DENIED, parameters);
|
|
|
|
log_debug("Received io.systemd.service.SetLogLevel(%" PRIi64 ")", level);
|
|
|
|
log_set_max_level(level);
|
|
|
|
return varlink_reply(link, NULL);
|
|
}
|