]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add %l as specifier for the hostname without any domain component
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 May 2020 14:16:19 +0000 (16:16 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 May 2020 15:36:44 +0000 (17:36 +0200)
As described in #15603, it is a fairly common setup to use a fqdn as the
configured hostname. But it is often convenient to use just the actual
hostname, i.e. until the first dot. This adds support in tmpfiles, sysusers,
and unit files for %l which expands to that.

Fixes #15603.

12 files changed:
man/standard-specifiers.xml
man/systemd.unit.xml
man/sysusers.d.xml
man/tmpfiles.d.xml
src/basic/hostname-util.c
src/basic/hostname-util.h
src/core/unit-printf.c
src/shared/specifier.c
src/shared/specifier.h
src/sysusers/sysusers.c
src/test/test-hostname-util.c
src/tmpfiles/tmpfiles.c

index 0c258241df334e00d7a83dbed98821c677ada5fe..3efbb6db00e84e2247cbbef9be70b858d6a68dbb 100644 (file)
     <entry>Host name</entry>
     <entry>The hostname of the running system.</entry>
   </row>
+  <row id='l'>
+    <entry><literal>%l</literal></entry>
+    <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='m'>
     <entry><literal>%m</literal></entry>
     <entry>Machine ID</entry>
index dd6d6af21d57db44705603a3f89324652f591b54..b451c5302064b35219213ab69efd2fa4b77b0b71 100644 (file)
@@ -1747,6 +1747,11 @@ Note that this setting is <emphasis>not</emphasis> influenced by the <varname>Us
             <entry>Host name</entry>
             <entry>The hostname of the running system at the point in time the unit configuration is loaded.</entry>
           </row>
+          <row>
+            <entry><literal>%l</literal></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>
+          </row>
           <row>
             <entry><literal>%i</literal></entry>
             <entry>Instance name</entry>
index f7db34ae90148c944a7d43b6defbaa2023dea70b..38a95d6e1fcdc280f241dcfdd876a0c572a622ef 100644 (file)
@@ -257,6 +257,7 @@ r     -        500-900
           <xi:include href="standard-specifiers.xml" xpointer="b"/>
           <xi:include href="standard-specifiers.xml" xpointer="B"/>
           <xi:include href="standard-specifiers.xml" xpointer="H"/>
+          <xi:include href="standard-specifiers.xml" xpointer="l"/>
           <xi:include href="standard-specifiers.xml" xpointer="m"/>
           <xi:include href="standard-specifiers.xml" xpointer="o"/>
           <row>
index 2e9ba5fe90c23814f9265c1a4414da4d981c5810..90234c3d43f5383f9368785e74e3dbe3455ea51a 100644 (file)
@@ -646,6 +646,7 @@ w- /proc/sys/vm/swappiness - - - - 10</programlisting></para>
               <entry>This is the home directory of the user running the command. In case of the system instance this resolves to <literal>/root</literal>.</entry>
             </row>
             <xi:include href="standard-specifiers.xml" xpointer="H"/>
+            <xi:include href="standard-specifiers.xml" xpointer="l"/>
             <row>
               <entry><literal>%L</literal></entry>
               <entry>System or user log directory</entry>
index 5a2d60f21dd2c68705d2158f025f51616c3afc30..90a3dfc86471d01d93c53d96678151593c796305 100644 (file)
@@ -31,6 +31,7 @@ bool hostname_is_set(void) {
 
 char* gethostname_malloc(void) {
         struct utsname u;
+        const char *s;
 
         /* This call tries to return something useful, either the actual hostname
          * or it makes something up. The only reason it might fail is OOM.
@@ -38,10 +39,28 @@ char* gethostname_malloc(void) {
 
         assert_se(uname(&u) >= 0);
 
-        if (isempty(u.nodename) || streq(u.nodename, "(none)"))
-                return strdup(FALLBACK_HOSTNAME);
+        s = u.nodename;
+        if (isempty(s) || streq(s, "(none)"))
+                s = FALLBACK_HOSTNAME;
 
-        return strdup(u.nodename);
+        return strdup(s);
+}
+
+char* gethostname_short_malloc(void) {
+        struct utsname u;
+        const char *s;
+
+        /* Like above, but kills the FQDN part if present. */
+
+        assert_se(uname(&u) >= 0);
+
+        s = u.nodename;
+        if (isempty(s) || streq(s, "(none)") || s[0] == '.') {
+                s = FALLBACK_HOSTNAME;
+                assert(s[0] != '.');
+        }
+
+        return strndup(s, strcspn(s, "."));
 }
 
 int gethostname_strict(char **ret) {
index 7ba386a0fd99c5162cd32b22e63b8ce24e344532..cafd6f020bff96eb6b9a1f2bf735774fb67351b5 100644 (file)
@@ -9,6 +9,7 @@
 bool hostname_is_set(void);
 
 char* gethostname_malloc(void);
+char* gethostname_short_malloc(void);
 int gethostname_strict(char **ret);
 
 bool valid_ldh_char(char c) _const_;
index 049a2f55963a473a2d2fc48db0dd2ae5d3b9b2b8..4fee5dc6dc3e83362397d5eaf1e474102dd70578 100644 (file)
@@ -291,6 +291,7 @@ int unit_full_printf(const Unit *u, const char *format, char **ret) {
 
                 { 'm', specifier_machine_id,               NULL },
                 { 'H', specifier_host_name,                NULL },
+                { 'l', specifier_short_host_name,          NULL },
                 { 'b', specifier_boot_id,                  NULL },
                 { 'v', specifier_kernel_release,           NULL },
                 {}
index c784222be68cd6f14b496848a0ad173ceaa443e2..112cf6f8fb524dae0a7e10590145604694d92134 100644 (file)
@@ -160,6 +160,17 @@ int specifier_host_name(char specifier, const void *data, const void *userdata,
         return 0;
 }
 
+int specifier_short_host_name(char specifier, const void *data, const void *userdata, char **ret) {
+        char *n;
+
+        n = gethostname_short_malloc();
+        if (!n)
+                return -ENOMEM;
+
+        *ret = n;
+        return 0;
+}
+
 int specifier_kernel_release(char specifier, const void *data, const void *userdata, char **ret) {
         struct utsname uts;
         char *n;
index 33c17eae6747831de9f50c4e062a36df31867f7b..50c6cbd6ab466644dcf59f58a9f6c92be9a1ec9d 100644 (file)
@@ -18,6 +18,7 @@ int specifier_string(char specifier, const void *data, const void *userdata, cha
 int specifier_machine_id(char specifier, const void *data, const void *userdata, char **ret);
 int specifier_boot_id(char specifier, const void *data, const void *userdata, char **ret);
 int specifier_host_name(char specifier, const void *data, const void *userdata, char **ret);
+int specifier_short_host_name(char specifier, const void *data, const void *userdata, char **ret);
 int specifier_kernel_release(char specifier, const void *data, const void *userdata, char **ret);
 int specifier_architecture(char specifier, const void *data, const void *userdata, char **ret);
 int specifier_os_id(char specifier, const void *data, const void *userdata, char **ret);
index 73a710bee7c492f4b16a422fde905b1db5a3d842..a13c35b648aa2c22a8354e735c0158cbc646663d 100644 (file)
@@ -1389,17 +1389,18 @@ static bool item_equal(Item *a, Item *b) {
 static int parse_line(const char *fname, unsigned line, const char *buffer) {
 
         static const Specifier specifier_table[] = {
-                { 'm', specifier_machine_id,     NULL },
-                { 'b', specifier_boot_id,        NULL },
-                { 'H', specifier_host_name,      NULL },
-                { 'v', specifier_kernel_release, NULL },
-                { 'a', specifier_architecture,   NULL },
-                { 'o', specifier_os_id,          NULL },
-                { 'w', specifier_os_version_id,  NULL },
-                { 'B', specifier_os_build_id,    NULL },
-                { 'W', specifier_os_variant_id,  NULL },
-                { 'T', specifier_tmp_dir,        NULL },
-                { 'V', specifier_var_tmp_dir,    NULL },
+                { 'm', specifier_machine_id,      NULL },
+                { 'b', specifier_boot_id,         NULL },
+                { 'H', specifier_host_name,       NULL },
+                { 'l', specifier_short_host_name, NULL },
+                { 'v', specifier_kernel_release,  NULL },
+                { 'a', specifier_architecture,    NULL },
+                { 'o', specifier_os_id,           NULL },
+                { 'w', specifier_os_version_id,   NULL },
+                { 'B', specifier_os_build_id,     NULL },
+                { 'W', specifier_os_variant_id,   NULL },
+                { 'T', specifier_tmp_dir,         NULL },
+                { 'V', specifier_var_tmp_dir,     NULL },
                 {}
         };
 
index fe1b23e1bbbfa9f51f424485ff672d28f395d73d..5ab82bba618e7162a172f5eefb0c0d608338d8fe 100644 (file)
@@ -140,6 +140,16 @@ static void test_read_etc_hostname(void) {
         unlink(path);
 }
 
+static void test_hostname_malloc(void) {
+        _cleanup_free_ char *h = NULL, *l = NULL;
+
+        assert_se(h = gethostname_malloc());
+        log_info("hostname_malloc: \"%s\"", h);
+
+        assert_se(l = gethostname_short_malloc());
+        log_info("hostname_short_malloc: \"%s\"", l);
+}
+
 static void test_fallback_hostname(void) {
         if (!hostname_is_valid(FALLBACK_HOSTNAME, false)) {
                 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
@@ -154,6 +164,7 @@ int main(int argc, char *argv[]) {
         test_hostname_is_valid();
         test_hostname_cleanup();
         test_read_etc_hostname();
+        test_hostname_malloc();
 
         test_fallback_hostname();
 
index 7137e9fbd725e758e7c7b615b06e0c0ff83171bf..2702b36bddb3d2be16931ee44cd4cd8472ffdc34 100644 (file)
@@ -184,6 +184,7 @@ static const Specifier specifier_table[] = {
         { 'm', specifier_machine_id_safe, NULL },
         { 'b', specifier_boot_id,         NULL },
         { 'H', specifier_host_name,       NULL },
+        { 'l', specifier_short_host_name, NULL },
         { 'v', specifier_kernel_release,  NULL },
         { 'a', specifier_architecture,    NULL },
         { 'o', specifier_os_id,           NULL },