Debian Codegraph @

src/udev/udev-builtin.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "bitfield.h"
7 #include "device-private.h"
8 #include "device-util.h"
9 #include "extract-word.h"
10 #include "string-util.h"
11 #include "strv.h"
12 #include "udev-builtin.h"
13
14 static const UdevBuiltin *const builtins[_UDEV_BUILTIN_MAX] = {
15 #if HAVE_BLKID
16 [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
17 #endif
18 [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs,
19 [UDEV_BUILTIN_DISSECT_IMAGE] = &udev_builtin_dissect_image,
20 [UDEV_BUILTIN_FACTORY_RESET] = &udev_builtin_factory_reset,
21 [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb,
22 [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
23 [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard,
24 #if HAVE_KMOD
25 [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
26 #endif
27 [UDEV_BUILTIN_NET_DRIVER] = &udev_builtin_net_driver,
28 [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id,
29 [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link,
30 [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
31 #if HAVE_TPM2
32 [UDEV_BUILTIN_TPM2_ID] = &udev_builtin_tpm2_id,
33 #endif
34 #if HAVE_ACL
35 [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
36 #endif
37 [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
38 };
39
40 void udev_builtin_init(void) {
41 FOREACH_ELEMENT(b, builtins)
42 if (*b && (*b)->init)
43 (*b)->init();
44 }
45
46 void udev_builtin_exit(void) {
47 FOREACH_ELEMENT(b, builtins)
48 if (*b && (*b)->exit)
49 (*b)->exit();
50 }
51
52 UdevReloadFlags udev_builtin_should_reload(void) {
53 UdevReloadFlags flags = 0;
54
55 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
56 if (builtins[i] && builtins[i]->should_reload && builtins[i]->should_reload())
57 flags |= 1u << i;
58
59 if (flags != 0)
60 flags |= UDEV_RELOAD_KILL_WORKERS;
61
62 return flags;
63 }
64
65 void udev_builtin_reload(UdevReloadFlags flags) {
66 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++) {
67 if (!BIT_SET(flags, i) || !builtins[i])
68 continue;
69 if (builtins[i]->exit)
70 builtins[i]->exit();
71 if (builtins[i]->init)
72 builtins[i]->init();
73 }
74 }
75
76 void udev_builtin_list(void) {
77 FOREACH_ELEMENT(b, builtins)
78 if (*b)
79 fprintf(stderr, " %-14s %s\n", (*b)->name, (*b)->help);
80 }
81
82 const char* udev_builtin_name(UdevBuiltinCommand cmd) {
83 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
84
85 if (!builtins[cmd])
86 return NULL;
87
88 return builtins[cmd]->name;
89 }
90
91 bool udev_builtin_run_once(UdevBuiltinCommand cmd) {
92 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
93
94 if (!builtins[cmd])
95 return false;
96
97 return builtins[cmd]->run_once;
98 }
99
100 UdevBuiltinCommand udev_builtin_lookup(const char *command) {
101 size_t n;
102
103 assert(command);
104
105 command += strspn(command, WHITESPACE);
106 n = strcspn(command, WHITESPACE);
107 for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
108 if (builtins[i] && strneq(builtins[i]->name, command, n))
109 return i;
110
111 return _UDEV_BUILTIN_INVALID;
112 }
113
114 int udev_builtin_run(UdevEvent *event, UdevBuiltinCommand cmd, const char *command) {
115 _cleanup_strv_free_ char **argv = NULL;
116 int r;
117
118 assert(event);
119 assert(event->dev);
120 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
121 assert(command);
122
123 if (!builtins[cmd])
124 return -EOPNOTSUPP;
125
126 r = strv_split_full(&argv, command, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_RETAIN_ESCAPE);
127 if (r < 0)
128 return r;
129
130 return builtins[cmd]->cmd(event, strv_length(argv), argv);
131 }
132
133 int udev_builtin_add_property(UdevEvent *event, const char *key, const char *val) {
134 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
135 int r;
136
137 assert(key);
138
139 val = empty_to_null(val);
140
141 r = device_add_property(dev, key, val);
142 if (r < 0)
143 return log_device_debug_errno(dev, r, "Failed to %s property '%s%s%s'",
144 val ? "add" : "remove",
145 key, val ? "=" : "", strempty(val));
146
147 if (event->event_mode == EVENT_UDEVADM_TEST_BUILTIN) {
148 if (val)
149 printf("%s=%s\n", key, val);
150 else
151 printf("%s (removed)\n", key);
152 }
153
154 return 0;
155 }
156
157 int udev_builtin_add_propertyf(UdevEvent *event, const char *key, const char *valf, ...) {
158 _cleanup_free_ char *val = NULL;
159 va_list ap;
160 int r;
161
162 assert(event);
163 assert(key);
164 assert(valf);
165
166 va_start(ap, valf);
167 r = vasprintf(&val, valf, ap);
168 va_end(ap);
169 if (r < 0)
170 return log_oom_debug();
171
172 return udev_builtin_add_property(event, key, val);
173 }
174
175 int udev_builtin_import_property(UdevEvent *event, const char *key) {
176 const char *val;
177 int r;
178
179 assert(event);
180 assert(event->dev);
181
182 if (!event->dev_db_clone)
183 return 0;
184
185 r = sd_device_get_property_value(event->dev_db_clone, key, &val);
186 if (r == -ENOENT)
187 return 0;
188 if (r < 0)
189 return log_device_debug_errno(event->dev_db_clone, r, "Failed to get property \"%s\", ignoring: %m", key);
190
191 r = udev_builtin_add_property(event, key, val);
192 if (r < 0)
193 return r;
194
195 return 1;
196 }