handler.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* handler.c - support handler loading */
  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/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/env.h>
  23. #include <grub/misc.h>
  24. #include <grub/command.h>
  25. #include <grub/handler.h>
  26. #include <grub/normal.h>
  27. #include <grub/lib.h>
  28. struct grub_handler_list
  29. {
  30. struct grub_handler_list *next;
  31. char *name;
  32. grub_command_t cmd;
  33. };
  34. static grub_list_t handler_list;
  35. static grub_err_t
  36. grub_handler_cmd (struct grub_command *cmd,
  37. int argc __attribute__ ((unused)),
  38. char **args __attribute__ ((unused)))
  39. {
  40. char *p;
  41. grub_handler_class_t class;
  42. grub_handler_t handler;
  43. p = grub_strchr (cmd->name, '.');
  44. if (! p)
  45. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid command name");
  46. if (cmd->data)
  47. {
  48. if (! grub_dl_get (cmd->data))
  49. {
  50. grub_dl_t mod;
  51. mod = grub_dl_load (cmd->data);
  52. if (mod)
  53. grub_dl_ref (mod);
  54. else
  55. return grub_errno;
  56. }
  57. grub_free (cmd->data);
  58. cmd->data = 0;
  59. }
  60. *p = 0;
  61. class = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_handler_class_list),
  62. cmd->name);
  63. *p = '.';
  64. if (! class)
  65. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "class not found");
  66. handler = grub_named_list_find (GRUB_AS_NAMED_LIST (class->handler_list),
  67. p + 1);
  68. if (! handler)
  69. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "handler not found");
  70. grub_handler_set_current (class, handler);
  71. return 0;
  72. }
  73. static void
  74. insert_handler (char *name, char *module)
  75. {
  76. struct grub_handler_list *item;
  77. char *data;
  78. if (grub_command_find (name))
  79. return;
  80. item = grub_malloc (sizeof (*item));
  81. if (! item)
  82. return;
  83. item->name = grub_strdup (name);
  84. if (! item->name)
  85. {
  86. grub_free (item);
  87. return;
  88. }
  89. if (module)
  90. {
  91. data = grub_strdup (module);
  92. if (! data)
  93. {
  94. grub_free (item->name);
  95. grub_free (item);
  96. return;
  97. }
  98. }
  99. else
  100. data = 0;
  101. item->cmd = grub_reg_cmd (item->name, grub_handler_cmd, 0,
  102. "Set active handler.", 0);
  103. if (! item->cmd)
  104. {
  105. grub_free (data);
  106. grub_free (item->name);
  107. grub_free (item);
  108. return;
  109. }
  110. item->cmd->data = data;
  111. grub_list_push (&handler_list, GRUB_AS_LIST (item));
  112. }
  113. static int
  114. iterate_handler (grub_handler_t handler, void *closure)
  115. {
  116. const char *class_name = closure;
  117. char name[grub_strlen (class_name) + grub_strlen (handler->name) + 2];
  118. grub_strcpy (name, class_name);
  119. grub_strcat (name, ".");
  120. grub_strcat (name, handler->name);
  121. insert_handler (name, 0);
  122. return 0;
  123. }
  124. static int
  125. iterate_class (grub_handler_class_t class,
  126. void *closure __attribute ((unused)))
  127. {
  128. grub_list_iterate (GRUB_AS_LIST (class->handler_list),
  129. (grub_list_hook_t) iterate_handler, (char *) class->name);
  130. return 0;
  131. }
  132. /* Read the file handler.lst for auto-loading. */
  133. void
  134. read_handler_list (void)
  135. {
  136. const char *prefix;
  137. static int first_time = 1;
  138. /* Make sure that this function does not get executed twice. */
  139. if (! first_time)
  140. return;
  141. first_time = 0;
  142. #ifdef GRUB_MACHINE_EMU
  143. (void) prefix;
  144. #else
  145. prefix = grub_env_get ("prefix");
  146. if (prefix)
  147. {
  148. char *filename;
  149. filename = grub_xasprintf ("%s/handler.lst", prefix);
  150. if (filename)
  151. {
  152. grub_file_t file;
  153. file = grub_file_open (filename);
  154. if (file)
  155. {
  156. char *buf = NULL;
  157. for (;; grub_free (buf))
  158. {
  159. char *p;
  160. buf = grub_getline (file);
  161. if (! buf)
  162. break;
  163. if (! grub_isgraph (buf[0]))
  164. continue;
  165. p = grub_strchr (buf, ':');
  166. if (! p)
  167. continue;
  168. *p = '\0';
  169. while (*++p == ' ')
  170. ;
  171. insert_handler (buf, p);
  172. }
  173. grub_file_close (file);
  174. }
  175. grub_free (filename);
  176. }
  177. }
  178. #endif
  179. grub_list_iterate (GRUB_AS_LIST (grub_handler_class_list),
  180. (grub_list_hook_t) iterate_class, 0);
  181. /* Ignore errors. */
  182. grub_errno = GRUB_ERR_NONE;
  183. }
  184. void
  185. free_handler_list (void)
  186. {
  187. struct grub_handler_list *item;
  188. while ((item = grub_list_pop (&handler_list)) != 0)
  189. {
  190. grub_free (item->cmd->data);
  191. grub_unregister_command (item->cmd);
  192. grub_free (item->name);
  193. grub_free (item);
  194. }
  195. }