3
0

find_root_device.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  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
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <dirent.h>
  24. #include <stdlib.h>
  25. #include "libbb.h"
  26. extern char *find_real_root_device_name(void)
  27. {
  28. DIR *dir;
  29. struct dirent *entry;
  30. struct stat statBuf, rootStat;
  31. char *fileName = NULL;
  32. dev_t dev;
  33. if (stat("/", &rootStat) != 0)
  34. bb_perror_msg("could not stat '/'");
  35. else {
  36. /* This check is here in case they pass in /dev name */
  37. if ((rootStat.st_mode & S_IFMT) == S_IFBLK)
  38. dev = rootStat.st_rdev;
  39. else
  40. dev = rootStat.st_dev;
  41. dir = opendir("/dev");
  42. if (!dir)
  43. bb_perror_msg("could not open '/dev'");
  44. else {
  45. while((entry = readdir(dir)) != NULL) {
  46. const char *myname = entry->d_name;
  47. /* Must skip ".." since that is "/", and so we
  48. * would get a false positive on ".." */
  49. if (myname[0] == '.' && myname[1] == '.' && !myname[2])
  50. continue;
  51. #ifdef CONFIG_FEATURE_DEVFS
  52. /* if there is a link named /dev/root skip that too */
  53. if (strcmp(myname, "root")==0)
  54. continue;
  55. #endif
  56. fileName = concat_path_file("/dev", myname);
  57. /* Some char devices have the same dev_t as block
  58. * devices, so make sure this is a block device */
  59. if (stat(fileName, &statBuf) == 0 &&
  60. S_ISBLK(statBuf.st_mode)!=0 &&
  61. statBuf.st_rdev == dev)
  62. break;
  63. free(fileName);
  64. fileName=NULL;
  65. }
  66. closedir(dir);
  67. }
  68. }
  69. if(fileName==NULL)
  70. fileName = bb_xstrdup("/dev/root");
  71. return fileName;
  72. }
  73. /* END CODE */
  74. /*
  75. Local Variables:
  76. c-file-style: "linux"
  77. c-basic-offset: 4
  78. tab-width: 4
  79. End:
  80. */