/init.d/sendsigs

http://github.com/brinkman83/bashrc · Shell · 120 lines · 71 code · 14 blank · 35 comment · 15 complexity · 2a1f251ca12ef06f46a166f9a37e2400 MD5 · raw file

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: sendsigs
  4. # Required-Start:
  5. # Required-Stop: umountnfs
  6. # Default-Start:
  7. # Default-Stop: 0 6
  8. # Short-Description: Kill all remaining processes.
  9. # Description:
  10. ### END INIT INFO
  11. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  12. . /lib/lsb/init-functions
  13. report_unkillable() {
  14. [ -x /usr/share/apport/unkillable_shutdown ] || return
  15. if [ ! -e /etc/default/apport ] || ! grep -q '^enabled[[:space:]]*=[[:space:]]*1' /etc/default/apport; then
  16. return
  17. fi
  18. /usr/share/apport/unkillable_shutdown $OMITPIDS
  19. }
  20. do_stop () {
  21. OMITPIDS=
  22. # The /var/run/sendsigs.omit file is used to be compatible
  23. # with Ubuntu.
  24. for omitfile in /var/run/sendsigs.omit /lib/init/rw/sendsigs.omit; do
  25. if [ -e $omitfile ]; then
  26. for pid in $(cat $omitfile); do
  27. OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
  28. done
  29. fi
  30. done
  31. # Load sendsigs.omit.d/packagename files too, to make it
  32. # possible for scripts that need to modify the list of pids at
  33. # run time without race conditions.
  34. if [ -d /lib/init/rw/sendsigs.omit.d/ ]; then
  35. for pidfile in /lib/init/rw/sendsigs.omit.d/*; do
  36. [ -f "$pidfile" ] || continue
  37. for pid in $(cat $pidfile); do
  38. OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
  39. done
  40. done
  41. fi
  42. # Upstart jobs have their own "stop on" clauses that sends
  43. # SIGTERM/SIGKILL just like this, so if they're still running,
  44. # they're supposed to be
  45. for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do
  46. OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
  47. done
  48. # Flush the kernel I/O buffer before we start to kill
  49. # processes, to make sure the IO of already stopped services to
  50. # not slow down the remaining processes to a point where they
  51. # are accidentily killed with SIGKILL because they did not
  52. # manage to shut down in time.
  53. sync
  54. # Kill all processes.
  55. log_action_begin_msg "Asking all remaining processes to terminate"
  56. killall5 -15 $OMITPIDS # SIGTERM
  57. log_action_end_msg 0
  58. alldead=""
  59. for seq in 1 2 3 4 5 6 7 8 9 10; do
  60. # use SIGCONT/signal 18 to check if there are
  61. # processes left. No need to check the exit code
  62. # value, because either killall5 work and it make
  63. # sense to wait for processes to die, or it fail and
  64. # there is nothing to wait for.
  65. # did an upstart job start since we last polled initctl? check
  66. # again on each loop and add any new jobs (e.g., plymouth) to
  67. # the list. If we did miss one starting up, this beats waiting
  68. # 10 seconds before shutting down.
  69. for pid in $(initctl list | sed -n -e "/process [0-9]/s/.*process //p"); do
  70. OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $pid"
  71. done
  72. if killall5 -18 $OMITPIDS ; then
  73. :
  74. else
  75. alldead=1
  76. break
  77. fi
  78. sleep 1
  79. done
  80. if [ -z "$alldead" ] ; then
  81. log_action_begin_msg "Killing all remaining processes"
  82. #report_unkillable
  83. killall5 -9 $OMITPIDS # SIGKILL
  84. log_action_end_msg 1
  85. else
  86. log_action_begin_msg "All processes ended within $seq seconds."
  87. log_action_end_msg 0
  88. fi
  89. }
  90. case "$1" in
  91. start)
  92. # No-op
  93. ;;
  94. restart|reload|force-reload)
  95. echo "Error: argument '$1' not supported" >&2
  96. exit 3
  97. ;;
  98. stop)
  99. do_stop
  100. ;;
  101. *)
  102. echo "Usage: $0 start|stop" >&2
  103. exit 3
  104. ;;
  105. esac
  106. :