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