From 2069b8eeaeda8aec13ce4cd1e7dd5c3d66936630 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 22 Dec 2024 01:29:56 +0900 Subject: [PATCH 1/3] varlink: rename io.systemd.service -> io.systemd.Service --- src/shared/meson.build | 2 +- ....systemd.service.c => varlink-io.systemd.Service.c} | 10 +++++----- ....systemd.service.h => varlink-io.systemd.Service.h} | 2 +- src/test/test-varlink-idl.c | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) rename src/shared/{varlink-io.systemd.service.c => varlink-io.systemd.Service.c} (90%) rename src/shared/{varlink-io.systemd.service.h => varlink-io.systemd.Service.h} (85%) diff --git a/src/shared/meson.build b/src/shared/meson.build index af9ef74b329..f78b90d1a25 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -192,9 +192,9 @@ shared_sources = files( 'varlink-io.systemd.PCRLock.c', 'varlink-io.systemd.Resolve.c', 'varlink-io.systemd.Resolve.Monitor.c', + 'varlink-io.systemd.Service.c', 'varlink-io.systemd.UserDatabase.c', 'varlink-io.systemd.oom.c', - 'varlink-io.systemd.service.c', 'varlink-io.systemd.sysext.c', 'varlink-serialize.c', 'verb-log-control.c', diff --git a/src/shared/varlink-io.systemd.service.c b/src/shared/varlink-io.systemd.Service.c similarity index 90% rename from src/shared/varlink-io.systemd.service.c rename to src/shared/varlink-io.systemd.Service.c index 666778bd41c..ccb544252a9 100644 --- a/src/shared/varlink-io.systemd.service.c +++ b/src/shared/varlink-io.systemd.Service.c @@ -3,7 +3,7 @@ #include #include "macro.h" -#include "varlink-io.systemd.service.h" +#include "varlink-io.systemd.Service.h" static SD_VARLINK_DEFINE_METHOD(Ping); @@ -14,8 +14,8 @@ static SD_VARLINK_DEFINE_METHOD( SD_VARLINK_DEFINE_INPUT(level, SD_VARLINK_INT, 0)); SD_VARLINK_DEFINE_INTERFACE( - io_systemd_service, - "io.systemd.service", + io_systemd_Service, + "io.systemd.Service", &vl_method_Ping, &vl_method_Reload, &vl_method_SetLogLevel); @@ -26,7 +26,7 @@ int varlink_method_ping(sd_varlink *link, sd_json_variant *parameters, sd_varlin if (sd_json_variant_elements(parameters) > 0) return sd_varlink_error_invalid_parameter(link, parameters); - log_debug("Received io.systemd.service.Ping"); + log_debug("Received io.systemd.Service.Ping"); return sd_varlink_reply(link, NULL); } @@ -63,7 +63,7 @@ int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, if (uid != getuid() && uid != 0) return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, parameters); - log_debug("Received io.systemd.service.SetLogLevel(%" PRIi64 ")", level); + log_debug("Received io.systemd.Service.SetLogLevel(%" PRIi64 ")", level); log_set_max_level(level); diff --git a/src/shared/varlink-io.systemd.service.h b/src/shared/varlink-io.systemd.Service.h similarity index 85% rename from src/shared/varlink-io.systemd.service.h rename to src/shared/varlink-io.systemd.Service.h index 3f164783f59..c8840549731 100644 --- a/src/shared/varlink-io.systemd.service.h +++ b/src/shared/varlink-io.systemd.Service.h @@ -4,7 +4,7 @@ #include "sd-varlink.h" #include "sd-varlink-idl.h" -extern const sd_varlink_interface vl_interface_io_systemd_service; +extern const sd_varlink_interface vl_interface_io_systemd_Service; int varlink_method_ping(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); diff --git a/src/test/test-varlink-idl.c b/src/test/test-varlink-idl.c index 182d59bd206..481a1558ab4 100644 --- a/src/test/test-varlink-idl.c +++ b/src/test/test-varlink-idl.c @@ -24,9 +24,9 @@ #include "varlink-io.systemd.PCRLock.h" #include "varlink-io.systemd.Resolve.h" #include "varlink-io.systemd.Resolve.Monitor.h" +#include "varlink-io.systemd.Service.h" #include "varlink-io.systemd.UserDatabase.h" #include "varlink-io.systemd.oom.h" -#include "varlink-io.systemd.service.h" #include "varlink-io.systemd.sysext.h" #include "varlink-org.varlink.service.h" #include "varlink-util.h" @@ -180,7 +180,7 @@ TEST(parse_format) { print_separator(); test_parse_format_one(&vl_interface_io_systemd_PCRLock); print_separator(); - test_parse_format_one(&vl_interface_io_systemd_service); + test_parse_format_one(&vl_interface_io_systemd_Service); print_separator(); test_parse_format_one(&vl_interface_io_systemd_sysext); print_separator(); From 7647b8b2c215c3f6876c10c1bebad12d3e4cda46 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 22 Dec 2024 01:23:31 +0900 Subject: [PATCH 2/3] varlink: split out varlink_dispatch_set_log_level() No functional change, preparation for later commits. --- src/shared/varlink-io.systemd.Service.c | 26 ++++++++++++++++++------- src/shared/varlink-io.systemd.Service.h | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/shared/varlink-io.systemd.Service.c b/src/shared/varlink-io.systemd.Service.c index ccb544252a9..7bd2970b97b 100644 --- a/src/shared/varlink-io.systemd.Service.c +++ b/src/shared/varlink-io.systemd.Service.c @@ -31,18 +31,16 @@ int varlink_method_ping(sd_varlink *link, sd_json_variant *parameters, sd_varlin return sd_varlink_reply(link, NULL); } -int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { +int varlink_dispatch_set_log_level(sd_varlink *link, sd_json_variant *parameters, int *ret_log_level) { static const sd_json_dispatch_field dispatch_table[] = { - { "level", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int64, 0, SD_JSON_MANDATORY }, + { "level", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, 0, SD_JSON_MANDATORY }, {} }; - int64_t level; - uid_t uid; - int r; + int r, level; assert(link); - assert(parameters); + assert(ret_log_level); /* NOTE: The method does have 1 parameter, but we must compare to 2 here, because * sd_json_variant_elements() breaks abstraction and exposes internal structure of JsonObject. */ @@ -56,6 +54,20 @@ int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, if (LOG_PRI(level) != level) return sd_varlink_error_invalid_parameter(link, parameters); + *ret_log_level = level; + return 0; +} + +int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { + int r, level; + uid_t uid; + + assert(link); + + r = varlink_dispatch_set_log_level(link, parameters, &level); + if (r < 0) + return r; + r = sd_varlink_get_peer_uid(link, &uid); if (r < 0) return r; @@ -63,7 +75,7 @@ int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, if (uid != getuid() && uid != 0) return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, parameters); - log_debug("Received io.systemd.Service.SetLogLevel(%" PRIi64 ")", level); + log_debug("Received io.systemd.Service.SetLogLevel(%i)", level); log_set_max_level(level); diff --git a/src/shared/varlink-io.systemd.Service.h b/src/shared/varlink-io.systemd.Service.h index c8840549731..b38781d08b8 100644 --- a/src/shared/varlink-io.systemd.Service.h +++ b/src/shared/varlink-io.systemd.Service.h @@ -7,4 +7,5 @@ extern const sd_varlink_interface vl_interface_io_systemd_Service; int varlink_method_ping(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); +int varlink_dispatch_set_log_level(sd_varlink *link, sd_json_variant *parameters, int *ret_log_level); int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); From 275f7479243ae7607696504095a325bb2b35e53d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 22 Dec 2024 01:34:04 +0900 Subject: [PATCH 3/3] varlink: invert uid check to reduce call of getuid() --- src/shared/varlink-io.systemd.Service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/varlink-io.systemd.Service.c b/src/shared/varlink-io.systemd.Service.c index 7bd2970b97b..b8d7a0b4e87 100644 --- a/src/shared/varlink-io.systemd.Service.c +++ b/src/shared/varlink-io.systemd.Service.c @@ -72,7 +72,7 @@ int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, if (r < 0) return r; - if (uid != getuid() && uid != 0) + if (uid != 0 && uid != getuid()) return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, parameters); log_debug("Received io.systemd.Service.SetLogLevel(%i)", level);