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 | # |
| 6 | # DBUS_SERVICE_${PN} += "org.openbmc.Foo" |
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 |
| 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 | 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 | f59b876 | 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. |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 24 | # |
| 25 | # DBUS_USER_${PN} = "dbususer" |
| 26 | # DBUS_USER_${unit} = "dbususer" |
| 27 | # The user a service/pkg should be configured to run as. |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 28 | |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 29 | |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 30 | inherit dbus-dir |
| 31 | inherit obmc-phosphor-utils |
Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 32 | |
Brad Bishop | 3db4ad7 | 2016-07-15 10:22:24 -0400 | [diff] [blame] | 33 | RDEPENDS_${PN}_append_class-target = " dbus-perms" |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 34 | DBUS_PACKAGES ?= "${PN}" |
| 35 | |
| 36 | _INSTALL_DBUS_CONFIGS="" |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 37 | _DEFAULT_DBUS_CONFIGS="" |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 38 | _INSTALL_DBUS_ACTIVATIONS="" |
| 39 | _DEFAULT_DBUS_ACTIVATIONS="" |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 40 | |
| 41 | |
| 42 | python dbus_do_postinst() { |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 43 | def make_default_dbus_config(d, service, user): |
Brad Bishop | 4117ace | 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 | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 50 | fd.write(' <policy user="%s">\n' % user) |
Brad Bishop | 4117ace | 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 | f59b876 | 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 | 502545d | 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 | f59b876 | 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 | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 74 | } |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 75 | |
Brad Bishop | ef552f6 | 2015-09-25 10:23:09 -0400 | [diff] [blame] | 76 | |
Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 77 | python() { |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 78 | searchpaths = d.getVar('FILESPATH', True) |
| 79 | |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 80 | 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 Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 89 | def add_dbus_config(d, service, pkg): |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 90 | path = bb.utils.which(searchpaths, '%s.conf' % service) |
| 91 | if not os.path.isfile(path): |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 92 | user = get_user(d, service, pkg) |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 93 | set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % ( |
| 94 | service, user)) |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 95 | else: |
| 96 | set_append(d, 'SRC_URI', 'file://%s.conf' % service) |
| 97 | set_append(d, '_INSTALL_DBUS_CONFIGS', '%s.conf' % service) |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 98 | set_append(d, 'FILES_%s' % pkg, '%s%s.conf' \ |
| 99 | % (d.getVar('dbus_system_confdir', True), service)) |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 100 | |
| 101 | |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 102 | def add_sd_unit(d, prefix, service, pkg): |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 103 | set_append( |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 104 | d, 'SYSTEMD_SERVICE_%s' % pkg, '%s%s.service' % ( |
| 105 | prefix, service)) |
| 106 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s%s.service' % (prefix, service), |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 107 | 'BUSNAME:%s' % service) |
| 108 | |
| 109 | |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 110 | def add_sd_user(d, prefix, service, pkg): |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 111 | var = None |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 112 | user = d.getVar( |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 113 | 'DBUS_USER_%s' % service, True) |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 114 | if user: |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 115 | 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 Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 124 | |
| 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 Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 129 | user = get_user(d, service, pkg) |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 130 | 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 Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 137 | |
| 138 | |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 139 | for pkg in listvar_to_list(d, 'DBUS_PACKAGES'): |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 140 | if pkg not in (d.getVar('SYSTEMD_PACKAGES', True) or ''): |
| 141 | set_append(d, 'SYSTEMD_PACKAGES', pkg) |
| 142 | |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 143 | services = listvar_to_list(d, 'DBUS_SERVICE_%s' % pkg) |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 144 | auto = listvar_to_list(d, 'DBUS_ACTIVATED_SERVICE_%s' % pkg) |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 145 | |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 146 | for service in set(services).union(auto): |
| 147 | prefix = 'dbus-' if service in auto else '' |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 148 | add_dbus_config(d, service, pkg) |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 149 | 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 155 | |
Brad Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 156 | do_install_append() { |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 157 | # install the dbus configuration files |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 158 | [ -z "${_INSTALL_DBUS_CONFIGS}" ] && \ |
| 159 | [ -z "${_DEFAULT_DBUS_CONFIGS}" ] || \ |
Brad Bishop | 8ffb402 | 2016-07-13 17:56:34 -0400 | [diff] [blame] | 160 | 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 164 | done |
Brad Bishop | f59b876 | 2016-07-13 20:11:44 -0400 | [diff] [blame] | 165 | # 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 Bishop | 3609840 | 2015-09-17 16:39:49 -0400 | [diff] [blame] | 173 | } |
Brad Bishop | 4117ace | 2016-07-13 18:25:03 -0400 | [diff] [blame] | 174 | |
| 175 | do_install[postfuncs] += "dbus_do_postinst" |
Brad Bishop | 502545d | 2016-07-13 19:10:19 -0400 | [diff] [blame] | 176 | |
| 177 | inherit obmc-phosphor-systemd |