Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / python / systemd.py
1 # -*- test-case-name: twisted.python.test.test_systemd -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 """
6 Integration with systemd.
7
8 Currently only the minimum APIs necessary for using systemd's socket activation
9 feature are supported.
10 """
11
12 __all__ = ['ListenFDs']
13
14 from os import getpid
15
16
17 class ListenFDs(object):
18     """
19     L{ListenFDs} provides access to file descriptors inherited from systemd.
20
21     Typically L{ListenFDs.fromEnvironment} should be used to construct a new
22     instance of L{ListenFDs}.
23
24     @cvar _START: File descriptors inherited from systemd are always
25         consecutively numbered, with a fixed lowest "starting" descriptor.  This
26         gives the default starting descriptor.  Since this must agree with the
27         value systemd is using, it typically should not be overridden.
28     @type _START: C{int}
29
30     @ivar _descriptors: A C{list} of C{int} giving the descriptors which were
31         inherited.
32     """
33     _START = 3
34
35     def __init__(self, descriptors):
36         """
37         @param descriptors: The descriptors which will be returned from calls to
38             C{inheritedDescriptors}.
39         """
40         self._descriptors = descriptors
41
42
43     @classmethod
44     def fromEnvironment(cls, environ=None, start=None):
45         """
46         @param environ: A dictionary-like object to inspect to discover
47             inherited descriptors.  By default, C{None}, indicating that the
48             real process environment should be inspected.  The default is
49             suitable for typical usage.
50
51         @param start: An integer giving the lowest value of an inherited
52             descriptor systemd will give us.  By default, C{None}, indicating
53             the known correct (that is, in agreement with systemd) value will be
54             used.  The default is suitable for typical usage.
55
56         @return: A new instance of C{cls} which can be used to look up the
57             descriptors which have been inherited.
58         """
59         if environ is None:
60             from os import environ
61         if start is None:
62             start = cls._START
63
64         descriptors = []
65
66         try:
67             pid = int(environ['LISTEN_PID'])
68         except (KeyError, ValueError):
69             pass
70         else:
71             if pid == getpid():
72                 try:
73                     count = int(environ['LISTEN_FDS'])
74                 except (KeyError, ValueError):
75                     pass
76                 else:
77                     descriptors = range(start, start + count)
78                     del environ['LISTEN_PID'], environ['LISTEN_FDS']
79
80         return cls(descriptors)
81
82
83     def inheritedDescriptors(self):
84         """
85         @return: The configured list of descriptors.
86         """
87         return list(self._descriptors)