1 /* vi: set sw=4 ts=4: */
4 * mdev - Mini udev for busybox
6 * Copyright 2005 Rob Landley <rob@landley.net>
7 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #define DEV_PATH "/dev"
19 int root_major, root_minor;
22 #define bbg mdev_globals
24 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
25 static void make_device(char *path, int delete)
28 int major, minor, type, len, fd;
32 char *temp = path + strlen(path);
35 /* Try to read major/minor string. Note that the kernel puts \n after
36 * the data, so we don't need to worry about null terminating the string
37 * because sscanf() will stop at the first nondigit, which \n is. We
38 * also depend on path having writeable space after it. */
41 fd = open(path, O_RDONLY);
42 len = read(fd, temp + 1, 64);
47 /* Determine device name, type, major and minor */
49 device_name = strrchr(path, '/') + 1;
50 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
51 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
53 /* If we have a config file, look up permissions for this device */
55 if (ENABLE_FEATURE_MDEV_CONF) {
56 char *conf, *pos, *end;
58 /* mmap the config file */
59 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
60 len = lseek(fd, 0, SEEK_END);
61 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
65 /* Loop through lines in mmaped file*/
66 for (pos=conf; pos-conf<len;) {
71 /* find end of this line */
72 for(end=pos; end-conf<len && *end!='\n'; end++)
75 /* Three fields: regex, uid:gid, mode */
76 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
80 while (pos<end && isspace(*pos)) pos++;
81 if (pos==end || *pos=='#') break;
83 end2<end && !isspace(*end2) && *end2!='#'; end2++)
87 /* Regex to match this device */
89 char *regex = strndupa(pos, end2-pos);
95 xregcomp(&match,regex, REG_EXTENDED);
96 result = regexec(&match, device_name, 1, &off, 0);
99 /* If not this device, skip rest of line */
100 if (result || off.rm_so
101 || off.rm_eo != strlen(device_name))
104 } else if (field == 1) {
110 for(s=pos; s<end2 && *s!=':'; s++)
112 if (s == end2) break;
115 uid = strtoul(pos,&s2,10);
118 pass = getpwnam(strndupa(pos, s-pos));
124 gid = strtoul(s, &s2, 10);
127 grp = getgrnam(strndupa(s, end2-s));
131 } else if (field == 2) {
134 mode = strtoul(pos, &pos, 8);
135 if (pos != end2) break;
136 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
138 char *s = "@$*", *s2;
139 if (!(s2 = strchr(s, *pos++))) {
144 if ((s2-s+1) & (1<<delete))
145 command = xstrndup(pos, end-pos);
151 /* Did everything parse happily? */
153 if (field > 2) break;
154 if (field) bb_error_msg_and_die("Bad line %d",line);
167 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
168 bb_perror_msg_and_die("mknod %s failed", device_name);
170 if (major == bbg.root_major && minor == bbg.root_minor)
171 symlink(device_name, "root");
173 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
179 s=xasprintf("MDEV=%s",device_name);
181 rc = system(command);
186 if (rc == -1) bb_perror_msg_and_die("Couldn't run %s", command);
188 if (delete) unlink(device_name);
191 /* Recursive search of /sys/block or /sys/class. path must be a writeable
192 * buffer of size PATH_MAX containing the directory string to start at. */
194 static void find_dev(char *path)
197 size_t len = strlen(path);
198 struct dirent *entry;
200 if ((dir = opendir(path)) == NULL)
203 while ((entry = readdir(dir)) != NULL) {
206 /* Skip "." and ".." (also skips hidden files, which is ok) */
208 if (entry->d_name[0] == '.')
211 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
213 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
214 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
217 /* If there's a dev entry, mknod it */
219 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
225 int mdev_main(int argc, char *argv[])
229 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
235 if (argc == 2 && !strcmp(argv[1],"-s")) {
238 stat("/", &st); // If this fails, we have bigger problems.
239 bbg.root_major=major(st.st_dev);
240 bbg.root_minor=minor(st.st_dev);
241 strcpy(temp,"/sys/block");
243 strcpy(temp,"/sys/class");
249 action = getenv("ACTION");
250 env_path = getenv("DEVPATH");
251 if (!action || !env_path)
254 sprintf(temp, "/sys%s", env_path);
255 if (!strcmp(action, "add")) make_device(temp,0);
256 else if (!strcmp(action, "remove")) make_device(temp,1);
259 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);