From: Lennart Poettering Date: Tue, 15 Jul 2025 06:44:48 +0000 (+0200) Subject: service-util: add generic parser for runtime scope X-Git-Tag: v259-rc1~433^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7f6bdbbd3a504d83eb0a15fa58c16444753b65a;p=thirdparty%2Fsystemd.git service-util: add generic parser for runtime scope --- diff --git a/src/home/homed.c b/src/home/homed.c index bb319925d03..9aaabd5c19e 100644 --- a/src/home/homed.c +++ b/src/home/homed.c @@ -25,6 +25,7 @@ static int run(int argc, char *argv[]) { "A service to create, remove, change or inspect home areas.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 06fb3f28789..9ba3d312bd1 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -1988,6 +1988,7 @@ static int run(int argc, char *argv[]) { "Manage the system hostname and related metadata.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/import/importd.c b/src/import/importd.c index f2715691abb..8603069047a 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -2019,6 +2019,7 @@ static int run(int argc, char *argv[]) { "VM and container image import and export service.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/locale/localed.c b/src/locale/localed.c index 11d9130bbca..041ba29cd8a 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -635,6 +635,7 @@ static int run(int argc, char *argv[]) { "Manage system locale settings and key mappings.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/login/logind.c b/src/login/logind.c index 9427bae815a..e1219fe73a8 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -1340,6 +1340,7 @@ static int run(int argc, char *argv[]) { "Manager for user logins and devices and privileged operations.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/machine/machined.c b/src/machine/machined.c index 46395d7df4a..ea83f97ad96 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -324,6 +324,7 @@ static int run(int argc, char *argv[]) { "Manage registrations of local VMs and containers.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/network/networkd.c b/src/network/networkd.c index 2ce32fe4f3e..7e09629ddbe 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -29,6 +29,7 @@ static int run(int argc, char *argv[]) { r = service_parse_argv("systemd-networkd.service", "Manage and configure network devices, create virtual network devices", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/portable/portabled.c b/src/portable/portabled.c index 65ceaf40b6d..4059de01ed9 100644 --- a/src/portable/portabled.c +++ b/src/portable/portabled.c @@ -133,6 +133,7 @@ static int run(int argc, char *argv[]) { "Manage registrations of portable images.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/resolve/resolved.c b/src/resolve/resolved.c index ea3e6a689c4..aa165927a42 100644 --- a/src/resolve/resolved.c +++ b/src/resolve/resolved.c @@ -30,6 +30,7 @@ static int run(int argc, char *argv[]) { "Provide name resolution with caching using DNS, mDNS, LLMNR.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/shared/service-util.c b/src/shared/service-util.c index 2645fe2c40b..70d59af6c51 100644 --- a/src/shared/service-util.c +++ b/src/shared/service-util.c @@ -8,9 +8,19 @@ #include "bus-object.h" #include "log.h" #include "pretty-print.h" +#include "runtime-scope.h" #include "service-util.h" -static int help(const char *program_path, const char *service, const char *description, bool bus_introspect) { +typedef enum HelpFlags { + HELP_WITH_BUS_INTROSPECT = 1 << 0, + HELP_WITH_RUNTIME_SCOPE = 1 << 1, +} HelpFlags; + +static int help(const char *program_path, + const char *service, + const char *description, + HelpFlags flags) { + _cleanup_free_ char *link = NULL; int r; @@ -25,6 +35,7 @@ static int help(const char *program_path, const char *service, const char *descr " -h --help Show this help\n" " --version Show package version\n" "%8$s" + "%9$s" "\nSee the %2$s for details.\n", program_path, link, @@ -33,7 +44,9 @@ static int help(const char *program_path, const char *service, const char *descr ansi_highlight(), ansi_normal(), description, - bus_introspect ? " --bus-introspect=PATH Write D-Bus XML introspection data\n" : ""); + FLAGS_SET(flags, HELP_WITH_BUS_INTROSPECT) ? " --bus-introspect=PATH Write D-Bus XML introspection data\n" : "", + FLAGS_SET(flags, HELP_WITH_RUNTIME_SCOPE) ? " --system Start service in system mode\n" + " --user Start service in user mode\n" : ""); return 0; /* No further action */ } @@ -42,17 +55,22 @@ int service_parse_argv( const char *service, const char *description, const BusObjectImplementation* const* bus_objects, + RuntimeScope *runtime_scope, int argc, char *argv[]) { enum { ARG_VERSION = 0x100, ARG_BUS_INTROSPECT, + ARG_SYSTEM, + ARG_USER, }; static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, { "bus-introspect", required_argument, NULL, ARG_BUS_INTROSPECT }, + { "system", no_argument, NULL, ARG_SYSTEM }, + { "user", no_argument, NULL, ARG_USER }, {} }; @@ -65,7 +83,11 @@ int service_parse_argv( switch (c) { case 'h': - return help(argv[0], service, description, bus_objects); + return help(argv[0], + service, + description, + (bus_objects ? HELP_WITH_BUS_INTROSPECT : 0) | + (runtime_scope ? HELP_WITH_RUNTIME_SCOPE : 0)); case ARG_VERSION: return version(); @@ -76,6 +98,14 @@ int service_parse_argv( optarg, bus_objects); + case ARG_SYSTEM: + case ARG_USER: + if (!runtime_scope) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This service cannot be run in --system or --user mode, refusing."); + + *runtime_scope = c == ARG_SYSTEM ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER; + break; + case '?': return -EINVAL; diff --git a/src/shared/service-util.h b/src/shared/service-util.h index af36354e80e..462ba2b3fad 100644 --- a/src/shared/service-util.h +++ b/src/shared/service-util.h @@ -7,4 +7,5 @@ int service_parse_argv( const char *service, const char *description, const BusObjectImplementation* const* bus_objects, + RuntimeScope *runtime_scope, int argc, char *argv[]); diff --git a/src/sysupdate/sysupdated.c b/src/sysupdate/sysupdated.c index b1438ff825a..1ade24b3748 100644 --- a/src/sysupdate/sysupdated.c +++ b/src/sysupdate/sysupdated.c @@ -2079,6 +2079,7 @@ static int run(int argc, char *argv[]) { "System update management service.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 662cc29ac5f..6e21d1a73ab 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -1164,6 +1164,7 @@ static int run(int argc, char *argv[]) { "Manage the system clock and timezone and NTP enablement.", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r; diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index 40305c046bc..96d0dd5c2ba 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -151,6 +151,7 @@ static int run(int argc, char *argv[]) { r = service_parse_argv("systemd-timesyncd.service", "Network time synchronization", BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), + /* runtime_scope= */ NULL, argc, argv); if (r <= 0) return r;