file-creation-mask.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # GNU Shepherd --- Test the #:file-creation-mask option of 'make-forkexec-constructor'.
  2. # Copyright © 2020 Diego N. Barbato <dnbarbato@posteo.de>
  3. #
  4. # This file is part of the GNU Shepherd.
  5. #
  6. # The GNU Shepherd is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # The GNU Shepherd is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
  18. shepherd --version
  19. herd --version
  20. socket="t-socket-$$"
  21. conf="t-conf-$$"
  22. log="t-log-$$"
  23. pid="t-pid-$$"
  24. service_log="t-service-log-$$"
  25. service_new_file="t-service-new-file-$$"
  26. herd="herd -s $socket"
  27. trap "cat $log || true;
  28. rm -f $socket $conf $log $service_log $service_new_file;
  29. test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
  30. function wait_for_file
  31. {
  32. i=0
  33. while ! test -f "$1" && test $i -lt 20
  34. do
  35. sleep 0.3
  36. i=`expr $i + 1`
  37. done
  38. test -f "$1"
  39. }
  40. cat > "$conf"<<EOF
  41. (define %command
  42. '("$SHELL" "-c" "touch $PWD/$service_new_file; echo foo"))
  43. (register-services
  44. (make <service>
  45. #:provides '(test)
  46. #:start (make-forkexec-constructor %command
  47. #:log-file "$PWD/$service_log"
  48. ;; Set the umask such that file
  49. ;; permissions are #o600.
  50. #:file-creation-mask #o177)
  51. #:stop (make-kill-destructor)
  52. #:respawn? #f))
  53. EOF
  54. rm -f "$pid"
  55. shepherd -I -s "$socket" -c "$conf" -l "$log" --pid="$pid" &
  56. # Wait till it's ready.
  57. wait_for_file "$pid"
  58. # Start the service.
  59. $herd start test
  60. # Make sure the log file is created with the right permissions independently
  61. # of the value of #:file-creation-mask.
  62. wait_for_file "$service_log"
  63. test `stat -c %a "$service_log"` -eq 640
  64. # Make sure the service creates files with the right permissions as determined
  65. # by the value of #:file-creation-mask.
  66. wait_for_file "$service_new_file"
  67. test `stat -c %a "$service_new_file"` -eq 600