diff options
author | Rob Landley <rob@landley.net> | 2005-12-13 08:21:33 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-12-13 08:21:33 +0000 |
commit | 70f7ef7be385f782e57106df523f1c5e16fbdc47 (patch) | |
tree | e560de3934ac70ae03dd77695450cff91bdfce0b /util-linux/mdev.c | |
parent | 3858bf18d5d3b6a858ca46acb6c8628715520d1c (diff) | |
download | busybox-w32-70f7ef7be385f782e57106df523f1c5e16fbdc47.tar.gz busybox-w32-70f7ef7be385f782e57106df523f1c5e16fbdc47.tar.bz2 busybox-w32-70f7ef7be385f782e57106df523f1c5e16fbdc47.zip |
Nothing to see here. Move along.
Not buying it, eh?
I know I said new features before 1.1, but, well... (I was weak!)
The config file and hotplug modes aren't implemented yet. Might take a stab at
those tomorrow. (I _should_ go back to focusing on the bug triage list.)
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r-- | util-linux/mdev.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c new file mode 100644 index 000000000..49904d17a --- /dev/null +++ b/util-linux/mdev.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* vi:set ts=4: | ||
2 | * | ||
3 | * mdev - Mini udev for busybox | ||
4 | * | ||
5 | * Copyright 2005 Rob Landley <rob@landley.net> | ||
6 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | |||
11 | #include <dirent.h> | ||
12 | #include <errno.h> | ||
13 | #include <fcntl.h> | ||
14 | #include <stdio.h> | ||
15 | #include <string.h> | ||
16 | #include <sys/stat.h> | ||
17 | #include <sys/types.h> | ||
18 | #include <unistd.h> | ||
19 | |||
20 | #define DEV_PATH "/dev" | ||
21 | #define DEV_MODE 0660 | ||
22 | |||
23 | #include <busybox.h> | ||
24 | |||
25 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ | ||
26 | void make_device(char *path) | ||
27 | { | ||
28 | char *device_name, *s; | ||
29 | int major,minor,type,len,fd; | ||
30 | |||
31 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); | ||
32 | |||
33 | /* Try to read major/minor string */ | ||
34 | |||
35 | snprintf(temp, PATH_MAX, "%s/dev", path); | ||
36 | fd = open(temp, O_RDONLY); | ||
37 | len = read(fd, temp, PATH_MAX-1); | ||
38 | if (len<1) goto end; | ||
39 | temp[len] = 0; | ||
40 | close(fd); | ||
41 | |||
42 | /* Determine device name, type, major and minor */ | ||
43 | |||
44 | device_name = strrchr(path, '/') + 1; | ||
45 | type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK; | ||
46 | major = minor = 0; | ||
47 | for(s = temp; *s; s++) { | ||
48 | if(*s == ':') { | ||
49 | major = minor; | ||
50 | minor = 0; | ||
51 | } else { | ||
52 | minor *= 10; | ||
53 | minor += (*s) - '0'; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | /* Open config file here, look up permissions */ | ||
58 | |||
59 | sprintf(temp, "%s/%s", DEV_PATH, device_name); | ||
60 | if(mknod(temp, DEV_MODE | type, makedev(major, minor)) && errno != EEXIST) | ||
61 | bb_perror_msg_and_die("mknod %s failed", temp); | ||
62 | |||
63 | /* Perform shellout here */ | ||
64 | |||
65 | end: | ||
66 | RELEASE_CONFIG_BUFFER(temp); | ||
67 | } | ||
68 | |||
69 | /* Recursive search of /sys/block or /sys/class. path must be a writeable | ||
70 | * buffer of size PATH_MAX containing the directory string to start at. */ | ||
71 | |||
72 | void find_dev(char *path) | ||
73 | { | ||
74 | DIR *dir; | ||
75 | int len=strlen(path); | ||
76 | |||
77 | if(!(dir = opendir(path))) | ||
78 | bb_perror_msg_and_die("No %s",path); | ||
79 | |||
80 | for(;;) { | ||
81 | struct dirent *entry = readdir(dir); | ||
82 | |||
83 | if(!entry) break; | ||
84 | |||
85 | /* Skip "." and ".." (also skips hidden files, which is ok) */ | ||
86 | |||
87 | if (entry->d_name[0]=='.') continue; | ||
88 | |||
89 | if (entry->d_type == DT_DIR) { | ||
90 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); | ||
91 | find_dev(path); | ||
92 | path[len] = 0; | ||
93 | } | ||
94 | |||
95 | /* If there's a dev entry, mknod it */ | ||
96 | |||
97 | if (strcmp(entry->d_name, "dev")) make_device(path); | ||
98 | } | ||
99 | |||
100 | closedir(dir); | ||
101 | } | ||
102 | |||
103 | int mdev_main(int argc, char *argv[]) | ||
104 | { | ||
105 | if (argc > 1) { | ||
106 | if(argc == 2 && !strcmp(argv[1],"-s")) { | ||
107 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); | ||
108 | strcpy(temp,"/sys/block"); | ||
109 | find_dev(temp); | ||
110 | strcpy(temp,"/sys/class"); | ||
111 | find_dev(temp); | ||
112 | if(ENABLE_FEATURE_CLEAN_UP) | ||
113 | RELEASE_CONFIG_BUFFER(temp); | ||
114 | return 0; | ||
115 | } else bb_show_usage(); | ||
116 | } | ||
117 | |||
118 | /* hotplug support goes here */ | ||
119 | |||
120 | return 0; | ||
121 | } | ||