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 GPL version 2, see file LICENSE in this tarball for details.
16 int root_major, root_minor;
18 #define G (*(struct globals*)&bb_common_bufsiz1)
19 #define root_major (G.root_major)
20 #define root_minor (G.root_minor)
22 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
23 static void make_device(char *path, int delete)
26 int major, minor, type, len;
30 char *temp = path + strlen(path);
33 /* Try to read major/minor string. Note that the kernel puts \n after
34 * the data, so we don't need to worry about null terminating the string
35 * because sscanf() will stop at the first nondigit, which \n is. We
36 * also depend on path having writeable space after it. */
40 len = open_read_close(path, temp + 1, 64);
45 /* Determine device name, type, major and minor */
47 device_name = strrchr(path, '/') + 1;
48 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
50 /* If we have a config file, look up permissions for this device */
52 if (ENABLE_FEATURE_MDEV_CONF) {
53 char *conf, *pos, *end;
56 /* mmap the config file */
57 fd = open("/etc/mdev.conf", O_RDONLY);
60 len = xlseek(fd, 0, SEEK_END);
61 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
67 /* Loop through lines in mmaped file*/
68 for (pos=conf; pos-conf<len;) {
73 /* find end of this line */
74 for (end=pos; end-conf<len && *end!='\n'; end++)
77 /* Three fields: regex, uid:gid, mode */
78 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
82 while (pos<end && isspace(*pos)) pos++;
83 if (pos==end || *pos=='#') break;
85 end2<end && !isspace(*end2) && *end2!='#'; end2++)
89 /* Regex to match this device */
91 char *regex = strndupa(pos, end2-pos);
97 xregcomp(&match,regex, REG_EXTENDED);
98 result = regexec(&match, device_name, 1, &off, 0);
101 /* If not this device, skip rest of line */
102 if (result || off.rm_so
103 || off.rm_eo != strlen(device_name))
112 for (s=pos; s<end2 && *s!=':'; s++)
114 if (s == end2) break;
117 uid = strtoul(pos, &s2, 10);
120 pass = getpwnam(strndupa(pos, s-pos));
126 gid = strtoul(s, &s2, 10);
129 grp = getgrnam(strndupa(s, end2-s));
137 mode = strtoul(pos, &pos, 8);
138 if (pos != end2) break;
140 if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
142 const char *s = "@$*";
144 s2 = strchr(s, *pos++);
150 if ((s2-s+1) & (1<<delete))
151 command = xstrndup(pos, end-pos);
157 /* Did everything parse happily? */
159 if (field > 2) break;
160 if (field) bb_error_msg_and_die("bad line %d",line);
166 end_parse: /* nothing */ ;
171 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
172 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
173 bb_perror_msg_and_die("mknod %s", device_name);
175 if (major == root_major && minor == root_minor)
176 symlink(device_name, "root");
178 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
181 /* setenv will leak memory, so use putenv */
182 char *s = xasprintf("MDEV=%s", device_name);
184 if (system(command) == -1)
185 bb_perror_msg_and_die("cannot run %s", command);
191 if (delete) unlink(device_name);
194 /* Recursive search of /sys/block or /sys/class. path must be a writeable
195 * buffer of size PATH_MAX containing the directory string to start at. */
197 static void find_dev(char *path)
200 size_t len = strlen(path);
201 struct dirent *entry;
207 while ((entry = readdir(dir)) != NULL) {
210 /* Skip "." and ".." (also skips hidden files, which is ok) */
212 if (entry->d_name[0] == '.')
215 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
217 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
218 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
221 /* If there's a dev entry, mknod it */
223 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
229 int mdev_main(int argc, char **argv);
230 int mdev_main(int argc, char **argv)
234 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
240 if (argc == 2 && !strcmp(argv[1],"-s")) {
244 root_major = major(st.st_dev);
245 root_minor = minor(st.st_dev);
246 strcpy(temp,"/sys/block");
248 strcpy(temp,"/sys/class");
254 action = getenv("ACTION");
255 env_path = getenv("DEVPATH");
256 if (!action || !env_path)
259 sprintf(temp, "/sys%s", env_path);
260 if (!strcmp(action, "add")) make_device(temp,0);
261 else if (!strcmp(action, "remove")) make_device(temp,1);
264 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);