1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 06:25:37 +03:00

Merge pull request #22705 from mrc0mmand/pretty_hostname_specifier

core: introduce %R specifier for pretty hostname
This commit is contained in:
Luca Boccassi 2022-03-10 21:45:34 +00:00 committed by GitHub
commit bed1f67874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 1 deletions

View File

@ -34,6 +34,11 @@
<entry>Short host name</entry> <entry>Short host name</entry>
<entry>The hostname of the running system, truncated at the first dot to remove any domain component.</entry> <entry>The hostname of the running system, truncated at the first dot to remove any domain component.</entry>
</row> </row>
<row id='R'>
<entry><literal>%R</literal></entry>
<entry>Pretty host name</entry>
<entry>The pretty hostname of the running system, as read from the <varname>PRETTY_HOSTNAME=</varname> field of <filename>/etc/machine-info</filename>. If not set, resolves to the short hostname. See <citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more information.</entry>
</row>
<row id='m'> <row id='m'>
<entry><literal>%m</literal></entry> <entry><literal>%m</literal></entry>
<entry>Machine ID</entry> <entry>Machine ID</entry>

View File

@ -2031,6 +2031,8 @@ Note that this setting is <emphasis>not</emphasis> influenced by the <varname>Us
</row> </row>
<row> <row>
<entry><literal>%l</literal></entry> <entry><literal>%l</literal></entry>
<!-- We do not use the common definition from standard-specifiers.xml here since we want a
slightly more verbose explanation here, referring to the reload cycle. -->
<entry>Short host name</entry> <entry>Short host name</entry>
<entry>The hostname of the running system at the point in time the unit configuration is loaded, truncated at the first dot to remove any domain component.</entry> <entry>The hostname of the running system at the point in time the unit configuration is loaded, truncated at the first dot to remove any domain component.</entry>
</row> </row>
@ -2062,6 +2064,13 @@ Note that this setting is <emphasis>not</emphasis> influenced by the <varname>Us
<entry>Unescaped prefix name</entry> <entry>Unescaped prefix name</entry>
<entry>Same as <literal>%p</literal>, but with escaping undone.</entry> <entry>Same as <literal>%p</literal>, but with escaping undone.</entry>
</row> </row>
<row>
<!-- We do not use the common definition from standard-specifiers.xml here since we want a
slightly more verbose explanation here, referring to the reload cycle. -->
<entry><literal>%R</literal></entry>
<entry>Pretty host name</entry>
<entry>The pretty hostname of the running system at the point in time the unit configuration is loaded, as read from the <varname>PRETTY_HOSTNAME=</varname> field of <filename>/etc/machine-info</filename>. If not set, resolves to the short hostname. See <citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry> for more information.</entry>
</row>
<row> <row>
<entry><literal>%s</literal></entry> <entry><literal>%s</literal></entry>
<entry>User shell</entry> <entry>User shell</entry>

View File

