early.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <sys/mount.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/sysmacros.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include "../utils/utils.h"
  23. #include "init.h"
  24. #include "../libc-compat.h"
  25. #include "../container.h"
  26. static void
  27. early_dev(void)
  28. {
  29. mkdev("*", 0600);
  30. mknod("/dev/null", 0666, makedev(1, 3));
  31. }
  32. static void
  33. early_console(const char *dev)
  34. {
  35. struct stat s;
  36. if (stat(dev, &s)) {
  37. ERROR("Failed to stat %s: %m\n", dev);
  38. return;
  39. }
  40. if (patch_stdio(dev)) {
  41. ERROR("Failed to setup i/o redirection\n");
  42. return;
  43. }
  44. fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
  45. }
  46. static void
  47. early_mounts(void)
  48. {
  49. unsigned int oldumask = umask(0);
  50. if (!is_container()) {
  51. mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
  52. mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
  53. mount("cgroup2", "/sys/fs/cgroup", "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, "nsdelegate");
  54. mount("tmpfs", "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "mode=0755,size=512K");
  55. ignore(symlink("/tmp/shm", "/dev/shm"));
  56. mkdir("/dev/pts", 0755);
  57. mount("devpts", "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, 0);
  58. early_dev();
  59. }
  60. early_console("/dev/console");
  61. if (mount_zram_on_tmp()) {
  62. mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, "mode=01777");
  63. mkdir("/tmp/shm", 01777);
  64. } else {
  65. mkdir("/tmp/shm", 01777);
  66. mount("tmpfs", "/tmp/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME,
  67. "mode=01777");
  68. }
  69. mkdir("/tmp/run", 0755);
  70. mkdir("/tmp/lock", 0755);
  71. mkdir("/tmp/state", 0755);
  72. umask(oldumask);
  73. }
  74. static void
  75. early_env(void)
  76. {
  77. setenv("PATH", EARLY_PATH, 1);
  78. }
  79. void
  80. early(void)
  81. {
  82. if (getpid() != 1)
  83. return;
  84. early_mounts();
  85. early_env();
  86. LOG("Console is alive\n");
  87. }