early.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL);
  52. mount("sysfs", "/sys", "sysfs", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL);
  53. mount("efivars", "/sys/firmware/efi/efivars", "efivarfs", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL);
  54. mount("cgroup2", "/sys/fs/cgroup", "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, "nsdelegate");
  55. mount("tmpfs", "/dev", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_RELATIME, "mode=0755,size=512K");
  56. ignore(symlink("/tmp/shm", "/dev/shm"));
  57. mkdir("/dev/pts", 0755);
  58. mount("devpts", "/dev/pts", "devpts", MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL);
  59. early_dev();
  60. }
  61. early_console("/dev/console");
  62. mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, "mode=01777");
  63. mkdir("/tmp/shm", 01777);
  64. mkdir("/tmp/run", 0755);
  65. mkdir("/tmp/lock", 0755);
  66. mkdir("/tmp/state", 0755);
  67. umask(oldumask);
  68. }
  69. static void
  70. early_env(void)
  71. {
  72. setenv("PATH", EARLY_PATH, 1);
  73. }
  74. void
  75. early(void)
  76. {
  77. if (getpid() != 1)
  78. return;
  79. early_mounts();
  80. early_env();
  81. LOG("Console is alive\n");
  82. }