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