autofs.c 3.0 KB

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