[systemd-devel] [PATCH 4/4] hostname-setup: try to set hostname only when necessary

Michal Sekletar msekleta at redhat.com
Thu Oct 2 00:57:44 PDT 2014


When a system already has hostname set to the configured value
don't try to set it again. This will prevent an error being reported when
running inside an unprivileged container, i.e. in an environment where we
don't have CAP_SYS_ADMIN capability and therefore the container manager is
responsible for setting hostname for us.
---
 src/core/hostname-setup.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c
index 8aa1cff..1df7a56 100644
--- a/src/core/hostname-setup.c
+++ b/src/core/hostname-setup.c
@@ -80,6 +80,15 @@ int hostname_setup(void) {
                         log_info("No hostname configured.");
 
                 hn = "localhost";
+        } else {
+                char old_hn[HOST_NAME_MAX + 1] = {};
+
+                r = gethostname(old_hn, sizeof(old_hn));
+                if (!r) {
+                        if (streq(hn, old_hn))
+                                return 0;
+                } else
+                        log_warning("Failed to get hostname: %m");
         }
 
         if (sethostname(hn, strlen(hn)) < 0) {
-- 
2.0.1



More information about the systemd-devel mailing list