]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: introduce %R specifier for pretty hostname 22705/head
authorFrantisek Sumsal <frantisek@sumsal.cz>
Thu, 10 Mar 2022 15:15:54 +0000 (16:15 +0100)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Thu, 10 Mar 2022 18:03:22 +0000 (19:03 +0100)
Resolves: #20054

man/standard-specifiers.xml
man/systemd.unit.xml
src/shared/specifier.c
src/shared/specifier.h
src/test/test-specifier.c

index f1666365b9c19ba7a9129067215f94e22e969600..abbd47f7782a65178863326cfd751b5c22184cfd 100644 (file)
     <entry>Short host name</entry>
     <entry>The hostname of the running system, truncated at the first dot to remove any domain component.</entry>
   </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'>
     <entry><literal>%m</literal></entry>
     <entry>Machine ID</entry>
index b20072f0a2a68547c597fbb56c282bedadf75a8e..8f2f649747067c2db5b34a04162b2d3e9e3b0972 100644 (file)
@@ -2031,6 +2031,8 @@ Note that this setting is <emphasis>not</emphasis> influenced by the <varname>Us
           </row>
           <row>
             <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>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>
@@ -2062,6 +2064,13 @@ Note that this setting is <emphasis>not</emphasis> influenced by the <varname>Us
             <entry>Unescaped prefix name</entry>
             <entry>Same as <literal>%p</literal>, but with escaping undone.</entry>
           </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>
             <entry><literal>%s</literal></entry>
             <entry>User shell</entry>
index 8c5a4fa831f59dae42797b0d9230219e760a2d06..a02012407ba518640fc25725a2c005941dd404bd 100644 (file)
@@ -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;
index eae5f12ad7660ae4f6f72f1c9bb26fe8d2dac707..2f2553cfdc0e13cb900c396c083ef63eefcba1a4 100644 (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_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 }, \
index ded2dcd55a0c27756c010598cd4ff1f0e3c05667..231625de958ccb453be5197144a99a2b24cc5d94 100644 (file)
@@ -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);