summaryrefslogtreecommitdiff
path: root/libbb/find_root_device.c
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-05-23 14:45:09 +0000
committerMatt Kraai <kraai@debian.org>2001-05-23 14:45:09 +0000
commit774d135b66f4ba914ee8649ce6e8c3f7d3e36c35 (patch)
treeb308aeef4bb165d343a8971b8a12325846aab7af /libbb/find_root_device.c
parent3200f5ac689288b830a572e43dbdfd35bdac119c (diff)
downloadbusybox-w32-774d135b66f4ba914ee8649ce6e8c3f7d3e36c35.tar.gz
busybox-w32-774d135b66f4ba914ee8649ce6e8c3f7d3e36c35.tar.bz2
busybox-w32-774d135b66f4ba914ee8649ce6e8c3f7d3e36c35.zip
Make more robust (patch by Larry Doolittle).
Diffstat (limited to 'libbb/find_root_device.c')
-rw-r--r--libbb/find_root_device.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
index 75ed1a979..edfd7085a 100644
--- a/libbb/find_root_device.c
+++ b/libbb/find_root_device.c
@@ -39,16 +39,18 @@ extern char *find_real_root_device_name(const char* name)
39 struct dirent *entry; 39 struct dirent *entry;
40 struct stat statBuf, rootStat; 40 struct stat statBuf, rootStat;
41 char *fileName; 41 char *fileName;
42 dev_t dev;
42 43
43 if (stat("/", &rootStat) != 0) { 44 if (stat("/", &rootStat) != 0) {
44 error_msg("could not stat '/'"); 45 perror_msg("could not stat '/'");
45 return NULL; 46 return NULL;
46 } 47 }
48 if ((dev = rootStat.st_rdev)==0) dev=rootStat.st_dev;
47 49
48 dir = opendir("/dev"); 50 dir = opendir("/dev");
49 if (!dir) { 51 if (!dir) {
50 error_msg("could not open '/dev'"); 52 perror_msg("could not open '/dev'");
51 return NULL; 53 goto fallback;
52 } 54 }
53 55
54 while((entry = readdir(dir)) != NULL) { 56 while((entry = readdir(dir)) != NULL) {
@@ -64,14 +66,18 @@ extern char *find_real_root_device_name(const char* name)
64 * devices, so make sure this is a block device */ 66 * devices, so make sure this is a block device */
65 if (stat(fileName, &statBuf) == 0 && 67 if (stat(fileName, &statBuf) == 0 &&
66 S_ISBLK(statBuf.st_mode)!=0 && 68 S_ISBLK(statBuf.st_mode)!=0 &&
67 statBuf.st_rdev == rootStat.st_rdev) { 69 statBuf.st_rdev == dev) {
68 return fileName; 70 return fileName;
69 } 71 }
70 free(fileName); 72 free(fileName);
71 } 73 }
72 closedir(dir); 74 closedir(dir);
73 75
74 return NULL; 76fallback:
77 /* don't use stack space, caller expects to free() result */
78 fileName=xmalloc(20);
79 sprintf(fileName,"(rdev %u)",(unsigned int) rootStat.st_rdev);
80 return fileName;
75} 81}
76 82
77 83