shepherd-supplementary-groups.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From f847126a0747ebb66214e7e85603e48d74950758 Mon Sep 17 00:00:00 2001
  2. From: Oleg Pykhalov <go.wigust@gmail.com>
  3. Date: Sun, 24 May 2020 20:30:27 +0300
  4. Subject: [PATCH] service: Add #:supplementary-groups.
  5. * modules/shepherd/service.scm (format-supplementary-groups): New procedure.
  6. (exec-command, fork+exec-command): Add '#:supplementary-groups'.
  7. * doc/shepherd.texi (Service De- and Constructors): Document this.
  8. ---
  9. doc/shepherd.texi | 39 +++++++++++++++++++++---------------
  10. modules/shepherd/service.scm | 12 ++++++++++-
  11. 2 files changed, 34 insertions(+), 17 deletions(-)
  12. diff --git a/doc/shepherd.texi b/doc/shepherd.texi
  13. index 1de49af..18f1a4d 100644
  14. --- a/doc/shepherd.texi
  15. +++ b/doc/shepherd.texi
  16. @@ -11,7 +11,8 @@
  17. @copying
  18. Copyright @copyright{} @value{OLD-YEARS} Wolfgang J@"ahrling@*
  19. Copyright @copyright{} @value{NEW-YEARS} Ludovic Courtès@*
  20. -Copyright @copyright{} 2020 Brice Waegeneire
  21. +Copyright @copyright{} 2020 Brice Waegeneire@*
  22. +Copyright @copyright{} 2020 Oleg Pykhalov
  23. Permission is granted to copy, distribute and/or modify this document
  24. under the terms of the GNU Free Documentation License, Version 1.3 or
  25. @@ -893,21 +894,24 @@ execution of the @var{command} was successful, @code{#t} if not.
  26. @deffn {procedure} make-forkexec-constructor @var{command} @
  27. [#:user #f] @
  28. [#:group #f] @
  29. + [#:supplementary-groups '()] @
  30. [#:pid-file #f] [#:pid-file-timeout (default-pid-file-timeout)] @
  31. [#:log-file #f] @
  32. [#:directory (default-service-directory)] @
  33. [#:file-creation-mask #f] @
  34. [#:environment-variables (default-environment-variables)]
  35. Return a procedure that forks a child process, closes all file
  36. -descriptors except the standard output and standard error descriptors, sets
  37. -the current directory to @var{directory}, sets the umask to
  38. -@var{file-creation-mask} unless it is @code{#f}, changes the environment to
  39. -@var{environment-variables} (using the @code{environ} procedure), sets the
  40. -current user to @var{user} and the current group to @var{group} unless they
  41. -are @code{#f}, and executes @var{command} (a list of strings.) The result of
  42. -the procedure will be the PID of the child process. Note that this will
  43. -not work as expected if the process ``daemonizes'' (forks); in that
  44. -case, you will need to pass @code{#:pid-file}, as explained below.
  45. +descriptors except the standard output and standard error descriptors,
  46. +sets the current directory to @var{directory}, sets the umask to
  47. +@var{file-creation-mask} unless it is @code{#f}, changes the environment
  48. +to @var{environment-variables} (using the @code{environ} procedure),
  49. +sets the current user to @var{user} the current group to @var{group}
  50. +unless they are @code{#f} and supplementary groups to
  51. +@var{supplementary-groups} unless they are @code{'()}, and executes
  52. +@var{command} (a list of strings.) The result of the procedure will be
  53. +the PID of the child process. Note that this will not work as expected
  54. +if the process ``daemonizes'' (forks); in that case, you will need to
  55. +pass @code{#:pid-file}, as explained below.
  56. When @var{pid-file} is true, it must be the name of a PID file
  57. associated with the process being launched; the return value is the PID
  58. @@ -937,6 +941,7 @@ procedures.
  59. @deffn {procedure} exec-command @var{command} @
  60. [#:user #f] @
  61. [#:group #f] @
  62. + [#:supplementary-groups '()] @
  63. [#:log-file #f] @
  64. [#:directory (default-service-directory)] @
  65. [#:file-creation-mask #f] @
  66. @@ -944,6 +949,7 @@ procedures.
  67. @deffnx {procedure} fork+exec-command @var{command} @
  68. [#:user #f] @
  69. [#:group #f] @
  70. + [#:supplementary-groups '()] @
  71. [#:directory (default-service-directory)] @
  72. [#:file-creation-mask #f] @
  73. [#:environment-variables (default-environment-variables)]
  74. @@ -955,12 +961,13 @@ if it's true, whereas file descriptor 0
  75. (standard input) points to @file{/dev/null}; all other file descriptors
  76. are closed prior to yielding control to @var{command}.
  77. -By default, @var{command} is run as the current user. If the
  78. -@var{user} keyword argument is present and not false, change to
  79. -@var{user} immediately before invoking @var{command}. @var{user} may
  80. -be a string, indicating a user name, or a number, indicating a user
  81. -ID. Likewise, @var{command} will be run under the current group,
  82. -unless the @var{group} keyword argument is present and not false.
  83. +By default, @var{command} is run as the current user. If the @var{user}
  84. +keyword argument is present and not false, change to @var{user}
  85. +immediately before invoking @var{command}. @var{user} may be a string,
  86. +indicating a user name, or a number, indicating a user ID. Likewise,
  87. +@var{command} will be run under the current group, unless the
  88. +@var{group} keyword argument is present and not false, and
  89. +supplementary-groups is not '().
  90. @code{fork+exec-command} does the same as @code{exec-command}, but in
  91. a separate process whose PID it returns.
  92. diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
  93. index 347b8cc..587ff68 100644
  94. --- a/modules/shepherd/service.scm
  95. +++ b/modules/shepherd/service.scm
  96. @@ -6,6 +6,7 @@
  97. ;; Copyright (C) 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
  98. ;; Copyright (C) 2019 Ricardo Wurmus <rekado@elephly.net>
  99. ;; Copyright (C) 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  100. +;; Copyright (C) 2020 Oleg Pykhalov <go.wigust@gmail.com>
  101. ;;
  102. ;; This file is part of the GNU Shepherd.
  103. ;;
  104. @@ -773,10 +774,15 @@ daemon writing FILE is running in a separate PID namespace."
  105. (try-again)
  106. (apply throw args)))))))
  107. +(define (format-supplementary-groups supplementary-groups)
  108. + (list->vector (map (lambda (group) (group:gid (getgr group)))
  109. + supplementary-groups)))
  110. +
  111. (define* (exec-command command
  112. #:key
  113. (user #f)
  114. (group #f)
  115. + (supplementary-groups '())
  116. (log-file #f)
  117. (directory (default-service-directory))
  118. (file-creation-mask #f)
  119. @@ -832,7 +838,7 @@ false."
  120. (catch #t
  121. (lambda ()
  122. ;; Clear supplementary groups.
  123. - (setgroups #())
  124. + (setgroups (format-supplementary-groups supplementary-groups))
  125. (setgid (group:gid (getgr group))))
  126. (lambda (key . args)
  127. (format (current-error-port)
  128. @@ -879,6 +885,7 @@ false."
  129. #:key
  130. (user #f)
  131. (group #f)
  132. + (supplementary-groups '())
  133. (log-file #f)
  134. (directory (default-service-directory))
  135. (file-creation-mask #f)
  136. @@ -909,6 +916,7 @@ its PID."
  137. (exec-command command
  138. #:user user
  139. #:group group
  140. + #:supplementary-groups supplementary-groups
  141. #:log-file log-file
  142. #:directory directory
  143. #:file-creation-mask file-creation-mask
  144. @@ -919,6 +927,7 @@ its PID."
  145. #:key
  146. (user #f)
  147. (group #f)
  148. + (supplementary-groups '())
  149. (directory (default-service-directory))
  150. (environment-variables
  151. (default-environment-variables))
  152. @@ -956,6 +965,7 @@ start."
  153. (let ((pid (fork+exec-command command
  154. #:user user
  155. #:group group
  156. + #:supplementary-groups supplementary-groups
  157. #:log-file log-file
  158. #:directory directory
  159. #:file-creation-mask file-creation-mask
  160. --
  161. 2.26.2