@ -40,6 +40,7 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
char *t; char *t;
int r; int r;
assert(ret);
assert(text); assert(text);
assert(table); assert(table);
@ -112,6 +113,8 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *n = NULL; char *n = NULL;
assert(ret);
if (!isempty(data)) { if (!isempty(data)) {
n = strdup(data); n = strdup(data);
if (!n) if (!n)
@ -125,6 +128,8 @@ int specifier_string(char specifier, const void *data, const char *root, const v
int specifier_real_path(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_real_path(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
const char *path = data; const char *path = data;
assert(ret);
if (!path) if (!path)
return -ENOENT; return -ENOENT;
@ -135,6 +140,8 @@ int specifier_real_directory(char specifier, const void *data, const char *root,
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
int r; int r;
assert(ret);
r = specifier_real_path(specifier, data, root, userdata, &path); r = specifier_real_path(specifier, data, root, userdata, &path);
if (r < 0) if (r < 0)
return r; return r;
@ -148,6 +155,8 @@ int specifier_machine_id(char specifier, const void *data, const char *root, con
char *n; char *n;
int r; int r;
assert(ret);
if (root) { if (root) {
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
@ -174,6 +183,8 @@ int specifier_boot_id(char specifier, const void *data, const char *root, const
char *n; char *n;
int r; int r;
assert(ret);
r = sd_id128_get_boot(&id); r = sd_id128_get_boot(&id);
if (r < 0) if (r < 0)
return r; return r;
@ -189,6 +200,8 @@ int specifier_boot_id(char specifier, const void *data, const char *root, const
int specifier_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *n; char *n;
assert(ret);
n = gethostname_malloc(); n = gethostname_malloc();
if (!n) if (!n)
return -ENOMEM; return -ENOMEM;
@ -200,6 +213,8 @@ int specifier_host_name(char specifier, const void *data, const char *root, cons
int specifier_short_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_short_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *n; char *n;
assert(ret);
n = gethostname_short_malloc(); n = gethostname_short_malloc();
if (!n) if (!n)
return -ENOMEM; return -ENOMEM;
@ -208,10 +223,27 @@ int specifier_short_host_name(char specifier, const void *data, const char *root
return 0; return 0;
} }
int specifier_pretty_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *n = NULL;
assert(ret);
if (get_pretty_hostname(&n) < 0) {
n = gethostname_short_malloc();
if (!n)
return -ENOMEM;
}
*ret = n;
return 0;
}
int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
struct utsname uts; struct utsname uts;
char *n; char *n;
assert(ret);
if (uname(&uts) < 0) if (uname(&uts) < 0)
return -errno; return -errno;
@ -226,6 +258,8 @@ int specifier_kernel_release(char specifier, const void *data, const char *root,
int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *t; char *t;
assert(ret);
t = strdup(architecture_to_string(uname_architecture())); t = strdup(architecture_to_string(uname_architecture()));
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
@ -238,32 +272,40 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
* otherwise. We'll return an empty value or NULL in that case from the functions below. */ * otherwise. We'll return an empty value or NULL in that case from the functions below. */
int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "ID", ret); return parse_os_release(root, "ID", ret);
} }
int specifier_os_version_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_version_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "VERSION_ID", ret); return parse_os_release(root, "VERSION_ID", ret);
} }
int specifier_os_build_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_build_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "BUILD_ID", ret); return parse_os_release(root, "BUILD_ID", ret);
} }
int specifier_os_variant_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_variant_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "VARIANT_ID", ret); return parse_os_release(root, "VARIANT_ID", ret);
} }
int specifier_os_image_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_image_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "IMAGE_ID", ret); return parse_os_release(root, "IMAGE_ID", ret);
} }
int specifier_os_image_version(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_os_image_version(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
return parse_os_release(root, "IMAGE_VERSION", ret); return parse_os_release(root, "IMAGE_VERSION", ret);
} }
int specifier_group_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_group_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *t; char *t;
assert(ret);
t = gid_to_name(getgid()); t = gid_to_name(getgid());
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
@ -273,6 +315,8 @@ int specifier_group_name(char specifier, const void *data, const char *root, con
} }
int specifier_group_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_group_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
if (asprintf(ret, UID_FMT, getgid()) < 0) if (asprintf(ret, UID_FMT, getgid()) < 0)
return -ENOMEM; return -ENOMEM;
@ -282,6 +326,8 @@ int specifier_group_id(char specifier, const void *data, const char *root, const
int specifier_user_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_user_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *t; char *t;
assert(ret);
/* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able /* 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. * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
@ -298,6 +344,8 @@ int specifier_user_name(char specifier, const void *data, const char *root, cons
} }
int specifier_user_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_user_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
if (asprintf(ret, UID_FMT, getuid()) < 0) if (asprintf(ret, UID_FMT, getuid()) < 0)
return -ENOMEM; return -ENOMEM;
@ -305,6 +353,7 @@ int specifier_user_id(char specifier, const void *data, const char *root, const
} }
int specifier_user_home(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_user_home(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
/* On PID 1 (which runs as root) this will not result in NSS, /* On PID 1 (which runs as root) this will not result in NSS,
* which is good. See above */ * which is good. See above */
@ -313,6 +362,7 @@ int specifier_user_home(char specifier, const void *data, const char *root, cons
} }
int specifier_user_shell(char specifier, const void *data, const char *root, const void *userdata, char **ret) { int specifier_user_shell(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
/* On PID 1 (which runs as root) this will not result in NSS, /* On PID 1 (which runs as root) this will not result in NSS,
* which is good. See above */ * which is good. See above */
@ -325,6 +375,8 @@ int specifier_tmp_dir(char specifier, const void *data, const char *root, const
char *copy; char *copy;
int r; int r;
assert(ret);
if (root) /* If root dir is set, don't honour $TMP or similar */ if (root) /* If root dir is set, don't honour $TMP or similar */
p = "/tmp"; p = "/tmp";
else { else {
@ -345,6 +397,8 @@ int specifier_var_tmp_dir(char specifier, const void *data, const char *root, co
char *copy; char *copy;
int r; int r;
assert(ret);
if (root) if (root)
p = "/var/tmp"; p = "/var/tmp";
else { else {

View File

@ -21,6 +21,7 @@ int specifier_machine_id(char specifier, const void *data, const char *root, con
int specifier_boot_id(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_boot_id(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_short_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_short_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_pretty_host_name(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret);
int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret); int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret);
@ -75,6 +76,7 @@ int specifier_var_tmp_dir(char specifier, const void *data, const char *root, co
{ 'B', specifier_os_build_id, NULL }, \ { 'B', specifier_os_build_id, NULL }, \
{ 'H', specifier_host_name, NULL }, \ { 'H', specifier_host_name, NULL }, \
{ 'l', specifier_short_host_name, NULL }, \ { 'l', specifier_short_host_name, NULL }, \
{ 'R', specifier_pretty_host_name,NULL }, \
{ 'm', specifier_machine_id, NULL }, \ { 'm', specifier_machine_id, NULL }, \
{ 'M', specifier_os_image_id, NULL }, \ { 'M', specifier_os_image_id, NULL }, \
{ 'o', specifier_os_id, NULL }, \ { 'o', specifier_os_id, NULL }, \

View File

@ -73,7 +73,7 @@ TEST(specifier_printf) {
assert_se(streq(w, "xxx a=AAAA b=BBBB e= yyy")); assert_se(streq(w, "xxx a=AAAA b=BBBB e= yyy"));
free(w); free(w);
r = specifier_printf("machine=%m, boot=%b, host=%H, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w); r = specifier_printf("machine=%m, boot=%b, host=%H, pretty=%R, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w);
assert_se(r >= 0); assert_se(r >= 0);
assert_se(w); assert_se(w);
puts(w); puts(w);