]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin.c
analyze: fix typo
[thirdparty/systemd.git] / src / udev / udev-builtin.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d7867b31 2
07630cea 3#include <getopt.h>
d7867b31 4#include <stdio.h>
d7867b31 5
e548ca38 6#include "alloc-util.h"
1e4e5572 7#include "bitfield.h"
0c9c0634 8#include "device-private.h"
63918f92 9#include "device-util.h"
6553db60 10#include "extract-word.h"
07630cea 11#include "string-util.h"
0c9c0634 12#include "strv.h"
07a26e42 13#include "udev-builtin.h"
d7867b31 14
25de7aa7 15static const UdevBuiltin *const builtins[_UDEV_BUILTIN_MAX] = {
349cc4a5 16#if HAVE_BLKID
f1ad44d6 17 [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
f553b3b1 18#endif
f1ad44d6 19 [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs,
a8b2302b 20 [UDEV_BUILTIN_DISSECT_IMAGE] = &udev_builtin_dissect_image,
9f0c830b 21 [UDEV_BUILTIN_FACTORY_RESET] = &udev_builtin_factory_reset,
f1ad44d6
YW
22 [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb,
23 [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
24 [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard,
349cc4a5 25#if HAVE_KMOD
f1ad44d6 26 [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
e3043162 27#endif
f1ad44d6
YW
28 [UDEV_BUILTIN_NET_DRIVER] = &udev_builtin_net_driver,
29 [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id,
30 [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link,
31 [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
349cc4a5 32#if HAVE_ACL
f1ad44d6 33 [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
83cd6b75 34#endif
f1ad44d6 35 [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
d7867b31
KS
36};
37
2024ed61 38void udev_builtin_init(void) {
97511532
YW
39 FOREACH_ELEMENT(b, builtins)
40 if (*b && (*b)->init)
41 (*b)->init();
aa29418a
KS
42}
43
2024ed61 44void udev_builtin_exit(void) {
97511532
YW
45 FOREACH_ELEMENT(b, builtins)
46 if (*b && (*b)->exit)
47 (*b)->exit();
aa29418a
KS
48}
49
0f72af53
YW
50UdevReloadFlags udev_builtin_should_reload(void) {
51 UdevReloadFlags flags = 0;
52
23ebdf4f 53 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
f9b3b990 54 if (builtins[i] && builtins[i]->should_reload && builtins[i]->should_reload())
0f72af53
YW
55 flags |= 1u << i;
56
57 if (flags != 0)
58 flags |= UDEV_RELOAD_KILL_WORKERS;
59
60 return flags;
61}
62
63void udev_builtin_reload(UdevReloadFlags flags) {
64 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++) {
1e4e5572 65 if (!BIT_SET(flags, i) || !builtins[i])
0f72af53
YW
66 continue;
67 if (builtins[i]->exit)
68 builtins[i]->exit();
69 if (builtins[i]->init)
70 builtins[i]->init();
71 }
4f1795cc
KS
72}
73
2024ed61 74void udev_builtin_list(void) {
97511532
YW
75 FOREACH_ELEMENT(b, builtins)
76 if (*b)
77 fprintf(stderr, " %-14s %s\n", (*b)->name, (*b)->help);
d7867b31
KS
78}
79
bfd5a068 80const char* udev_builtin_name(UdevBuiltinCommand cmd) {
c45b369d
YW
81 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
82
f89d10ae
DM
83 if (!builtins[cmd])
84 return NULL;
85
912541b0 86 return builtins[cmd]->name;
d7867b31
KS
87}
88
25de7aa7 89bool udev_builtin_run_once(UdevBuiltinCommand cmd) {
c45b369d
YW
90 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
91
f89d10ae 92 if (!builtins[cmd])
4e18de3d 93 return false;
f89d10ae 94
912541b0 95 return builtins[cmd]->run_once;
81dadce5
KS
96}
97
25de7aa7 98UdevBuiltinCommand udev_builtin_lookup(const char *command) {
9b917abe 99 size_t n;
912541b0 100
9b917abe
YW
101 assert(command);
102
103 command += strspn(command, WHITESPACE);
104 n = strcspn(command, WHITESPACE);
23ebdf4f 105 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
9b917abe 106 if (builtins[i] && strneq(builtins[i]->name, command, n))
912541b0 107 return i;
c45b369d
YW
108
109 return _UDEV_BUILTIN_INVALID;
d7867b31
KS
110}
111
089bef66 112int udev_builtin_run(UdevEvent *event, UdevBuiltinCommand cmd, const char *command) {
6d93eeb7 113 _cleanup_strv_free_ char **argv = NULL;
0645b83a 114 int r;
912541b0 115
5668f3a7
YW
116 assert(event);
117 assert(event->dev);
c45b369d
YW
118 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
119 assert(command);
120
f89d10ae
DM
121 if (!builtins[cmd])
122 return -EOPNOTSUPP;
123
c799c93c 124 r = strv_split_full(&argv, command, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_RETAIN_ESCAPE);
0645b83a
ZJS
125 if (r < 0)
126 return r;
6d93eeb7 127
e5f2783e
KS
128 /* we need '0' here to reset the internal state */
129 optind = 0;
089bef66 130 return builtins[cmd]->cmd(event, strv_length(argv), argv);
d7867b31
KS
131}
132
036c75de
YW
133int udev_builtin_add_property(UdevEvent *event, const char *key, const char *val) {
134 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
0c9c0634
YW
135 int r;
136
c45b369d
YW
137 assert(key);
138
0c9c0634
YW
139 r = device_add_property(dev, key, val);
140 if (r < 0)
63918f92
YW
141 return log_device_debug_errno(dev, r, "Failed to add property '%s%s%s'",
142 key, val ? "=" : "", strempty(val));
d7867b31 143
036c75de 144 if (event->event_mode == EVENT_UDEVADM_TEST_BUILTIN)
7e8bd58e 145 printf("%s=%s\n", key, strempty(val));
63918f92 146
912541b0 147 return 0;
d7867b31 148}
85737dd3 149
036c75de 150int udev_builtin_add_propertyf(UdevEvent *event, const char *key, const char *valf, ...) {
85737dd3
LP
151 _cleanup_free_ char *val = NULL;
152 va_list ap;
153 int r;
154
036c75de 155 assert(event);
85737dd3
LP
156 assert(key);
157 assert(valf);
158
159 va_start(ap, valf);
160 r = vasprintf(&val, valf, ap);
161 va_end(ap);
162 if (r < 0)
163 return log_oom_debug();
164
036c75de 165 return udev_builtin_add_property(event, key, val);
85737dd3 166}
50a0379d 167
036c75de 168int udev_builtin_import_property(UdevEvent *event, const char *key) {
50a0379d
YW
169 const char *val;
170 int r;
171
036c75de
YW
172 assert(event);
173 assert(event->dev);
50a0379d 174
036c75de 175 if (!event->dev_db_clone)
50a0379d
YW
176 return 0;
177
036c75de 178 r = sd_device_get_property_value(event->dev_db_clone, key, &val);
50a0379d
YW
179 if (r == -ENOENT)
180 return 0;
181 if (r < 0)
036c75de 182 return log_device_debug_errno(event->dev_db_clone, r, "Failed to get property \"%s\", ignoring: %m", key);
50a0379d 183
036c75de 184 r = udev_builtin_add_property(event, key, val);
50a0379d
YW
185 if (r < 0)
186 return r;
187
188 return 1;
189}