preinit.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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("INITRAMFS");
  82. unsetenv("PREINIT");
  83. unlink("/tmp/.preinit");
  84. check_sysupgrade();
  85. DEBUG(2, "Exec to real procd now\n");
  86. if (wdt_fd)
  87. setenv("WDTFD", wdt_fd, 1);
  88. check_dbglvl();
  89. if (debug > 0) {
  90. snprintf(dbg, 2, "%d", debug);
  91. setenv("DBGLVL", dbg, 1);
  92. }
  93. execvp(argv[0], argv);
  94. }
  95. static void
  96. plugd_proc_cb(struct uloop_process *proc, int ret)
  97. {
  98. proc->pid = 0;
  99. }
  100. void
  101. preinit(void)
  102. {
  103. char *init[] = { "/bin/sh", "/etc/preinit", NULL };
  104. char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
  105. int fd;
  106. LOG("- preinit -\n");
  107. plugd_proc.cb = plugd_proc_cb;
  108. plugd_proc.pid = fork();
  109. if (!plugd_proc.pid) {
  110. execvp(plug[0], plug);
  111. ERROR("Failed to start plugd: %m\n");
  112. exit(EXIT_FAILURE);
  113. }
  114. if (plugd_proc.pid <= 0) {
  115. ERROR("Failed to start new plugd instance: %m\n");
  116. return;
  117. }
  118. uloop_process_add(&plugd_proc);
  119. setenv("PREINIT", "1", 1);
  120. fd = creat("/tmp/.preinit", 0600);
  121. if (fd < 0)
  122. ERROR("Failed to create sentinel file: %m\n");
  123. else
  124. close(fd);
  125. preinit_proc.cb = spawn_procd;
  126. preinit_proc.pid = fork();
  127. if (!preinit_proc.pid) {
  128. execvp(init[0], init);
  129. ERROR("Failed to start preinit: %m\n");
  130. exit(EXIT_FAILURE);
  131. }
  132. if (preinit_proc.pid <= 0) {
  133. ERROR("Failed to start new preinit instance: %m\n");
  134. return;
  135. }
  136. uloop_process_add(&preinit_proc);
  137. DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
  138. }