1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <getopt.h>
4 #include <stdio.h>
5 
6 #include "device-private.h"
7 #include "device-util.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "udev-builtin.h"
11 
12 static bool initialized;
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_HWDB] = &udev_builtin_hwdb,
20         [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
21         [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard,
22 #if HAVE_KMOD
23         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
24 #endif
25         [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id,
26         [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link,
27         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
28         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
29 #if HAVE_ACL
30         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
31 #endif
32 };
33 
udev_builtin_init(void)34 void udev_builtin_init(void) {
35         if (initialized)
36                 return;
37 
38         for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
39                 if (builtins[i] && builtins[i]->init)
40                         builtins[i]->init();
41 
42         initialized = true;
43 }
44 
udev_builtin_exit(void)45 void udev_builtin_exit(void) {
46         if (!initialized)
47                 return;
48 
49         for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
50                 if (builtins[i] && builtins[i]->exit)
51                         builtins[i]->exit();
52 
53         initialized = false;
54 }
55 
udev_builtin_validate(void)56 bool udev_builtin_validate(void) {
57         for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
58                 if (builtins[i] && builtins[i]->validate && builtins[i]->validate())
59                         return true;
60         return false;
61 }
62 
udev_builtin_list(void)63 void udev_builtin_list(void) {
64         for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
65                 if (builtins[i])
66                         fprintf(stderr, "  %-14s  %s\n", builtins[i]->name, builtins[i]->help);
67 }
68 
udev_builtin_name(UdevBuiltinCommand cmd)69 const char *udev_builtin_name(UdevBuiltinCommand cmd) {
70         assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
71 
72         if (!builtins[cmd])
73                 return NULL;
74 
75         return builtins[cmd]->name;
76 }
77 
udev_builtin_run_once(UdevBuiltinCommand cmd)78 bool udev_builtin_run_once(UdevBuiltinCommand cmd) {
79         assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
80 
81         if (!builtins[cmd])
82                 return false;
83 
84         return builtins[cmd]->run_once;
85 }
86 
udev_builtin_lookup(const char * command)87 UdevBuiltinCommand udev_builtin_lookup(const char *command) {
88         size_t n;
89 
90         assert(command);
91 
92         command += strspn(command, WHITESPACE);
93         n = strcspn(command, WHITESPACE);
94         for (UdevBuiltinCommand i = 0; i < _UDEV_BUILTIN_MAX; i++)
95                 if (builtins[i] && strneq(builtins[i]->name, command, n))
96                         return i;
97 
98         return _UDEV_BUILTIN_INVALID;
99 }
100 
udev_builtin_run(sd_device * dev,sd_netlink ** rtnl,UdevBuiltinCommand cmd,const char * command,bool test)101 int udev_builtin_run(sd_device *dev, sd_netlink **rtnl, UdevBuiltinCommand cmd, const char *command, bool test) {
102         _cleanup_strv_free_ char **argv = NULL;
103         int r;
104 
105         assert(dev);
106         assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
107         assert(command);
108 
109         if (!builtins[cmd])
110                 return -EOPNOTSUPP;
111 
112         r = strv_split_full(&argv, command, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_RETAIN_ESCAPE);
113         if (r < 0)
114                 return r;
115 
116         /* we need '0' here to reset the internal state */
117         optind = 0;
118         return builtins[cmd]->cmd(dev, rtnl, strv_length(argv), argv, test);
119 }
120 
udev_builtin_add_property(sd_device * dev,bool test,const char * key,const char * val)121 int udev_builtin_add_property(sd_device *dev, bool test, const char *key, const char *val) {
122         int r;
123 
124         assert(dev);
125         assert(key);
126 
127         r = device_add_property(dev, key, val);
128         if (r < 0)
129                 return log_device_debug_errno(dev, r, "Failed to add property '%s%s%s'",
130                                               key, val ? "=" : "", strempty(val));
131 
132         if (test)
133                 printf("%s=%s\n", key, strempty(val));
134 
135         return 0;
136 }
137