MCPcopy Index your code
hub / github.com/borgbackup/borg / _daemonize

Function _daemonize

src/borg/helpers/process.py:22–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20
21@contextlib.contextmanager
22def _daemonize():
23 from ..platform import get_process_id
24
25 old_id = get_process_id()
26 pid = os.fork()
27 if pid:
28 exit_code = EXIT_SUCCESS
29 try:
30 yield old_id, None
31 except _ExitCodeException as e:
32 exit_code = e.exit_code
33 finally:
34 logger.debug("Daemonizing: Foreground process (%s, %s, %s) is now dying." % old_id)
35 os._exit(exit_code)
36 os.setsid()
37 pid = os.fork()
38 if pid:
39 os._exit(0)
40 os.chdir("/")
41 os.close(0)
42 os.close(1)
43 fd = os.open(os.devnull, os.O_RDWR)
44 os.dup2(fd, 0)
45 os.dup2(fd, 1)
46 new_id = get_process_id()
47 try:
48 yield old_id, new_id
49 finally:
50 # Close / redirect stderr to /dev/null only now
51 # for the case that we want to log something before yield returns.
52 os.close(2)
53 os.dup2(fd, 2)
54
55
56def daemonize():

Callers 2

daemonizeFunction · 0.85
daemonizingFunction · 0.85

Calls 4

get_process_idFunction · 0.85
debugMethod · 0.80
closeMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected