syslog() is a blocking call on eglibc. as procd provides the actual syslog, weneed...
[project/procd.git] / mkdev.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _BSD_SOURCE
16
17 #include <sys/stat.h>
18 #include <sys/types.h>
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdbool.h>
25 #include <dirent.h>
26 #include <limits.h>
27 #include <fnmatch.h>
28
29 #include "procd.h"
30
31 static char **patterns;
32 static int n_patterns;
33 static char buf[PATH_MAX];
34 static char buf2[PATH_MAX];
35 static unsigned int mode = 0600;
36
37 static bool find_pattern(const char *name)
38 {
39         int i;
40
41         for (i = 0; i < n_patterns; i++)
42                 if (!fnmatch(patterns[i], name, 0))
43                         return true;
44
45         return false;
46 }
47
48 static void make_dev(const char *path, bool block, int major, int minor)
49 {
50         unsigned int oldumask = umask(0);
51         unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
52
53         DEBUG(2, "Creating %s device %s(%d,%d)\n",
54                 block ? "block" : "character",
55                 path, major, minor);
56
57         mknod(path, _mode, makedev(major, minor));
58         umask(oldumask);
59 }
60
61 static void find_devs(bool block)
62 {
63         char *path = block ? "/sys/dev/block" : "/sys/dev/char";
64         struct dirent *dp;
65         DIR *dir;
66
67         dir = opendir(path);
68         if (!dir)
69                 return;
70
71         path = buf2 + sprintf(buf2, "%s/", path);
72         while ((dp = readdir(dir)) != NULL) {
73                 char *c;
74                 int major = 0, minor = 0;
75                 int len;
76
77                 if (dp->d_type != DT_LNK)
78                         continue;
79
80                 if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
81                         continue;
82
83                 strcpy(path, dp->d_name);
84                 len = readlink(buf2, buf, sizeof(buf));
85                 if (len <= 0)
86                         continue;
87
88                 buf[len] = 0;
89                 if (!find_pattern(buf))
90                         continue;
91
92                 c = strrchr(buf, '/');
93                 if (!c)
94                         continue;
95
96                 c++;
97                 make_dev(c, block, major, minor);
98         }
99         closedir(dir);
100 }
101
102 static char *add_pattern(const char *name)
103 {
104         char *str = malloc(strlen(name) + 2);
105
106         str[0] = '*';
107         strcpy(str + 1, name);
108         return str;
109 }
110
111 int mkdev(const char *name, int _mode)
112 {
113         char *pattern;
114
115         if (chdir("/dev"))
116                 return 1;
117
118         pattern = add_pattern(name);
119         patterns = &pattern;
120         mode = _mode;
121         n_patterns = 1;
122         find_devs(true);
123         find_devs(false);
124         chdir("/");
125
126         return 0;
127 }