blob: 029d2609364202771a4444b088a63cff5b47e79c [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#
Brad Bishopbe42fe92016-08-16 09:40:10 -04006# 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
14# with any SYSTEMD_SERVICE_%s variables translated appropriately.
15#
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
Brad Bishop3db4ad72016-07-15 10:22:24 -040026RDEPENDS_${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)
Brad Bishop502545d2016-07-13 19:10:19 -040098 set_append(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:
Brad Bishopbe42fe92016-08-16 09:40:10 -0400101 set_append(d, 'SRC_URI', 'file://%s.conf' % unit.base)
102 set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % unit.base)
Brad Bishop8ffb4022016-07-13 17:56:34 -0400103 set_append(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)
Brad Bishopf59b8762016-07-13 20:11:44 -0400118 set_append(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:
Brad Bishopbe42fe92016-08-16 09:40:10 -0400121 set_append(d, 'SRC_URI', 'file://%s' % search_match)
122 set_append(d, '_INSTALL_DBUS_ACTIVATIONS', search_match)
123 set_append(d, 'FILES_%s' % pkg, '%s%s' \
124 % (d.getVar('dbus_system_servicesdir', True), search_match))
Brad Bishop502545d2016-07-13 19:10:19 -0400125
126
Brad Bishop8ffb4022016-07-13 17:56:34 -0400127 for pkg in listvar_to_list(d, 'DBUS_PACKAGES'):
Brad Bishop502545d2016-07-13 19:10:19 -0400128 if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''):
129 set_append(d, 'SYSTEMD_PACKAGES', pkg)
130
Brad Bishopbe42fe92016-08-16 09:40:10 -0400131 svc = listvar_to_list(d, 'DBUS_SERVICE_%s' % pkg)
132 svc = [SystemdUnit(x) for x in svc]
133 inst = [x for x in svc if x.is_instance]
134 tmpl = [x.template for x in svc if x.is_instance]
135 tmpl = list(set(tmpl))
136 tmpl = [SystemdUnit(x) for x in tmpl]
137 svc = [x for x in svc if not x.is_instance]
Brad Bishop8ffb4022016-07-13 17:56:34 -0400138
Brad Bishopbe42fe92016-08-16 09:40:10 -0400139 for unit in inst:
140 set_append(
141 d, 'SYSTEMD_SERVICE_%s' % pkg, unit.name)
142
143 for unit in tmpl + svc:
144 add_dbus_config(d, unit, pkg)
145 add_dbus_activation(d, unit, pkg)
146 set_append(
147 d, 'SYSTEMD_SERVICE_%s' % pkg, unit.name)
148 set_append(d, 'SYSTEMD_SUBSTITUTIONS',
149 'BUSNAME:%s:%s' % (unit.base, unit.name))
Brad Bishop36098402015-09-17 16:39:49 -0400150}
151
Brad Bishop8ffb4022016-07-13 17:56:34 -0400152
Brad Bishop36098402015-09-17 16:39:49 -0400153do_install_append() {
Brad Bishop8ffb4022016-07-13 17:56:34 -0400154 # install the dbus configuration files
Brad Bishop4117ace2016-07-13 18:25:03 -0400155 [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \
156 [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \
Brad Bishop8ffb4022016-07-13 17:56:34 -0400157 install -d ${D}${dbus_system_confdir}
158 for c in ${_INSTALL_DBUS_CONFIGS}; do
159 install -m 0644 ${WORKDIR}/$c \
160 ${D}${dbus_system_confdir}$c
Brad Bishop36098402015-09-17 16:39:49 -0400161 done
Brad Bishopf59b8762016-07-13 20:11:44 -0400162 # install the dbus activation files
163 [ -z "${_INSTALL_DBUS_ACTIVATIONS}" ] && \
164 [ -z "${_DEFAULT_DBUS_ACTIVATIONS}" ] || \
165 install -d ${D}${dbus_system_servicesdir}
166 for s in ${_INSTALL_DBUS_ACTIVATIONS}; do
167 install -m 0644 ${WORKDIR}/$s\
168 ${D}${dbus_system_servicesdir}$s
169 done
Brad Bishop36098402015-09-17 16:39:49 -0400170}
Brad Bishop4117ace2016-07-13 18:25:03 -0400171
172do_install[postfuncs] += "dbus_do_postinst"
Brad Bishop502545d2016-07-13 19:10:19 -0400173
174inherit obmc-phosphor-systemd