add missing includes
[project/fstools.git] / libblkid-tiny / 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 _DEFAULT_SOURCE
16
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/sysmacros.h>
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdbool.h>
26 #include <dirent.h>
27 #include <limits.h>
28 #include <fnmatch.h>
29
30 #include "libblkid-tiny.h"
31
32 #include <syslog.h>
33
34 static char buf[PATH_MAX];
35 static char buf2[PATH_MAX];
36 static unsigned int mode = 0600;
37
38 static void make_dev(const char *path, bool block, int major, int minor)
39 {
40 unsigned int _mode = mode | (block ? S_IFBLK : S_IFCHR);
41
42 mknod(path, _mode, makedev(major, minor));
43 }
44
45 static void find_devs(bool block)
46 {
47 char *path = block ? "/sys/dev/block" : "/sys/dev/char";
48 struct dirent *dp;
49 DIR *dir;
50
51 dir = opendir(path);
52 if (!dir)
53 return;
54
55 path = buf2 + sprintf(buf2, "%s/", path);
56 while ((dp = readdir(dir)) != NULL) {
57 char *c;
58 int major = 0, minor = 0;
59 int len;
60
61 if (dp->d_type != DT_LNK)
62 continue;
63
64 if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
65 continue;
66
67 strcpy(path, dp->d_name);
68 len = readlink(buf2, buf, sizeof(buf));
69 if (len <= 0)
70 continue;
71
72 buf[len] = 0;
73
74 c = strrchr(buf, '/');
75 if (!c)
76 continue;
77
78
79 c++;
80 make_dev(c, block, major, minor);
81 }
82 closedir(dir);
83 }
84
85 int mkblkdev(void)
86 {
87 if (chdir("/dev"))
88 return 1;
89
90 mode = 0600;
91 find_devs(true);
92
93 return chdir("/");
94 }