classes-systemd: Add user

Add a SYSTEMD_USER_${PN}_foo.service variable for setting
the service user in systemd service files.

Change-Id: I9354b19f6cf5563fe33ee71dc5b8b07c4f380847
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
index 0aa5493..04b8135 100644
--- a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
+++ b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
@@ -21,14 +21,24 @@
 #      VAR:VALUE
 #    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.
 
 
 inherit obmc-phosphor-utils
 inherit systemd
+inherit useradd
 
 _INSTALL_SD_UNITS=""
 SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
 
+# Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
+# If there are users to be added, we'll add them in our post-parse.
+# If not...there don't seem to be any ill effects...
+USERADD_PACKAGES ?= " "
+USERADD_PARAM_${PN} ?= ";"
+
 
 def systemd_is_service(unit):
     return unit.endswith('.service')
@@ -77,6 +87,33 @@
             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 = [
+            '--system',
+            '--home',
+            '/',
+            '--no-create-home',
+            '--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])),
+                ';')
+            if pkg not in d.getVar('USERADD_PACKAGES', True):
+                set_append(d, 'USERADD_PACKAGES', pkg)
+
 
     pn = d.getVar('PN', True)
     if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
@@ -86,6 +123,7 @@
         for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
             check_sd_unit(d, unit)
             add_sd_unit(d, unit, pkg)
+            add_sd_user(d, unit, pkg)
 }