From c881c733bb05286f7147d0ca49ad238b86f6d848 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 19 Nov 2007 09:04:22 +0000 Subject: cleanup comments and coding style and rewrite the mdev.conf parser to use common busybox functions and call strdup() less often ... saves a ~few hundred bytes --- util-linux/mdev.c | 193 +++++++++++++++++++++++++++--------------------------- 1 file changed, 98 insertions(+), 95 deletions(-) (limited to 'util-linux/mdev.c') diff --git a/util-linux/mdev.c b/util-linux/mdev.c index a9c146942..927adf66f 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -35,154 +35,146 @@ static void make_device(char *path, int delete) /* Try to read major/minor string. Note that the kernel puts \n after * the data, so we don't need to worry about null terminating the string * because sscanf() will stop at the first nondigit, which \n is. We - * also depend on path having writeable space after it. */ - + * also depend on path having writeable space after it. + */ if (!delete) { strcat(path, "/dev"); len = open_read_close(path, temp + 1, 64); *temp++ = 0; - if (len < 1) return; + if (len < 1) + return; } /* Determine device name, type, major and minor */ - device_name = bb_basename(path); - type = path[5]=='c' ? S_IFCHR : S_IFBLK; - - /* If we have a config file, look up permissions for this device */ + type = (path[5] == 'c' ? S_IFCHR : S_IFBLK); if (ENABLE_FEATURE_MDEV_CONF) { - char *conf, *pos, *end; - int line, fd; + FILE *fp; + char *line, *vline; + size_t lineno = 0; - /* 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) + /* If we have a config file, look up the user settings */ + fp = fopen_or_warn("/etc/mdev.conf", "r"); + if (!fp) goto end_parse; - line = 0; - /* Loop through lines in mmaped file*/ - for (pos=conf; pos-confpw_uid; - } - s++; + else + uid = strtoul(str_uid, NULL, 10); + /* parse GID */ - gid = strtoul(s, &s2, 10); - if (end2 != s2) { - struct group *grp; - char *_grnam = xstrndup(s, end2-s); - grp = getgrnam(_grnam); - free(_grnam); - if (!grp) break; + grp = getgrnam(str_gid); + if (grp) gid = grp->gr_gid; - } - } - if (field == 2) { - /* mode */ + else + gid = strtoul(str_gid, NULL, 10); - mode = strtoul(pos, &pos, 8); - if (pos != end2) break; - } - if (ENABLE_FEATURE_MDEV_EXEC && field == 3) { - // Command to run + } else if (field == 2) { + + /* Mode device permissions */ + mode = strtoul(val, NULL, 8); + + } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) { + + /* Optional command to run */ const char *s = "@$*"; - const char *s2; - s2 = strchr(s, *pos++); + const char *s2 = strchr(s, *val); + if (!s2) { - // Force error + /* Force error */ field = 1; break; } - if ((s2-s+1) & (1< 2) break; - if (field) bb_error_msg_and_die("bad line %d",line); - - /* Next line */ - pos = ++end; + next_line: + free(line); + if (ENABLE_FEATURE_MDEV_EXEC) + free(orig_line); } - munmap(conf, len); + + if (ENABLE_FEATURE_CLEAN_UP) + fclose(fp); + end_parse: /* nothing */ ; } umask(0); if (!delete) { - if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; + 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); if (major == root_major && minor == root_minor) symlink(device_name, "root"); - if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); + if (ENABLE_FEATURE_MDEV_CONF) + chown(device_name, uid, gid); } if (command) { /* setenv will leak memory, so use putenv */ @@ -195,7 +187,8 @@ static void make_device(char *path, int delete) free(s); free(command); } - if (delete) unlink(device_name); + if (delete) + remove_file(device_name, FILEUTILS_FORCE); } /* File callback for /sys/ traversal */ @@ -296,9 +289,12 @@ int mdev_main(int argc, char **argv) xchdir("/dev"); - /* Scan */ - if (argc == 2 && !strcmp(argv[1],"-s")) { + + /* Scan: + * mdev -s + */ + struct stat st; xstat("/", &st); @@ -313,9 +309,14 @@ int mdev_main(int argc, char **argv) ACTION_RECURSE | ACTION_FOLLOWLINKS, fileAction, dirAction, temp, 0); - /* Hotplug */ - } else { + + /* Hotplug: + * env ACTION=... DEVPATH=... mdev + * ACTION can be "add" or "remove" + * DEVPATH is like "/block/sda" or "/class/input/mice" + */ + action = getenv("ACTION"); env_path = getenv("DEVPATH"); if (!action || !env_path) @@ -332,6 +333,8 @@ int mdev_main(int argc, char **argv) } } - if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); + if (ENABLE_FEATURE_CLEAN_UP) + RELEASE_CONFIG_BUFFER(temp); + return 0; } -- cgit v1.2.3