]>
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 KS |
19 | |
20 | #include <unistd.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <stddef.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
26 | #include <getopt.h> | |
27 | ||
28 | #include "udev.h" | |
29 | ||
7781e063 KS |
30 | static bool initialized; |
31 | ||
d7867b31 | 32 | static const struct udev_builtin *builtins[] = { |
f553b3b1 | 33 | #ifdef HAVE_BLKID |
912541b0 | 34 | [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid, |
f553b3b1 | 35 | #endif |
0bb91b50 | 36 | [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs, |
912541b0 | 37 | [UDEV_BUILTIN_FIRMWARE] = &udev_builtin_firmware, |
796b06c2 | 38 | [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb, |
912541b0 KS |
39 | [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id, |
40 | [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod, | |
a660c63c | 41 | [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id, |
912541b0 | 42 | [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id, |
912541b0 | 43 | [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id, |
83cd6b75 KS |
44 | #ifdef HAVE_ACL |
45 | [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess, | |
46 | #endif | |
d7867b31 KS |
47 | }; |
48 | ||
4af113f9 | 49 | void udev_builtin_init(struct udev *udev) |
aa29418a | 50 | { |
912541b0 | 51 | unsigned int i; |
912541b0 | 52 | |
7781e063 | 53 | if (initialized) |
4af113f9 | 54 | return; |
7781e063 | 55 | |
4af113f9 KS |
56 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
57 | if (builtins[i]->init) | |
58 | builtins[i]->init(udev); | |
7781e063 KS |
59 | |
60 | initialized = true; | |
aa29418a KS |
61 | } |
62 | ||
4f1795cc | 63 | void udev_builtin_exit(struct udev *udev) |
aa29418a | 64 | { |
912541b0 | 65 | unsigned int i; |
aa29418a | 66 | |
7781e063 KS |
67 | if (!initialized) |
68 | return; | |
69 | ||
8fef0ff2 | 70 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
912541b0 KS |
71 | if (builtins[i]->exit) |
72 | builtins[i]->exit(udev); | |
7781e063 KS |
73 | |
74 | initialized = false; | |
aa29418a KS |
75 | } |
76 | ||
4f1795cc KS |
77 | bool udev_builtin_validate(struct udev *udev) |
78 | { | |
912541b0 | 79 | unsigned int i; |
912541b0 | 80 | |
8fef0ff2 | 81 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
4af113f9 KS |
82 | if (builtins[i]->validate && builtins[i]->validate(udev)) |
83 | return true; | |
84 | return false; | |
4f1795cc KS |
85 | } |
86 | ||
87 | void udev_builtin_list(struct udev *udev) | |
d7867b31 | 88 | { |
912541b0 | 89 | unsigned int i; |
d7867b31 | 90 | |
8fef0ff2 | 91 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
912541b0 | 92 | fprintf(stderr, " %-12s %s\n", builtins[i]->name, builtins[i]->help); |
d7867b31 KS |
93 | } |
94 | ||
95 | const char *udev_builtin_name(enum udev_builtin_cmd cmd) | |
96 | { | |
912541b0 | 97 | return builtins[cmd]->name; |
d7867b31 KS |
98 | } |
99 | ||
81dadce5 | 100 | bool udev_builtin_run_once(enum udev_builtin_cmd cmd) |
d7867b31 | 101 | { |
912541b0 | 102 | return builtins[cmd]->run_once; |
81dadce5 KS |
103 | } |
104 | ||
105 | enum udev_builtin_cmd udev_builtin_lookup(const char *command) | |
106 | { | |
912541b0 KS |
107 | char name[UTIL_PATH_SIZE]; |
108 | enum udev_builtin_cmd i; | |
109 | char *pos; | |
110 | ||
111 | util_strscpy(name, sizeof(name), command); | |
112 | pos = strchr(name, ' '); | |
113 | if (pos) | |
114 | pos[0] = '\0'; | |
8fef0ff2 | 115 | for (i = 0; i < ELEMENTSOF(builtins); i++) |
912541b0 KS |
116 | if (strcmp(builtins[i]->name, name) == 0) |
117 | return i; | |
118 | return UDEV_BUILTIN_MAX; | |
d7867b31 KS |
119 | } |
120 | ||
81dadce5 | 121 | int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) |
d7867b31 | 122 | { |
912541b0 KS |
123 | char arg[UTIL_PATH_SIZE]; |
124 | int argc; | |
125 | char *argv[128]; | |
126 | ||
e5f2783e KS |
127 | /* we need '0' here to reset the internal state */ |
128 | optind = 0; | |
912541b0 KS |
129 | util_strscpy(arg, sizeof(arg), command); |
130 | udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv); | |
131 | return builtins[cmd]->cmd(dev, argc, argv, test); | |
d7867b31 KS |
132 | } |
133 | ||
c38a141b | 134 | int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val) |
d7867b31 | 135 | { |
912541b0 | 136 | struct udev_list_entry *entry; |
d7867b31 | 137 | |
912541b0 KS |
138 | entry = udev_device_add_property(dev, key, val); |
139 | /* store in db, skip private keys */ | |
140 | if (key[0] != '.') | |
141 | udev_list_entry_set_num(entry, true); | |
d7867b31 | 142 | |
912541b0 KS |
143 | if (test) |
144 | printf("%s=%s\n", key, val); | |
145 | return 0; | |
d7867b31 | 146 | } |