Hi all,
After a discution with Rob Landley I have again to modify mdev, now, we
can write in mdev.config:

<[path/]device regex> <uid:gid> <octal permissions> [<@|$|*> <command>]

snd/timer 0:29 0660 @ blah blah blah

this line creates timer device in /dev/snd

input/event[0-9] 0:0 0660

The patch is attached.

Regards.

Malek
--- busybox/util-linux/Config.in.orig	2007-05-24 11:30:47.000000000 +0200
+++ busybox/util-linux/Config.in	2007-05-23 20:26:05.000000000 +0200
@@ -294,6 +294,26 @@ config FEATURE_MDEV_EXEC
 
 	  For more information, please see docs/mdev.txt
 
+config FEATURE_MDEV_MKDIR
+	bool "Support sub-dir creation"
+	default n
+	depends on FEATURE_MDEV_CONF
+	help
+	  This option allows mdev to create sub-directories in /dev,
+	  for example:
+		/dev/bus/usb/001/
+		/dev/disk/by-label/
+
+config FEATURE_MDEV_SYMLINK
+	bool "Support symlinks creation"
+	default n
+	depends on FEATURE_MDEV_CONF
+	help
+	  This option allows mdev to create symlinks in /dev,
+	  for example:
+		ram -> ram1
+		stderr -> /proc/self/fd/2
+
 config MKSWAP
 	bool "mkswap"
 	default n
