chiark / gitweb /
udev: libudev - bump soname, remove deprecated functions, introduce symbol versions
[elogind.git] / src / libudev / libudev.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <stdarg.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <time.h>
21
22 #include "libudev.h"
23 #include "libudev-private.h"
24
25 /**
26  * SECTION:libudev
27  * @short_description: libudev context
28  *
29  * The context contains the default values read from the udev config file,
30  * and is passed to all library operations.
31  */
32
33 /**
34  * udev:
35  *
36  * Opaque object representing the library context.
37  */
38 struct udev {
39         int refcount;
40         void (*log_fn)(struct udev *udev,
41                        int priority, const char *file, int line, const char *fn,
42                        const char *format, va_list args);
43         void *userdata;
44         struct udev_list properties_list;
45         int log_priority;
46 };
47
48 void udev_log(struct udev *udev,
49               int priority, const char *file, int line, const char *fn,
50               const char *format, ...)
51 {
52         va_list args;
53
54         va_start(args, format);
55         udev->log_fn(udev, priority, file, line, fn, format, args);
56         va_end(args);
57 }
58
59 static void log_stderr(struct udev *udev,
60                        int priority, const char *file, int line, const char *fn,
61                        const char *format, va_list args)
62 {
63         fprintf(stderr, "libudev: %s: ", fn);
64         vfprintf(stderr, format, args);
65 }
66
67 /**
68  * udev_get_userdata:
69  * @udev: udev library context
70  *
71  * Retrieve stored data pointer from library context. This might be useful
72  * to access from callbacks like a custom logging function.
73  *
74  * Returns: stored userdata
75  **/
76 _public_ void *udev_get_userdata(struct udev *udev)
77 {
78         if (udev == NULL)
79                 return NULL;
80         return udev->userdata;
81 }
82
83 /**
84  * udev_set_userdata:
85  * @udev: udev library context
86  * @userdata: data pointer
87  *
88  * Store custom @userdata in the library context.
89  **/
90 _public_ void udev_set_userdata(struct udev *udev, void *userdata)
91 {
92         if (udev == NULL)
93                 return;
94         udev->userdata = userdata;
95 }
96
97 /**
98  * udev_new:
99  *
100  * Create udev library context. This reads the udev configuration
101  * file, and fills in the default values.
102  *
103  * The initial refcount is 1, and needs to be decremented to
104  * release the resources of the udev library context.
105  *
106  * Returns: a new udev library context
107  **/
108 _public_ struct udev *udev_new(void)
109 {
110         struct udev *udev;
111         const char *env;
112         FILE *f;
113
114         udev = calloc(1, sizeof(struct udev));
115         if (udev == NULL)
116                 return NULL;
117         udev->refcount = 1;
118         udev->log_fn = log_stderr;
119         udev->log_priority = LOG_ERR;
120         udev_list_init(udev, &udev->properties_list, true);
121
122         f = fopen(SYSCONFDIR "/udev/udev.conf", "re");
123         if (f != NULL) {
124                 char line[UTIL_LINE_SIZE];
125                 int line_nr = 0;
126
127                 while (fgets(line, sizeof(line), f)) {
128                         size_t len;
129                         char *key;
130                         char *val;
131
132                         line_nr++;
133
134                         /* find key */
135                         key = line;
136                         while (isspace(key[0]))
137                                 key++;
138
139                         /* comment or empty line */
140                         if (key[0] == '#' || key[0] == '\0')
141                                 continue;
142
143                         /* split key/value */
144                         val = strchr(key, '=');
145                         if (val == NULL) {
146                                 udev_err(udev, "missing <key>=<value> in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
147                                 continue;
148                         }
149                         val[0] = '\0';
150                         val++;
151
152                         /* find value */
153                         while (isspace(val[0]))
154                                 val++;
155
156                         /* terminate key */
157                         len = strlen(key);
158                         if (len == 0)
159                                 continue;
160                         while (isspace(key[len-1]))
161                                 len--;
162                         key[len] = '\0';
163
164                         /* terminate value */
165                         len = strlen(val);
166                         if (len == 0)
167                                 continue;
168                         while (isspace(val[len-1]))
169                                 len--;
170                         val[len] = '\0';
171
172                         if (len == 0)
173                                 continue;
174
175                         /* unquote */
176                         if (val[0] == '"' || val[0] == '\'') {
177                                 if (val[len-1] != val[0]) {
178                                         udev_err(udev, "inconsistent quoting in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
179                                         continue;
180                                 }
181                                 val[len-1] = '\0';
182                                 val++;
183                         }
184
185                         if (strcmp(key, "udev_log") == 0) {
186                                 udev_set_log_priority(udev, util_log_priority(val));
187                                 continue;
188                         }
189                 }
190                 fclose(f);
191         }
192
193         /* environment overrides config */
194         env = getenv("UDEV_LOG");
195         if (env != NULL)
196                 udev_set_log_priority(udev, util_log_priority(env));
197
198         return udev;
199 }
200
201 /**
202  * udev_ref:
203  * @udev: udev library context
204  *
205  * Take a reference of the udev library context.
206  *
207  * Returns: the passed udev library context
208  **/
209 _public_ struct udev *udev_ref(struct udev *udev)
210 {
211         if (udev == NULL)
212                 return NULL;
213         udev->refcount++;
214         return udev;
215 }
216
217 /**
218  * udev_unref:
219  * @udev: udev library context
220  *
221  * Drop a reference of the udev library context. If the refcount
222  * reaches zero, the resources of the context will be released.
223  *
224  **/
225 _public_ struct udev *udev_unref(struct udev *udev)
226 {
227         if (udev == NULL)
228                 return NULL;
229         udev->refcount--;
230         if (udev->refcount > 0)
231                 return udev;
232         udev_list_cleanup(&udev->properties_list);
233         free(udev);
234         return NULL;
235 }
236
237 /**
238  * udev_set_log_fn:
239  * @udev: udev library context
240  * @log_fn: function to be called for logging messages
241  *
242  * The built-in logging writes to stderr. It can be
243  * overridden by a custom function, to plug log messages
244  * into the users' logging functionality.
245  *
246  **/
247 _public_ void udev_set_log_fn(struct udev *udev,
248                      void (*log_fn)(struct udev *udev,
249                                     int priority, const char *file, int line, const char *fn,
250                                     const char *format, va_list args))
251 {
252         udev->log_fn = log_fn;
253         udev_dbg(udev, "custom logging function %p registered\n", log_fn);
254 }
255
256 /**
257  * udev_get_log_priority:
258  * @udev: udev library context
259  *
260  * The initial logging priority is read from the udev config file
261  * at startup.
262  *
263  * Returns: the current logging priority
264  **/
265 _public_ int udev_get_log_priority(struct udev *udev)
266 {
267         return udev->log_priority;
268 }
269
270 /**
271  * udev_set_log_priority:
272  * @udev: udev library context
273  * @priority: the new logging priority
274  *
275  * Set the current logging priority. The value controls which messages
276  * are logged.
277  **/
278 _public_ void udev_set_log_priority(struct udev *udev, int priority)
279 {
280         char num[32];
281
282         udev->log_priority = priority;
283         snprintf(num, sizeof(num), "%u", udev->log_priority);
284         udev_add_property(udev, "UDEV_LOG", num);
285 }
286
287 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
288 {
289         if (value == NULL) {
290                 struct udev_list_entry *list_entry;
291
292                 list_entry = udev_get_properties_list_entry(udev);
293                 list_entry = udev_list_entry_get_by_name(list_entry, key);
294                 if (list_entry != NULL)
295                         udev_list_entry_delete(list_entry);
296                 return NULL;
297         }
298         return udev_list_entry_add(&udev->properties_list, key, value);
299 }
300
301 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
302 {
303         return udev_list_get_entry(&udev->properties_list);
304 }