blob: c5d50d00243b712781e693ef4f0aca696df3cfda [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.
Brad Bishopbcd1b652016-08-15 22:35:58 -040024#
25# DBUS_USER_${PN} = "dbususer"
26# DBUS_USER_${unit} = "dbususer"
27# The user a service/pkg should be configured to run as.
Brad Bishopf90a17d2016-07-13 19:10:19 -040028
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
Brad Bishopbcd1b652016-08-15 22:35:58 -040080 def get_user(d, service, pkg):
81 user = d.getVar(
82 'DBUS_USER_%s' % service, True)
83 if user is None:
84 user = d.getVar(
85 'DBUS_USER_%s' % pkg, True) or 'root'
86 return user
87
88
Brad Bishopff7dc1d2016-07-13 17:56:34 -040089 def add_dbus_config(d, service, pkg):
Brad Bishop7ebf8542016-07-13 18:25:03 -040090 path = bb.utils.which(searchpaths, '%s.conf' % service)
91 if not os.path.isfile(path):
Brad Bishopbcd1b652016-08-15 22:35:58 -040092 user = get_user(d, service, pkg)
Brad Bishopf90a17d2016-07-13 19:10:19 -040093 set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % (
94 service, user))
Brad Bishop7ebf8542016-07-13 18:25:03 -040095 else:
96 set_append(d, 'SRC_URI', 'file://%s.conf' % service)
97 set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % service)
Brad Bishopff7dc1d2016-07-13 17:56:34 -040098 set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \
99 % (d.getVar('dbus_system_confdir', True), service))
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400100
101
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400102 def add_sd_unit(d, prefix, service, pkg):
Brad Bishopf90a17d2016-07-13 19:10:19 -0400103 set_append(
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400104 d, 'SYSTEMD_SERVICE_%s' % pkg, '%s%s.service' % (
105 prefix, service))
106 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s%s.service' % (prefix, service),
Brad Bishopf90a17d2016-07-13 19:10:19 -0400107 'BUSNAME:%s' % service)
108
109
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400110 def add_sd_user(d, prefix, service, pkg):
Brad Bishopbcd1b652016-08-15 22:35:58 -0400111 var = None
Brad Bishopf90a17d2016-07-13 19:10:19 -0400112 user = d.getVar(
Brad Bishopbcd1b652016-08-15 22:35:58 -0400113 'DBUS_USER_%s' % service, True)
Brad Bishopf90a17d2016-07-13 19:10:19 -0400114 if user:
Brad Bishopbcd1b652016-08-15 22:35:58 -0400115 var = 'SYSTEMD_USER_%s%s.service' % (prefix, service)
116 else:
117 user = d.getVar(
118 'DBUS_USER_%s' % pkg, True)
119 if user:
120 var = 'SYSTEMD_USER_%s' % pkg
121
122 if var and user not in listvar_to_list(d, var):
123 set_append(d, var, user)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400124
125
126 def add_dbus_activation(d, service, pkg):
127 path = bb.utils.which(searchpaths, '%s.service' % service)
128 if not os.path.isfile(path):
Brad Bishopbcd1b652016-08-15 22:35:58 -0400129 user = get_user(d, service, pkg)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400130 set_append(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % (
131 service, user))
132 else:
133 set_append(d, 'SRC_URI', 'file://%s.service' % service)
134 set_append(d, '_INSTALL_DBUS_ACTIVATIONS', '%s.service' % service)
135 set_append(d, 'FILES_%s' % pkg, '%s%s.service' \
136 % (d.getVar('dbus_system_servicesdir', True), service))
Brad Bishopf90a17d2016-07-13 19:10:19 -0400137
138
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400139 for pkg in listvar_to_list(d, 'DBUS_PACKAGES'):
Brad Bishopf90a17d2016-07-13 19:10:19 -0400140 if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''):
141 set_append(d, 'SYSTEMD_PACKAGES', pkg)
142
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400143 services = listvar_to_list(d, 'DBUS_SERVICE_%s' % pkg)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400144 auto = listvar_to_list(d, 'DBUS_ACTIVATED_SERVICE_%s' % pkg)
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400145
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400146 for service in set(services).union(auto):
147 prefix = 'dbus-' if service in auto else ''
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400148 add_dbus_config(d, service, pkg)
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400149 add_sd_unit(d, prefix, service, pkg)
150 add_sd_user(d, prefix, service, pkg)
151 if prefix:
152 add_dbus_activation(d, service, pkg)
Brad Bishop9ee75e12015-09-17 16:39:49 -0400153}
154
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400155
Brad Bishop9ee75e12015-09-17 16:39:49 -0400156do_install_append() {
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400157 # install the dbus configuration files
Brad Bishop7ebf8542016-07-13 18:25:03 -0400158 [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \
159 [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \
Brad Bishopff7dc1d2016-07-13 17:56:34 -0400160 install -d ${D}${dbus_system_confdir}
161 for c in ${_INSTALL_DBUS_CONFIGS}; do
162 install -m 0644 ${WORKDIR}/$c \
163 ${D}${dbus_system_confdir}$c
Brad Bishop9ee75e12015-09-17 16:39:49 -0400164 done
Brad Bishop9e28d6e2016-07-13 20:11:44 -0400165 # install the dbus activation files
166 [ -z "${_INSTALL_DBUS_ACTIVATIONS}" ] && \
167 [ -z "${_DEFAULT_DBUS_ACTIVATIONS}" ] || \
168 install -d ${D}${dbus_system_servicesdir}
169 for s in ${_INSTALL_DBUS_ACTIVATIONS}; do
170 install -m 0644 ${WORKDIR}/$s\
171 ${D}${dbus_system_servicesdir}$s
172 done
Brad Bishop9ee75e12015-09-17 16:39:49 -0400173}
Brad Bishop7ebf8542016-07-13 18:25:03 -0400174
175do_install[postfuncs] += "dbus_do_postinst"
Brad Bishopf90a17d2016-07-13 19:10:19 -0400176
177inherit obmc-phosphor-systemd