Debian Codegraph @

lib/ansible/module_utils/facts/system/service_mgr.py

raw
1 # Collect facts related to system service manager and init.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import os
21 import platform
22 import re
23 import typing as t
24
25 from ansible.module_utils.common.text.converters import to_native
26
27 from ansible.module_utils.facts.utils import get_file_content
28 from ansible.module_utils.facts.collector import BaseFactCollector
29
30 # The distutils module is not shipped with SUNWPython on Solaris.
31 # It's in the SUNWPython-devel package which also contains development files
32 # that don't belong on production boxes. Since our Solaris code doesn't
33 # depend on LooseVersion, do not import it on Solaris.
34 if platform.system() != 'SunOS':
35 from ansible.module_utils.compat.version import LooseVersion
36
37
38 class ServiceMgrFactCollector(BaseFactCollector):
39 name = 'service_mgr'
40 _fact_ids = set() # type: t.Set[str]
41 required_facts = set(['platform', 'distribution'])
42
43 @staticmethod
44 def is_systemd_managed(module):
45 # tools must be installed
46 if module.get_bin_path('systemctl'):
47
48 # this should show if systemd is the boot init system, if checking init failed to mark as systemd
49 # these mirror systemd's own sd_boot test http://www.freedesktop.org/software/systemd/man/sd_booted.html
50 for canary in ["/run/systemd/system/", "/dev/.run/systemd/", "/dev/.systemd/"]:
51 if os.path.exists(canary):
52 return True
53 return False
54
55 @staticmethod
56 def is_systemd_managed_offline(module):
57 # tools must be installed
58 if module.get_bin_path('systemctl'):
59 # check if /sbin/init is a symlink to systemd
60 # on SUSE, /sbin/init may be missing if systemd-sysvinit package is not installed.
61 if os.path.islink('/sbin/init') and os.path.basename(os.readlink('/sbin/init')) == 'systemd':
62 return True
63 return False
64
65 def collect(self, module=None, collected_facts=None):
66 facts_dict = {}
67
68 if not module:
69 return facts_dict
70
71 collected_facts = collected_facts or {}
72 service_mgr_name = None
73
74 # TODO: detect more custom init setups like bootscripts, dmd, s6, Epoch, etc
75 # also other OSs other than linux might need to check across several possible candidates
76
77 # Mapping of proc_1 values to more useful names
78 proc_1_map = {
79 'procd': 'openwrt_init',
80 'runit-init': 'runit',
81 'svscan': 'svc',
82 'openrc-init': 'openrc',
83 }
84
85 # try various forms of querying pid 1
86 proc_1 = get_file_content('/proc/1/comm')
87 if proc_1 is None:
88 rc, proc_1, err = module.run_command("ps -p 1 -o comm|tail -n 1", use_unsafe_shell=True)
89
90 # if command fails, or stdout is empty string or the output of the command starts with what looks like a PID,
91 # then the 'ps' command probably didn't work the way we wanted, probably because it's busybox
92 if rc != 0 or not proc_1.strip() or re.match(r' *[0-9]+ ', proc_1):
93 proc_1 = None
94
95 # The ps command above may return "COMMAND" if the user cannot read /proc, e.g. with grsecurity
96 if proc_1 == "COMMAND\n":
97 proc_1 = None
98
99 if proc_1 is None and os.path.islink('/sbin/init'):
100 proc_1 = os.readlink('/sbin/init')
101
102 if proc_1 is not None:
103 proc_1 = os.path.basename(proc_1)
104 proc_1 = to_native(proc_1)
105 proc_1 = proc_1.strip()
106
107 if proc_1 is not None and (proc_1 == 'init' or proc_1.endswith('sh')):
108 # many systems return init, so this cannot be trusted, if it ends in 'sh' it probably is a shell in a container
109 proc_1 = None
110
111 # if not init/None it should be an identifiable or custom init, so we are done!
112 if proc_1 is not None:
113 # Lookup proc_1 value in map and use proc_1 value itself if no match
114 service_mgr_name = proc_1_map.get(proc_1, proc_1)
115
116 # start with the easy ones
117 elif collected_facts.get('ansible_distribution', None) == 'MacOSX':
118 # FIXME: find way to query executable, version matching is not ideal
119 if LooseVersion(platform.mac_ver()[0]) >= LooseVersion('10.4'):
120 service_mgr_name = 'launchd'
121 else:
122 service_mgr_name = 'systemstarter'
123 elif 'BSD' in collected_facts.get('ansible_system', '') or collected_facts.get('ansible_system') in ['Bitrig', 'DragonFly']:
124 # FIXME: we might want to break out to individual BSDs or 'rc'
125 service_mgr_name = 'bsdinit'
126 elif collected_facts.get('ansible_system') == 'AIX':
127 service_mgr_name = 'src'
128 elif collected_facts.get('ansible_system') == 'SunOS':
129 service_mgr_name = 'smf'
130 elif collected_facts.get('ansible_distribution') == 'OpenWrt':
131 service_mgr_name = 'openwrt_init'
132 elif collected_facts.get('ansible_distribution') == 'SMGL':
133 service_mgr_name = 'simpleinit_msb'
134 elif collected_facts.get('ansible_system') == 'Linux':
135 # FIXME: mv is_systemd_managed
136 if self.is_systemd_managed(module=module):
137 service_mgr_name = 'systemd'
138 elif module.get_bin_path('initctl') and os.path.exists("/etc/init/"):
139 service_mgr_name = 'upstart'
140 elif os.path.exists('/sbin/openrc'):
141 service_mgr_name = 'openrc'
142 elif self.is_systemd_managed_offline(module=module):
143 service_mgr_name = 'systemd'
144 elif os.path.exists('/etc/init.d/'):
145 service_mgr_name = 'sysvinit'
146 elif os.path.exists('/etc/dinit.d/'):
147 service_mgr_name = 'dinit'
148
149 if not service_mgr_name:
150 # if we cannot detect, fallback to generic 'service'
151 service_mgr_name = 'service'
152
153 facts_dict['service_mgr'] = service_mgr_name
154 return facts_dict