From 6ceb0a4094908dd213a78b9f6d0c59a684831ab0 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Thu, 10 Mar 2022 16:15:54 +0100 Subject: core: introduce %R specifier for pretty hostname Resolves: #20054 --- man/standard-specifiers.xml | 5 +++++ man/systemd.unit.xml | 9 +++++++++ src/shared/specifier.c | 15 +++++++++++++++ src/shared/specifier.h | 2 ++ src/test/test-specifier.c | 2 +- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/man/standard-specifiers.xml b/man/standard-specifiers.xml index f1666365b9..abbd47f778 100644 --- a/man/standard-specifiers.xml +++ b/man/standard-specifiers.xml @@ -34,6 +34,11 @@ Short host name The hostname of the running system, truncated at the first dot to remove any domain component. + + %R + Pretty host name + The pretty hostname of the running system, as read from the PRETTY_HOSTNAME= field of /etc/machine-info. If not set, resolves to the short hostname. See machine-info5 for more information. + %m Machine ID diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index b20072f0a2..8f2f649747 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -2031,6 +2031,8 @@ Note that this setting is not influenced by the Us %l + Short host name 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. @@ -2062,6 +2064,13 @@ Note that this setting is not influenced by the Us Unescaped prefix name Same as %p, but with escaping undone. + + + %R + Pretty host name + The pretty hostname of the running system at the point in time the unit configuration is loaded, as read from the PRETTY_HOSTNAME= field of /etc/machine-info. If not set, resolves to the short hostname. See machine-info5 for more information. + %s User shell diff --git a/src/shared/specifier.c b/src/shared/specifier.c index 8c5a4fa831..a02012407b 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -223,6 +223,21 @@ int specifier_short_host_name(char specifier, const void *data, const char *root 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) { struct utsname uts; char *n; diff --git a/src/shared/specifier.h b/src/shared/specifier.h index eae5f12ad7..2f2553cfdc 100644 --- a/src/shared/specifier.h +++ b/src/shared/specifier.h @@ -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_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_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); @@ -75,6 +76,7 @@ int specifier_var_tmp_dir(char specifier, const void *data, const char *root, co { 'B', specifier_os_build_id, NULL }, \ { 'H', specifier_host_name, NULL }, \ { 'l', specifier_short_host_name, NULL }, \ + { 'R', specifier_pretty_host_name,NULL }, \ { 'm', specifier_machine_id, NULL }, \ { 'M', specifier_os_image_id, NULL }, \ { 'o', specifier_os_id, NULL }, \ diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c index ded2dcd55a..231625de95 100644 --- a/src/test/test-specifier.c +++ b/src/test/test-specifier.c @@ -73,7 +73,7 @@ TEST(specifier_printf) { assert_se(streq(w, "xxx a=AAAA b=BBBB e= yyy")); 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(w); puts(w); -- cgit v1.2.3