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