chiark / gitweb /
libudev: fix --disable-log
[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 "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31
32 #include "libudev.h"
33 #include "libudev-private.h"
34 #include "../udev.h"
35
36 struct udev {
37         int refcount;
38         void (*log_fn)(struct udev *udev,
39                        int priority, const char *file, int line, const char *fn,
40                        const char *format, va_list args);
41 };
42
43 void udev_log(struct udev *udev,
44               int priority, const char *file, int line, const char *fn,
45               const char *format, ...)
46 {
47         va_list args;
48
49         va_start(args, format);
50         udev->log_fn(udev, priority, file, line, fn, format, args);
51         va_end(args);
52 }
53
54 static void log_stderr(struct udev *udev,
55                        int priority, const char *file, int line, const char *fn,
56                        const char *format, va_list args)
57 {
58         static int log = -1;
59
60         if (log == -1) {
61                 if (getenv("LIBUDEV_DEBUG") != NULL)
62                         log = 1;
63                 else
64                         log = 0;
65         }
66
67         if (log == 1) {
68                 fprintf(stderr, "libudev: %s: ", fn);
69                 vfprintf(stderr, format, args);
70         }
71 }
72
73 /* glue to udev logging, needed until udev logging code is "fixed" */
74 #ifdef USE_LOG
75 void log_message(int priority, const char *format, ...)
76 {
77         va_list args;
78
79         va_start(args, format);
80         log_stderr(NULL, priority, NULL, 0, "", format, args);
81         va_end(args);
82 }
83 #endif
84
85 /**
86  * udev_new:
87  *
88  * Create udev library context.
89  *
90  * The initial refcount is 1, and needs to be decremented to
91  * release the ressources of the udev library context.
92  *
93  * Returns: a new udev library context
94  **/
95 struct udev *udev_new(void)
96 {
97         struct udev *udev;
98
99         udev = malloc(sizeof(struct udev));
100         if (udev == NULL)
101                 return NULL;
102         memset(udev, 0x00, (sizeof(struct udev)));
103         udev->refcount = 1;
104         udev->log_fn = log_stderr;
105         udev_config_init();
106         sysfs_init();
107         log_info(udev, "context %p created\n", udev);
108         return udev;
109 }
110
111 /**
112  * udev_ref:
113  * @udev: udev library context
114  *
115  * Take a reference of the udev library context.
116  *
117  * Returns: the passed udev library context
118  **/
119 struct udev *udev_ref(struct udev *udev)
120 {
121         if (udev == NULL)
122                 return NULL;
123         udev->refcount++;
124         return udev;
125 }
126
127 /**
128  * udev_unref:
129  * @udev: udev library context
130  *
131  * Drop a reference of the udev library context. If the refcount
132  * reaches zero, the ressources of the context will be released.
133  *
134  **/
135 void udev_unref(struct udev *udev)
136 {
137         if (udev == NULL)
138                 return;
139         udev->refcount--;
140         if (udev->refcount > 0)
141                 return;
142         sysfs_cleanup();
143         log_info(udev, "context %p released\n", udev);
144         free(udev);
145 }
146
147 /**
148  * udev_set_log_fn:
149  * @udev: udev library context
150  * @log_fn: function to be called for logging messages
151  *
152  * The built-in logging, which writes to stderr if the
153  * LIBUDEV_DEBUG environment variable is set, can be
154  * overridden by a custom function, to plug log messages
155  * into the users logging functionality.
156  *
157  **/
158 void udev_set_log_fn(struct udev *udev,
159                      void (*log_fn)(struct udev *udev,
160                                     int priority, const char *file, int line, const char *fn,
161                                     const char *format, va_list args))
162 {
163         udev->log_fn = log_fn;
164         log_info(udev, "custom logging function %p registered\n", udev);
165 }
166
167 /**
168  * udev_get_sys_path:
169  * @udev: udev library context
170  *
171  * Retrieve the sysfs mount point. The default is "/sys". For
172  * testing purposes, it can be overridden with the environment
173  * variable SYSFS_PATH.
174  *
175  * Returns: the sys mount point
176  **/
177 const char *udev_get_sys_path(struct udev *udev)
178 {
179         if (udev == NULL)
180                 return NULL;
181         return sysfs_path;
182 }
183
184 /**
185  * udev_get_dev_path:
186  * @udev: udev library context
187  *
188  * Retrieve the device directory path. The default value is "/dev",
189  * the actual value may be overridden in the udev configuration
190  * file.
191  *
192  * Returns: the device directory path
193  **/
194 const char *udev_get_dev_path(struct udev *udev)
195 {
196         if (udev == NULL)
197                 return NULL;
198         return udev_root;
199 }