blob: 2600ce5e08e30c595869a8d7dbf62b1ebac6e64d [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Utility routines.
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Eric Andersenaad1a882001-03-16 22:47:14 +000020 */
21
22#include <stdio.h>
23#include <string.h>
24#include <dirent.h>
Eric Andersenc911a432001-05-15 17:42:16 +000025#include <stdlib.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000026#include "libbb.h"
27
28
29
Eric Andersen93d7fba2004-08-03 00:14:02 +000030extern char *find_real_root_device_name(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000031{
32 DIR *dir;
33 struct dirent *entry;
34 struct stat statBuf, rootStat;
Eric Andersen8b113f92001-06-01 21:47:15 +000035 char *fileName = NULL;
Matt Kraai774d1352001-05-23 14:45:09 +000036 dev_t dev;
Eric Andersenaad1a882001-03-16 22:47:14 +000037
Eric Andersenc7bda1c2004-03-15 08:29:22 +000038 if (stat("/", &rootStat) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_perror_msg("could not stat '/'");
Eric Andersen8b113f92001-06-01 21:47:15 +000040 else {
Eric Andersen1cda7152004-01-13 11:39:22 +000041 /* This check is here in case they pass in /dev name */
42 if ((rootStat.st_mode & S_IFMT) == S_IFBLK)
43 dev = rootStat.st_rdev;
44 else
45 dev = rootStat.st_dev;
Eric Andersenaad1a882001-03-16 22:47:14 +000046
Eric Andersen8b113f92001-06-01 21:47:15 +000047 dir = opendir("/dev");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048 if (!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_msg("could not open '/dev'");
Eric Andersen8b113f92001-06-01 21:47:15 +000050 else {
51 while((entry = readdir(dir)) != NULL) {
Eric Andersendd92c772003-06-20 09:25:34 +000052 const char *myname = entry->d_name;
Glenn L McGrath24833432003-06-10 17:22:49 +000053 /* Must skip ".." since that is "/", and so we
54 * would get a false positive on ".." */
Eric Andersendd92c772003-06-20 09:25:34 +000055 if (myname[0] == '.' && myname[1] == '.' && !myname[2])
Eric Andersen8b113f92001-06-01 21:47:15 +000056 continue;
Eric Andersen93d7fba2004-08-03 00:14:02 +000057#ifdef CONFIG_FEATURE_DEVFS
58 /* if there is a link named /dev/root skip that too */
59 if (strcmp(myname, "root")==0)
60 continue;
61#endif
Eric Andersendd92c772003-06-20 09:25:34 +000062 fileName = concat_path_file("/dev", myname);
Glenn L McGrath24833432003-06-10 17:22:49 +000063
Eric Andersen8b113f92001-06-01 21:47:15 +000064 /* Some char devices have the same dev_t as block
65 * devices, so make sure this is a block device */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066 if (stat(fileName, &statBuf) == 0 &&
Eric Andersen8b113f92001-06-01 21:47:15 +000067 S_ISBLK(statBuf.st_mode)!=0 &&
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 statBuf.st_rdev == dev)
Eric Andersen8b113f92001-06-01 21:47:15 +000069 break;
70 free(fileName);
71 fileName=NULL;
72 }
73 closedir(dir);
Eric Andersenaad1a882001-03-16 22:47:14 +000074 }
75 }
Eric Andersen8b113f92001-06-01 21:47:15 +000076 if(fileName==NULL)
Glenn L McGrath24833432003-06-10 17:22:49 +000077 fileName = bb_xstrdup("/dev/root");
Matt Kraai774d1352001-05-23 14:45:09 +000078 return fileName;
Eric Andersenaad1a882001-03-16 22:47:14 +000079}
80
81
82/* END CODE */
83/*
84Local Variables:
85c-file-style: "linux"
86c-basic-offset: 4
87tab-width: 4
88End:
89*/