OLD | NEW |
1 /* Taken from util-linux source. GPLv2. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Emits the current rootfs device. | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * Works by searching /dev recursively for a BLK device with the same device | 3 * found in the LICENSE file. |
4 * number as '/'. | 4 * |
| 5 * Implements root device discovery via sysfs with optional bells and whistles. |
5 */ | 6 */ |
6 | 7 |
| 8 #include "rootdev.h" |
| 9 |
| 10 #include <ctype.h> |
| 11 #include <dirent.h> |
| 12 #include <err.h> |
| 13 #include <errno.h> |
| 14 #include <fcntl.h> |
| 15 #include <stdbool.h> |
| 16 #include <stddef.h> |
7 #include <stdio.h> | 17 #include <stdio.h> |
8 #include <err.h> | 18 #include <stdlib.h> |
| 19 #include <string.h> |
| 20 #include <sys/stat.h> |
9 #include <sys/types.h> | 21 #include <sys/types.h> |
10 #include <dirent.h> | 22 #include <unistd.h> |
11 #include <sys/stat.h> | 23 |
12 #include <string.h> | 24 static const char *kDefaultSearchPath = "/sys/block"; |
13 | 25 static const char *kDefaultDevPath = "/dev"; |
14 | 26 |
15 static int | 27 /* Encode the root device structuring here for Chromium OS */ |
16 find_dev_recursive(char *dirnamebuf, int number, int deviceOnly) { | 28 static const char kActiveRoot[] = "/dev/ACTIVE_ROOT"; |
17 DIR *dp; | 29 static const char kRootDev[] = "/dev/ROOT"; |
18 struct dirent *dir; | 30 static const char kRootA[] = "/dev/ROOT0"; |
19 struct stat s; | 31 static const char kRootB[] = "/dev/ROOT1"; |
20 int dirnamelen = 0; | 32 |
21 | 33 struct part_config { |
22 if ((dp = opendir(dirnamebuf)) == NULL) | 34 const char *name; |
23 err(1, "can't read directory %s", dirnamebuf); | 35 int offset; |
24 dirnamelen = strlen(dirnamebuf); | 36 }; |
25 while ((dir = readdir(dp)) != NULL) { | 37 |
26 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) | 38 #define CHROMEOS_PRIMARY_PARTITION 3 |
27 continue; | 39 static const struct part_config kPrimaryPart[] = { { kRootA, 0 }, |
28 if (dirnamelen + 1 + strlen(dir->d_name) > PATH_MAX) | 40 { kRootDev, -3 }, |
29 continue; | 41 { kRootB, 2 } }; |
30 dirnamebuf[dirnamelen] = '/'; | 42 #define CHROMEOS_SECONDARY_PARTITION 5 |
31 strcpy(dirnamebuf+dirnamelen+1, dir->d_name); | 43 static const struct part_config kSecondaryPart[] = { { kRootB, 0 }, |
32 if (lstat(dirnamebuf, &s) < 0) | 44 { kRootDev, -5 }, |
33 continue; | 45 { kRootA, -2 } }; |
34 if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number){ | 46 |
35 if (deviceOnly) { | 47 /* The number of entries in a part_config so we could add RootC easily. */ |
36 int len = strlen(dirnamebuf); | 48 static const int kPartitionEntries = 3; |
37 char c = 0; | 49 |
38 do { | 50 /* Converts a file of %u:%u -> dev_t. */ |
39 c = dirnamebuf[len-1]; | 51 static dev_t devt_from_file(const char *file) { |
40 --len; | 52 char candidate[10]; /* TODO(wad) system-provided constant? */ |
41 }while(c > 0 && c < 9 && len > 0); | 53 ssize_t bytes = 0; |
42 /* arm has "p" for partition */ | 54 unsigned int major = 0; |
43 if (dirnamebuf[len-1] == 'p') | 55 unsigned int minor = 0; |
44 --len; | 56 dev_t dev = 0; |
45 dirnamebuf[len]='\0'; | 57 int fd = -1; |
46 } | 58 |
47 return 1; | 59 /* Never hang. Either get the data or return 0. */ |
48 } | 60 fd = open(file, O_NONBLOCK | O_RDONLY); |
49 if ((s.st_mode & S_IFMT) == S_IFDIR && | 61 if (fd < 0) |
50 find_dev_recursive(dirnamebuf, number, deviceOnly)) | 62 return 0; |
51 return 1; | 63 bytes = read(fd, candidate, sizeof(candidate)); |
52 } | 64 close(fd); |
53 dirnamebuf[dirnamelen] = 0; | 65 |
54 closedir(dp); | 66 /* 0:0 should be considered the minimum size. */ |
55 return 0; | 67 if (bytes < 3) |
56 } | 68 return 0; |
57 | 69 candidate[bytes] = 0; |
58 void usage(){ | 70 if (sscanf(candidate, "%u:%u", &major, &minor) == 2) { |
59 printf ("rootdev \n\t-d (for device only)\n"); | 71 /* candidate's size artificially limits the size of the converted |
60 } | 72 * %u to safely convert to a signed int. */ |
61 | 73 dev = makedev(major, minor); |
62 int main(int argc, char *argv[]) { | 74 } |
63 struct stat s; | 75 return dev; |
64 char *file = "/"; | 76 } |
65 static char name[PATH_MAX+1]; | 77 |
66 int deviceOnly=0; | 78 /* Walks sysfs and will recurse into any directory/link that represents |
67 int c; | 79 * a block device to find sub-devices (partitions). |
68 extern char *optarg; | 80 * If dev == 0, the first device in the directory will be returned. */ |
69 extern int optind, optopt; | 81 static int match_sysfs_device(char *name, size_t name_len, |
70 while ((c = getopt(argc, argv, "hd")) != -1) { | 82 const char *basedir, dev_t *dev) { |
71 switch(c) { | 83 int found = -1; |
72 case 'd': | 84 size_t basedir_len; |
73 deviceOnly=1; | 85 DIR *dirp = NULL; |
74 break; | 86 struct dirent *entry = NULL; |
75 case 'h': | 87 struct dirent *next = NULL; |
76 default: | 88 char *working_path = NULL; |
77 usage(); | 89 long working_path_size = 0; |
78 return 1; | 90 |
79 } | 91 if (!name || !name_len || !basedir || !dev) { |
80 } | 92 warnx("match_sysfs_device: invalid arguments supplied"); |
81 if (argc - optind >= 1) | 93 return -1; |
82 file = argv[optind]; | 94 } |
83 | 95 basedir_len = strlen(basedir); |
84 if (stat(file, &s) < 0) | 96 if (!basedir_len) { |
85 err(1, "unable to stat %s", file); | 97 warnx("match_sysfs_device: basedir must not be empty"); |
86 | 98 return -1; |
87 if (!s.st_dev) | 99 } |
88 err(1, "unknown device number 0"); | 100 |
89 | 101 errno = 0; |
90 strcpy(name, "/dev"); | 102 dirp = opendir(basedir); |
91 | 103 if (!dirp) { |
92 if (!find_dev_recursive(name, s.st_dev, deviceOnly)) { | 104 /* Don't complain if the directory doesn't exist. */ |
93 fprintf(stderr, "unable to find match\n"); | 105 if (errno != ENOENT) |
94 return 1; | 106 warn("match_sysfs_device:opendir(%s)", basedir); |
95 } | 107 return found; |
96 | 108 } |
97 printf("%s\n", name); | 109 |
98 | 110 /* Grab a platform appropriate path to work with. |
99 return 0; | 111 * Ideally, this won't vary under sys/block. */ |
100 } | 112 working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1; |
| 113 /* Fallback to PATH_MAX on any pathconf error. */ |
| 114 if (working_path_size < 0) |
| 115 working_path_size = PATH_MAX; |
| 116 |
| 117 working_path = malloc(working_path_size); |
| 118 if (!working_path) { |
| 119 warn("malloc(dirent)"); |
| 120 closedir(dirp); |
| 121 return found; |
| 122 } |
| 123 |
| 124 /* Allocate a properly sized entry. */ |
| 125 entry = malloc(offsetof(struct dirent, d_name) + working_path_size); |
| 126 if (!entry) { |
| 127 warn("malloc(dirent)"); |
| 128 free(working_path); |
| 129 closedir(dirp); |
| 130 return found; |
| 131 } |
| 132 |
| 133 while (readdir_r(dirp, entry, &next) == 0 && next) { |
| 134 size_t candidate_len = strlen(entry->d_name); |
| 135 size_t path_len = 0; |
| 136 dev_t found_devt = 0; |
| 137 /* Ignore the usual */ |
| 138 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) |
| 139 continue; |
| 140 /* TODO(wad) determine how to best bubble up this case. */ |
| 141 if (candidate_len > name_len) |
| 142 continue; |
| 143 /* Only traverse directories or symlinks (to directories ideally) */ |
| 144 switch (entry->d_type) { |
| 145 case DT_UNKNOWN: |
| 146 case DT_DIR: |
| 147 case DT_LNK: |
| 148 break; |
| 149 default: |
| 150 continue; |
| 151 } |
| 152 /* Determine path to block device number */ |
| 153 path_len = snprintf(working_path, working_path_size, "%s/%s/dev", |
| 154 basedir, entry->d_name); |
| 155 /* Ignore if truncation occurs. */ |
| 156 if (path_len != candidate_len + basedir_len + 5) |
| 157 continue; |
| 158 |
| 159 found_devt = devt_from_file(working_path); |
| 160 /* *dev == 0 is a wildcard. */ |
| 161 if (!*dev || found_devt == *dev) { |
| 162 snprintf(name, name_len, "%s", entry->d_name); |
| 163 *dev = found_devt; |
| 164 found = 1; |
| 165 break; |
| 166 } |
| 167 |
| 168 /* Recurse one level for devices that may have a matching partition. */ |
| 169 if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) { |
| 170 sprintf(working_path, "%s/%s", basedir, entry->d_name); |
| 171 found = match_sysfs_device(name, name_len, working_path, dev); |
| 172 if (found > 0) |
| 173 break; |
| 174 } |
| 175 } |
| 176 |
| 177 free(working_path); |
| 178 free(entry); |
| 179 closedir(dirp); |
| 180 return found; |
| 181 } |
| 182 |
| 183 const char *rootdev_get_partition(const char *dst, size_t len) { |
| 184 const char *end = dst + strnlen(dst, len); |
| 185 const char *part = end - 1; |
| 186 if (!len) |
| 187 return NULL; |
| 188 |
| 189 if (!isdigit(*part--)) |
| 190 return NULL; |
| 191 |
| 192 while (part > dst && isdigit(*part)) part--; |
| 193 part++; |
| 194 |
| 195 if (part >= end) |
| 196 return NULL; |
| 197 |
| 198 return part; |
| 199 } |
| 200 |
| 201 void rootdev_strip_partition(char *dst, size_t len) { |
| 202 char *part = (char *)rootdev_get_partition(dst, len); |
| 203 if (!part) |
| 204 return; |
| 205 /* For devices that end with a digit, the kernel uses a 'p' |
| 206 * as a separator. E.g., mmcblk1p2. */ |
| 207 if (*(part - 1) == 'p') |
| 208 part--; |
| 209 *part = '\0'; |
| 210 } |
| 211 |
| 212 int rootdev_symlink_active(const char *path) { |
| 213 int ret = 0; |
| 214 /* Don't overwrite an existing link. */ |
| 215 errno = 0; |
| 216 if ((symlink(path, kActiveRoot)) && errno != EEXIST) { |
| 217 warn("failed to symlink %s -> %s", kActiveRoot, path); |
| 218 ret = -1; |
| 219 } |
| 220 return ret; |
| 221 } |
| 222 |
| 223 int rootdev_get_device(char *dst, size_t size, dev_t dev, |
| 224 const char *search) { |
| 225 struct stat active_root_statbuf; |
| 226 |
| 227 if (search == NULL) |
| 228 search = kDefaultSearchPath; |
| 229 |
| 230 /* Check if the -s symlink exists. */ |
| 231 if ((stat(kActiveRoot, &active_root_statbuf) == 0) && |
| 232 active_root_statbuf.st_rdev == dev) { |
| 233 /* Note, if the link is not fully qualified, this won't be |
| 234 * either. */ |
| 235 ssize_t len = readlink(kActiveRoot, dst, PATH_MAX); |
| 236 if (len > 0) { |
| 237 dst[len] = 0; |
| 238 return 0; |
| 239 } |
| 240 /* If readlink fails or is empty, fall through */ |
| 241 } |
| 242 |
| 243 snprintf(dst, size, "%s", search); |
| 244 if (match_sysfs_device(dst, size, dst, &dev) <= 0) { |
| 245 fprintf (stderr, "unable to find match\n"); |
| 246 return 1; |
| 247 } |
| 248 |
| 249 return 0; |
| 250 } |
| 251 |
| 252 int rootdev_get_device_slave(char *slave, size_t size, dev_t *dev, |
| 253 const char *device, const char *search) { |
| 254 char dst[PATH_MAX]; |
| 255 int len = 0; |
| 256 |
| 257 if (search == NULL) |
| 258 search = kDefaultSearchPath; |
| 259 |
| 260 /* So far, I've only seen top-level block devices with slaves. */ |
| 261 len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, device); |
| 262 if (len < 0 || len != strlen(device) + strlen(search) + 8) { |
| 263 warnx("rootdev_get_device_slave: device name too long"); |
| 264 return -1; |
| 265 } |
| 266 *dev = 0; |
| 267 if (match_sysfs_device(slave, size, dst, dev) <= 0) |
| 268 return -1; |
| 269 |
| 270 return 0; |
| 271 } |
| 272 |
| 273 int rootdev_create_devices(const char *name, dev_t dev, bool symlink) { |
| 274 int ret = 0; |
| 275 unsigned int major = major(dev); |
| 276 unsigned int minor = minor(dev); |
| 277 int i; |
| 278 const struct part_config *config; |
| 279 const char *part_s = rootdev_get_partition(name, strlen(name)); |
| 280 |
| 281 if (part_s == NULL) { |
| 282 warnx("create_devices: unable to determine partition"); |
| 283 return -1; |
| 284 } |
| 285 |
| 286 switch (atoi(part_s)) { |
| 287 case CHROMEOS_PRIMARY_PARTITION: |
| 288 config = kPrimaryPart; |
| 289 break; |
| 290 case CHROMEOS_SECONDARY_PARTITION: |
| 291 config = kSecondaryPart; |
| 292 break; |
| 293 default: |
| 294 warnx("create_devices: unable to determine partition: %s", |
| 295 part_s); |
| 296 return -1; |
| 297 } |
| 298 |
| 299 for (i = 0; i < kPartitionEntries; ++i) { |
| 300 dev = makedev(major, minor + config[i].offset); |
| 301 errno = 0; |
| 302 if (mknod(config[i].name, |
| 303 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 304 dev) && errno != EEXIST) { |
| 305 warn("failed to create %s", config[i].name); |
| 306 return -1; |
| 307 } |
| 308 } |
| 309 |
| 310 if (symlink) |
| 311 ret = rootdev_symlink_active(config[0].name); |
| 312 return ret; |
| 313 } |
| 314 |
| 315 int rootdev_get_path(char *path, size_t size, const char *device, |
| 316 dev_t dev, const char *dev_path) { |
| 317 int path_len; |
| 318 struct stat dev_statbuf; |
| 319 |
| 320 if (!dev_path) |
| 321 dev_path = kDefaultDevPath; |
| 322 |
| 323 if (!path || !size || !device) |
| 324 return -1; |
| 325 |
| 326 path_len = snprintf(path, size, "%s/%s", dev_path, device); |
| 327 if (path_len != strlen(dev_path) + 1 + strlen(device)) |
| 328 return -1; |
| 329 |
| 330 if (stat(path, &dev_statbuf) != 0) |
| 331 return 1; |
| 332 |
| 333 if (dev && dev != dev_statbuf.st_rdev) |
| 334 return 2; |
| 335 |
| 336 return 0; |
| 337 } |
| 338 |
| 339 int rootdev_wrapper(char *path, size_t size, |
| 340 bool full, bool strip, |
| 341 dev_t *dev, |
| 342 const char *search, const char *dev_path) { |
| 343 int res = 0; |
| 344 char devname[PATH_MAX]; |
| 345 if (!search) |
| 346 search = kDefaultSearchPath; |
| 347 if (!dev_path) |
| 348 dev_path = kDefaultDevPath; |
| 349 if (!dev) |
| 350 return -1; |
| 351 |
| 352 res = rootdev_get_device(devname, sizeof(devname), *dev, search); |
| 353 if (res != 0) |
| 354 return res; |
| 355 |
| 356 if (full) |
| 357 res = rootdev_get_device_slave(devname, sizeof(devname), dev, devname, |
| 358 search); |
| 359 |
| 360 /* TODO(wad) we should really just track the block dev, partition number, and |
| 361 * dev path. When we rewrite this, we can track all the sysfs info |
| 362 * in the class. */ |
| 363 if (strip) { |
| 364 /* When we strip the partition, we don't want get_path to return non-zero |
| 365 * because of dev mismatch. Passing in 0 tells it to not test. */ |
| 366 *dev = 0; |
| 367 rootdev_strip_partition(devname, size); |
| 368 } |
| 369 |
| 370 res = rootdev_get_path(path, size, devname, *dev, dev_path); |
| 371 |
| 372 return res; |
| 373 } |
| 374 |
| 375 int rootdev(char *path, size_t size, bool full, bool strip) { |
| 376 struct stat root_statbuf; |
| 377 |
| 378 /* Yields the containing dev_t in st_dev. */ |
| 379 if (stat("/", &root_statbuf) != 0) |
| 380 return -1; |
| 381 |
| 382 return rootdev_wrapper(path, |
| 383 size, |
| 384 full, |
| 385 strip, |
| 386 &root_statbuf.st_dev, |
| 387 NULL, /* default /sys dir */ |
| 388 NULL); /* default /dev dir */ |
| 389 } |
OLD | NEW |