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 | # |
| 6 | # DBUS_SERVICE_${PN} += "org.openbmc.Foo" |
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 |
| 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 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 | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 16 | # 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 Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 25 | # 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 Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 29 | |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 30 | inherit dbus-dir |
| 31 | inherit obmc-phosphor-utils |
Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 32 | |
Brad Bishop | a181102 | 2016-07-15 10:22:24 -0400 | [diff] [blame] | 33 | RDEPENDS_${PN}_append_class-target = " dbus-perms" |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 34 | DBUS_PACKAGES ?= "${PN}" |
| 35 | |
| 36 | _INSTALL_DBUS_CONFIGS="" |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 37 | _DEFAULT_DBUS_CONFIGS="" |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 38 | _INSTALL_DBUS_ACTIVATIONS="" |
| 39 | _DEFAULT_DBUS_ACTIVATIONS="" |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 40 | |
| 41 | |
| 42 | python dbus_do_postinst() { |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 43 | def make_default_dbus_config(d, service, user): |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 44 | 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 Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 50 | fd.write(' <policy user="%s">\n' % user) |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 51 | 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 Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 58 | 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 Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 70 | for service_user in listvar_to_list(d, '_DEFAULT_DBUS_CONFIGS'): |
| 71 | make_default_dbus_config(d, *service_user.split(':')) |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 72 | for service_user in listvar_to_list(d, '_DEFAULT_DBUS_ACTIVATIONS'): |
| 73 | make_default_dbus_activation(d, *service_user.split(':')) |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 74 | } |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 75 | |
Brad Bishop | 065a1be | 2015-09-25 10:23:09 -0400 | [diff] [blame] | 76 | |
Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 77 | python() { |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 78 | searchpaths = d.getVar('FILESPATH', True) |
| 79 | |
| 80 | def add_dbus_config(d, service, pkg): |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 81 | path = bb.utils.which(searchpaths, '%s.conf' % service) |
| 82 | if not os.path.isfile(path): |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 83 | 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 Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 87 | else: |
| 88 | set_append(d, 'SRC_URI', 'file://%s.conf' % service) |
| 89 | set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % service) |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 90 | set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \ |
| 91 | % (d.getVar('dbus_system_confdir', True), service)) |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 92 | |
| 93 | |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 94 | def add_sd_unit(d, prefix, service, pkg): |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 95 | set_append( |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 96 | d, 'SYSTEMD_SERVICE_%s' % pkg, '%s%s.service' % ( |
| 97 | prefix, service)) |
| 98 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s%s.service' % (prefix, service), |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 99 | 'BUSNAME:%s' % service) |
| 100 | |
| 101 | |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 102 | def add_sd_user(d, prefix, service, pkg): |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 103 | user = d.getVar( |
| 104 | 'DBUS_USER_%s_%s' % (pkg, service), True) |
| 105 | if user: |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 106 | 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 Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 122 | |
| 123 | |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 124 | for pkg in listvar_to_list(d, 'DBUS_PACKAGES'): |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 125 | if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''): |
| 126 | set_append(d, 'SYSTEMD_PACKAGES', pkg) |
| 127 | |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 128 | services = listvar_to_list(d, 'DBUS_SERVICE_%s' % pkg) |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 129 | auto = listvar_to_list(d, 'DBUS_ACTIVATED_SERVICE_%s' % pkg) |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 130 | |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 131 | for service in set(services).union(auto): |
| 132 | prefix = 'dbus-' if service in auto else '' |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 133 | add_dbus_config(d, service, pkg) |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 134 | 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 Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 140 | |
Brad Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 141 | do_install_append() { |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 142 | # install the dbus configuration files |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 143 | [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \ |
| 144 | [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \ |
Brad Bishop | ff7dc1d | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 145 | 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 Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 149 | done |
Brad Bishop | 9e28d6e | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 150 | # 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 Bishop | 9ee75e1 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 158 | } |
Brad Bishop | 7ebf854 | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 159 | |
| 160 | do_install[postfuncs] += "dbus_do_postinst" |
Brad Bishop | f90a17d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 161 | |
| 162 | inherit obmc-phosphor-systemd |