| Brad Bishop | ff7dc1d | 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 | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 6 | #  DBUS_SERVICE_${PN} += "org.openbmc.Foo.service" | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 7 | #    A list of dbus service names.  The class will look for a | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 8 | #    dbus configuration file with the same base name with .conf | 
| Brad Bishop | 7ebf854 | 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 | f90a17d | 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 | a5983c9 | 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 | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 21 |  | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 22 |  | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 23 | inherit dbus-dir | 
 | 24 | inherit obmc-phosphor-utils | 
| Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 25 |  | 
| Brad Bishop | a181102 | 2016-07-15 10:22:24 -0400 | [diff] [blame] | 26 | RDEPENDS_${PN}_append_class-target = " dbus-perms" | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 27 | DBUS_PACKAGES ?= "${PN}" | 
 | 28 |  | 
 | 29 | _INSTALL_DBUS_CONFIGS="" | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 30 | _DEFAULT_DBUS_CONFIGS="" | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 31 | _INSTALL_DBUS_ACTIVATIONS="" | 
 | 32 | _DEFAULT_DBUS_ACTIVATIONS="" | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 33 |  | 
 | 34 |  | 
 | 35 | python dbus_do_postinst() { | 
| Brad Bishop | a5983c9 | 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 | 7ebf854 | 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 | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 43 |         with open('%s/%s.conf' % (path, unit.base), 'w+') as fd: | 
| Brad Bishop | 7ebf854 | 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 | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 47 |             fd.write('        <policy user="%s">\n' % user) | 
| Brad Bishop | a5983c9 | 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 | 7ebf854 | 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 | a5983c9 | 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 | 9e28d6e | 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 | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 64 |         with open('%s/%s.service' % (path, dest), 'w+') as fd: | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 65 |             fd.write('[D-BUS Service]\n') | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 66 |             fd.write('Name=%s\n' % dest) | 
| Brad Bishop | 9e28d6e | 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 | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 69 |             fd.write('SystemdService=dbus-%s.service\n' % sd_unit) | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 70 |             fd.close() | 
 | 71 |  | 
 | 72 |  | 
| Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 73 |     for service_user in listvar_to_list(d, '_DEFAULT_DBUS_CONFIGS'): | 
| Brad Bishop | a5983c9 | 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 | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 76 |     for service_user in listvar_to_list(d, '_DEFAULT_DBUS_ACTIVATIONS'): | 
| Brad Bishop | a5983c9 | 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 | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 79 | } | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 80 |  | 
| Brad Bishop | 065a1be | 2015-09-25 10:23:09 -0400 | [diff] [blame] | 81 |  | 
| Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 82 | python() { | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 83 |     searchpaths = d.getVar('FILESPATH', True) | 
 | 84 |  | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 85 |     def get_user(d, service, pkg): | 
 | 86 |         user = d.getVar( | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 87 |             'SYSTEMD_USER_%s' % service, True) | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 88 |         if user is None: | 
 | 89 |             user = d.getVar( | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 90 |                 'SYSTEMD_USER_%s' % pkg, True) or 'root' | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 91 |         return user | 
 | 92 |  | 
 | 93 |  | 
| Brad Bishop | a5983c9 | 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 | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 96 |         if not os.path.isfile(path): | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 97 |             user = get_user(d, unit.name, pkg) | 
| Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 98 |             set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % ( | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 99 |                 unit.name, user)) | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 100 |         else: | 
| Brad Bishop | a5983c9 | 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 | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 103 |         set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \ | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 104 |             % (d.getVar('dbus_system_confdir', True), unit.base)) | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 105 |  | 
 | 106 |  | 
| Brad Bishop | a5983c9 | 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 | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 113 |  | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 114 |         path = bb.utils.which(searchpaths, search_match) | 
| Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 115 |  | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 116 |         if not os.path.isfile(path): | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 117 |             user = get_user(d, unit.base, pkg) | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 118 |             set_append(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % ( | 
| Brad Bishop | a5983c9 | 2016-08-16 09:40:10 -0400 | [diff] [blame] | 119 |                 unit.name, user)) | 
| Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 120 |         else: | 
| Brad Bishop | a5983c9 | 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 | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 125 |  | 
 | 126 |  | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 127 |     for pkg in listvar_to_list(d, 'DBUS_PACKAGES'): | 
| Brad Bishop | f90a17d | 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 | a5983c9 | 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 | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 138 |  | 
| Brad Bishop | a5983c9 | 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 | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 150 | } | 
 | 151 |  | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 152 |  | 
| Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 153 | do_install_append() { | 
| Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 154 |         # install the dbus configuration files | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 155 |         [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \ | 
 | 156 |                 [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \ | 
| Brad Bishop | ff7dc1d | 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 | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 161 |         done | 
| Brad Bishop | 9e28d6e | 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 | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 170 | } | 
| Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 171 |  | 
 | 172 | do_install[postfuncs] += "dbus_do_postinst" | 
| Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 173 |  | 
 | 174 | inherit obmc-phosphor-systemd |