]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 |
a660c63c | 25 | [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id, |
0b99c9f8 | 26 | [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link, |
912541b0 | 27 | [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id, |
912541b0 | 28 | [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id, |
349cc4a5 | 29 | #if HAVE_ACL |
83cd6b75 KS |
30 | [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess, |
31 | #endif | |
d7867b31 KS |
32 | }; |
33 | ||
2024ed61 | 34 | void udev_builtin_init(void) { |
14cb109d | 35 | unsigned i; |
912541b0 | 36 | |
7781e063 | 37 | if (initialized) |
4af113f9 | 38 | return; |
7781e063 | 39 | |
c45b369d | 40 | for (i = 0; i < _UDEV_BUILTIN_MAX; i++) |
8cacf69b | 41 | if (builtins[i] && builtins[i]->init) |
2024ed61 | 42 | builtins[i]->init(); |
7781e063 KS |
43 | |
44 | initialized = true; | |
aa29418a KS |
45 | } |
46 | ||
2024ed61 | 47 | void udev_builtin_exit(void) { |
14cb109d | 48 | unsigned i; |
aa29418a | 49 | |
7781e063 KS |
50 | if (!initialized) |
51 | return; | |
52 | ||
c45b369d | 53 | for (i = 0; i < _UDEV_BUILTIN_MAX; i++) |
8cacf69b | 54 | if (builtins[i] && builtins[i]->exit) |
2024ed61 | 55 | builtins[i]->exit(); |
7781e063 KS |
56 | |
57 | initialized = false; | |
aa29418a KS |
58 | } |
59 | ||
2024ed61 | 60 | bool udev_builtin_validate(void) { |
14cb109d | 61 | unsigned i; |
912541b0 | 62 | |
c45b369d | 63 | for (i = 0; i < _UDEV_BUILTIN_MAX; i++) |
2024ed61 | 64 | if (builtins[i] && builtins[i]->validate && builtins[i]->validate()) |
4af113f9 KS |
65 | return true; |
66 | return false; | |
4f1795cc KS |
67 | } |
68 | ||
2024ed61 | 69 | void udev_builtin_list(void) { |
14cb109d | 70 | unsigned i; |
d7867b31 | 71 | |
c45b369d | 72 | for (i = 0; i < _UDEV_BUILTIN_MAX; i++) |
8cacf69b LZ |
73 | if (builtins[i]) |
74 | fprintf(stderr, " %-14s %s\n", builtins[i]->name, builtins[i]->help); | |
d7867b31 KS |
75 | } |
76 | ||
25de7aa7 | 77 | const char *udev_builtin_name(UdevBuiltinCommand cmd) { |
c45b369d YW |
78 | assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); |
79 | ||
f89d10ae DM |
80 | if (!builtins[cmd]) |
81 | return NULL; | |
82 | ||
912541b0 | 83 | return builtins[cmd]->name; |
d7867b31 KS |
84 | } |
85 | ||
25de7aa7 | 86 | bool udev_builtin_run_once(UdevBuiltinCommand cmd) { |
c45b369d YW |
87 | assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); |
88 | ||
f89d10ae | 89 | if (!builtins[cmd]) |
4e18de3d | 90 | return false; |
f89d10ae | 91 | |
912541b0 | 92 | return builtins[cmd]->run_once; |
81dadce5 KS |
93 | } |
94 | ||
25de7aa7 YW |
95 | UdevBuiltinCommand udev_builtin_lookup(const char *command) { |
96 | UdevBuiltinCommand i; | |
9b917abe | 97 | size_t n; |
912541b0 | 98 | |
9b917abe YW |
99 | assert(command); |
100 | ||
101 | command += strspn(command, WHITESPACE); | |
102 | n = strcspn(command, WHITESPACE); | |
c45b369d | 103 | for (i = 0; i < _UDEV_BUILTIN_MAX; i++) |
9b917abe | 104 | if (builtins[i] && strneq(builtins[i]->name, command, n)) |
912541b0 | 105 | return i; |
c45b369d YW |
106 | |
107 | return _UDEV_BUILTIN_INVALID; | |
d7867b31 KS |
108 | } |
109 | ||
25de7aa7 | 110 | int udev_builtin_run(sd_device *dev, UdevBuiltinCommand cmd, const char *command, bool test) { |
6d93eeb7 | 111 | _cleanup_strv_free_ char **argv = NULL; |
912541b0 | 112 | |
c45b369d YW |
113 | assert(dev); |
114 | assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); | |
115 | assert(command); | |
116 | ||
f89d10ae DM |
117 | if (!builtins[cmd]) |
118 | return -EOPNOTSUPP; | |
119 | ||
6d93eeb7 YW |
120 | argv = strv_split_full(command, NULL, SPLIT_QUOTES | SPLIT_RELAX); |
121 | if (!argv) | |
122 | return -ENOMEM; | |
123 | ||
e5f2783e KS |
124 | /* we need '0' here to reset the internal state */ |
125 | optind = 0; | |
3d6194e8 | 126 | return builtins[cmd]->cmd(dev, strv_length(argv), argv, test); |
d7867b31 KS |
127 | } |
128 | ||
0c9c0634 YW |
129 | int udev_builtin_add_property(sd_device *dev, bool test, const char *key, const char *val) { |
130 | int r; | |
131 | ||
c45b369d YW |
132 | assert(dev); |
133 | assert(key); | |
134 | ||
0c9c0634 YW |
135 | r = device_add_property(dev, key, val); |
136 | if (r < 0) | |
63918f92 YW |
137 | return log_device_debug_errno(dev, r, "Failed to add property '%s%s%s'", |
138 | key, val ? "=" : "", strempty(val)); | |
d7867b31 | 139 | |
912541b0 | 140 | if (test) |
7e8bd58e | 141 | printf("%s=%s\n", key, strempty(val)); |
63918f92 | 142 | |
912541b0 | 143 | return 0; |
d7867b31 | 144 | } |