Allow package wide user specificiation

Allow the user to be specified package wide or per unit.

Change-Id: I19a56001bd2115462f132c3079690c4b2d0a5ddf
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/classes/obmc-phosphor-dbus-service.bbclass b/classes/obmc-phosphor-dbus-service.bbclass
index 4066c6c..c5d50d0 100644
--- a/classes/obmc-phosphor-dbus-service.bbclass
+++ b/classes/obmc-phosphor-dbus-service.bbclass
@@ -21,10 +21,10 @@
 #    dbus activation file with the same name with .service appended.
 #    If one is found, it added to the package and used verbatim.
 #    If it is not found, a default one is generated and used.
-
-#  DBUS_USER_${PN}_org.openbmc.Foo = "dbususer"
-#    The user a service should be configured to run as.  If unspecified
-#    no User property is added.
+#
+#  DBUS_USER_${PN} = "dbususer"
+#  DBUS_USER_${unit} = "dbususer"
+#    The user a service/pkg should be configured to run as.
 
 
 inherit dbus-dir
@@ -77,11 +77,19 @@
 python() {
     searchpaths = d.getVar('FILESPATH', True)
 
+    def get_user(d, service, pkg):
+        user = d.getVar(
+            'DBUS_USER_%s' % service, True)
+        if user is None:
+            user = d.getVar(
+                'DBUS_USER_%s' % pkg, True) or 'root'
+        return user
+
+
     def add_dbus_config(d, service, pkg):
         path = bb.utils.which(searchpaths, '%s.conf' % service)
         if not os.path.isfile(path):
-            user = d.getVar(
-                'DBUS_USER_%s_%s' % (pkg, service), True) or 'root'
+            user = get_user(d, service, pkg)
             set_append(d, '_DEFAULT_DBUS_CONFIGS', '%s:%s' % (
                 service, user))
         else:
@@ -100,18 +108,25 @@
 
 
     def add_sd_user(d, prefix, service, pkg):
+        var = None
         user = d.getVar(
-            'DBUS_USER_%s_%s' % (pkg, service), True)
+            'DBUS_USER_%s' % service, True)
         if user:
-            set_append(d, 'SYSTEMD_USER_%s_%s%s.service' % (
-                pkg, prefix, service), user)
+            var = 'SYSTEMD_USER_%s%s.service' % (prefix, service)
+        else:
+            user = d.getVar(
+                'DBUS_USER_%s' % pkg, True)
+            if user:
+                var = 'SYSTEMD_USER_%s' % pkg
+
+        if var and user not in listvar_to_list(d, var):
+            set_append(d, var, user)
 
 
     def add_dbus_activation(d, service, pkg):
         path = bb.utils.which(searchpaths, '%s.service' % service)
         if not os.path.isfile(path):
-            user = d.getVar(
-                'DBUS_USER_%s_%s' % (pkg, service), True) or 'root'
+            user = get_user(d, service, pkg)
             set_append(d, '_DEFAULT_DBUS_ACTIVATIONS', '%s:%s' % (
                 service, user))
         else:
diff --git a/classes/obmc-phosphor-systemd.bbclass b/classes/obmc-phosphor-systemd.bbclass
index 04b8135..2cb060c 100644
--- a/classes/obmc-phosphor-systemd.bbclass
+++ b/classes/obmc-phosphor-systemd.bbclass
@@ -22,8 +22,9 @@
 #    where {VAR} is the format string bitbake should look for in the
 #    unit file and VALUE is the value to substitute.
 #
-# SYSTEMD_USER_${PN}_${PN}.service = "foo"
-#    The user for the unit.
+# SYSTEMD_USER_${PN}.service = "foo"
+# SYSTEMD_USER_${unit}.service = "foo"
+#    The user for the unit/package.
 
 
 inherit obmc-phosphor-utils
@@ -87,12 +88,6 @@
             set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
                 '%s:%s' % (x, d.getVar(x, True)))
 
-        user = d.getVar(
-            'SYSTEMD_USER_%s_%s' % (pkg, unit), True)
-        if user:
-            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
-                'USER:%s' % d.getVar('SYSTEMD_USER_%s_%s' % (pkg, unit), True))
-
 
     def add_sd_user(d, unit, pkg):
         opts = [
@@ -103,14 +98,24 @@
             '--shell /sbin/nologin',
             '--user-group']
 
-        user = d.getVar(
-            'SYSTEMD_USER_%s_%s' % (pkg, unit), True)
-        if user:
-            set_append(
-                d,
-                'USERADD_PARAM_%s' % pkg,
-                '%s' % (' '.join(opts + [user])),
-                ';')
+        var = 'SYSTEMD_USER_%s' % unit
+        user = listvar_to_list(d, var)
+        if len(user) is 0:
+            var = 'SYSTEMD_USER_%s' % pkg
+            user = listvar_to_list(d, var)
+        if len(user) is not 0:
+            if len(user) is not 1:
+                bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
+
+            user = user[0]
+            set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
+                'USER:%s' % user)
+            if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
+                set_append(
+                    d,
+                    'USERADD_PARAM_%s' % pkg,
+                    '%s' % (' '.join(opts + [user])),
+                    ';')
             if pkg not in d.getVar('USERADD_PACKAGES', True):
                 set_append(d, 'USERADD_PACKAGES', pkg)