--- busybox/util-linux/mdev.c.orig	2007-05-12 09:20:16.000000000 +0200
+++ busybox/util-linux/mdev.c	2007-05-24 10:05:34.000000000 +0200
@@ -24,9 +24,9 @@ struct mdev_globals
 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
 static void make_device(char *path, int delete)
 {
-	char *device_name;
-	int major, minor, type, len;
-	int mode = 0660;
+	char *device_name, *device_path = NULL;
+	int major, minor, type, len, mk_dir=0;
+	mode_t mode = 0660, mode_path = 0755;
 	uid_t uid = 0;
 	gid_t gid = 0;
 	char *temp = path + strlen(path);
@@ -47,129 +47,154 @@ static void make_device(char *path, int 
 	/* Determine device name, type, major and minor */
 
 	device_name = strrchr(path, '/') + 1;
-	type = path[5]=='c' ? S_IFCHR : S_IFBLK;
+	type = (path[5]=='b') ? S_IFBLK : S_IFCHR;
 
 	/* If we have a config file, look up permissions for this device */
 
 	if (ENABLE_FEATURE_MDEV_CONF) {
-		char *conf, *pos, *end;
-		int line, fd;
+		char *line, *tok_tmp, *tok_line=NULL, **tok = NULL;
+		FILE *file;
+		int i;
 
-		/* mmap the config file */
-		fd = open("/etc/mdev.conf", O_RDONLY);
-		if (fd < 0)
-			goto end_parse;
-		len = xlseek(fd, 0, SEEK_END);
-		conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
-		close(fd);
-		if (!conf)
+		file = fopen_or_warn("/etc/mdev.conf", "r");
+		if (file < 0)
 			goto end_parse;
 
-		line = 0;
-		/* Loop through lines in mmaped file*/
-		for (pos=conf; pos-conf<len;) {
-			int field;
-			char *end2;
+		while ((line = xmalloc_getline(file)) != NULL) {
+			char *regex = NULL;
+			regex_t match;
+			regmatch_t off;
+			int result, tok_len = 1;
+			char *tok_id[2], *tok_id_tmp;
+			char *s;
 
-			line++;
-			/* find end of this line */
-			for (end=pos; end-conf<len && *end!='\n'; end++)
-				;
+			tok_line = strdupa(line);
+			if (tok_line[0] == '#' || strlen(tok_line)==0) continue;
 
-			/* Three fields: regex, uid:gid, mode */
-			for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
-					field++)
-			{
-				/* Skip whitespace */
-				while (pos<end && isspace(*pos)) pos++;
-				if (pos==end || *pos=='#') break;
-				for (end2=pos;
-					end2<end && !isspace(*end2) && *end2!='#'; end2++)
-					;
+			for (i=0; i<strlen(tok_line); i++) {
+				if (isspace(tok_line[i]) && !isspace(tok_line[i+1]))
+					tok_len++;
+			}
+			tok = (char **) xrealloc(tok, tok_len * sizeof(char *));
 
-				if (field == 0) {
-					/* Regex to match this device */
+			for (i=0; (tok_tmp=strtok(tok_line, " \t")); i++) {
+				tok[i] = tok_tmp;
+				tok_line = NULL;
+			}
 
-					char *regex = strndupa(pos, end2-pos);
-					regex_t match;
-					regmatch_t off;
-					int result;
+			if (tok_len == 2 && ENABLE_FEATURE_MDEV_MKDIR) {
+				mode_path = (mode_t)strtoul(tok[1], &s, 8);
+				bb_make_directory(tok[0], mode_path, FILEUTILS_RECUR);
+				continue;
+			}
 
-					/* Is this it? */
-					xregcomp(&match,regex, REG_EXTENDED);
-					result = regexec(&match, device_name, 1, &off, 0);
-					regfree(&match);
+			if (!strcmp(tok[1], "->") && ENABLE_FEATURE_MDEV_SYMLINK) {
+				if (symlink(tok[2], tok[0]) && errno != EEXIST)
+					bb_perror_msg_and_die("symlink %s", tok[0]);
+				continue;
+			}
 
-					/* If not this device, skip rest of line */
-					if (result || off.rm_so
-							|| off.rm_eo != strlen(device_name))
-						break;
-				}
-				if (field == 1) {
-					/* uid:gid */
+			/* Regex to match this device */
+			if ((regex = strrchr(tok[0], '/')))
+				regex += 1;
+			else
+				regex = tok[0];
 
-					char *s, *s2;
+			xregcomp(&match,regex, REG_EXTENDED);
+			result = regexec(&match, device_name, 1, &off, 0);
+			regfree(&match);
 
-					/* Find : */
-					for (s=pos; s<end2 && *s!=':'; s++)
-						;
-					if (s == end2) break;
+			/* If not this device, skip rest of line */
+			if (result || off.rm_so || off.rm_eo != strlen(device_name))
+				continue;
 
-					/* Parse UID */
-					uid = strtoul(pos, &s2, 10);
-					if (s != s2) {
-						struct passwd *pass;
-						pass = getpwnam(strndupa(pos, s-pos));
-						if (!pass) break;
-						uid = pass->pw_uid;
-					}
-					s++;
-					/* parse GID */
-					gid = strtoul(s, &s2, 10);
-					if (end2 != s2) {
-						struct group *grp;
-						grp = getgrnam(strndupa(s, end2-s));
-						if (!grp) break;
-						gid = grp->gr_gid;
-					}
-				}
-				if (field == 2) {
-					/* mode */
+			if (strcmp(regex, tok[0])) {
+				mk_dir = 1;
+				device_path = strndupa(tok[0], strlen(tok[0])-strlen(regex));
+				mode_path = 0755;
+			}
 
-					mode = strtoul(pos, &pos, 8);
-					if (pos != end2) break;
-				}
-				if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
-					// Command to run
-					const char *s = "@$*";
-					const char *s2;
-					s2 = strchr(s, *pos++);
-					if (!s2) {
-						// Force error
-						field = 1;
-						break;
-					}
-					if ((s2-s+1) & (1<<delete))
-						command = xstrndup(pos, end-pos);
-				}
 
-				pos = end2;
+			for (i=0; (tok_id_tmp=strtok(tok[1], ":")); i++) {
+				if (tok_id_tmp) tok_id[i] = tok_id_tmp;
+				tok[1] = NULL;
 			}
 
-			/* Did everything parse happily? */
+			/* uid:gid */
+			uid = strtoul(tok_id[0], &s, 10);
+			if (tok_id[0] == s) {
+				struct passwd *pass;
+				pass = getpwnam(tok_id[0]);
+				if (!pass) continue;
+				uid = pass->pw_uid;
+			}
 
-			if (field > 2) break;
-			if (field) bb_error_msg_and_die("bad line %d",line);
+			gid = strtoul(tok_id[1], &s, 10);
+			if (tok_id[1] == s) {
+				struct group *grp;
+				grp = getgrnam(tok_id[1]);
+				if (!grp) continue;
+				gid = grp->gr_gid;
+			}
 
-			/* Next line */
-			pos = ++end;
+			/* mode */
+			mode = (mode_t)strtoul(tok[2], &s, 8);
+
+			if (tok_len > 3 && ENABLE_FEATURE_MDEV_EXEC) {
+				const char *s2 = "@$*";
+				unsigned int cmd = 0;
+
+				if (strpbrk(tok[3], s2)) {
+					int command_len = 0;
+					int cmd_len = strlen(tok[3]) == 1 ? 1 : 0;
+
+					cmd = *tok[3];
+
+					for (i=4+cmd_len; i<tok_len; i++)
+						command_len += strlen(tok[i]);
+					command_len += i-cmd_len;
+
+					command = xzalloc(command_len);
+					if (cmd_len == 1) {
+						strcat(command, tok[4]);
+					} else {
+						strcat(command, strrchr(tok[3], tok[3][1]));
+					}
+
+					for (i=4+cmd_len; i<tok_len; i++)
+						strcat(strcat(command, " "), tok[i]);
+				}
+
+				switch (cmd) {
+				case '@':
+					if (delete){
+						free(command);
+						command = NULL;
+					}
+					break;
+				case '$':
+					if (!delete) {
+						free(command);
+						command = NULL;
+					}
+					break;
+				case '*':
+				default :
+					break;
+				}
+			}
+			free(line);
 		}
-		munmap(conf, len);
+		fclose(file);
  end_parse:	/* nothing */ ;
 	}
 
 	umask(0);
 	if (!delete) {
+		if (mk_dir == 1) {
+			bb_make_directory(device_path, mode_path, FILEUTILS_RECUR);
+			device_name = strcat(device_path, device_name);
+		}
 		if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
 		if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
 			bb_perror_msg_and_die("mknod %s", device_name);
@@ -179,6 +204,7 @@ static void make_device(char *path, int 
 
 		if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
 	}
+
 	if (command) {
 		int rc;
 		char *s;
@@ -189,45 +215,65 @@ static void make_device(char *path, int 
 		s[4] = 0;
 		putenv(s);
 		free(s);
-		free(command);
 		if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
 	}
-	if (delete) unlink(device_name);
+
+	if (delete) {
+		if (device_path) {
+			char *tmp_path;
+
+			tmp_path = strdupa(device_path);
+			device_name = strcat(tmp_path, device_name);
+		}
+
+		unlink(device_name);
+
+		if (device_path) {
+			remove_file(device_path, FILEUTILS_FORCE | FILEUTILS_RECUR);
+		}
+	}
+
 }
 
 /* Recursive search of /sys/block or /sys/class.  path must be a writeable
  * buffer of size PATH_MAX containing the directory string to start at. */
 
-static void find_dev(char *path)
+static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
 {
-	DIR *dir;
-	size_t len = strlen(path);
-	struct dirent *entry;
+	char *last;
 
-	dir = opendir(path);
-	if (dir == NULL)
-		return;
+	last = bb_get_last_path_component((char *)fileName);
 
-	while ((entry = readdir(dir)) != NULL) {
-		struct stat st;
+	if(!strcmp(last, "subsystem")) return FALSE;
 
-		/* Skip "." and ".." (also skips hidden files, which is ok) */
+	if (S_ISLNK(statbuf->st_mode)) {
+		char *link_path, *new_path, *tmp_link;
+		RESERVE_CONFIG_BUFFER(device_path, PATH_MAX);
 
-		if (entry->d_name[0] == '.')
-			continue;
+		tmp_link = xmalloc_readlink_or_warn(fileName);
+		link_path = strdupa(tmp_link);
+		free(tmp_link);
+		new_path = link_path;
 
-		// uClibc doesn't fill out entry->d_type reliably. so we use lstat().
+		while (!strncmp(new_path, "../", 3)) {
+			new_path = new_path + 3;
+		}
 
-		snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
-		if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
-		path[len] = 0;
+		sprintf(device_path, "/sys/%s", new_path);
+		make_device(device_path, 0);
+		free(device_path);
+		return TRUE;
+	}
 
-		/* If there's a dev entry, mknod it */
+	if (!strcmp(last, "dev")) {
+		char *tmp_name;
 
-		if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
+		tmp_name = strndupa(fileName, strlen(fileName) - 4);
+		make_device(tmp_name, 0);
+		return TRUE;
 	}
 
-	closedir(dir);
+	return FALSE;
 }
 
 int mdev_main(int argc, char **argv);
@@ -248,9 +294,9 @@ int mdev_main(int argc, char **argv)
 		bbg.root_major = major(st.st_dev);
 		bbg.root_minor = minor(st.st_dev);
 		strcpy(temp,"/sys/block");
-		find_dev(temp);
+		recursive_action(temp, ACTION_RECURSE, fileAction, 0, NULL, 0);
 		strcpy(temp,"/sys/class");
-		find_dev(temp);
+		recursive_action(temp, ACTION_RECURSE, fileAction, 0, NULL, 0);
 
 	/* Hotplug */
 
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to