chiark / gitweb /
70b20e3a289c2e96cd7bd83837dfe5079df44c0a
[elogind.git] / udev_utils.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <syslog.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/utsname.h>
35
36 #include "udev_libc_wrapper.h"
37 #include "udev.h"
38 #include "logging.h"
39 #include "udev_utils.h"
40 #include "list.h"
41
42
43 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
44 {
45         char *pos;
46
47         memset(udev, 0x00, sizeof(struct udevice));
48         INIT_LIST_HEAD(&udev->symlink_list);
49         INIT_LIST_HEAD(&udev->run_list);
50
51         if (subsystem)
52                 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
53
54         if (action)
55                 strlcpy(udev->action, action, sizeof(udev->action));
56
57         if (devpath) {
58                 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
59                 remove_trailing_char(udev->devpath, '/');
60
61                 if (strncmp(udev->devpath, "/block/", 7) == 0)
62                         udev->type = DEV_BLOCK;
63                 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
64                         udev->type = DEV_NET;
65                 else if (strncmp(udev->devpath, "/class/", 7) == 0)
66                         udev->type = DEV_CLASS;
67                 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
68                         udev->type = DEV_DEVICE;
69
70                 /* get kernel name */
71                 pos = strrchr(udev->devpath, '/');
72                 if (pos) {
73                         strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
74                         dbg("kernel_name='%s'", udev->kernel_name);
75
76                         /* Some block devices have '!' in their name, change that to '/' */
77                         pos = udev->kernel_name;
78                         while (pos[0] != '\0') {
79                                 if (pos[0] == '!')
80                                         pos[0] = '/';
81                                 pos++;
82                         }
83
84                         /* get kernel number */
85                         pos = &udev->kernel_name[strlen(udev->kernel_name)];
86                         while (isdigit(pos[-1]))
87                                 pos--;
88                         strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
89                         dbg("kernel_number='%s'", udev->kernel_number);
90                 }
91         }
92
93         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
94                 udev->mode = 0660;
95                 strcpy(udev->owner, "root");
96                 strcpy(udev->group, "root");
97         }
98
99         return 0;
100 }
101
102 void udev_cleanup_device(struct udevice *udev)
103 {
104         struct name_entry *name_loop;
105         struct name_entry *temp_loop;
106
107         list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
108                 list_del(&name_loop->node);
109                 free(name_loop);
110         }
111 }
112
113 int string_is_true(const char *str)
114 {
115         if (strcasecmp(str, "true") == 0)
116                 return 1;
117         if (strcasecmp(str, "yes") == 0)
118                 return 1;
119         if (strcasecmp(str, "1") == 0)
120                 return 1;
121         return 0;
122 }
123
124 int log_priority(const char *priority)
125 {
126         char *endptr;
127         int prio;
128
129         prio = strtol(priority, &endptr, 10);
130         if (endptr[0] == '\0')
131                 return prio;
132         if (strncasecmp(priority, "err", 3) == 0)
133                 return LOG_ERR;
134         if (strcasecmp(priority, "info") == 0)
135                 return LOG_INFO;
136         if (strcasecmp(priority, "debug") == 0)
137                 return LOG_DEBUG;
138         if (string_is_true(priority))
139                 return LOG_ERR;
140
141         return 0;
142 }
143
144 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
145 {
146         static unsigned int kversion = 0;
147         static unsigned int kpatchlevel;
148         static unsigned int ksublevel;
149
150         if (kversion == 0) {
151                 struct utsname uts;
152                 if (uname(&uts) != 0)
153                         return -1;
154
155                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
156                         kversion = 0;
157                         return -1;
158                 }
159         }
160
161         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
162                 return 1;
163         else
164                 return 0;
165 }
166
167 int create_path(const char *path)
168 {
169         char p[PATH_SIZE];
170         char *pos;
171         struct stat stats;
172
173         strcpy (p, path);
174         pos = strrchr(p, '/');
175         if (pos == p || pos == NULL)
176                 return 0;
177
178         while (pos[-1] == '/')
179                 pos--;
180
181         pos[0] = '\0';
182
183         dbg("stat '%s'\n", p);
184         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
185                 return 0;
186
187         if (create_path (p) != 0)
188                 return -1;
189
190         dbg("mkdir '%s'\n", p);
191         return mkdir(p, 0755);
192 }
193
194 /* Reset permissions on the device node, before unlinking it to make sure,
195  * that permisions of possible hard links will be removed too.
196  */
197 int unlink_secure(const char *filename)
198 {
199         int retval;
200
201         retval = chown(filename, 0, 0);
202         if (retval)
203                 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
204
205         retval = chmod(filename, 0000);
206         if (retval)
207                 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
208
209         retval = unlink(filename);
210         if (errno == ENOENT)
211                 retval = 0;
212
213         if (retval)
214                 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
215
216         return retval;
217 }
218
219 int file_map(const char *filename, char **buf, size_t *bufsize)
220 {
221         struct stat stats;
222         int fd;
223
224         fd = open(filename, O_RDONLY);
225         if (fd < 0) {
226                 return -1;
227         }
228
229         if (fstat(fd, &stats) < 0) {
230                 close(fd);
231                 return -1;
232         }
233
234         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
235         if (*buf == MAP_FAILED) {
236                 close(fd);
237                 return -1;
238         }
239         *bufsize = stats.st_size;
240
241         close(fd);
242
243         return 0;
244 }
245
246 void file_unmap(char *buf, size_t bufsize)
247 {
248         munmap(buf, bufsize);
249 }
250
251 /* return number of chars until the next newline, skip escaped newline */
252 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
253 {
254         int escape = 0;
255         size_t count;
256
257         for (count = cur; count < buflen; count++) {
258                 if (!escape && buf[count] == '\n')
259                         break;
260
261                 if (buf[count] == '\\')
262                         escape = 1;
263                 else
264                         escape = 0;
265         }
266
267         return count - cur;
268 }
269
270 void replace_untrusted_chars(char *string)
271 {
272         size_t len;
273
274         for (len = 0; string[len] != '\0'; len++) {
275                 if (strchr(";,~\\()\'", string[len])) {
276                         info("replace '%c' in '%s'", string[len], string);
277                         string[len] = '_';
278                 }
279         }
280 }
281
282 void remove_trailing_char(char *path, char c)
283 {
284         size_t len;
285
286         len = strlen(path);
287         while (len > 0 && path[len-1] == c)
288                 path[--len] = '\0';
289 }
290
291 int name_list_add(struct list_head *name_list, const char *name, int sort)
292 {
293         struct name_entry *loop_name;
294         struct name_entry *new_name;
295
296         list_for_each_entry(loop_name, name_list, node) {
297                 /* avoid doubles */
298                 if (strcmp(loop_name->name, name) == 0) {
299                         dbg("'%s' is already in the list", name);
300                         return 0;
301                 }
302                 if (sort && strcmp(loop_name->name, name) > 0)
303                         break;
304         }
305
306         new_name = malloc(sizeof(struct name_entry));
307         if (new_name == NULL) {
308                 dbg("error malloc");
309                 return -ENOMEM;
310         }
311
312         strlcpy(new_name->name, name, sizeof(new_name->name));
313         list_add_tail(&new_name->node, &loop_name->node);
314
315         return 0;
316 }
317
318 /* calls function for every file found in specified directory */
319 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
320 {
321         struct dirent *ent;
322         DIR *dir;
323         char *ext;
324         char filename[PATH_SIZE];
325
326         dbg("open directory '%s'", dirname);
327         dir = opendir(dirname);
328         if (dir == NULL) {
329                 dbg("unable to open '%s'", dirname);
330                 return -1;
331         }
332
333         while (1) {
334                 ent = readdir(dir);
335                 if (ent == NULL || ent->d_name[0] == '\0')
336                         break;
337
338                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
339                         continue;
340
341                 /* look for file matching with specified suffix */
342                 ext = strrchr(ent->d_name, '.');
343                 if (ext == NULL)
344                         continue;
345
346                 if (strcmp(ext, suffix) != 0)
347                         continue;
348
349                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
350
351                 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
352                 filename[sizeof(filename)-1] = '\0';
353                 name_list_add(name_list, filename, 1);
354         }
355
356         closedir(dir);
357         return 0;
358 }
359
360 int execute_command(const char *command, const char *subsystem)
361 {
362         int retval;
363         pid_t pid;
364         char arg[PATH_SIZE];
365         char *argv[(PATH_SIZE / 2) + 1];
366         char *pos;
367         int devnull;
368         int i;
369
370         strlcpy(arg, command, sizeof(arg));
371         i = 0;
372         if (strchr(arg, ' ')) {
373                 pos = arg;
374                 while (pos != NULL) {
375                         if (pos[0] == '\'') {
376                                 /* don't separate if in apostrophes */
377                                 pos++;
378                                 argv[i] = strsep(&pos, "\'");
379                                 while (pos && pos[0] == ' ')
380                                         pos++;
381                         } else {
382                                 argv[i] = strsep(&pos, " ");
383                         }
384                         dbg("arg[%i] '%s'", i, argv[i]);
385                         i++;
386                 }
387                 argv[i] =  NULL;
388                 dbg("execute '%s' with parsed arguments", arg);
389         } else {
390                 argv[0] = arg;
391                 argv[1] = (char *) subsystem;
392                 argv[2] = NULL;
393                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
394         }
395
396         pid = fork();
397         switch (pid) {
398         case 0:
399                 /* child */
400                 devnull = open("/dev/null", O_RDWR);
401                 if (devnull >= 0) {
402                         dup2(devnull, STDIN_FILENO);
403                         dup2(devnull, STDOUT_FILENO);
404                         dup2(devnull, STDERR_FILENO);
405                         close(devnull);
406                 }
407                 retval = execv(arg, argv);
408                 err("exec of child failed");
409                 _exit(1);
410         case -1:
411                 dbg("fork of child failed");
412                 break;
413                 return -1;
414         default:
415                 waitpid(pid, NULL, 0);
416         }
417
418         return 0;
419 }