From 36444d2213e7a3c5d23e0c4a04e90ee4e8c11a2e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 21 Nov 2017 17:57:56 +0100 Subject: specifier: unify specifier implementations for user-related specifiers The code in install-printf.c and unit-printf.c for these is pretty much the same and very generic. Let's move this all over to the generic specifier.c, and share the implementations. --- src/shared/specifier.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/shared/specifier.c') diff --git a/src/shared/specifier.c b/src/shared/specifier.c index dc7be0a993..9bef890346 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -33,6 +33,7 @@ #include "specifier.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" /* * Generic infrastructure for replacing %x style specifiers in @@ -193,6 +194,48 @@ int specifier_kernel_release(char specifier, void *data, void *userdata, char ** return 0; } +int specifier_user_name(char specifier, void *data, void *userdata, char **ret) { + char *t; + + /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able + * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed. + + * We don't user getusername_malloc() here, because we don't want to look at $USER, to remain consistent with + * specifer_user_id() below. + */ + + t = uid_to_name(getuid()); + if (!t) + return -ENOMEM; + + *ret = t; + return 0; +} + +int specifier_user_id(char specifier, void *data, void *userdata, char **ret) { + + if (asprintf(ret, UID_FMT, getuid()) < 0) + return -ENOMEM; + + return 0; +} + +int specifier_user_home(char specifier, void *data, void *userdata, char **ret) { + + /* On PID 1 (which runs as root) this will not result in NSS, + * which is good. See above */ + + return get_home_dir(ret); +} + +int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) { + + /* On PID 1 (which runs as root) this will not result in NSS, + * which is good. See above */ + + return get_shell(ret); +} + int specifier_escape_strv(char **l, char ***ret) { char **z, **p, **q; -- cgit v1.2.3