blob: 4066c6c1ab5a830e839d140553039a4508db7b94 [file] [log] [blame]
Brad Bishopff7dc1d2016-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#
6# DBUS_SERVICE_${PN} += "org.openbmc.Foo"
Brad Bishop7ebf8542016-07-13 18:25:03 -04007# A list of dbus service names. The class will look for a
8# dbus configuration file with the same name with .conf
9# 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 Bishopf90a17d2016-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 Bishop9e28d6e2016-07-13 20:11:44 -040016# DBUS_ACTIVATED_SERVICE_${PN} += "org.openbmc.Foo"
17# A list of services that should have dbus activation configured.
18# Services that appear here need not be in DBUS_SERVICE_%s.
19# If used, the search pattern for the systemd unit file is
20# changed to be dbus-%s.service. The class will look for a
21# dbus activation file with the same name with .service appended.
22# If one is found, it added to the package and used verbatim.
23# If it is not found, a default one is generated and used.
24
Brad Bishopf90a17d2016-07-13 19:10:19 -040025# DBUS_USER_${PN}_org.openbmc.Foo = "dbususer"
26# The user a service should be configured to run as. If unspecified
27# no User property is added.
28
Brad Bishop7ebf8542016-07-13 18:25:03 -040029
Brad Bishopff7dc1d2016-07-13 17:56:34 -040030inherit dbus-dir
31inherit obmc-phosphor-utils
Brad Bishop9ee75e12015-09-17 16:39:49 -040032
Brad Bishopa1811022016-07-15 10:22:24 -040033RDEPENDS_${PN}_append_class-target = " dbus-perms"
Brad Bishopff7dc1d2016-07-13 17:56:34 -040034DBUS_PACKAGES ?= "${PN}"
35
36_INSTALL_DBUS_CONFIGS=""
Brad Bishop7ebf8542016-07-13 18:25:03 -040037_DEFAULT_DBUS_CONFIGS=""
Brad Bishop9e28d6e2016-07-13 20:11:44 -040038_INSTALL_DBUS_ACTIVATIONS=""
39_DEFAULT_DBUS_ACTIVATIONS=""
Brad Bishop7ebf8542016-07-13 18:25:03 -040040
41
42python dbus_do_postinst() {
Brad Bishopf90a17d2016-07-13 19:10:19 -040043 def make_default_dbus_config(d, service, user):
Brad Bishop7ebf8542016-07-13 18:25:03 -040044 path = d.getVar('D', True)
45 path += d.getVar('dbus_system_confdir', True)
46 with open('%s/%s.conf' % (path, service), 'w+') as fd:
47 fd.write('<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"\n')
48 fd.write(' "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">\n')
49 fd.write('<busconfig>\n')
Brad Bishopf90a17d2016-07-13 19:10:19 -040050 fd.write(' <policy user="%s">\n' % user)
Brad Bishop7ebf8542016-07-13 18:25:03 -040051 fd.write(' <allow own="%s"/>\n' % service)
52 fd.write(' <allow send_destination="%s"/>\n' % service)
53 fd.write(' </policy>\n')
54 fd.write('</busconfig>\n')
55 fd.close()
56
57
Brad Bishop9e28d6e2016-07-13 20:11:44 -040058 def make_default_dbus_activation(d, service, user):
59 path = d.getVar('D', True)
60 path += d.getVar('dbus_system_servicesdir', True)
61 with open('%s/%s.service' % (path, service), 'w+') as fd:
62 fd.write('[D-BUS Service]\n')
63 fd.write('Name=%s\n' % service)
64 fd.write('Exec=/bin/false\n')
65 fd.write('User=%s\n' % user)
66 fd.write('SystemdService=dbus-%s.service\n' % service)
67 fd.close()
68
69
Brad Bishopf90a17d2016-07-13 19:10:19 -040070 for service_user in listvar_to_list(d, '_DEFAULT_DBUS_CONFIGS'):
71 make_default_dbus_config(d, *service_user.split(':'))
Brad Bishop9e28d6e2016-07-13 20:11:44 -040072 for service_user in listvar_to_list(d, '_DEFAULT_DBUS_ACTIVATIONS'):
73 make_default_dbus_activation(d, *service_user.split(':'))
Brad Bishop7ebf8542016-07-13 18:25:03 -040074}
Brad Bishopff7dc1d2016-07-13 17:56:34 -040075
Brad Bishop065a1be2015-09-25 10:23:09 -040076
Brad Bishop9ee75e12015-09-17 16:39:49 -040077python() {
Brad Bishopff7dc1d2016-07-13 17:56:34 -040078 searchpaths = d.getVar('FILESPATH', True)
79
80 def add_dbus_config(d, service, pkg):
Brad Bishop7ebf8542016-07-13 18:25:03 -040081 path = bb.utils.which(searchpaths, '%s.conf' % service)
82 if not os.path.isfile(path):
Brad Bishopf90a17d2016-07-13 19:10:19 -040083 user = d.getVar(
84 'DBUS_USER_%s_%s' % (pkg, service), True) or 'root'
85 set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % (
86 service, user))
Brad Bishop7ebf8542016-07-13 18:25:03 -040087 else:
88 set_append(d, 'SRC_URI', 'file://%s.conf' % service)
89 set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % service)
Brad Bishopff7dc1d2016-07-13 17:56:34 -040090 set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \
91 % (d.getVar('dbus_system_confdir', True), service))
Brad Bishopff7dc1d2016-07-13 17:56:34 -040092
93
Brad Bishop9e28d6e2016-07-13 20:11:44 -040094 def add_sd_unit(d, prefix, service, pkg):
Brad Bishopf90a17d2016-07-13 19:10:19 -040095 set_append(
Brad Bishop9e28d6e2016-07-13 20:11:44 -040096 d, 'SYSTEMD_SERVICE_%s' % pkg, '%s%s.service' % (
97 prefix, service))
98 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s%s.service' % (prefix, service),
Brad Bishopf90a17d2016-07-13 19:10:19 -040099 'BUSNAME:%s' % service)
100
101
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400102 def add_sd_user(d, prefix, service, pkg):
Brad Bishopf90a17d2016-07-13 19:10:19 -0400103 user = d.getVar(
104 'DBUS_USER_%s_%s' % (pkg, service), True)
105 if user:
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400106 set_append(d, 'SYSTEMD_USER_%s_%s%s.service' % (
107 pkg, prefix, service), user)
108
109
110 def add_dbus_activation(d, service, pkg):
111 path = bb.utils.which(searchpaths, '%s.service' % service)
112 if not os.path.isfile(path):
113 user = d.getVar(
114 'DBUS_USER_%s_%s' % (pkg, service), True) or 'root'
115 set_append(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % (
116 service, user))
117 else:
118 set_append(d, 'SRC_URI', 'file://%s.service' % service)
119 set_append(d, '_INSTALL_DBUS_ACTIVATIONS', '%s.service' % service)
120 set_append(d, 'FILES_%s' % pkg, '%s%s.service' \
121 % (d.getVar('dbus_system_servicesdir', True), service))
Brad Bishopf90a17d2016-07-13 19:10:19 -0400122
123
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400124 for pkg in listvar_to_list(d, 'DBUS_PACKAGES'):
Brad Bishopf90a17d2016-07-13 19:10:19 -0400125 if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''):
126 set_append(d, 'SYSTEMD_PACKAGES', pkg)
127
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400128 services = listvar_to_list(d, 'DBUS_SERVICE_%s' % pkg)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400129 auto = listvar_to_list(d, 'DBUS_ACTIVATED_SERVICE_%s' % pkg)
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400130
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400131 for service in set(services).union(auto):
132 prefix = 'dbus-' if service in auto else ''
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400133 add_dbus_config(d, service, pkg)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400134 add_sd_unit(d, prefix, service, pkg)
135 add_sd_user(d, prefix, service, pkg)
136 if prefix:
137 add_dbus_activation(d, service, pkg)
Brad Bishop9ee75e12015-09-17 16:39:49 -0400138}
139
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400140
Brad Bishop9ee75e12015-09-17 16:39:49 -0400141do_install_append() {
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400142 # install the dbus configuration files
Brad Bishop7ebf8542016-07-13 18:25:03 -0400143 [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \
144 [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400145 install -d ${D}${dbus_system_confdir}
146 for c in ${_INSTALL_DBUS_CONFIGS}; do
147 install -m 0644 ${WORKDIR}/$c \
148 ${D}${dbus_system_confdir}$c
Brad Bishop9ee75e12015-09-17 16:39:49 -0400149 done
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400150 # install the dbus activation files
151 [ -z "${_INSTALL_DBUS_ACTIVATIONS}" ] && \
152 [ -z "${_DEFAULT_DBUS_ACTIVATIONS}" ] || \
153 install -d ${D}${dbus_system_servicesdir}
154 for s in ${_INSTALL_DBUS_ACTIVATIONS}; do
155 install -m 0644 ${WORKDIR}/$s\
156 ${D}${dbus_system_servicesdir}$s
157 done
Brad Bishop9ee75e12015-09-17 16:39:49 -0400158}
Brad Bishop7ebf8542016-07-13 18:25:03 -0400159
160do_install[postfuncs] += "dbus_do_postinst"
Brad Bishopf90a17d2016-07-13 19:10:19 -0400161
162inherit obmc-phosphor-systemd