]>
Commit | Line | Data |
---|---|---|
88a6477e KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
25e773ee | 4 | Copyright 2008-2014 Kay Sievers <kay@vrfy.org> |
88a6477e KS |
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 | ***/ | |
33a5cc29 | 19 | |
33a5cc29 KS |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <stddef.h> | |
4cf23685 | 23 | #include <stdarg.h> |
33a5cc29 KS |
24 | #include <unistd.h> |
25 | #include <errno.h> | |
26 | #include <string.h> | |
7d563a17 | 27 | #include <ctype.h> |
4f1795cc | 28 | #include <time.h> |
33a5cc29 KS |
29 | |
30 | #include "libudev.h" | |
31 | #include "libudev-private.h" | |
4db17f29 | 32 | #include "missing.h" |
33a5cc29 | 33 | |
ce1d6d7f KS |
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 | ||
1e511322 KS |
42 | /** |
43 | * udev: | |
44 | * | |
ce1d6d7f | 45 | * Opaque object representing the library context. |
1e511322 | 46 | */ |
ba6929f6 | 47 | struct udev { |
912541b0 KS |
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; | |
ba6929f6 KS |
53 | }; |
54 | ||
1e511322 KS |
55 | /** |
56 | * udev_get_userdata: | |
57 | * @udev: udev library context | |
58 | * | |
59 | * Retrieve stored data pointer from library context. This might be useful | |
25e773ee | 60 | * to access from callbacks. |
1e511322 KS |
61 | * |
62 | * Returns: stored userdata | |
63 | **/ | |
25e773ee | 64 | _public_ void *udev_get_userdata(struct udev *udev) { |
912541b0 KS |
65 | if (udev == NULL) |
66 | return NULL; | |
67 | return udev->userdata; | |
c8e32461 KS |
68 | } |
69 | ||
1e511322 KS |
70 | /** |
71 | * udev_set_userdata: | |
72 | * @udev: udev library context | |
73 | * @userdata: data pointer | |
74 | * | |
75 | * Store custom @userdata in the library context. | |
76 | **/ | |
25e773ee | 77 | _public_ void udev_set_userdata(struct udev *udev, void *userdata) { |
912541b0 KS |
78 | if (udev == NULL) |
79 | return; | |
80 | udev->userdata = userdata; | |
c8e32461 KS |
81 | } |
82 | ||
33a5cc29 KS |
83 | /** |
84 | * udev_new: | |
85 | * | |
1e511322 KS |
86 | * Create udev library context. This reads the udev configuration |
87 | * file, and fills in the default values. | |
33a5cc29 KS |
88 | * |
89 | * The initial refcount is 1, and needs to be decremented to | |
be7de409 | 90 | * release the resources of the udev library context. |
33a5cc29 KS |
91 | * |
92 | * Returns: a new udev library context | |
93 | **/ | |
25e773ee | 94 | _public_ struct udev *udev_new(void) { |
912541b0 | 95 | struct udev *udev; |
ea55caa6 | 96 | _cleanup_fclose_ FILE *f = NULL; |
912541b0 | 97 | |
955d98c9 | 98 | udev = new0(struct udev, 1); |
912541b0 KS |
99 | if (udev == NULL) |
100 | return NULL; | |
101 | udev->refcount = 1; | |
912541b0 | 102 | |
ff944daa | 103 | f = fopen("/etc/udev/udev.conf", "re"); |
912541b0 KS |
104 | if (f != NULL) { |
105 | char line[UTIL_LINE_SIZE]; | |
fe756ed9 | 106 | unsigned line_nr = 0; |
912541b0 KS |
107 | |
108 | while (fgets(line, sizeof(line), f)) { | |
109 | size_t len; | |
110 | char *key; | |
111 | char *val; | |
112 | ||
113 | line_nr++; | |
114 | ||
115 | /* find key */ | |
116 | key = line; | |
117 | while (isspace(key[0])) | |
118 | key++; | |
119 | ||
120 | /* comment or empty line */ | |
121 | if (key[0] == '#' || key[0] == '\0') | |
122 | continue; | |
123 | ||
124 | /* split key/value */ | |
125 | val = strchr(key, '='); | |
126 | if (val == NULL) { | |
ff49bc32 | 127 | log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr); |
912541b0 KS |
128 | continue; |
129 | } | |
130 | val[0] = '\0'; | |
131 | val++; | |
132 | ||
133 | /* find value */ | |
134 | while (isspace(val[0])) | |
135 | val++; | |
136 | ||
137 | /* terminate key */ | |
138 | len = strlen(key); | |
139 | if (len == 0) | |
140 | continue; | |
141 | while (isspace(key[len-1])) | |
142 | len--; | |
143 | key[len] = '\0'; | |
144 | ||
145 | /* terminate value */ | |
146 | len = strlen(val); | |
147 | if (len == 0) | |
148 | continue; | |
149 | while (isspace(val[len-1])) | |
150 | len--; | |
151 | val[len] = '\0'; | |
152 | ||
153 | if (len == 0) | |
154 | continue; | |
155 | ||
156 | /* unquote */ | |
157 | if (val[0] == '"' || val[0] == '\'') { | |
158 | if (val[len-1] != val[0]) { | |
ff49bc32 | 159 | log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr); |
912541b0 KS |
160 | continue; |
161 | } | |
162 | val[len-1] = '\0'; | |
163 | val++; | |
164 | } | |
165 | ||
090be865 | 166 | if (streq(key, "udev_log")) { |
ee7122c0 ZJS |
167 | int prio; |
168 | ||
169 | prio = util_log_priority(val); | |
170 | if (prio < 0) | |
ff49bc32 | 171 | log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr, val); |
ee7122c0 | 172 | else |
25e773ee | 173 | log_set_max_level(prio); |
912541b0 KS |
174 | continue; |
175 | } | |
912541b0 | 176 | } |
912541b0 KS |
177 | } |
178 | ||
912541b0 | 179 | return udev; |
33a5cc29 KS |
180 | } |
181 | ||
182 | /** | |
183 | * udev_ref: | |
184 | * @udev: udev library context | |
185 | * | |
186 | * Take a reference of the udev library context. | |
187 | * | |
188 | * Returns: the passed udev library context | |
189 | **/ | |
25e773ee | 190 | _public_ struct udev *udev_ref(struct udev *udev) { |
912541b0 KS |
191 | if (udev == NULL) |
192 | return NULL; | |
193 | udev->refcount++; | |
194 | return udev; | |
33a5cc29 KS |
195 | } |
196 | ||
197 | /** | |
198 | * udev_unref: | |
199 | * @udev: udev library context | |
200 | * | |
201 | * Drop a reference of the udev library context. If the refcount | |
be7de409 | 202 | * reaches zero, the resources of the context will be released. |
33a5cc29 | 203 | * |
c1959569 | 204 | * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise. |
33a5cc29 | 205 | **/ |
25e773ee | 206 | _public_ struct udev *udev_unref(struct udev *udev) { |
912541b0 | 207 | if (udev == NULL) |
20bbd54f | 208 | return NULL; |
912541b0 KS |
209 | udev->refcount--; |
210 | if (udev->refcount > 0) | |
20bbd54f | 211 | return udev; |
912541b0 | 212 | free(udev); |
20bbd54f | 213 | return NULL; |
33a5cc29 KS |
214 | } |
215 | ||
216 | /** | |
217 | * udev_set_log_fn: | |
218 | * @udev: udev library context | |
f47ad593 | 219 | * @log_fn: function to be called for log messages |
33a5cc29 | 220 | * |
25e773ee | 221 | * This function is deprecated. |
33a5cc29 KS |
222 | * |
223 | **/ | |
54cf0b7f | 224 | _public_ void udev_set_log_fn(struct udev *udev, |
912541b0 KS |
225 | void (*log_fn)(struct udev *udev, |
226 | int priority, const char *file, int line, const char *fn, | |
25e773ee KS |
227 | const char *format, va_list args)) { |
228 | return; | |
7d563a17 KS |
229 | } |
230 | ||
1e511322 KS |
231 | /** |
232 | * udev_get_log_priority: | |
233 | * @udev: udev library context | |
234 | * | |
25e773ee | 235 | * This function is deprecated. |
1e511322 | 236 | * |
1e511322 | 237 | **/ |
25e773ee KS |
238 | _public_ int udev_get_log_priority(struct udev *udev) { |
239 | return log_get_max_level(); | |
7d563a17 KS |
240 | } |
241 | ||
1e511322 KS |
242 | /** |
243 | * udev_set_log_priority: | |
244 | * @udev: udev library context | |
f47ad593 | 245 | * @priority: the new log priority |
1e511322 | 246 | * |
25e773ee KS |
247 | * This function is deprecated. |
248 | * | |
1e511322 | 249 | **/ |
25e773ee KS |
250 | _public_ void udev_set_log_priority(struct udev *udev, int priority) { |
251 | log_set_max_level(priority); | |
7d563a17 | 252 | } |