]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/hostname/hostnamed.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / hostname / hostnamed.c
index 7ff3a4e224142a08467067e804d0d0794273ca33..8bff7d4b39f797317ed06f71fb77837321845a63 100644 (file)
 #include <unistd.h>
 #include <sys/utsname.h>
 
-#include "util.h"
-#include "strv.h"
+#include "bus-util.h"
 #include "def.h"
-#include "virt.h"
 #include "env-util.h"
-#include "fileio-label.h"
-#include "bus-util.h"
 #include "event-util.h"
-#include "selinux-util.h"
+#include "fileio-label.h"
 #include "hostname-util.h"
+#include "parse-util.h"
+#include "selinux-util.h"
+#include "strv.h"
+#include "util.h"
+#include "virt.h"
 
 #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
 
@@ -63,10 +64,8 @@ static void context_reset(Context *c) {
 
         assert(c);
 
-        for (p = 0; p < _PROP_MAX; p++) {
-                free(c->data[p]);
-                c->data[p] = NULL;
-        }
+        for (p = 0; p < _PROP_MAX; p++)
+                c->data[p] = mfree(c->data[p]);
 }
 
 static void context_free(Context *c) {
@@ -114,12 +113,11 @@ static int context_read_data(Context *c) {
                            "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
                            "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
                            NULL);
-        if (r == -ENOENT) {
+        if (r == -ENOENT)
                 r = parse_env_file("/usr/lib/os-release", NEWLINE,
                                    "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
                                    "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
                                    NULL);
-        }
 
         if (r < 0 && r != -ENOENT)
                 return r;
@@ -155,11 +153,11 @@ static const char* fallback_chassis(void) {
         unsigned t;
         int v;
 
-        v = detect_virtualization(NULL);
+        v = detect_virtualization();
 
-        if (v == VIRTUALIZATION_VM)
+        if (VIRTUALIZATION_IS_VM(v))
                 return "vm";
-        if (v == VIRTUALIZATION_CONTAINER)
+        if (VIRTUALIZATION_IS_CONTAINER(v))
                 return "container";
 
         r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
@@ -424,7 +422,7 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *
         if (isempty(name))
                 name = "localhost";
 
-        if (!hostname_is_valid(name))
+        if (!hostname_is_valid(name, false))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
 
         if (streq_ptr(name, c->data[PROP_HOSTNAME]))
@@ -434,6 +432,7 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *
                         m,
                         CAP_SYS_ADMIN,
                         "org.freedesktop.hostname1.set-hostname",
+                        NULL,
                         interactive,
                         UID_INVALID,
                         &c->polkit_registry,
@@ -486,6 +485,7 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
                         m,
                         CAP_SYS_ADMIN,
                         "org.freedesktop.hostname1.set-static-hostname",
+                        NULL,
                         interactive,
                         UID_INVALID,
                         &c->polkit_registry,
@@ -496,12 +496,11 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_
                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
         if (isempty(name)) {
-                free(c->data[PROP_STATIC_HOSTNAME]);
-                c->data[PROP_STATIC_HOSTNAME] = NULL;
+                c->data[PROP_STATIC_HOSTNAME] = mfree(c->data[PROP_STATIC_HOSTNAME]);
         } else {
                 char *h;
 
-                if (!hostname_is_valid(name))
+                if (!hostname_is_valid(name, false))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
 
                 h = strdup(name);
@@ -557,6 +556,7 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
                         m,
                         CAP_SYS_ADMIN,
                         prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
+                        NULL,
                         interactive,
                         UID_INVALID,
                         &c->polkit_registry,
@@ -567,8 +567,7 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess
                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
         if (isempty(name)) {
-                free(c->data[prop]);
-                c->data[prop] = NULL;
+                c->data[prop] = mfree(c->data[prop]);
         } else {
                 char *h;
 
@@ -663,7 +662,7 @@ static const sd_bus_vtable hostname_vtable[] = {
 };
 
 static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
-        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+        _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
         int r;
 
         assert(c);
@@ -695,7 +694,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
 int main(int argc, char *argv[]) {
         Context context = {};
         _cleanup_event_unref_ sd_event *event = NULL;
-        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+        _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
         int r;
 
         log_set_target(LOG_TARGET_AUTO);
@@ -711,12 +710,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (argc != 1) {
-                log_error("This program takes no arguments.");
-                r = -EINVAL;
-                goto finish;
-        }
-
         r = sd_event_default(&event);
         if (r < 0) {
                 log_error_errno(r, "Failed to allocate event loop: %m");