The freezer.lib.pep3143daemon.daemon Module

Implementation of PEP 3143 DaemonContext

class freezer.lib.pep3143daemon.daemon.DaemonContext(chroot_directory=None, working_directory='/', umask=0, uid=None, gid=None, prevent_core=True, detach_process=None, files_preserve=None, pidfile=None, stdin=None, stdout=None, stderr=None, signal_map=None)[source]

Bases: object

Implementation of PEP 3143 DaemonContext class

This class should be instantiated only once in every program that has to become a Unix Daemon. Typically you should call its open method after you have done everything that may require root privileges. For example opening port <= 1024.

Each option can be passed as a keyword argument to the constructor, but can also be changed by assigning a new value to the corresponding attribute on the instance.

Altering attributes after open() is called, will have no effect. In future versions, trying to do so, will may raise a DaemonError.

Parameters:
  • chroot_directory (str) – Full path to the directory that should be set as effective root directory. If None, the root directory is not changed.
  • working_directory (str.) – Full Path to the working directory to which to change to. If chroot_directory is not None, and working_directory is not starting with chroot_directory, working directory is prefixed with chroot_directory.
  • umask (int.) – File access creation mask for this daemon after start
  • uid (int.) – Effective user id after daemon start.
  • gid (int.) – Effective group id after daemon start.
  • prevent_core (bool.) – Prevent core file generation.
  • detach_process (bool.) – If True, do the double fork magic. If the process was started by inet or an init like program, you may don´t need to detach. If not set, we try to figure out if forking is needed.
  • files_preserve (list) – List of integers, or objects with a fileno method, that represent files that should not be closed while daemonizing.
  • pidfile (Instance of Class that implements a pidfile behaviour) – Instance that implements a pidfile, while daemonizing its acquire method will be called.
  • stdin (file object.) – Redirect stdin to this file, if None, redirect to /dev/null.
  • stdout (file object.) – Redirect stdout to this file, if None, redirect to /dev/null.
  • stderr (file object.) – Redirect stderr to this file, if None, redirect to /dev/null.
  • signal_map (instance of dict) – Mapping from operating system signal to callback actions.
close()[source]

Dummy function

is_open

True when this instances open method was called

Returns:bool
open()[source]

Daemonize this process

Do everything that is needed to become a Unix daemon.

Returns:None
Raise:DaemonError
terminate(signal_number, stack_frame)[source]

Terminate this process

Simply terminate this process by raising SystemExit. This method is called if signal.SIGTERM was received.

Check carefully if this really is what you want!

Most likely it is not!

You should implement a function/method that is able to cleanly shutdown you daemon. Like gracefully terminating child processes, threads. or closing files.

You can create a custom handler by overriding this method, ot setting a custom handler via the signal_map. It is also possible to set the signal handlers directly via signal.signal().

Returns:None
Raise:SystemExit
working_directory

The working_directory property

Returns:str
exception freezer.lib.pep3143daemon.daemon.DaemonError[source]

Bases: exceptions.Exception

Exception raised by DaemonContext

freezer.lib.pep3143daemon.daemon.close_filenos(preserve)[source]

Close unprotected file descriptors

Close all open file descriptors that are not in preserve.

If ulimit -nofile is “unlimited”, all is defined filenos <= 4096, else all is <= the output of resource.getrlimit().

Parameters:preserve (set) – set with protected files
Returns:None
freezer.lib.pep3143daemon.daemon.default_signal_map()[source]

Create the default signal map for this system.

Returns:dict
freezer.lib.pep3143daemon.daemon.detach_required()[source]

Check if detaching is required

This is done by collecting the results of parent_is_inet and parent_is_init. If one of them is True, detaching, aka the daemonizing, aka the double fork magic, is not required, and can be skipped.

Returns:bool
freezer.lib.pep3143daemon.daemon.parent_is_inet()[source]

Check if parent is inet

Check if our parent seems ot be a superserver, aka inetd/xinetd.

This is done by checking if sys.__stdin__ is a network socket.

Returns:bool
freezer.lib.pep3143daemon.daemon.parent_is_init()[source]

Check if parent is Init

Check if the parent process is init, or something else that owns PID 1.

Returns:bool
freezer.lib.pep3143daemon.daemon.redirect_stream(system, target)[source]

Redirect Unix streams

If None, redirect Stream to /dev/null, else redirect to target.

Parameters:
  • system (file object) – ether sys.stdin, sys.stdout, or sys.stderr
  • target (None, File Object) – File like object, or None
Returns:

None

Raise:

DaemonError