summaryrefslogtreecommitdiff
path: root/src/shared/install-printf.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-01-28 22:34:38 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-01-29 19:01:41 -0500
commit3f0b2f0f452e94444e4fb7b62030ea05738bb1b6 (patch)
tree8429adf52808221ed9e9be24e57a2e8e508aefc6 /src/shared/install-printf.c
parent7584d236eac91f9b7128b1eb08bddf18be2bce9f (diff)
install: allow %u an and %U specifiers in WantedBy/RequiredBy/Alias
Diffstat (limited to 'src/shared/install-printf.c')
-rw-r--r--src/shared/install-printf.c41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/shared/install-printf.c b/src/shared/install-printf.c
index 85aebc42cc..10b0cf379b 100644
--- a/src/shared/install-printf.c
+++ b/src/shared/install-printf.c
@@ -24,6 +24,7 @@
#include "specifier.h"
#include "unit-name.h"
+#include "util.h"
#include "install-printf.h"
static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
@@ -56,6 +57,42 @@ static char *specifier_instance(char specifier, void *data, void *userdata) {
return strdup("");
}
+static char *specifier_user_name(char specifier, void *data, void *userdata) {
+ InstallInfo *i = userdata;
+ const char *username;
+ char _cleanup_free_ *tmp = NULL;
+ char *printed = NULL;
+
+ assert(i);
+
+ if (i->user)
+ username = i->user;
+ else
+ /* get USER env from env or our own uid */
+ username = tmp = getusername_malloc();
+
+ switch (specifier) {
+ case 'u':
+ printed = strdup(username);
+ break;
+ case 'U': {
+ /* fish username from passwd */
+ uid_t uid;
+ int r;
+
+ r = get_user_creds(&username, &uid, NULL, NULL, NULL);
+ if (r < 0)
+ return NULL;
+
+ if (asprintf(&printed, "%d", uid) < 0)
+ return NULL;
+ break;
+ }}
+
+ return printed;
+}
+
+
char *install_full_printf(InstallInfo *i, const char *format) {
/* This is similar to unit_full_printf() but does not support
@@ -79,8 +116,8 @@ char *install_full_printf(InstallInfo *i, const char *format) {
{ 'p', specifier_prefix, NULL },
{ 'i', specifier_instance, NULL },
-// { 'U', specifier_user_name, NULL },
-// { 'u', specifier_user_name, NULL },
+ { 'U', specifier_user_name, NULL },
+ { 'u', specifier_user_name, NULL },
{ 'm', specifier_machine_id, NULL },
{ 'H', specifier_host_name, NULL },