[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[shepherd] 02/02: service: Add #:create-session? parameter to 'exec-comm
From: |
Ludovic Courtès |
Subject: |
[shepherd] 02/02: service: Add #:create-session? parameter to 'exec-command' & co. |
Date: |
Tue, 10 Aug 2021 08:50:51 -0400 (EDT) |
civodul pushed a commit to branch master
in repository shepherd.
commit c43f81794344dbac31bcb8b8f1c0d266f47f14fa
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Tue Aug 10 14:15:13 2021 +0200
service: Add #:create-session? parameter to 'exec-command' & co.
Suggested by muradm <mail@muradm.net>.
* modules/shepherd/service.scm (exec-command, fork+exec-command)
(make-forkexec-constructor): Add #:create-session? and honor it.
* doc/shepherd.texi (Service De- and Constructors): Update accordingly.
---
doc/shepherd.texi | 23 +++++++++++++++--------
modules/shepherd/service.scm | 22 +++++++++++++++-------
2 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/doc/shepherd.texi b/doc/shepherd.texi
index 02d4d89..94f6131 100644
--- a/doc/shepherd.texi
+++ b/doc/shepherd.texi
@@ -901,7 +901,7 @@ execution of the @var{command} was successful, @code{#t} if
not.
[#:pid-file #f] [#:pid-file-timeout (default-pid-file-timeout)] @
[#:log-file #f] @
[#:directory (default-service-directory)] @
- [#:file-creation-mask #f] @
+ [#:file-creation-mask #f] [#:create-session? #t] @
[#:environment-variables (default-environment-variables)]
Return a procedure that forks a child process, closes all file
descriptors except the standard output and standard error descriptors,
@@ -911,10 +911,15 @@ to @var{environment-variables} (using the @code{environ}
procedure),
sets the current user to @var{user} the current group to @var{group}
unless they are @code{#f} and supplementary groups to
@var{supplementary-groups} unless they are @code{'()}, and executes
-@var{command} (a list of strings.) The result of the procedure will be
-the PID of the child process. Note that this will not work as expected
-if the process ``daemonizes'' (forks); in that case, you will need to
-pass @code{#:pid-file}, as explained below.
+@var{command} (a list of strings.) When
+@var{create-session?} is true, the child process creates a new session with
+@code{setsid} and becomes its leader. The result of the procedure will be
+the PID of the child process.
+
+@quotation Note
+This will not work as expected if the process ``daemonizes'' (forks); in
+that case, you will need to pass @code{#:pid-file}, as explained below.
+@end quotation
When @var{pid-file} is true, it must be the name of a PID file
associated with the process being launched; the return value is the PID
@@ -947,14 +952,14 @@ procedures.
[#:supplementary-groups '()] @
[#:log-file #f] @
[#:directory (default-service-directory)] @
- [#:file-creation-mask #f] @
+ [#:file-creation-mask #f] [#:create-session? #t] @
[#:environment-variables (default-environment-variables)]
@deffnx {procedure} fork+exec-command @var{command} @
[#:user #f] @
[#:group #f] @
[#:supplementary-groups '()] @
[#:directory (default-service-directory)] @
- [#:file-creation-mask #f] @
+ [#:file-creation-mask #f] [#:create-session? #t] @
[#:environment-variables (default-environment-variables)]
Run @var{command} as the current process from @var{directory}, with
@var{file-creation-mask} if it's true, and with
@@ -962,7 +967,9 @@ Run @var{command} as the current process from
@var{directory}, with
File descriptors 1 and 2 are kept as is or redirected to @var{log-file}
if it's true, whereas file descriptor 0
(standard input) points to @file{/dev/null}; all other file descriptors
-are closed prior to yielding control to @var{command}.
+are closed prior to yielding control to @var{command}. When
+@var{create-session?} is true, call @code{setsid} first
+(@pxref{Processes, @code{setsid},, guile, GNU Guile Reference Manual}).
By default, @var{command} is run as the current user. If the @var{user}
keyword argument is present and not false, change to @var{user}
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 587ff68..ad8608b 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -1,5 +1,5 @@
;; service.scm -- Representation of services.
-;; Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic
Courtès <ludo@gnu.org>
+;; Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic
Courtès <ludo@gnu.org>
;; Copyright (C) 2002, 2003 Wolfgang Järling <wolfgang@pro-linux.de>
;; Copyright (C) 2014 Alex Sassmannshausen <alex.sassmannshausen@gmail.com>
;; Copyright (C) 2016 Alex Kost <alezost@gmail.com>
@@ -786,13 +786,14 @@ daemon writing FILE is running in a separate PID
namespace."
(log-file #f)
(directory (default-service-directory))
(file-creation-mask #f)
+ (create-session? #t)
(environment-variables (default-environment-variables)))
"Run COMMAND as the current process from DIRECTORY, with FILE-CREATION-MASK
if it's true, and with ENVIRONMENT-VARIABLES (a list of strings like
\"PATH=/bin\"). File descriptors 1 and 2 are kept as is or redirected to
LOG-FILE if it's true, whereas file descriptor 0 (standard input) points to
/dev/null; all other file descriptors are closed prior to yielding control to
-COMMAND.
+COMMAND. When CREATE-SESSION? is true, call 'setsid' first.
By default, COMMAND is run as the current user. If the USER keyword
argument is present and not false, change to USER immediately before
@@ -802,9 +803,10 @@ current group, unless the GROUP keyword argument is
present and not
false."
(match command
((program args ...)
- ;; Become the leader of a new session and session group.
- ;; Programs such as 'mingetty' expect this.
- (setsid)
+ (when create-session?
+ ;; Become the leader of a new session and session group.
+ ;; Programs such as 'mingetty' expect this.
+ (setsid))
(chdir directory)
(environ environment-variables)
@@ -889,6 +891,7 @@ false."
(log-file #f)
(directory (default-service-directory))
(file-creation-mask #f)
+ (create-session? #t)
(environment-variables
(default-environment-variables)))
"Spawn a process that executed COMMAND as per 'exec-command', and return
@@ -920,6 +923,7 @@ its PID."
#:log-file log-file
#:directory directory
#:file-creation-mask file-creation-mask
+ #:create-session? create-session?
#:environment-variables environment-variables))
pid))))
@@ -932,6 +936,7 @@ its PID."
(environment-variables
(default-environment-variables))
(file-creation-mask #f)
+ (create-session? #t)
(pid-file #f)
(pid-file-timeout
(default-pid-file-timeout))
@@ -942,8 +947,10 @@ the current directory to @var{directory}, sets the umask to
@var{file-creation-mask} unless it is @code{#f}, changes the environment to
@var{environment-variables} (using the @code{environ} procedure), sets the
current user to @var{user} and the current group to @var{group} unless they
-are @code{#f}, and executes @var{command} (a list of strings.) The result of
-the procedure will be the PID of the child process.
+are @code{#f}, and executes @var{command} (a list of strings.) When
+@var{create-session?} is true, the child process creates a new session with
+'setsid' and becomes its leader. The result of the procedure will be the
+ PID of the child process.
When @var{pid-file} is true, it must be the name of a PID file associated with
the process being launched; the return value is the PID read from that file,
@@ -969,6 +976,7 @@ start."
#:log-file log-file
#:directory directory
#:file-creation-mask file-creation-mask
+ #:create-session? create-session?
#:environment-variables
environment-variables)))
(if pid-file