- if (c && c->user)
- username = c->user;
- else
- /* get USER env from env or our own uid */
- username = tmp = getusername_malloc();
-
- /* fish username from passwd */
- r = get_user_creds(&username, &uid, NULL, NULL, NULL);
- if (r < 0)
- return r;
-
- switch (specifier) {
- case 'U':
- if (asprintf(&printed, "%d", uid) < 0)
- return -ENOMEM;
- break;
- case 'u':
+ if (u->manager->running_as == SYSTEMD_SYSTEM) {
+
+ /* We cannot use NSS from PID 1, hence try to make the
+ * best of it in that case, and fail if we can't help
+ * it */
+
+ if (!c->user || streq(c->user, "root") || streq(c->user, "0"))
+ printed = strdup(specifier == 'u' ? "root" : "0");
+ else {
+ if (specifier == 'u')
+ printed = strdup(c->user);
+ else {
+ uid_t uid;
+
+ r = parse_uid(c->user, &uid);
+ if (r < 0)
+ return -ENODATA;
+
+ r = asprintf(&printed, UID_FMT, uid);
+ }
+ }
+
+ } else {
+ _cleanup_free_ char *tmp = NULL;
+ const char *username = NULL;
+ uid_t uid;
+
+ if (c->user)
+ username = c->user;
+ else
+ /* get USER env from env or our own uid */
+ username = tmp = getusername_malloc();
+
+ /* fish username from passwd */
+ r = get_user_creds(&username, &uid, NULL, NULL, NULL);
+ if (r < 0)
+ return r;
+
+ if (specifier == 'u')