service-util: add generic parser for runtime scope

This commit is contained in:
Lennart Poettering
2025-09-25 22:43:59 +02:00
parent 0011ecd6fa
commit b7f6bdbbd3
14 changed files with 46 additions and 3 deletions
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+33 -3
View File
@@ -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;
+1
View File
@@ -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[]);
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;