rc.init 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/sh -e
  2. # This is stage 2, the part of the initialization that is run once
  3. # the s6 supervision tree has been set up. This is where all the
  4. # services are brought up, and the CMD, if any, is run.
  5. trap : INT # guard against ^C as much as possible
  6. prog=/run/s6/basedir/scripts/rc.init
  7. top="$1" ; shift
  8. if test -d /run/s6/container_environment ; then
  9. s6-chmod 0755 /run/s6/container_environment
  10. fi
  11. if v=`printcontenv S6_VERBOSITY` && eltest "$v" =~ '^[[:digit:]]+$' ; then : ; else
  12. v=2
  13. fi
  14. cv=$((v - 1))
  15. if test "$cv" -lt 0 ; then
  16. cv=0
  17. fi
  18. if hook=`printcontenv S6_STAGE2_HOOK` && test -n "$hook" ; then
  19. set +e
  20. $hook
  21. r=$?
  22. set -e
  23. if eltest "$r" -gt 0 -a "$v" -gt 0 ; then
  24. echo "$prog: warning: hook $hook exited $r" 1>&2
  25. fi
  26. fi
  27. if profile=`printcontenv S6_RUNTIME_PROFILE` ; then
  28. etc="/etc/cont-profile.d/$profile"
  29. else
  30. etc=/etc
  31. fi
  32. s6-rc-compile -v"$cv" /run/s6/db "$etc/s6-overlay/s6-rc.d" /package/admin/s6-overlay-@VERSION@/etc/s6-rc/sources
  33. s6-rc-init -c /run/s6/db /run/service
  34. if timeout=`printcontenv S6_CMD_WAIT_FOR_SERVICES_MAXTIME` && eltest "$timeout" =~ '^[[:digit:]]+$' ; then : ; else
  35. timeout=0
  36. fi
  37. set +e
  38. s6-rc -v$v -u -t "$timeout" -- change "$top"
  39. r=$?
  40. set -e
  41. if b=`printcontenv S6_BEHAVIOUR_IF_STAGE2_FAILS` && eltest "$r" -gt 0 -a "$b" =~ '^[[:digit:]]+$' -a "$b" -gt 0 ; then
  42. echo "$prog: warning: s6-rc failed to properly bring all the services up! Check your logs (in /run/uncaught-logs/current if you have in-container logging) for more information." 1>&2
  43. if test "$b" -ge 2 ; then
  44. echo "$prog: fatal: stopping the container." 1>&2
  45. echo "$r" > /run/s6-linux-init-container-results/exitcode
  46. exec /run/s6/basedir/bin/halt
  47. fi
  48. fi
  49. if test "$#" -gt 0 ; then
  50. cd `s6-cat < /run/s6/workdir`
  51. set +e
  52. arg0=`printcontenv S6_CMD_ARG0`
  53. if b=`printcontenv S6_CMD_USE_TERMINAL` && eltest "$b" =~ '^[[:digit:]]+$' -a "$b" -gt 0 && b=`tty` ; then
  54. arg0="redirfd -w 1 $b fdmove -c 2 1 $arg0"
  55. fi
  56. if b=`printcontenv S6_CMD_RECEIVE_SIGNALS` && eltest "$b" =~ '^[[:digit:]]+$' -a "$b" -gt 0 ; then
  57. $arg0 "$@" &
  58. cmdpid="$!"
  59. echo "$cmdpid" > /run/s6/cmdpid
  60. wait "$cmdpid"
  61. r="$?"
  62. rm -f /run/s6/cmdpid
  63. else
  64. $arg0 "$@"
  65. r="$?"
  66. fi
  67. set -e
  68. echo "$r" > /run/s6-linux-init-container-results/exitcode
  69. exec /run/s6/basedir/bin/halt
  70. fi