forking-service.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # GNU Shepherd --- Test detecting a forked process' termination
  2. # Copyright © 2016, 2020 Ludovic Courtès <ludo@gnu.org>
  3. # Copyright © 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
  4. #
  5. # This file is part of the GNU Shepherd.
  6. #
  7. # The GNU Shepherd is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or (at
  10. # your option) any later version.
  11. #
  12. # The GNU Shepherd is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
  19. shepherd --version
  20. herd --version
  21. socket="t-socket-$$"
  22. conf="t-conf-$$"
  23. log="t-log-$$"
  24. pid="t-pid-$$"
  25. service_pid="t-service-pid-$$"
  26. service2_pid="t-service2-pid-$$"
  27. service2_started="t-service2-starts-$$"
  28. herd="herd -s $socket"
  29. function cleanup
  30. {
  31. cat $log || true
  32. rm -f $socket $conf $log $service2_started
  33. test -f $pid && kill "`cat $pid`" || true
  34. rm -f $pid
  35. test -f $service_pid && kill "`cat $service_pid`" || true
  36. rm -f $service_pid
  37. test -f $service2_pid && kill "`cat $service2_pid`" || true
  38. rm -f $service2_pid
  39. }
  40. trap cleanup EXIT
  41. cat > "$conf"<<EOF
  42. ;; Leave a timeout long enough for slow machines.
  43. (default-pid-file-timeout 10)
  44. (define %command
  45. '("$SHELL" "-c" "sleep 600 & echo \$! > $PWD/$service_pid"))
  46. (register-services
  47. (make <service>
  48. ;; A service that forks into a different process.
  49. #:provides '(test)
  50. #:start (make-forkexec-constructor %command
  51. #:pid-file "$PWD/$service_pid")
  52. #:stop (make-kill-destructor)
  53. #:respawn? #f))
  54. (define %command2
  55. '("$SHELL" "-c" "echo started >> $PWD/$service2_started; sleep 600 & echo \$! > $PWD/$service2_pid"))
  56. (register-services
  57. (make <service>
  58. ;; A service that forks into a different process.
  59. #:provides '(test2)
  60. #:start (make-forkexec-constructor %command2
  61. #:pid-file "$PWD/$service2_pid")
  62. #:stop (make-kill-destructor)
  63. #:respawn? #t))
  64. (define %command3
  65. '("$SHELL" "-c" "sleep 600"))
  66. (register-services
  67. (make <service>
  68. ;; A service that forks into a different process.
  69. #:provides '(test3)
  70. #:start (make-forkexec-constructor %command3)
  71. #:stop (make-kill-destructor)
  72. #:respawn? #t))
  73. EOF
  74. cat $conf
  75. rm -f "$pid"
  76. shepherd -I -s "$socket" -c "$conf" -l "$log" --pid="$pid" &
  77. # Wait till it's ready.
  78. while ! test -f "$pid" ; do sleep 0.3 ; done
  79. shepherd_pid="`cat $pid`"
  80. # start both of the services
  81. $herd start test
  82. $herd start test2
  83. # make sure "test" is started
  84. until $herd status test | grep started; do sleep 0.3; done
  85. test -f "$service_pid"
  86. service_pid_value="`cat $service_pid`"
  87. # now kill it
  88. kill "$service_pid_value"
  89. while kill -0 "$service_pid_value"; do sleep 0.3; done
  90. # shepherd should notice that the service has stopped within one second
  91. sleep 1
  92. $herd status test | grep stopped
  93. # make sure "test2" has started
  94. until $herd status test2 | grep started; do sleep 0.3; done
  95. test -f "$service2_pid"
  96. service2_pid_value="`cat $service2_pid`"
  97. test "`cat $PWD/$service2_started`" = "started"
  98. # now kill it
  99. rm -f "$service2_pid"
  100. kill $service2_pid_value
  101. while kill -0 "$service2_pid_value"; do sleep 0.3; done
  102. # shepherd should notice that the service has stopped, and restart it, within one second
  103. sleep 1;
  104. $herd status test2 | grep started
  105. test "`cat $PWD/$service2_started`" = "started
  106. started"
  107. # Try to trigger eventual race conditions, when killing a process between fork
  108. # and execv calls.
  109. for i in `seq 1 50`
  110. do
  111. $herd restart test3
  112. done