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