diff options
| author | Rob Landley <rob@landley.net> | 2006-05-08 02:53:23 +0000 |
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2006-05-08 02:53:23 +0000 |
| commit | 15fe2e11d7886d04450cabc8b40f0d396b6b6d85 (patch) | |
| tree | 5bd4757d5fd5076e8caeee1866f66e942c8f454d /util-linux | |
| parent | 17507fa4c506269f169ff8e99948086ced58f89f (diff) | |
| download | busybox-w32-15fe2e11d7886d04450cabc8b40f0d396b6b6d85.tar.gz busybox-w32-15fe2e11d7886d04450cabc8b40f0d396b6b6d85.tar.bz2 busybox-w32-15fe2e11d7886d04450cabc8b40f0d396b6b6d85.zip | |
Shrink the code about 50 bytes, allocate less run-time memory, and add a
comment that null terminating the string we sscanf() shouldn't be required
since the kernel adds \n to the end of it and sscanf will stop there.
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/mdev.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 5fea1b8e2..73938f8de 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
| @@ -38,23 +38,25 @@ static void make_device(char *path) | |||
| 38 | int mode = 0660; | 38 | int mode = 0660; |
| 39 | uid_t uid = 0; | 39 | uid_t uid = 0; |
| 40 | gid_t gid = 0; | 40 | gid_t gid = 0; |
| 41 | char *temp = path + strlen(path); | ||
| 41 | 42 | ||
| 42 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX); | 43 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 44 | * the data, so we don't need to worry about null terminating the string | ||
| 45 | * because sscanf() will stop at the first nondigit, which \n is. We | ||
| 46 | * also depend on path having writeable space after it. */ | ||
| 43 | 47 | ||
| 44 | /* Try to read major/minor string */ | 48 | strcat(path, "/dev"); |
| 45 | 49 | fd = open(path, O_RDONLY); | |
| 46 | snprintf(temp, PATH_MAX, "%s/dev", path); | 50 | len = read(fd, temp + 1, 64); |
| 47 | fd = open(temp, O_RDONLY); | 51 | *temp++ = 0; |
| 48 | len = read(fd, temp, PATH_MAX-1); | ||
| 49 | close(fd); | 52 | close(fd); |
| 50 | if (len < 1) goto end; | 53 | if (len < 1) return; |
| 51 | 54 | ||
| 52 | /* Determine device name, type, major and minor */ | 55 | /* Determine device name, type, major and minor */ |
| 53 | 56 | ||
| 54 | device_name = strrchr(path, '/') + 1; | 57 | device_name = strrchr(path, '/') + 1; |
| 55 | type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK; | 58 | type = path[5]=='c' ? S_IFCHR : S_IFBLK; |
| 56 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) | 59 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; |
| 57 | goto end; | ||
| 58 | 60 | ||
| 59 | /* If we have a config file, look up permissions for this device */ | 61 | /* If we have a config file, look up permissions for this device */ |
| 60 | 62 | ||
| @@ -167,18 +169,14 @@ found_device: | |||
| 167 | } | 169 | } |
| 168 | } | 170 | } |
| 169 | 171 | ||
| 170 | sprintf(temp, "%s/%s", DEV_PATH, device_name); | ||
| 171 | umask(0); | 172 | umask(0); |
| 172 | if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST) | 173 | if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
| 173 | bb_perror_msg_and_die("mknod %s failed", temp); | 174 | bb_perror_msg_and_die("mknod %s failed", device_name); |
| 174 | 175 | ||
| 175 | if (major==root_major && minor==root_minor) | 176 | if (major==root_major && minor==root_minor) |
| 176 | symlink(temp,DEV_PATH "/root"); | 177 | symlink(device_name, "root"); |
| 177 | 178 | ||
| 178 | if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid); | 179 | if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); |
| 179 | |||
| 180 | end: | ||
| 181 | RELEASE_CONFIG_BUFFER(temp); | ||
| 182 | } | 180 | } |
| 183 | 181 | ||
| 184 | /* Recursive search of /sys/block or /sys/class. path must be a writeable | 182 | /* Recursive search of /sys/block or /sys/class. path must be a writeable |
| @@ -220,6 +218,8 @@ int mdev_main(int argc, char *argv[]) | |||
| 220 | char *env_path; | 218 | char *env_path; |
| 221 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); | 219 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 222 | 220 | ||
| 221 | bb_xchdir(DEV_PATH); | ||
| 222 | |||
| 223 | /* Scan */ | 223 | /* Scan */ |
| 224 | 224 | ||
| 225 | if (argc == 2 && !strcmp(argv[1],"-s")) { | 225 | if (argc == 2 && !strcmp(argv[1],"-s")) { |
| @@ -245,8 +245,7 @@ int mdev_main(int argc, char *argv[]) | |||
| 245 | sprintf(temp, "/sys%s", env_path); | 245 | sprintf(temp, "/sys%s", env_path); |
| 246 | make_device(temp); | 246 | make_device(temp); |
| 247 | } else if (!strcmp(action, "remove")) { | 247 | } else if (!strcmp(action, "remove")) { |
| 248 | sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1); | 248 | unlink(strrchr(env_path, '/') + 1); |
| 249 | unlink(temp); | ||
| 250 | } | 249 | } |
| 251 | } | 250 | } |
| 252 | 251 | ||
