2
0

preinit.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/mount.h>
  18. #include <fcntl.h>
  19. #include <libubox/uloop.h>
  20. #include <libubox/utils.h>
  21. #include <libubus.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include "init.h"
  26. #include "../watchdog.h"
  27. #include "../sysupgrade.h"
  28. static struct uloop_process preinit_proc;
  29. static struct uloop_process plugd_proc;
  30. static void
  31. check_dbglvl(void)
  32. {
  33. FILE *fp = fopen("/tmp/debug_level", "r");
  34. int lvl = 0;
  35. if (!fp)
  36. return;
  37. if (fscanf(fp, "%d", &lvl) == EOF)
  38. ERROR("failed to read debug level: %m\n");
  39. fclose(fp);
  40. unlink("/tmp/debug_level");
  41. if (lvl > 0 && lvl < 5)
  42. debug = lvl;
  43. }
  44. static void
  45. check_sysupgrade(void)
  46. {
  47. char *prefix = NULL, *path = NULL, *command = NULL;
  48. size_t n;
  49. if (chdir("/"))
  50. return;
  51. FILE *sysupgrade = fopen("/tmp/sysupgrade", "r");
  52. if (!sysupgrade)
  53. return;
  54. n = 0;
  55. if (getdelim(&prefix, &n, 0, sysupgrade) < 0)
  56. goto fail;
  57. n = 0;
  58. if (getdelim(&path, &n, 0, sysupgrade) < 0)
  59. goto fail;
  60. n = 0;
  61. if (getdelim(&command, &n, 0, sysupgrade) < 0)
  62. goto fail;
  63. fclose(sysupgrade);
  64. sysupgrade_exec_upgraded(prefix, path, NULL, command, NULL);
  65. while (true)
  66. sleep(1);
  67. fail:
  68. fclose(sysupgrade);
  69. free(prefix);
  70. free(path);
  71. free(command);
  72. }
  73. static void
  74. spawn_procd(struct uloop_process *proc, int ret)
  75. {
  76. char *wdt_fd = watchdog_fd();
  77. char *argv[] = { "/sbin/procd", NULL};
  78. char dbg[2];
  79. if (plugd_proc.pid > 0)
  80. kill(plugd_proc.pid, SIGKILL);
  81. unsetenv("PREINIT");
  82. unlink("/tmp/.preinit");
  83. check_sysupgrade();
  84. DEBUG(2, "Exec to real procd now\n");
  85. if (wdt_fd)
  86. setenv("WDTFD", wdt_fd, 1);
  87. check_dbglvl();
  88. if (debug > 0) {
  89. snprintf(dbg, 2, "%d", debug);
  90. setenv("DBGLVL", dbg, 1);
  91. }
  92. execvp(argv[0], argv);
  93. }
  94. static void
  95. plugd_proc_cb(struct uloop_process *proc, int ret)
  96. {
  97. proc->pid = 0;
  98. }
  99. void
  100. preinit(void)
  101. {
  102. char *init[] = { "/bin/sh", "/etc/preinit", NULL };
  103. char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
  104. int fd;
  105. LOG("- preinit -\n");
  106. plugd_proc.cb = plugd_proc_cb;
  107. plugd_proc.pid = fork();
  108. if (!plugd_proc.pid) {
  109. execvp(plug[0], plug);
  110. ERROR("Failed to start plugd: %m\n");
  111. exit(EXIT_FAILURE);
  112. }
  113. if (plugd_proc.pid <= 0) {
  114. ERROR("Failed to start new plugd instance: %m\n");
  115. return;
  116. }
  117. uloop_process_add(&plugd_proc);
  118. setenv("PREINIT", "1", 1);
  119. fd = creat("/tmp/.preinit", 0600);
  120. if (fd < 0)
  121. ERROR("Failed to create sentinel file: %m\n");
  122. else
  123. close(fd);
  124. preinit_proc.cb = spawn_procd;
  125. preinit_proc.pid = fork();
  126. if (!preinit_proc.pid) {
  127. execvp(init[0], init);
  128. ERROR("Failed to start preinit: %m\n");
  129. exit(EXIT_FAILURE);
  130. }
  131. if (preinit_proc.pid <= 0) {
  132. ERROR("Failed to start new preinit instance: %m\n");
  133. return;
  134. }
  135. uloop_process_add(&preinit_proc);
  136. DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
  137. }