summaryrefslogtreecommitdiff
path: root/libbb/find_root_device.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-06-01 21:47:15 +0000
committerEric Andersen <andersen@codepoet.org>2001-06-01 21:47:15 +0000
commit8b113f93b9b9157ea1e013667eaaf00aed97a251 (patch)
treec833c8f3a72637660af61b061b90d69d987fed25 /libbb/find_root_device.c
parent4f6753e586dba5e6c240e670d41fc8fd011034e1 (diff)
downloadbusybox-w32-8b113f93b9b9157ea1e013667eaaf00aed97a251.tar.gz
busybox-w32-8b113f93b9b9157ea1e013667eaaf00aed97a251.tar.bz2
busybox-w32-8b113f93b9b9157ea1e013667eaaf00aed97a251.zip
Vladimir's last_patch13, containing several bugfixes.
Diffstat (limited to 'libbb/find_root_device.c')
-rw-r--r--libbb/find_root_device.c67
1 files changed, 30 insertions, 37 deletions
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
index edfd7085a..f8f68464d 100644
--- a/libbb/find_root_device.c
+++ b/libbb/find_root_device.c
@@ -1,10 +1,9 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * Copyright (C) 2000,2001 by Lineo, inc.
4 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
4 * 5 *
5 * Copyright (C) tons of folks. Tracking down who wrote what 6 * Patched by a bunch of people. Feel free to acknowledge your work.
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 * 7 *
9 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
@@ -20,9 +19,6 @@
20 * along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * 21 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */ 22 */
27 23
28#include <stdio.h> 24#include <stdio.h>
@@ -38,45 +34,42 @@ extern char *find_real_root_device_name(const char* name)
38 DIR *dir; 34 DIR *dir;
39 struct dirent *entry; 35 struct dirent *entry;
40 struct stat statBuf, rootStat; 36 struct stat statBuf, rootStat;
41 char *fileName; 37 char *fileName = NULL;
42 dev_t dev; 38 dev_t dev;
43 39
44 if (stat("/", &rootStat) != 0) { 40 if (stat("/", &rootStat) != 0)
45 perror_msg("could not stat '/'"); 41 perror_msg("could not stat '/'");
46 return NULL; 42 else {
47 } 43 if ((dev = rootStat.st_rdev)==0)
48 if ((dev = rootStat.st_rdev)==0) dev=rootStat.st_dev; 44 dev=rootStat.st_dev;
49 45
50 dir = opendir("/dev"); 46 dir = opendir("/dev");
51 if (!dir) { 47 if (!dir)
52 perror_msg("could not open '/dev'"); 48 perror_msg("could not open '/dev'");
53 goto fallback; 49 else {
54 } 50 while((entry = readdir(dir)) != NULL) {
55 51
56 while((entry = readdir(dir)) != NULL) { 52 /* Must skip ".." since that is "/", and so we
53 * would get a false positive on ".." */
54 if (strcmp(entry->d_name, "..") == 0)
55 continue;
57 56
58 /* Must skip ".." since that is "/", and so we 57 fileName = concat_path_file("/dev", entry->d_name);
59 * would get a false positive on ".." */
60 if (strcmp(entry->d_name, "..") == 0)
61 continue;
62 58
63 fileName = concat_path_file("/dev/", entry->d_name); 59 /* Some char devices have the same dev_t as block
64 60 * devices, so make sure this is a block device */
65 /* Some char devices have the same dev_t as block 61 if (stat(fileName, &statBuf) == 0 &&
66 * devices, so make sure this is a block device */ 62 S_ISBLK(statBuf.st_mode)!=0 &&
67 if (stat(fileName, &statBuf) == 0 && 63 statBuf.st_rdev == dev)
68 S_ISBLK(statBuf.st_mode)!=0 && 64 break;
69 statBuf.st_rdev == dev) { 65 free(fileName);
70 return fileName; 66 fileName=NULL;
67 }
68 closedir(dir);
71 } 69 }
72 free(fileName);
73 } 70 }
74 closedir(dir); 71 if(fileName==NULL)
75 72 fileName=xstrdup("/dev/root");
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; 73 return fileName;
81} 74}
82 75