autofs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. while ((p = fs_module_list) != NULL)
  34. {
  35. if (! grub_dl_get (p->name) && grub_dl_load (p->name))
  36. {
  37. ret = 1;
  38. break;
  39. }
  40. if (grub_errno)
  41. grub_print_error ();
  42. fs_module_list = p->next;
  43. grub_free (p->name);
  44. grub_free (p);
  45. }
  46. return ret;
  47. }
  48. /* Read the file fs.lst for auto-loading. */
  49. void
  50. read_fs_list (const char *prefix)
  51. {
  52. if (prefix)
  53. {
  54. char *filename;
  55. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
  56. "/fs.lst", prefix);
  57. if (filename)
  58. {
  59. grub_file_t file;
  60. grub_fs_autoload_hook_t tmp_autoload_hook;
  61. /* This rules out the possibility that read_fs_list() is invoked
  62. recursively when we call grub_file_open() below. */
  63. tmp_autoload_hook = grub_fs_autoload_hook;
  64. grub_fs_autoload_hook = NULL;
  65. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
  66. if (file)
  67. {
  68. /* Override previous fs.lst. */
  69. while (fs_module_list)
  70. {
  71. grub_named_list_t tmp;
  72. tmp = fs_module_list->next;
  73. grub_free (fs_module_list);
  74. fs_module_list = tmp;
  75. }
  76. while (1)
  77. {
  78. char *buf;
  79. char *p;
  80. char *q;
  81. grub_named_list_t fs_mod;
  82. buf = grub_file_getline (file);
  83. if (! buf)
  84. break;
  85. p = buf;
  86. q = buf + grub_strlen (buf) - 1;
  87. /* Ignore space. */
  88. while (grub_isspace (*p))
  89. p++;
  90. while (p < q && grub_isspace (*q))
  91. *q-- = '\0';
  92. /* If the line is empty, skip it. */
  93. if (p >= q)
  94. {
  95. grub_free (buf);
  96. continue;
  97. }
  98. fs_mod = grub_malloc (sizeof (*fs_mod));
  99. if (! fs_mod)
  100. {
  101. grub_free (buf);
  102. continue;
  103. }
  104. fs_mod->name = grub_strdup (p);
  105. grub_free (buf);
  106. if (! fs_mod->name)
  107. {
  108. grub_free (fs_mod);
  109. continue;
  110. }
  111. fs_mod->next = fs_module_list;
  112. fs_module_list = fs_mod;
  113. }
  114. grub_file_close (file);
  115. grub_fs_autoload_hook = tmp_autoload_hook;
  116. }
  117. grub_free (filename);
  118. }
  119. }
  120. /* Ignore errors. */
  121. grub_errno = GRUB_ERR_NONE;
  122. /* Set the hook. */
  123. grub_fs_autoload_hook = autoload_fs_module;
  124. }