Skip to content

Commit

Permalink
Start storage hook in own process group
Browse files Browse the repository at this point in the history
Prevents terminals from sending SIGINT etc.
  • Loading branch information
Unrud committed Aug 31, 2020
1 parent d4af2cd commit 1e011e7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions radicale/storage/multifilesystem/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ def acquire_lock(self, mode, user=None):
hook = self.configuration.get("storage", "hook")
if mode == "w" and hook:
folder = self.configuration.get("storage", "filesystem_folder")
logger.debug("Running hook")
debug = logger.isEnabledFor(logging.DEBUG)
p = subprocess.Popen(
hook % {"user": shlex.quote(user or "Anonymous")},
popen_kwargs = dict(
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE if debug else subprocess.DEVNULL,
stderr=subprocess.PIPE if debug else subprocess.DEVNULL,
shell=True, universal_newlines=True, cwd=folder)
# Use new process group for child to prevent terminals
# from sending SIGINT etc.
if os.name == "posix":
popen_kwargs["preexec_fn"] = os.setpgrp
elif os.name == "nt":
popen_kwargs["creationflags"] = (
subprocess.CREATE_NEW_PROCESS_GROUP)
command = hook % {"user": shlex.quote(user or "Anonymous")}
logger.debug("Running hook")
p = subprocess.Popen(command, **popen_kwargs)
try:
stdout_data, stderr_data = p.communicate()
except BaseException: # e.g. KeyboardInterrupt or SystemExit
Expand Down

0 comments on commit 1e011e7

Please sign in to comment.