2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #define _DEFAULT_SOURCE
18 #include <sys/types.h>
19 #include <sys/sysmacros.h>
32 static char **patterns;
33 static int n_patterns;
34 static char buf[PATH_MAX];
35 static char buf2[PATH_MAX];
36 static unsigned int mode = 0600;
38 static bool find_pattern(const char *name)
42 for (i = 0; i < n_patterns; i++)
43 if (!fnmatch(patterns[i], name, 0))
49 static void make_dev(const char *path, bool block, int major, int minor)
51 unsigned int oldumask = umask(0);
52 unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
54 DEBUG(4, "Creating %s device %s(%d,%d)\n",
55 block ? "block" : "character",
58 mknod(path, _mode, makedev(major, minor));
62 static void find_devs(bool block)
64 char *path = block ? "/sys/dev/block" : "/sys/dev/char";
72 path = buf2 + sprintf(buf2, "%s/", path);
73 while ((dp = readdir(dir)) != NULL) {
75 int major = 0, minor = 0;
78 if (dp->d_type != DT_LNK)
81 if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
84 strcpy(path, dp->d_name);
85 len = readlink(buf2, buf, sizeof(buf));
90 if (!find_pattern(buf))
93 c = strrchr(buf, '/');
98 make_dev(c, block, major, minor);
103 static char *add_pattern(const char *name)
105 char *str = malloc(strlen(name) + 2);
108 strcpy(str + 1, name);
112 int mkdev(const char *name, int _mode)
119 pattern = add_pattern(name);