| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
#define VERB_ANY (UINT_MAX) |
| 7 |
|
| 8 |
typedef enum VerbFlags { |
| 9 |
VERB_DEFAULT = 1 << 0, /* The verb to run if no verb is specified */ |
| 10 |
VERB_ONLINE_ONLY = 1 << 1, /* Just do nothing when running in chroot or offline */ |
| 11 |
VERB_GROUP_MARKER = 1 << 2, /* Fake verb entry to separate groups */ |
| 12 |
} VerbFlags; |
| 13 |
|
| 14 |
/* Note: see the comment on struct Option in options.h for why _alignptr_ is required here. */ |
| 15 |
typedef struct _alignptr_ { |
| 16 |
const char *verb; |
| 17 |
unsigned min_args, max_args; |
| 18 |
VerbFlags flags; |
| 19 |
int (* const dispatch)(int argc, char *argv[], uintptr_t data, void *userdata); |
| 20 |
uintptr_t data; |
| 21 |
const char *argspec; |
| 22 |
const char *help; |
| 23 |
} Verb; |
| 24 |
assert_cc(sizeof(Verb) % sizeof(void*) == 0); |
| 25 |
|
| 26 |
#define _VERB_DATA(d, v, a, amin, amax, f, dat, h) \ |
| 27 |
_section_("SYSTEMD_VERBS") \ |
| 28 |
_alignptr_ \ |
| 29 |
_used_ \ |
| 30 |
_retain_ \ |
| 31 |
_no_reorder_ \ |
| 32 |
_variable_no_sanitize_address_ \ |
| 33 |
static const Verb CONCATENATE(verb_data_, __COUNTER__) = { \ |
| 34 |
.verb = v, \ |
| 35 |
.min_args = amin, \ |
| 36 |
.max_args = amax, \ |
| 37 |
.flags = f, \ |
| 38 |
.dispatch = d, \ |
| 39 |
.data = dat, \ |
| 40 |
.argspec = a, \ |
| 41 |
.help = h, \ |
| 42 |
} |
| 43 |
|
| 44 |
/* Forward-define function d. scope specifies the scope, e.g. static. */ |
| 45 |
#define VERB_SCOPE_FULL(scope, d, v, a, amin, amax, f, dat, h) \ |
| 46 |
DISABLE_WARNING_REDUNDANT_DECLS \ |
| 47 |
scope int d(int, char**, uintptr_t, void*); \ |
| 48 |
REENABLE_WARNING \ |
| 49 |
_VERB_DATA(d, v, a, amin, amax, f, dat, h) |
| 50 |
/* The same as VERB_SCOPE_FULL with scope hardwired to 'static'. */ |
| 51 |
#define VERB_FULL(d, v, a, amin, amax, f, dat, h) \ |
| 52 |
VERB_SCOPE_FULL(static, d, v, a, amin, amax, f, dat, h) |
| 53 |
|
| 54 |
/* The same as VERB_SCOPE_FULL/VERB_FULL, but without the data argument. */ |
| 55 |
#define VERB_SCOPE(scope, d, v, a, amin, amax, f, h) \ |
| 56 |
VERB_SCOPE_FULL(scope, d, v, a, amin, amax, f, /* dat= */ 0, h) |
| 57 |
#define VERB(d, v, a, amin, amax, f, h) \ |
| 58 |
VERB_SCOPE(static, d, v, a, amin, amax, f, h) |
| 59 |
|
| 60 |
/* Simplified VERB_SCOPE/VERB for verbs that take no argument. */ |
| 61 |
#define VERB_SCOPE_NOARG(scope, d, v, h) \ |
| 62 |
VERB_SCOPE(scope, d, v, /* a= */ NULL, /* amin= */ VERB_ANY, /* amax= */ 1, /* f= */ 0, h) |
| 63 |
#define VERB_NOARG(d, v, h) \ |
| 64 |
VERB_SCOPE_NOARG(static, d, v, h) |
| 65 |
#define VERB_DEFAULT_NOARG(d, v, h) \ |
| 66 |
VERB_SCOPE(static, d, v, /* a= */ NULL, /* amin= */ VERB_ANY, /* amax= */ 1, /* f= */ VERB_DEFAULT, h) |
| 67 |
|
| 68 |
/* Magic entry in the table (which will not be returned) that designates the start of the group <gr>. |
| 69 |
* The macro works as a separator between groups and must be between other VERB* stanzas. */ |
| 70 |
#define VERB_GROUP(gr) \ |
| 71 |
_VERB_DATA(/* d= */ NULL, /* v= */ gr, /* a= */ NULL, /* amin= */ 0, /* amax= */ 0, \ |
| 72 |
/* f= */ VERB_GROUP_MARKER, /* dat= */ 0, /* h= */ NULL) |
| 73 |
|
| 74 |
/* This is magically mapped to the beginning and end of the section */ |
| 75 |
extern const Verb __start_SYSTEMD_VERBS[]; |
| 76 |
extern const Verb __stop_SYSTEMD_VERBS[]; |
| 77 |
|
| 78 |
bool running_in_chroot_or_offline(void); |
| 79 |
|
| 80 |
bool should_bypass(const char *env_prefix); |
| 81 |
|
| 82 |
const Verb* verbs_find_verb(const char *name, const Verb verbs[], const Verb verbs_end[]); |
| 83 |
|
| 84 |
int _dispatch_verb(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata); |
| 85 |
#define dispatch_verb(args, userdata) \ |
| 86 |
_dispatch_verb(args, __start_SYSTEMD_VERBS, __stop_SYSTEMD_VERBS, userdata) |
| 87 |
|
| 88 |
int _verbs_get_help_table( |
| 89 |
const Verb verbs[], |
| 90 |
const Verb verbs_end[], |
| 91 |
const char *group, |
| 92 |
Table **ret); |
| 93 |
#define verbs_get_help_table_group(group, ret) \ |
| 94 |
_verbs_get_help_table(__start_SYSTEMD_VERBS, __stop_SYSTEMD_VERBS, group, ret) |
| 95 |
#define verbs_get_help_table(ret) \ |
| 96 |
verbs_get_help_table_group(/* group= */ NULL, ret) |
| 97 |
|
| 98 |
#define _VERB_COMMON_HELP_IMPL(impl) \ |
| 99 |
static int verb_help(int argc, char **argv, uintptr_t data, void *userdata) { \ |
| 100 |
return impl(); \ |
| 101 |
} |
| 102 |
|
| 103 |
#define VERB_COMMON_HELP(impl) \ |
| 104 |
VERB(verb_help, "help", NULL, VERB_ANY, VERB_ANY, 0, "Show this help"); \ |
| 105 |
_VERB_COMMON_HELP_IMPL(impl) |
| 106 |
|
| 107 |
#define VERB_COMMON_HELP_HIDDEN(impl) \ |
| 108 |
VERB(verb_help, "help", NULL, VERB_ANY, VERB_ANY, 0, NULL); \ |
| 109 |
_VERB_COMMON_HELP_IMPL(impl) |