]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c3cfed0d KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2007-2012 Kay Sievers <kay@vrfy.org> | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
d7867b31 | 20 | |
07630cea | 21 | #include <getopt.h> |
d7867b31 | 22 | #include <stdio.h> |
d7867b31 | 23 | #include <string.h> |
d7867b31 | 24 | |
07630cea | 25 | #include "string-util.h" |
d7867b31 KS |
26 | #include "udev.h" |
27 | ||
7781e063 KS |
28 | static bool initialized; |
29 | ||
d7867b31 | 30 | static const struct udev_builtin *builtins[] = { |
349cc4a5 | 31 | #if HAVE_BLKID |
912541b0 | 32 | [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid, |
f553b3b1 | 33 | #endif |
0bb91b50 | 34 | [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs, |
796b06c2 | 35 | [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb, |
912541b0 | 36 | [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id, |
9d7d42bc | 37 | [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard, |
349cc4a5 | 38 | #if HAVE_KMOD |
912541b0 | 39 | [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod, |
e3043162 | 40 | #endif |
a660c63c | 41 | [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id, |
0b99c9f8 | 42 | [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link, |
912541b0 | 43 | [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id, |
912541b0 | 44 | [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id, |
349cc4a5 | 45 | #if HAVE_ACL |
83cd6b75 KS |
46 | [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess, |
47 | #endif | |
d7867b31 KS |
48 | }; |
49 | ||
9ec6e95b | 50 | void udev_builtin_init(struct udev *udev) { |
912541b0 | 51 | unsigned int i; |
912541b0 | 52 | |
7781e063 | 53 | if (initialized) |
4af113f9 | 54 | return; |
7781e063 | 55 | |
4af113f9 | 56 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
8cacf69b | 57 | if (builtins[i] && builtins[i]->init) |
4af113f9 | 58 | builtins[i]->init(udev); |
7781e063 KS |
59 | |
60 | initialized = true; | |
aa29418a KS |
61 | } |
62 | ||
9ec6e95b | 63 | void udev_builtin_exit(struct udev *udev) { |
912541b0 | 64 | unsigned int i; |
aa29418a | 65 | |
7781e063 KS |
66 | if (!initialized) |
67 | return; | |
68 | ||
8fef0ff2 | 69 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
8cacf69b | 70 | if (builtins[i] && builtins[i]->exit) |
912541b0 | 71 | builtins[i]->exit(udev); |
7781e063 KS |
72 | |
73 | initialized = false; | |
aa29418a KS |
74 | } |
75 | ||
9ec6e95b | 76 | bool udev_builtin_validate(struct udev *udev) { |
912541b0 | 77 | unsigned int i; |
912541b0 | 78 | |
8fef0ff2 | 79 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
8cacf69b | 80 | if (builtins[i] && builtins[i]->validate && builtins[i]->validate(udev)) |
4af113f9 KS |
81 | return true; |
82 | return false; | |
4f1795cc KS |
83 | } |
84 | ||
9ec6e95b | 85 | void udev_builtin_list(struct udev *udev) { |
912541b0 | 86 | unsigned int i; |
d7867b31 | 87 | |
8fef0ff2 | 88 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
8cacf69b LZ |
89 | if (builtins[i]) |
90 | fprintf(stderr, " %-14s %s\n", builtins[i]->name, builtins[i]->help); | |
d7867b31 KS |
91 | } |
92 | ||
9ec6e95b | 93 | const char *udev_builtin_name(enum udev_builtin_cmd cmd) { |
f89d10ae DM |
94 | if (!builtins[cmd]) |
95 | return NULL; | |
96 | ||
912541b0 | 97 | return builtins[cmd]->name; |
d7867b31 KS |
98 | } |
99 | ||
9ec6e95b | 100 | bool udev_builtin_run_once(enum udev_builtin_cmd cmd) { |
f89d10ae | 101 | if (!builtins[cmd]) |
4e18de3d | 102 | return false; |
f89d10ae | 103 | |
912541b0 | 104 | return builtins[cmd]->run_once; |
81dadce5 KS |
105 | } |
106 | ||
9ec6e95b | 107 | enum udev_builtin_cmd udev_builtin_lookup(const char *command) { |
912541b0 KS |
108 | char name[UTIL_PATH_SIZE]; |
109 | enum udev_builtin_cmd i; | |
110 | char *pos; | |
111 | ||
d5a89d7d | 112 | strscpy(name, sizeof(name), command); |
912541b0 KS |
113 | pos = strchr(name, ' '); |
114 | if (pos) | |
115 | pos[0] = '\0'; | |
8fef0ff2 | 116 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
8cacf69b | 117 | if (builtins[i] && streq(builtins[i]->name, name)) |
912541b0 KS |
118 | return i; |
119 | return UDEV_BUILTIN_MAX; | |
d7867b31 KS |
120 | } |
121 | ||
9ec6e95b | 122 | int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) { |
912541b0 KS |
123 | char arg[UTIL_PATH_SIZE]; |
124 | int argc; | |
125 | char *argv[128]; | |
126 | ||
f89d10ae DM |
127 | if (!builtins[cmd]) |
128 | return -EOPNOTSUPP; | |
129 | ||
e5f2783e KS |
130 | /* we need '0' here to reset the internal state */ |
131 | optind = 0; | |
d5a89d7d | 132 | strscpy(arg, sizeof(arg), command); |
912541b0 KS |
133 | udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv); |
134 | return builtins[cmd]->cmd(dev, argc, argv, test); | |
d7867b31 KS |
135 | } |
136 | ||
9ec6e95b | 137 | int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val) { |
b94da8cf | 138 | udev_device_add_property(dev, key, val); |
d7867b31 | 139 | |
912541b0 KS |
140 | if (test) |
141 | printf("%s=%s\n", key, val); | |
142 | return 0; | |
d7867b31 | 143 | } |