1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "device-private.h"
8 #include "device-util.h"
9 #include "string-util.h"
11 #include "udev-builtin.h"
13 static bool initialized
;
15 static const struct udev_builtin
*const builtins
[_UDEV_BUILTIN_MAX
] = {
17 [UDEV_BUILTIN_BLKID
] = &udev_builtin_blkid
,
19 [UDEV_BUILTIN_BTRFS
] = &udev_builtin_btrfs
,
20 [UDEV_BUILTIN_HWDB
] = &udev_builtin_hwdb
,
21 [UDEV_BUILTIN_INPUT_ID
] = &udev_builtin_input_id
,
22 [UDEV_BUILTIN_KEYBOARD
] = &udev_builtin_keyboard
,
24 [UDEV_BUILTIN_KMOD
] = &udev_builtin_kmod
,
26 [UDEV_BUILTIN_NET_ID
] = &udev_builtin_net_id
,
27 [UDEV_BUILTIN_NET_LINK
] = &udev_builtin_net_setup_link
,
28 [UDEV_BUILTIN_PATH_ID
] = &udev_builtin_path_id
,
29 [UDEV_BUILTIN_USB_ID
] = &udev_builtin_usb_id
,
31 [UDEV_BUILTIN_UACCESS
] = &udev_builtin_uaccess
,
35 void udev_builtin_init(void) {
41 for (i
= 0; i
< _UDEV_BUILTIN_MAX
; i
++)
42 if (builtins
[i
] && builtins
[i
]->init
)
48 void udev_builtin_exit(void) {
54 for (i
= 0; i
< _UDEV_BUILTIN_MAX
; i
++)
55 if (builtins
[i
] && builtins
[i
]->exit
)
61 bool udev_builtin_validate(void) {
64 for (i
= 0; i
< _UDEV_BUILTIN_MAX
; i
++)
65 if (builtins
[i
] && builtins
[i
]->validate
&& builtins
[i
]->validate())
70 void udev_builtin_list(void) {
73 for (i
= 0; i
< _UDEV_BUILTIN_MAX
; i
++)
75 fprintf(stderr
, " %-14s %s\n", builtins
[i
]->name
, builtins
[i
]->help
);
78 const char *udev_builtin_name(enum udev_builtin_cmd cmd
) {
79 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
84 return builtins
[cmd
]->name
;
87 bool udev_builtin_run_once(enum udev_builtin_cmd cmd
) {
88 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
93 return builtins
[cmd
]->run_once
;
96 enum udev_builtin_cmd
udev_builtin_lookup(const char *command
) {
97 enum udev_builtin_cmd i
;
102 command
+= strspn(command
, WHITESPACE
);
103 n
= strcspn(command
, WHITESPACE
);
104 for (i
= 0; i
< _UDEV_BUILTIN_MAX
; i
++)
105 if (builtins
[i
] && strneq(builtins
[i
]->name
, command
, n
))
108 return _UDEV_BUILTIN_INVALID
;
111 int udev_builtin_run(sd_device
*dev
, enum udev_builtin_cmd cmd
, const char *command
, bool test
) {
112 _cleanup_strv_free_
char **argv
= NULL
;
115 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
121 argv
= strv_split_full(command
, NULL
, SPLIT_QUOTES
| SPLIT_RELAX
);
125 /* we need '0' here to reset the internal state */
127 return builtins
[cmd
]->cmd(dev
, strv_length(argv
), argv
, test
);
130 int udev_builtin_add_property(sd_device
*dev
, bool test
, const char *key
, const char *val
) {
136 r
= device_add_property(dev
, key
, val
);
138 return log_device_debug_errno(dev
, r
, "Failed to add property '%s%s%s'",
139 key
, val
? "=" : "", strempty(val
));
142 printf("%s=%s\n", key
, strempty(val
));