blob: b60c9b84c2ce840628ee95ecf714461c531010e2 [file] [log] [blame]
Brad Bishop8ffb4022016-07-13 17:56:34 -04001# Utilities and shortcuts for recipes providing D-Bus services.
2# Variables:
3# DBUS_PACKAGES ?= "${PN}"
4# The list of packages to which files should be added.
5#
Patrick Williams12fc9392021-08-06 09:16:53 -05006# DBUS_SERVICE:${PN} += "org.openbmc.Foo.service"
Brad Bishop4117ace2016-07-13 18:25:03 -04007# A list of dbus service names. The class will look for a
Brad Bishopbe42fe92016-08-16 09:40:10 -04008# dbus configuration file with the same base name with .conf
Brad Bishop4117ace2016-07-13 18:25:03 -04009# appended. If one is found, it is added to the package
10# and used verbatim. If it is not found, a default one
11# (with very open permissions) is generated and used.
Brad Bishop502545d2016-07-13 19:10:19 -040012#
13# Additionally the class will instantiate obmc-phosphor-systemd
Patrick Williams12fc9392021-08-06 09:16:53 -050014# with any SYSTEMD_SERVICE:%s variables translated appropriately.
Brad Bishop502545d2016-07-13 19:10:19 -040015#
Brad Bishopbe42fe92016-08-16 09:40:10 -040016# If a service begins with 'dbus-' DBus activation will be
17# configured. The class will look for an activation file
18# with the 'dbus-' prefix removed. If found, it is added to
19# the package and used verbatim. If it is not found, a default
20# one is generated and used.
Brad Bishop502545d2016-07-13 19:10:19 -040021
Brad Bishop4117ace2016-07-13 18:25:03 -040022
Brad Bishop8ffb4022016-07-13 17:56:34 -040023inherit dbus-dir
24inherit obmc-phosphor-utils
Brad Bishop36098402015-09-17 16:39:49 -040025
Patrick Williams12fc9392021-08-06 09:16:53 -050026RDEPENDS:${PN}:append:class-target = " dbus-perms"
Brad Bishop8ffb4022016-07-13 17:56:34 -040027DBUS_PACKAGES ?= "${PN}"
28
29_INSTALL_DBUS_CONFIGS=""
Brad Bishop4117ace2016-07-13 18:25:03 -040030_DEFAULT_DBUS_CONFIGS=""
Brad Bishopf59b8762016-07-13 20:11:44 -040031_INSTALL_DBUS_ACTIVATIONS=""
32_DEFAULT_DBUS_ACTIVATIONS=""
Brad Bishop4117ace2016-07-13 18:25:03 -040033
34
35python dbus_do_postinst() {
Brad Bishopbe42fe92016-08-16 09:40:10 -040036 def make_default_dbus_config(d, unit, user):
37 bus = unit.base
38 if unit.is_template:
39 bus = '%s*' % bus
40
Brad Bishop4117ace2016-07-13 18:25:03 -040041 path = d.getVar('D', True)
42 path += d.getVar('dbus_system_confdir', True)
Brad Bishopbe42fe92016-08-16 09:40:10 -040043 with open('%s/%s.conf' % (path, unit.base), 'w+') as fd:
Brad Bishop4117ace2016-07-13 18:25:03 -040044 fd.write('<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"\n')
45 fd.write(' "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">\n')
46 fd.write('<busconfig>\n')
Brad Bishop502545d2016-07-13 19:10:19 -040047 fd.write(' <policy user="%s">\n' % user)
Brad Bishopbe42fe92016-08-16 09:40:10 -040048 fd.write(' <allow own="%s"/>\n' % bus)
49 fd.write(' <allow send_destination="%s"/>\n' % bus)
Brad Bishop4117ace2016-07-13 18:25:03 -040050 fd.write(' </policy>\n')
51 fd.write('</busconfig>\n')
52 fd.close()
53
54
Brad Bishopbe42fe92016-08-16 09:40:10 -040055 def make_default_dbus_activation(d, unit, user):
56 dest = unit.base
57 sd_unit = unit.name
58 if unit.is_instance:
59 dest = '%s.%s' % (unit.base, unit.instance)
60 sd_unit = '%s@%s' % (unit.base, unit.instance)
61
Brad Bishopf59b8762016-07-13 20:11:44 -040062 path = d.getVar('D', True)
63 path += d.getVar('dbus_system_servicesdir', True)
Brad Bishopbe42fe92016-08-16 09:40:10 -040064 with open('%s/%s.service' % (path, dest), 'w+') as fd:
Brad Bishopf59b8762016-07-13 20:11:44 -040065 fd.write('[D-BUS Service]\n')
Brad Bishopbe42fe92016-08-16 09:40:10 -040066 fd.write('Name=%s\n' % dest)
Brad Bishopf59b8762016-07-13 20:11:44 -040067 fd.write('Exec=/bin/false\n')
68 fd.write('User=%s\n' % user)
Brad Bishopbe42fe92016-08-16 09:40:10 -040069 fd.write('SystemdService=dbus-%s.service\n' % sd_unit)
Brad Bishopf59b8762016-07-13 20:11:44 -040070 fd.close()
71
72
Brad Bishop502545d2016-07-13 19:10:19 -040073 for service_user in listvar_to_list(d, '_DEFAULT_DBUS_CONFIGS'):
Brad Bishopbe42fe92016-08-16 09:40:10 -040074 service, user = service_user.split(':')
75 make_default_dbus_config(d, SystemdUnit(service), user)
Brad Bishopf59b8762016-07-13 20:11:44 -040076 for service_user in listvar_to_list(d, '_DEFAULT_DBUS_ACTIVATIONS'):
Brad Bishopbe42fe92016-08-16 09:40:10 -040077 service, user = service_user.split(':')
78 make_default_dbus_activation(d, SystemdUnit(service), user)
Brad Bishop4117ace2016-07-13 18:25:03 -040079}
Brad Bishop8ffb4022016-07-13 17:56:34 -040080
Brad Bishopef552f62015-09-25 10:23:09 -040081
Brad Bishop36098402015-09-17 16:39:49 -040082python() {
Brad Bishop8ffb4022016-07-13 17:56:34 -040083 searchpaths = d.getVar('FILESPATH', True)
84
Brad Bishop58700d12016-08-15 22:35:58 -040085 def get_user(d, service, pkg):
86 user = d.getVar(
Brad Bishopbe42fe92016-08-16 09:40:10 -040087 'SYSTEMD_USER_%s' % service, True)
Brad Bishop58700d12016-08-15 22:35:58 -040088 if user is None:
89 user = d.getVar(
Brad Bishopbe42fe92016-08-16 09:40:10 -040090 'SYSTEMD_USER_%s' % pkg, True) or 'root'
Brad Bishop58700d12016-08-15 22:35:58 -040091 return user
92
93
Brad Bishopbe42fe92016-08-16 09:40:10 -040094 def add_dbus_config(d, unit, pkg):
95 path = bb.utils.which(searchpaths, '%s.conf' % unit.base)
Brad Bishop4117ace2016-07-13 18:25:03 -040096 if not os.path.isfile(path):
Brad Bishopbe42fe92016-08-16 09:40:10 -040097 user = get_user(d, unit.name, pkg)
Patrick Williams12fc9392021-08-06 09:16:53 -050098 set_doappend(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % (
Brad Bishopbe42fe92016-08-16 09:40:10 -040099 unit.name, user))
Brad Bishop4117ace2016-07-13 18:25:03 -0400100 else:
Patrick Williams12fc9392021-08-06 09:16:53 -0500101 set_doappend(d, 'SRC_URI', 'file://%s.conf' % unit.base)
102 set_doappend(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % unit.base)
103 set_doappend(d, 'FILES:%s' % pkg, '%s%s.conf' \
Brad Bishopbe42fe92016-08-16 09:40:10 -0400104 % (d.getVar('dbus_system_confdir', True), unit.base))
Brad Bishop8ffb4022016-07-13 17:56:34 -0400105
106
Brad Bishopbe42fe92016-08-16 09:40:10 -0400107 def add_dbus_activation(d, unit, pkg):
108 if not unit.is_activated or unit.is_template:
109 return
110 search_match = '%s.service' % unit.base
111 if unit.is_instance:
112 search_match = '%s.%s.service' % (unit.base, unit.instance)
Brad Bishop502545d2016-07-13 19:10:19 -0400113
Brad Bishopbe42fe92016-08-16 09:40:10 -0400114 path = bb.utils.which(searchpaths, search_match)
Brad Bishop502545d2016-07-13 19:10:19 -0400115
Brad Bishopf59b8762016-07-13 20:11:44 -0400116 if not os.path.isfile(path):
Brad Bishopbe42fe92016-08-16 09:40:10 -0400117 user = get_user(d, unit.base, pkg)
Patrick Williams12fc9392021-08-06 09:16:53 -0500118 set_doappend(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % (
Brad Bishopbe42fe92016-08-16 09:40:10 -0400119 unit.name, user))
Brad Bishopf59b8762016-07-13 20:11:44 -0400120 else:
Patrick Williams12fc9392021-08-06 09:16:53 -0500121 set_doappend(d, 'SRC_URI', 'file://%s' % search_match)
122 set_doappend(d, '_INSTALL_DBUS_ACTIVATIONS', search_match)
123 set_doappend(d, 'FILES:%s' % pkg, '%s%s' \
Brad Bishopbe42fe92016-08-16 09:40:10 -0400124 % (d.getVar('dbus_system_servicesdir', True), search_match))
Brad Bishop502545d2016-07-13 19:10:19 -0400125
126
Brad Bishop038b44b2018-01-25 07:00:27 -0500127 if d.getVar('CLASSOVERRIDE', True) != 'class-target':
128 return
129
130 d.appendVarFlag('do_install', 'postfuncs', ' dbus_do_postinst')
131
Brad Bishop8ffb4022016-07-13 17:56:34 -0400132 for pkg in listvar_to_list(d, 'DBUS_PACKAGES'):
Brad Bishop502545d2016-07-13 19:10:19 -0400133 if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''):
Patrick Williams12fc9392021-08-06 09:16:53 -0500134 set_doappend(d, 'SYSTEMD_PACKAGES', pkg)
Brad Bishop502545d2016-07-13 19:10:19 -0400135
Patrick Williams12fc9392021-08-06 09:16:53 -0500136 svc = listvar_to_list(d, 'DBUS_SERVICE:%s' % pkg)
Brad Bishopbe42fe92016-08-16 09:40:10 -0400137 svc = [SystemdUnit(x) for x in svc]
138 inst = [x for x in svc if x.is_instance]
139 tmpl = [x.template for x in svc if x.is_instance]
140 tmpl = list(set(tmpl))
141 tmpl = [SystemdUnit(x) for x in tmpl]
142 svc = [x for x in svc if not x.is_instance]
Brad Bishop8ffb4022016-07-13 17:56:34 -0400143
Brad Bishopbe42fe92016-08-16 09:40:10 -0400144 for unit in inst:
Patrick Williams12fc9392021-08-06 09:16:53 -0500145 set_doappend(
146 d, 'SYSTEMD_SERVICE:%s' % pkg, unit.name)
Brad Bishopbe42fe92016-08-16 09:40:10 -0400147
148 for unit in tmpl + svc:
149 add_dbus_config(d, unit, pkg)
150 add_dbus_activation(d, unit, pkg)
Patrick Williams12fc9392021-08-06 09:16:53 -0500151 set_doappend(
152 d, 'SYSTEMD_SERVICE:%s' % pkg, unit.name)
153 set_doappend(d, 'SYSTEMD_SUBSTITUTIONS',
Brad Bishopbe42fe92016-08-16 09:40:10 -0400154 'BUSNAME:%s:%s' % (unit.base, unit.name))
Brad Bishop36098402015-09-17 16:39:49 -0400155}
156
Brad Bishop8ffb4022016-07-13 17:56:34 -0400157
Patrick Williams12fc9392021-08-06 09:16:53 -0500158do_install:append() {
Brad Bishop8ffb4022016-07-13 17:56:34 -0400159 # install the dbus configuration files
Brad Bishop4117ace2016-07-13 18:25:03 -0400160 [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \
161 [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \
Brad Bishop8ffb4022016-07-13 17:56:34 -0400162 install -d ${D}${dbus_system_confdir}
163 for c in ${_INSTALL_DBUS_CONFIGS}; do
164 install -m 0644 ${WORKDIR}/$c \
165 ${D}${dbus_system_confdir}$c
Brad Bishop36098402015-09-17 16:39:49 -0400166 done
Brad Bishopf59b8762016-07-13 20:11:44 -0400167 # install the dbus activation files
168 [ -z "${_INSTALL_DBUS_ACTIVATIONS}" ] && \
169 [ -z "${_DEFAULT_DBUS_ACTIVATIONS}" ] || \
170 install -d ${D}${dbus_system_servicesdir}
171 for s in ${_INSTALL_DBUS_ACTIVATIONS}; do
172 install -m 0644 ${WORKDIR}/$s\
173 ${D}${dbus_system_servicesdir}$s
174 done
Brad Bishop36098402015-09-17 16:39:49 -0400175}
Brad Bishop4117ace2016-07-13 18:25:03 -0400176
Brad Bishop502545d2016-07-13 19:10:19 -0400177inherit obmc-phosphor-systemd