]>
Commit | Line | Data |
---|---|---|
eb1f0e66 KS |
1 | /* |
2 | * libudev - interface to udev device information | |
3 | * | |
4 | * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "config.h" | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <stddef.h> | |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | #include <string.h> | |
28 | #include <dirent.h> | |
29 | #include <sys/stat.h> | |
30 | ||
31 | #include "libudev.h" | |
32 | #include "libudev-private.h" | |
33 | #include "../udev.h" | |
34 | ||
35 | static struct udev_device *device_init(struct udev *udev) | |
36 | { | |
37 | struct udev_device *udev_device; | |
38 | ||
39 | udev_device = malloc(sizeof(struct udev_device)); | |
40 | if (udev_device == NULL) | |
41 | return NULL; | |
42 | memset(udev_device, 0x00, sizeof(struct udev_device)); | |
43 | udev_device->refcount = 1; | |
44 | udev_device->udev = udev; | |
45 | return udev_device; | |
46 | } | |
47 | ||
48 | /** | |
49 | * udev_device_new_from_devpath: | |
50 | * @udev: udev library context | |
51 | * @devpath: sys device path | |
52 | * | |
53 | * Create new udev device, and fill in information from the sysfs | |
54 | * device and the udev database entry. The devpath must not contain | |
55 | * the sysfs mount path, and must contain a leading '/'. | |
56 | * | |
57 | * The initial refcount is 1, and needs to be decremented to | |
58 | * release the ressources of the udev device. | |
59 | * | |
60 | * Returns: a new udev device, or #NULL, if it does not exist | |
61 | **/ | |
62 | struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath) | |
63 | { | |
64 | char path[PATH_SIZE]; | |
65 | struct stat statbuf; | |
66 | struct udev_device *udev_device; | |
67 | int err; | |
68 | ||
69 | strlcpy(path, udev_get_sys_path(udev), sizeof(path)); | |
70 | strlcat(path, devpath, sizeof(path)); | |
71 | if (stat(path, &statbuf) != 0) | |
72 | return NULL; | |
73 | if (!S_ISDIR(statbuf.st_mode)) | |
74 | return NULL; | |
75 | ||
76 | udev_device = device_init(udev); | |
77 | if (udev_device == NULL) | |
78 | return NULL; | |
79 | ||
80 | udev_device->udevice = udev_device_init(NULL); | |
81 | if (udev_device->udevice == NULL) { | |
82 | free(udev_device); | |
83 | return NULL; | |
84 | } | |
85 | log_info(udev, "device %p created\n", udev_device); | |
86 | ||
87 | strlcpy(path, devpath, sizeof(path)); | |
88 | sysfs_resolve_link(path, sizeof(path)); | |
89 | ||
90 | err = udev_db_get_device(udev_device->udevice, path); | |
91 | if (err >= 0) | |
92 | log_info(udev, "device %p filled with udev database data\n", udev_device); | |
93 | log_info(udev, "device %p filled with %s data\n", udev_device, udev_device_get_devpath(udev_device)); | |
94 | return udev_device; | |
95 | } | |
96 | ||
97 | /** | |
98 | * udev_device_get_udev: | |
99 | * | |
100 | * Retrieve the udev library context the device was created with. | |
101 | * | |
102 | * Returns: the udev library context | |
103 | **/ | |
104 | struct udev *udev_device_get_udev(struct udev_device *udev_device) | |
105 | { | |
106 | return udev_device->udev; | |
107 | } | |
108 | ||
109 | /** | |
110 | * udev_device_ref: | |
111 | * @udev_device: udev device | |
112 | * | |
113 | * Take a reference of a udev device. | |
114 | * | |
115 | * Returns: the passed udev device | |
116 | **/ | |
117 | struct udev_device *udev_device_ref(struct udev_device *udev_device) | |
118 | { | |
119 | udev_device->refcount++; | |
120 | return udev_device; | |
121 | } | |
122 | ||
123 | /** | |
124 | * udev_device_unref: | |
125 | * @udev_device: udev device | |
126 | * | |
127 | * Drop a reference of a udev device. If the refcount reaches zero, | |
128 | * the ressources of the device will be released. | |
129 | * | |
130 | **/ | |
131 | void udev_device_unref(struct udev_device *udev_device) | |
132 | { | |
133 | udev_device->refcount--; | |
134 | if (udev_device->refcount > 0) | |
135 | return; | |
136 | udev_device_cleanup(udev_device->udevice); | |
137 | free(udev_device); | |
138 | } | |
139 | ||
140 | /** | |
141 | * udev_device_get_devpath: | |
142 | * @udev_device: udev device | |
143 | * | |
144 | * Retrieve the sys path of the udev device. The path does not contain | |
145 | * the sys mount point. | |
146 | * | |
147 | * Returns: the sys path of the udev device | |
148 | **/ | |
149 | const char *udev_device_get_devpath(struct udev_device *udev_device) | |
150 | { | |
151 | return udev_device->udevice->dev->devpath; | |
152 | } | |
153 | ||
154 | /** | |
155 | * udev_device_get_devname: | |
156 | * @udev_device: udev device | |
157 | * | |
158 | * Retrieve the device node file name belonging to the udev device. | |
159 | * The path does not contain the device directory, and does not contain | |
160 | * a leading '/'. | |
161 | * | |
162 | * Returns: the device node file name of the udev device, or #NULL if no device node exists | |
163 | **/ | |
164 | const char *udev_device_get_devname(struct udev_device *udev_device) | |
165 | { | |
166 | if (udev_device->udevice->name[0] == '\0') | |
167 | return NULL; | |
168 | return udev_device->udevice->name; | |
169 | } | |
170 | ||
171 | /** | |
172 | * udev_device_get_subsystem: | |
173 | * @udev_device: udev device | |
174 | * | |
175 | * Retrieve the subsystem string of the udev device. The string does not | |
176 | * contain any "/". | |
177 | * | |
178 | * Returns: the subsystem name of the udev device, or #NULL if it can not be determined | |
179 | **/ | |
180 | const char *udev_device_get_subsystem(struct udev_device *udev_device) | |
181 | { | |
182 | struct sysfs_device *dev = udev_device->udevice->dev; | |
183 | if (dev->subsystem[0] != '\0') | |
184 | return dev->subsystem; | |
185 | if (util_get_sys_subsystem(udev_device->udev, dev->devpath, | |
186 | dev->subsystem, sizeof(dev->subsystem)) < 2) | |
187 | return NULL; | |
188 | return dev->subsystem; | |
189 | } | |
190 | ||
191 | /** | |
192 | * udev_device_get_devlinks: | |
193 | * @udev_device: udev device | |
194 | * @cb: function to be called for every device link found | |
195 | * @data: data to be passed to the function | |
196 | * | |
197 | * Retrieve the device links pointing to the device file of the | |
198 | * udev device. For every device link, the passed function will be | |
199 | * called with the device link string. If the function returns 1, | |
200 | * remaning device links will be ignored. The device link path | |
201 | * does not contain the device directory, and does not contain | |
202 | * a leading '/'. | |
203 | * | |
204 | * Returns: the number of device links passed to the caller, or a negative value on error | |
205 | **/ | |
206 | int udev_device_get_devlinks(struct udev_device *udev_device, | |
207 | int (*cb)(struct udev_device *udev_device, const char *value, void *data), | |
208 | void *data) | |
209 | { | |
210 | struct name_entry *name_loop; | |
211 | int count = 0; | |
212 | ||
213 | list_for_each_entry(name_loop, &udev_device->udevice->symlink_list, node) { | |
214 | count++; | |
215 | if (cb(udev_device, name_loop->name, data) != 0) | |
216 | break; | |
217 | } | |
218 | return count; | |
219 | } | |
220 | ||
221 | /** | |
222 | * udev_device_get_properties: | |
223 | * @udev_device: udev device | |
224 | * @cb: function to be called for every property found | |
225 | * @data: data to be passed to the function | |
226 | * | |
227 | * Retrieve the property key/value pairs belonging to the | |
228 | * udev device. For every key/value pair, the passed function will be | |
229 | * called. If the function returns 1, remaning properties will be | |
230 | * ignored. | |
231 | * | |
232 | * Returns: the number of properties passed to the caller, or a negative value on error | |
233 | **/ | |
234 | int udev_device_get_properties(struct udev_device *udev_device, | |
235 | int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data), | |
236 | void *data) | |
237 | { | |
238 | struct name_entry *name_loop; | |
239 | int count = 0; | |
240 | ||
241 | list_for_each_entry(name_loop, &udev_device->udevice->env_list, node) { | |
242 | char name[PATH_SIZE]; | |
243 | char *val; | |
244 | ||
245 | strncpy(name, name_loop->name, PATH_SIZE); | |
246 | name[PATH_SIZE-1] = '\0'; | |
247 | val = strchr(name, '='); | |
248 | if (val == NULL) | |
249 | continue; | |
250 | val[0] = '\0'; | |
251 | val = &val[1]; | |
252 | if (cb(udev_device, name, val, data) != 0) | |
253 | break; | |
254 | } | |
255 | return count; | |
256 | } |