aboutsummaryrefslogtreecommitdiffstats
path: root/meta-oe/classes/systemd.bbclass
blob: 2881abff66e6e3387f64b3239127e565092b7221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
DEPENDS_append = " systemd-systemctl-native"

systemd_postinst() {
OPTS=""

if [ -n "$D" ]; then
    OPTS="--root=$D"
fi

systemctl $OPTS enable ${SYSTEMD_SERVICE}

if [ -z "$D" ]; then
    systemctl start ${SYSTEMD_SERVICE}
fi
}

systemd_prerm() {
if [ -z "$D" ]; then
    systemctl stop ${SYSTEMD_SERVICE}
fi
}

systemd_postrm() {
systemctl disable ${SYSTEMD_SERVICE}
}

def systemd_after_parse(d):
	def systemd_check_vars():
		if d.getVar('BB_WORKERCONTEXT', True) is not None:
			return

		bb_filename = d.getVar('FILE')
		packages = d.getVar('PACKAGES', 1)

		# check SYSTEMD_PACKAGES
		systemd_pkgs = d.getVar('SYSTEMD_PACKAGES', 1) or ""
		if systemd_pkgs == "":
			raise bb.build.FuncFailed, "\n\n%s inherits systemd but doesn't set SYSTEMD_PACKAGES" % bb_filename
		for pkg_systemd in systemd_pkgs.split():
			if pkg_systemd.find("-systemd") == -1:
				if pkg_systemd != d.getVar('PN', 1):
					raise bb.build.FuncFailed, \
						"\n\n%s: %s in SYSTEMD_PACKAGES does not match <existing-package>-systemd or ${PN} (deprecated)" % \
						(bb_filename, pkg_systemd)
				else:
					# Only complain if recipe lacks native systemd support
					native_systemd_support = d.getVar('NATIVE_SYSTEMD_SUPPORT', 1) or ""
					if native_systemd_support == "":
						bb.warn("%s: it is recommended to set SYSTEMD_PACKAGES as <existing-package>-systemd" % bb_filename)
			else:
				pkg_systemd_base = pkg_systemd.replace('-systemd', '')
				if pkg_systemd_base not in packages:
					raise bb.build.FuncFailed, \
						"\n\n%s: %s in SYSTEMD_PACKAGES does not match <existing-package>-systemd or ${PN} (deprecated)" % \
						( bb_filename, pkg_systemd)

	# prepend systemd-packages not already included
	def systemd_create_package(pkg_systemd):
		packages = d.getVar('PACKAGES', 1)
		if not pkg_systemd in packages:
			packages = "%s %s" % (pkg_systemd, packages)
			d.setVar('PACKAGES', packages)


	bpn = d.getVar('BPN', 1)
	if bpn + "-native" != d.getVar('PN', 1) and \
		    bpn + "-cross" != d.getVar('PN', 1) and \
		    bpn + "-nativesdk" != d.getVar('PN', 1):
		systemd_check_vars()
		for pkg_systemd in d.getVar('SYSTEMD_PACKAGES', 1).split():
			systemd_create_package(pkg_systemd)


python __anonymous() {
    systemd_after_parse(d)
}

# automatically install all *.service and *.socket supplied in recipe's SRC_URI
do_install_append() {
    for service in `find ${WORKDIR} -maxdepth 1 -name '*.service' -o -name '*.socket'` ; do
	# ensure installing systemd-files only (e.g not avahi *.service)
	if grep -q '\[Unit\]' $service ; then
		install -d ${D}${systemd_unitdir}/system
		install -m 644 $service ${D}${systemd_unitdir}/system
	fi
    done
}

