mirror of
https://github.com/systemd/systemd.git
synced 2024-11-01 09:21:26 +03:00
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.
This commit is contained in:
parent
e82f30d17f
commit
36444d2213
@ -158,46 +158,6 @@ static int specifier_special_directory(char specifier, void *data, void *userdat
|
||||
return 0;
|
||||
}
|
||||
|
||||
static 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. */
|
||||
|
||||
t = getusername_malloc();
|
||||
if (!t)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
|
||||
|
||||
if (asprintf(ret, UID_FMT, getuid()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
static 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 unit_name_printf(Unit *u, const char* format, char **ret) {
|
||||
|
||||
/*
|
||||
|
@ -103,34 +103,6 @@ static int specifier_instance(char specifier, void *data, void *userdata, char *
|
||||
return 0;
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
|
||||
|
||||
if (asprintf(ret, UID_FMT, getuid()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
|
||||
|
||||
/* This is similar to unit_full_printf() but does not support
|
||||
|
@ -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;
|
||||
|
||||
|
@ -39,6 +39,11 @@ int specifier_boot_id(char specifier, void *data, void *userdata, char **ret);
|
||||
int specifier_host_name(char specifier, void *data, void *userdata, char **ret);
|
||||
int specifier_kernel_release(char specifier, void *data, void *userdata, char **ret);
|
||||
|
||||
int specifier_user_name(char specifier, void *data, void *userdata, char **ret);
|
||||
int specifier_user_id(char specifier, void *data, void *userdata, char **ret);
|
||||
int specifier_user_home(char specifier, void *data, void *userdata, char **ret);
|
||||
int specifier_user_shell(char specifier, void *data, void *userdata, char **ret);
|
||||
|
||||
static inline char* specifier_escape(const char *string) {
|
||||
return strreplace(string, "%", "%%");
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ static int test_unit_printf(void) {
|
||||
assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
|
||||
assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
|
||||
assert_se(host = gethostname_malloc());
|
||||
assert_se(user = getusername_malloc());
|
||||
assert_se(user = uid_to_name(getuid()));
|
||||
assert_se(asprintf(&uid, UID_FMT, getuid()));
|
||||
assert_se(get_home_dir(&home) >= 0);
|
||||
assert_se(get_shell(&shell) >= 0);
|
||||
|
Loading…
Reference in New Issue
Block a user