Reboot busybox style via procd
[project/procd.git] / initd / 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 "init.h"
30
31 #include "../log.h"
32
33 static char **patterns;
34 static int n_patterns;
35 static char buf[PATH_MAX];
36 static char buf2[PATH_MAX];
37 static unsigned int mode = 0600;
38
39 static bool find_pattern(const char *name)
40 {
41         int i;
42
43         for (i = 0; i < n_patterns; i++)
44                 if (!fnmatch(patterns[i], name, 0))
45                         return true;
46
47         return false;
48 }
49
50 static void make_dev(const char *path, bool block, int major, int minor)
51 {
52         unsigned int oldumask = umask(0);
53         unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
54
55         DEBUG(4, "Creating %s device %s(%d,%d)\n",
56                 block ? "block" : "character",
57                 path, major, minor);
58
59         mknod(path, _mode, makedev(major, minor));
60         umask(oldumask);
61 }
62
63 static void find_devs(bool block)
64 {
65         char *path = block ? "/sys/dev/block" : "/sys/dev/char";
66         struct dirent *dp;
67         DIR *dir;
68
69         dir = opendir(path);
70         if (!dir)
71                 return;
72
73         path = buf2 + sprintf(buf2, "%s/", path);
74         while ((dp = readdir(dir)) != NULL) {
75                 char *c;
76                 int major = 0, minor = 0;
77                 int len;
78
79                 if (dp->d_type != DT_LNK)
80                         continue;
81
82                 if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
83                         continue;
84
85                 strcpy(path, dp->d_name);
86                 len = readlink(buf2, buf, sizeof(buf));
87                 if (len <= 0)
88                         continue;
89
90                 buf[len] = 0;
91                 if (!find_pattern(buf))
92                         continue;
93
94                 c = strrchr(buf, '/');
95                 if (!c)
96                         continue;
97
98                 c++;
99                 make_dev(c, block, major, minor);
100         }
101         closedir(dir);
102 }
103
104 static char *add_pattern(const char *name)
105 {
106         char *str = malloc(strlen(name) + 2);
107
108         str[0] = '*';
109         strcpy(str + 1, name);
110         return str;
111 }
112
113 int mkdev(const char *name, int _mode)
114 {
115         char *pattern;
116
117         if (chdir("/dev"))
118                 return 1;
119
120         pattern = add_pattern(name);
121         patterns = &pattern;
122         mode = _mode;
123         n_patterns = 1;
124         find_devs(true);
125         find_devs(false);
126         chdir("/");
127
128         return 0;
129 }