python populate_packages_prepend () {
	def systemd_generate_package_scripts(pkg):
		bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
		localdata = bb.data.createCopy(d)
		overrides = bb.data.getVar("OVERRIDES", localdata, 1)
		bb.data.setVar("OVERRIDES", "%s:%s" % (pkg, overrides), localdata)
		bb.data.update_data(localdata)

		"""
		systemd postinst is appended here because pkg_postinst may require to
		execute on the target. Not doing so may cause systemd postinst invoked
		twice to cause unwanted warnings.
		""" 
		postinst = bb.data.getVar('pkg_postinst', localdata, 1)
		if not postinst:
			postinst = '#!/bin/sh\n'
		postinst += bb.data.getVar('systemd_postinst', localdata, 1)
		bb.data.setVar('pkg_postinst_%s' % pkg, postinst, d)

		prerm = bb.data.getVar('pkg_prerm', localdata, 1)
		if not prerm:
			prerm = '#!/bin/sh\n'
		prerm += bb.data.getVar('systemd_prerm', localdata, 1)
		bb.data.setVar('pkg_prerm_%s' % pkg, prerm, d)

		postrm = bb.data.getVar('pkg_postrm', localdata, 1)
		if not postrm:
			postrm = '#!/bin/sh\n'
		postrm += bb.data.getVar('systemd_postrm', localdata, 1)
		bb.data.setVar('pkg_postrm_%s' % pkg, postrm, d)

	# add files to FILES_*-systemd if existent and not already done
	def systemd_append_file(pkg_systemd, file_append):
		appended = False
		if os.path.exists('${D}' + file_append):
			var_name = "FILES_" + pkg_systemd
			files = d.getVar(var_name, 0) or ""
			if file_append not in files.split():
				d.setVar(var_name, "%s %s" % (files, file_append))
				appended = True
		return appended

	# add systemd files to FILES_*-systemd, parse for Also= and follow recursive
	def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
		# avoid infinite recursion
		if systemd_append_file(pkg_systemd, path + service):
			fullpath = '${D}' + path + service
			if service.find('.socket') != -1:
				# for *.socket add *.service and *@.service
				service_base = service.replace('.socket', '')
				systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
				systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
			for key in keys.split():
				# recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
				cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key)
				pipe = os.popen(cmd, 'r')
				line = pipe.readline()
				while line:
					line = line.replace('\n', '')
					systemd_add_files_and_parse(pkg_systemd, path, line, keys)
					line = pipe.readline()
				pipe.close()

	# check service-files and call systemd_add_files_and_parse for each entry
	def systemd_check_services():
		searchpaths = '/etc/systemd/system/ /lib/systemd/system/ /usr/lib/systemd/system/'
		systemd_packages = d.getVar('SYSTEMD_PACKAGES', 1)
		has_exactly_one_service = len(systemd_packages.split()) == 1
		if has_exactly_one_service:
			systemd_services = d.getVar('SYSTEMD_SERVICE' + "_" + systemd_packages, 1) or d.getVar('SYSTEMD_SERVICE', 1)
			has_exactly_one_service = len(systemd_services.split()) == 1

		keys = 'Also' # Conflicts??
		if has_exactly_one_service:
			# single service gets also the /dev/null dummies
			keys = 'Also Conflicts'
		# scan for all in SYSTEMD_SERVICE[]
		for pkg_systemd in systemd_packages.split():
			systemd_services = d.getVar('SYSTEMD_SERVICE' + "_" + pkg_systemd, 1) or d.getVar('SYSTEMD_SERVICE', 1)
			for service in systemd_services.split():
				path_found = ''
				for path in searchpaths.split():
					if os.path.exists('${D}' + path + service):
						path_found = path
				if path_found != '':
					systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
				else:
					raise bb.build.FuncFailed, "\n\nFor package %s SYSTEMD_SERVICE-entry %s does not exist" % \
						(pkg_systemd, service)

	# *-systemd packages get RDEPENDS to systemd and their base package
	def systemd_add_rdepends(pkg_systemd):
		# RDEPENDS_${pkg_systemd} += pkg_systemd_base systemd
		rdepends = d.getVar('RDEPENDS_' + pkg_systemd, 1) or ""
		rdepends_arr = rdepends.split()
		if not 'systemd' in rdepends_arr:
			rdepends = '%s %s' % (rdepends, 'systemd')
		pkg_systemd_base = pkg_systemd.replace('-systemd', '')
		# no automatism for:
		# recipes setting rdepends themselves AND
		# not rdepending myself AND
		# avoid double entries
		if len(rdepends_arr) == 0 and pkg_systemd != '${PN}' and not pkg_systemd_base in rdepends:
			rdepends = '%s %s' % (rdepends, pkg_systemd_base)
		d.setVar('RDEPENDS_' + pkg_systemd, rdepends)


	# run all modifications once when creating package
	if os.path.exists('${D}'):
		for pkg_systemd in d.getVar('SYSTEMD_PACKAGES', 1).split():
			if d.getVar('SYSTEMD_SERVICE' + "_" + pkg_systemd, 1) and d.getVar('SYSTEMD_SERVICE' + "_" + pkg_systemd, 1).strip():
				systemd_generate_package_scripts(pkg_systemd)
				systemd_add_rdepends(pkg_systemd)
		systemd_check_services()
}