| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 1 | # 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 Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 6 | #  DBUS_SERVICE_${PN} += "org.openbmc.Foo.service" | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 7 | #    A list of dbus service names.  The class will look for a | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 8 | #    dbus configuration file with the same base name with .conf | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 12 | # | 
|  | 13 | #    Additionally the class will instantiate obmc-phosphor-systemd | 
|  | 14 | #    with any SYSTEMD_SERVICE_%s variables translated appropriately. | 
|  | 15 | # | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 16 | #    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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 21 |  | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 22 |  | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 23 | inherit dbus-dir | 
|  | 24 | inherit obmc-phosphor-utils | 
| Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 25 |  | 
| Brad Bishop | 3db4ad7 | 2016-07-15 10:22:24 -0400 | [diff] [blame] | 26 | RDEPENDS_${PN}_append_class-target = " dbus-perms" | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 27 | DBUS_PACKAGES ?= "${PN}" | 
|  | 28 |  | 
|  | 29 | _INSTALL_DBUS_CONFIGS="" | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 30 | _DEFAULT_DBUS_CONFIGS="" | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 31 | _INSTALL_DBUS_ACTIVATIONS="" | 
|  | 32 | _DEFAULT_DBUS_ACTIVATIONS="" | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 33 |  | 
|  | 34 |  | 
|  | 35 | python dbus_do_postinst() { | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 36 | def make_default_dbus_config(d, unit, user): | 
|  | 37 | bus = unit.base | 
|  | 38 | if unit.is_template: | 
|  | 39 | bus = '%s*' % bus | 
|  | 40 |  | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 41 | path = d.getVar('D', True) | 
|  | 42 | path += d.getVar('dbus_system_confdir', True) | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 43 | with open('%s/%s.conf' % (path, unit.base), 'w+') as fd: | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 44 | 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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 47 | fd.write('        <policy user="%s">\n' % user) | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 48 | fd.write('                <allow own="%s"/>\n' % bus) | 
|  | 49 | fd.write('                <allow send_destination="%s"/>\n' % bus) | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 50 | fd.write('        </policy>\n') | 
|  | 51 | fd.write('</busconfig>\n') | 
|  | 52 | fd.close() | 
|  | 53 |  | 
|  | 54 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 55 | 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 Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 62 | path = d.getVar('D', True) | 
|  | 63 | path += d.getVar('dbus_system_servicesdir', True) | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 64 | with open('%s/%s.service' % (path, dest), 'w+') as fd: | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 65 | fd.write('[D-BUS Service]\n') | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 66 | fd.write('Name=%s\n' % dest) | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 67 | fd.write('Exec=/bin/false\n') | 
|  | 68 | fd.write('User=%s\n' % user) | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 69 | fd.write('SystemdService=dbus-%s.service\n' % sd_unit) | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 70 | fd.close() | 
|  | 71 |  | 
|  | 72 |  | 
| Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 73 | for service_user in listvar_to_list(d, '_DEFAULT_DBUS_CONFIGS'): | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 74 | service, user = service_user.split(':') | 
|  | 75 | make_default_dbus_config(d, SystemdUnit(service), user) | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 76 | for service_user in listvar_to_list(d, '_DEFAULT_DBUS_ACTIVATIONS'): | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 77 | service, user = service_user.split(':') | 
|  | 78 | make_default_dbus_activation(d, SystemdUnit(service), user) | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 79 | } | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 80 |  | 
| Brad Bishop | ef552f6 | 2015-09-25 10:23:09 -0400 | [diff] [blame] | 81 |  | 
| Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 82 | python() { | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 83 | searchpaths = d.getVar('FILESPATH', True) | 
|  | 84 |  | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 85 | def get_user(d, service, pkg): | 
|  | 86 | user = d.getVar( | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 87 | 'SYSTEMD_USER_%s' % service, True) | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 88 | if user is None: | 
|  | 89 | user = d.getVar( | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 90 | 'SYSTEMD_USER_%s' % pkg, True) or 'root' | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 91 | return user | 
|  | 92 |  | 
|  | 93 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 94 | def add_dbus_config(d, unit, pkg): | 
|  | 95 | path = bb.utils.which(searchpaths, '%s.conf' % unit.base) | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 96 | if not os.path.isfile(path): | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 97 | user = get_user(d, unit.name, pkg) | 
| Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 98 | set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % ( | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 99 | unit.name, user)) | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 100 | else: | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 101 | set_append(d, 'SRC_URI', 'file://%s.conf' % unit.base) | 
|  | 102 | set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % unit.base) | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 103 | set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \ | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 104 | % (d.getVar('dbus_system_confdir', True), unit.base)) | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 105 |  | 
|  | 106 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 107 | 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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 113 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 114 | path = bb.utils.which(searchpaths, search_match) | 
| Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 115 |  | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 116 | if not os.path.isfile(path): | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 117 | user = get_user(d, unit.base, pkg) | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 118 | set_append(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % ( | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 119 | unit.name, user)) | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 120 | else: | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 121 | 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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 125 |  | 
|  | 126 |  | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 127 | for pkg in listvar_to_list(d, 'DBUS_PACKAGES'): | 
| Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 128 | if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''): | 
|  | 129 | set_append(d, 'SYSTEMD_PACKAGES', pkg) | 
|  | 130 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 131 | 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 Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 138 |  | 
| Brad Bishop | be42fe9 | 2016-08-16 09:40:10 -0400 | [diff] [blame^] | 139 | 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 152 |  | 
| Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 153 | do_install_append() { | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 154 | # install the dbus configuration files | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 155 | [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \ | 
|  | 156 | [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \ | 
| Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 157 | 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 161 | done | 
| Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 162 | # 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 170 | } | 
| Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 171 |  | 
|  | 172 | do_install[postfuncs] += "dbus_do_postinst" | 
| Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 173 |  | 
|  | 174 | inherit obmc-phosphor-systemd |