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