config.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <config-util.h>
  20. #include <grub/emu/hostdisk.h>
  21. #include <grub/emu/exec.h>
  22. #include <grub/emu/config.h>
  23. #include <grub/util/install.h>
  24. #include <grub/util/misc.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. const char *
  31. grub_util_get_config_filename (void)
  32. {
  33. static char *value = NULL;
  34. if (!value)
  35. value = grub_util_path_concat (3, GRUB_SYSCONFDIR,
  36. "default", "grub");
  37. return value;
  38. }
  39. const char *
  40. grub_util_get_pkgdatadir (void)
  41. {
  42. const char *ret = getenv ("pkgdatadir");
  43. if (ret)
  44. return ret;
  45. return GRUB_DATADIR "/" PACKAGE;
  46. }
  47. const char *
  48. grub_util_get_pkglibdir (void)
  49. {
  50. return GRUB_LIBDIR "/" PACKAGE;
  51. }
  52. const char *
  53. grub_util_get_localedir (void)
  54. {
  55. return LOCALEDIR;
  56. }
  57. void
  58. grub_util_load_config (struct grub_util_config *cfg)
  59. {
  60. pid_t pid;
  61. const char *argv[4];
  62. char *script, *ptr;
  63. const char *cfgfile, *iptr;
  64. FILE *f = NULL;
  65. int fd;
  66. const char *v;
  67. memset (cfg, 0, sizeof (*cfg));
  68. v = getenv ("GRUB_ENABLE_CRYPTODISK");
  69. if (v && v[0] == 'y' && v[1] == '\0')
  70. cfg->is_cryptodisk_enabled = 1;
  71. v = getenv ("GRUB_DISTRIBUTOR");
  72. if (v)
  73. cfg->grub_distributor = xstrdup (v);
  74. cfgfile = grub_util_get_config_filename ();
  75. if (!grub_util_is_regular (cfgfile))
  76. return;
  77. argv[0] = "sh";
  78. argv[1] = "-c";
  79. script = xmalloc (4 * strlen (cfgfile) + 300);
  80. ptr = script;
  81. memcpy (ptr, ". '", 3);
  82. ptr += 3;
  83. for (iptr = cfgfile; *iptr; iptr++)
  84. {
  85. if (*iptr == '\\')
  86. {
  87. memcpy (ptr, "'\\''", 4);
  88. ptr += 4;
  89. continue;
  90. }
  91. *ptr++ = *iptr;
  92. }
  93. strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "
  94. "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
  95. argv[2] = script;
  96. argv[3] = '\0';
  97. pid = grub_util_exec_pipe (argv, &fd);
  98. if (pid)
  99. f = fdopen (fd, "r");
  100. if (f)
  101. {
  102. grub_util_parse_config (f, cfg, 1);
  103. fclose (f);
  104. }
  105. if (pid)
  106. {
  107. close (fd);
  108. waitpid (pid, NULL, 0);
  109. }
  110. if (f)
  111. return;
  112. f = grub_util_fopen (cfgfile, "r");
  113. if (f)
  114. {
  115. grub_util_parse_config (f, cfg, 0);
  116. fclose (f);
  117. }
  118. else
  119. grub_util_warn (_("cannot open configuration file `%s': %s"),
  120. cfgfile, strerror (errno));
  121. }