chiark / gitweb /
fix list handling in enumerate and rules file sorting
[elogind.git] / udev / lib / libudev.c
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 <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "libudev.h"
30 #include "libudev-private.h"
31
32 struct udev {
33         int refcount;
34         void (*log_fn)(struct udev *udev,
35                        int priority, const char *file, int line, const char *fn,
36                        const char *format, va_list args);
37         void *userdata;
38         char *sys_path;
39         char *dev_path;
40         char *rules_path;
41         struct udev_list_node properties_list;
42         int log_priority;
43         int run;
44 };
45
46 void udev_log(struct udev *udev,
47               int priority, const char *file, int line, const char *fn,
48               const char *format, ...)
49 {
50         va_list args;
51
52         if (priority > udev->log_priority)
53                 return;
54
55         va_start(args, format);
56         udev->log_fn(udev, priority, file, line, fn, format, args);
57         va_end(args);
58 }
59
60 static void log_stderr(struct udev *udev,
61                        int priority, const char *file, int line, const char *fn,
62                        const char *format, va_list args)
63 {
64         fprintf(stderr, "libudev: %s: ", fn);
65         vfprintf(stderr, format, args);
66 }
67
68 void *udev_get_userdata(struct udev *udev)
69 {
70         if (udev == NULL)
71                 return NULL;
72         return udev->userdata;
73 }
74
75 void udev_set_userdata(struct udev *udev, void *userdata)
76 {
77         if (udev == NULL)
78                 return;
79         udev->userdata = userdata;
80 }
81
82 /**
83  * udev_new:
84  *
85  * Create udev library context.
86  *
87  * The initial refcount is 1, and needs to be decremented to
88  * release the resources of the udev library context.
89  *
90  * Returns: a new udev library context
91  **/
92 struct udev *udev_new(void)
93 {
94         struct udev *udev;
95         const char *env;
96         char *config_file;
97         FILE *f;
98
99         udev = calloc(1, sizeof(struct udev));
100         if (udev == NULL)
101                 return NULL;
102         udev->refcount = 1;
103         udev->log_fn = log_stderr;
104         udev->log_priority = LOG_ERR;
105         udev_list_init(&udev->properties_list);
106         udev->run = 1;
107         udev->dev_path = strdup(UDEV_PREFIX "/dev");
108         udev->sys_path = strdup("/sys");
109         config_file = strdup(SYSCONFDIR "/udev/udev.conf");
110         if (udev->dev_path == NULL ||
111             udev->sys_path == NULL ||
112             config_file == NULL)
113                 goto err;
114
115         /* settings by environment and config file */
116         env = getenv("SYSFS_PATH");
117         if (env != NULL) {
118                 free(udev->sys_path);
119                 udev->sys_path = strdup(env);
120                 util_remove_trailing_chars(udev->sys_path, '/');
121                 udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
122         }
123
124         env = getenv("UDEV_RUN");
125         if (env != NULL && strcmp(env, "0") == 0)
126                 udev->run = 0;
127
128         env = getenv("UDEV_CONFIG_FILE");
129         if (env != NULL) {
130                 free(config_file);
131                 config_file = strdup(env);
132                 util_remove_trailing_chars(config_file, '/');
133         }
134         if (config_file == NULL)
135                 goto err;
136         f = fopen(config_file, "r");
137         if (f != NULL) {
138                 char line[UTIL_LINE_SIZE];
139                 int line_nr = 0;
140
141                 while (fgets(line, sizeof(line), f)) {
142                         size_t len;
143                         char *key;
144                         char *val;
145
146                         line_nr++;
147
148                         /* find key */
149                         key = line;
150                         while (isspace(key[0]))
151                                 key++;
152
153                         /* comment or empty line */
154                         if (key[0] == '#' || key[0] == '\0')
155                                 continue;
156
157                         /* split key/value */
158                         val = strchr(key, '=');
159                         if (val == NULL) {
160                                 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
161                                 continue;
162                         }
163                         val[0] = '\0';
164                         val++;
165
166                         /* find value */
167                         while (isspace(val[0]))
168                                 val++;
169
170                         /* terminate key */
171                         len = strlen(key);
172                         if (len == 0)
173                                 continue;
174                         while (isspace(key[len-1]))
175                                 len--;
176                         key[len] = '\0';
177
178                         /* terminate value */
179                         len = strlen(val);
180                         if (len == 0)
181                                 continue;
182                         while (isspace(val[len-1]))
183                                 len--;
184                         val[len] = '\0';
185
186                         if (len == 0)
187                                 continue;
188
189                         /* unquote */
190                         if (val[0] == '"' || val[0] == '\'') {
191                                 if (val[len-1] != val[0]) {
192                                         err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
193                                         continue;
194                                 }
195                                 val[len-1] = '\0';
196                                 val++;
197                         }
198
199                         if (strcasecmp(key, "udev_log") == 0) {
200                                 udev_set_log_priority(udev, util_log_priority(val));
201                                 continue;
202                         }
203                         if (strcasecmp(key, "udev_root") == 0) {
204                                 free(udev->dev_path);
205                                 udev->dev_path = strdup(val);
206                                 util_remove_trailing_chars(udev->dev_path, '/');
207                                 continue;
208                         }
209                         if (strcasecmp(key, "udev_rules") == 0) {
210                                 free(udev->rules_path);
211                                 udev->rules_path = strdup(val);
212                                 util_remove_trailing_chars(udev->rules_path, '/');
213                                 continue;
214                         }
215                 }
216                 fclose(f);
217         }
218
219         env = getenv("UDEV_ROOT");
220         if (env != NULL) {
221                 free(udev->dev_path);
222                 udev->dev_path = strdup(env);
223                 util_remove_trailing_chars(udev->dev_path, '/');
224                 udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
225         }
226
227         env = getenv("UDEV_LOG");
228         if (env != NULL)
229                 udev_set_log_priority(udev, util_log_priority(env));
230
231         if (udev->dev_path == NULL || udev->sys_path == NULL)
232                 goto err;
233         dbg(udev, "context %p created\n", udev);
234         dbg(udev, "log_priority=%d\n", udev->log_priority);
235         dbg(udev, "config_file='%s'\n", config_file);
236         dbg(udev, "dev_path='%s'\n", udev->dev_path);
237         dbg(udev, "sys_path='%s'\n", udev->sys_path);
238         if (udev->rules_path != NULL)
239                 dbg(udev, "rules_path='%s'\n", udev->rules_path);
240         free(config_file);
241         return udev;
242 err:
243         free(config_file);
244         err(udev, "context creation failed\n");
245         udev_unref(udev);
246         return NULL;
247 }
248
249 /**
250  * udev_ref:
251  * @udev: udev library context
252  *
253  * Take a reference of the udev library context.
254  *
255  * Returns: the passed udev library context
256  **/
257 struct udev *udev_ref(struct udev *udev)
258 {
259         if (udev == NULL)
260                 return NULL;
261         udev->refcount++;
262         return udev;
263 }
264
265 /**
266  * udev_unref:
267  * @udev: udev library context
268  *
269  * Drop a reference of the udev library context. If the refcount
270  * reaches zero, the resources of the context will be released.
271  *
272  **/
273 void udev_unref(struct udev *udev)
274 {
275         if (udev == NULL)
276                 return;
277         udev->refcount--;
278         if (udev->refcount > 0)
279                 return;
280         udev_list_cleanup_entries(udev, &udev->properties_list);
281         free(udev->dev_path);
282         free(udev->sys_path);
283         free(udev->rules_path);
284         dbg(udev, "context %p released\n", udev);
285         free(udev);
286 }
287
288 /**
289  * udev_set_log_fn:
290  * @udev: udev library context
291  * @log_fn: function to be called for logging messages
292  *
293  * The built-in logging writes to stderr. It can be
294  * overridden by a custom function, to plug log messages
295  * into the users' logging functionality.
296  *
297  **/
298 void udev_set_log_fn(struct udev *udev,
299                      void (*log_fn)(struct udev *udev,
300                                     int priority, const char *file, int line, const char *fn,
301                                     const char *format, va_list args))
302 {
303         udev->log_fn = log_fn;
304         info(udev, "custom logging function %p registered\n", udev);
305 }
306
307 int udev_get_log_priority(struct udev *udev)
308 {
309         return udev->log_priority;
310 }
311
312 void udev_set_log_priority(struct udev *udev, int priority)
313 {
314         char num[32];
315
316         udev->log_priority = priority;
317         snprintf(num, sizeof(num), "%u", udev->log_priority);
318         udev_add_property(udev, "UDEV_LOG", num);
319 }
320
321 const char *udev_get_rules_path(struct udev *udev)
322 {
323         return udev->rules_path;
324 }
325
326 int udev_get_run(struct udev *udev)
327 {
328         return udev->run;
329 }
330
331 /**
332  * udev_get_sys_path:
333  * @udev: udev library context
334  *
335  * Retrieve the sysfs mount point. The default is "/sys". For
336  * testing purposes, it can be overridden with the environment
337  * variable SYSFS_PATH.
338  *
339  * Returns: the sys mount point
340  **/
341 const char *udev_get_sys_path(struct udev *udev)
342 {
343         if (udev == NULL)
344                 return NULL;
345         return udev->sys_path;
346 }
347
348 /**
349  * udev_get_dev_path:
350  * @udev: udev library context
351  *
352  * Retrieve the device directory path. The default value is "/dev",
353  * the actual value may be overridden in the udev configuration
354  * file.
355  *
356  * Returns: the device directory path
357  **/
358 const char *udev_get_dev_path(struct udev *udev)
359 {
360         if (udev == NULL)
361                 return NULL;
362         return udev->dev_path;
363 }
364
365 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
366 {
367         if (value == NULL) {
368                 struct udev_list_entry *list_entry;
369
370                 list_entry = udev_get_properties_list_entry(udev);
371                 list_entry = udev_list_entry_get_by_name(list_entry, key);
372                 if (list_entry != NULL)
373                         udev_list_entry_delete(list_entry);
374                 return NULL;
375         }
376         return udev_list_entry_add(udev, &udev->properties_list, key, value, 1, 0);
377 }
378
379 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
380 {
381         return udev_list_get_entry(&udev->properties_list);
382 }