diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-03-15 19:01:10 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-03-15 22:55:24 -0400 |
commit | b04c8c83e8d5670b0923c7cd7d6ea622b0187289 (patch) | |
tree | 9faeb93a6d086f1cb213535be5397fefe17b1581 /src/python-systemd/daemon.py | |
parent | 5e65c93a433447b15180249166f7b3944c3e6156 (diff) |
systemd-python: add systemd.daemon wrapping sd-daemon
Please see the documentation (e.g. pydoc3 systemd.daemon) for full
description. As usual, systemd._daemon wraps the raw interface, while
systemd.daemon provides the more pythonic API. sd_listen_fds,
sd_booted, sd_is_fifo, sd_is_socket, sd_is_socket_unix,
sd_is_socket_inet, sd_is_mq, and SD_LISTEN_FDS_START are currently
wrapped.
Diffstat (limited to 'src/python-systemd/daemon.py')
-rw-r--r-- | src/python-systemd/daemon.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/python-systemd/daemon.py b/src/python-systemd/daemon.py new file mode 100644 index 0000000000..4a02204433 --- /dev/null +++ b/src/python-systemd/daemon.py @@ -0,0 +1,53 @@ +from ._daemon import (booted, + _listen_fds, + _is_fifo, + _is_socket, + _is_socket_inet, + _is_socket_unix, + _is_mq, + LISTEN_FDS_START) +from socket import AF_UNSPEC as _AF_UNSPEC + +def _convert_fileobj(fileobj): + try: + return fileobj.fileno() + except AttributeError: + return fileobj + +def is_fifo(fileobj, path=None): + fd = _convert_fileobj(fileobj) + return _is_fifo(fd, path) + +def is_socket(fileobj, family=_AF_UNSPEC, type=0, listening=-1): + fd = _convert_fileobj(fileobj) + return _is_socket(fd, family, type, listening) + +def is_socket_inet(fileobj, family=_AF_UNSPEC, type=0, listening=-1, port=0): + fd = _convert_fileobj(fileobj) + return _is_socket_inet(fd, family, type, listening) + +def is_socket_unix(fileobj, type=0, listening=-1, path=None): + fd = _convert_fileobj(fileobj) + return _is_socket_unix(fd, type, listening, path) + +def is_mq(fileobj, path=None): + fd = _convert_fileobj(fileobj) + return _is_mq(fd, path) + +def listen_fds(unset_environment=True): + """Return a list of socket activated descriptors + + Example:: + + (in primary window) + $ systemd-activate -l 2000 python3 -c \\ + 'from systemd.daemon import listen_fds; print(listen_fds())' + (in another window) + $ telnet localhost 2000 + (in primary window) + ... + Execing python3 (...) + [3] + """ + num = _listen_fds(unset_environment) + return list(range(LISTEN_FDS_START, LISTEN_FDS_START + num)) |