autofs.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* autofs.c - support auto-loading from fs.lst */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/dl.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/fs.h>
  24. #include <grub/normal.h>
  25. /* This is used to store the names of filesystem modules for auto-loading. */
  26. static grub_named_list_t fs_module_list;
  27. /* The auto-loading hook for filesystems. */
  28. static int
  29. autoload_fs_module (void)
  30. {
  31. grub_named_list_t p;
  32. int ret = 0;
  33. grub_file_filter_t grub_file_filters_was[GRUB_FILE_FILTER_MAX];
  34. grub_memcpy (grub_file_filters_was, grub_file_filters_enabled,
  35. sizeof (grub_file_filters_enabled));
  36. grub_memcpy (grub_file_filters_enabled, grub_file_filters_all,
  37. sizeof (grub_file_filters_enabled));
  38. while ((p = fs_module_list) != NULL)
  39. {
  40. if (! grub_dl_get (p->name) && grub_dl_load (p->name))
  41. {
  42. ret = 1;
  43. break;
  44. }
  45. if (grub_errno)
  46. grub_print_error ();
  47. fs_module_list = p->next;
  48. grub_free (p->name);
  49. grub_free (p);
  50. }
  51. grub_memcpy (grub_file_filters_enabled, grub_file_filters_was,
  52. sizeof (grub_file_filters_enabled));
  53. return ret;
  54. }
  55. /* Read the file fs.lst for auto-loading. */
  56. void
  57. read_fs_list (const char *prefix)
  58. {
  59. if (prefix)
  60. {
  61. char *filename;
  62. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
  63. "/fs.lst", prefix);
  64. if (filename)
  65. {
  66. grub_file_t file;
  67. grub_fs_autoload_hook_t tmp_autoload_hook;
  68. /* This rules out the possibility that read_fs_list() is invoked
  69. recursively when we call grub_file_open() below. */
  70. tmp_autoload_hook = grub_fs_autoload_hook;
  71. grub_fs_autoload_hook = NULL;
  72. file = grub_file_open (filename);
  73. if (file)
  74. {
  75. /* Override previous fs.lst. */
  76. while (fs_module_list)
  77. {
  78. grub_named_list_t tmp;
  79. tmp = fs_module_list->next;
  80. grub_free (fs_module_list);
  81. fs_module_list = tmp;
  82. }
  83. while (1)
  84. {
  85. char *buf;
  86. char *p;
  87. char *q;
  88. grub_named_list_t fs_mod;
  89. buf = grub_file_getline (file);
  90. if (! buf)
  91. break;
  92. p = buf;
  93. q = buf + grub_strlen (buf) - 1;
  94. /* Ignore space. */
  95. while (grub_isspace (*p))
  96. p++;
  97. while (p < q && grub_isspace (*q))
  98. *q-- = '\0';
  99. /* If the line is empty, skip it. */
  100. if (p >= q)
  101. {
  102. grub_free (buf);
  103. continue;
  104. }
  105. fs_mod = grub_malloc (sizeof (*fs_mod));
  106. if (! fs_mod)
  107. {
  108. grub_free (buf);
  109. continue;
  110. }
  111. fs_mod->name = grub_strdup (p);
  112. grub_free (buf);
  113. if (! fs_mod->name)
  114. {
  115. grub_free (fs_mod);
  116. continue;
  117. }
  118. fs_mod->next = fs_module_list;
  119. fs_module_list = fs_mod;
  120. }
  121. grub_file_close (file);
  122. grub_fs_autoload_hook = tmp_autoload_hook;
  123. }
  124. grub_free (filename);
  125. }
  126. }
  127. /* Ignore errors. */
  128. grub_errno = GRUB_ERR_NONE;
  129. /* Set the hook. */
  130. grub_fs_autoload_hook = autoload_fs_module;
  131. }