chiark / gitweb /
Add DEPLOYMENT to hostnamectl
[elogind.git] / src / hostname / hostnamed.c
index eaae113..a4849b3 100644 (file)
 #include "bus-util.h"
 #include "event-util.h"
 
+#define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
+
 enum {
         PROP_HOSTNAME,
         PROP_STATIC_HOSTNAME,
         PROP_PRETTY_HOSTNAME,
         PROP_ICON_NAME,
         PROP_CHASSIS,
+        PROP_DEPLOYMENT,
         PROP_KERNEL_NAME,
         PROP_KERNEL_RELEASE,
         PROP_KERNEL_VERSION,
@@ -100,6 +103,7 @@ static int context_read_data(Context *c) {
                            "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
                            "ICON_NAME", &c->data[PROP_ICON_NAME],
                            "CHASSIS", &c->data[PROP_CHASSIS],
+                           "DEPLOYMENT", &c->data[PROP_DEPLOYMENT],
                            NULL);
         if (r < 0 && r != -ENOENT)
                 return r;
@@ -134,7 +138,6 @@ static bool check_nss(void) {
 }
 
 static bool valid_chassis(const char *chassis) {
-
         assert(chassis);
 
         return nulstr_contains(
@@ -149,6 +152,12 @@ static bool valid_chassis(const char *chassis) {
                         chassis);
 }
 
+static bool valid_deployment(const char *deployment) {
+        assert(deployment);
+
+        return strspn(deployment, VALID_DEPLOYMENT_CHARS) == strlen(deployment);
+}
+
 static const char* fallback_chassis(void) {
         int r;
         char *type;
@@ -258,6 +267,7 @@ static char* context_fallback_icon_name(Context *c) {
         return strdup("computer");
 }
 
+
 static bool hostname_is_useful(const char *hn) {
         return !isempty(hn) && !is_localhost(hn);
 }
@@ -312,7 +322,8 @@ static int context_write_data_machine_info(Context *c) {
         static const char * const name[_PROP_MAX] = {
                 [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
                 [PROP_ICON_NAME] = "ICON_NAME",
-                [PROP_CHASSIS] = "CHASSIS"
+                [PROP_CHASSIS] = "CHASSIS",
+                [PROP_DEPLOYMENT] = "DEPLOYMENT",
         };
 
         _cleanup_strv_free_ char **l = NULL;
@@ -324,7 +335,7 @@ static int context_write_data_machine_info(Context *c) {
         if (r < 0 && r != -ENOENT)
                 return r;
 
-        for (p = PROP_PRETTY_HOSTNAME; p <= PROP_CHASSIS; p++) {
+        for (p = PROP_PRETTY_HOSTNAME; p <= PROP_DEPLOYMENT; p++) {
                 char *t, **u;
 
                 assert(name[p]);
@@ -402,6 +413,23 @@ static int property_get_chassis(
         return sd_bus_message_append(reply, "s", name);
 }
 
+static int property_get_deployment(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Context *c = userdata;
+        const char *name;
+
+        name = c->data[PROP_DEPLOYMENT];
+
+        return sd_bus_message_append(reply, "s", name);
+}
+
 static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = userdata;
         const char *name;
@@ -555,6 +583,8 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
                 if (prop == PROP_CHASSIS && !valid_chassis(name))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
+                if (prop == PROP_DEPLOYMENT && !valid_deployment(name))
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name);
 
                 h = strdup(name);
                 if (!h)
@@ -572,11 +602,13 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop
 
         log_info("Changed %s to '%s'",
                  prop == PROP_PRETTY_HOSTNAME ? "pretty host name" :
+                 prop == PROP_DEPLOYMENT ? "deployment" :
                  prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
 
         sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
                                        prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
-                                       prop == PROP_CHASSIS ? "Chassis" : "IconName", NULL);
+                                       prop == PROP_DEPLOYMENT ? "Deployment" :
+                                       prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL);
 
         return sd_bus_reply_method_return(m, NULL);
 }
@@ -593,6 +625,10 @@ static int method_set_chassis(sd_bus *bus, sd_bus_message *m, void *userdata, sd
         return set_machine_info(userdata, bus, m, PROP_CHASSIS, method_set_chassis, error);
 }
 
+static int method_set_deployment(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        return set_machine_info(userdata, bus, m, PROP_DEPLOYMENT, method_set_deployment, error);
+}
+
 static const sd_bus_vtable hostname_vtable[] = {
         SD_BUS_VTABLE_START(0),
         SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, 0),
@@ -600,6 +636,7 @@ static const sd_bus_vtable hostname_vtable[] = {
         SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("Deployment", "s", property_get_deployment, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("KernelName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("KernelRelease", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_RELEASE, SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST),
@@ -610,6 +647,7 @@ static const sd_bus_vtable hostname_vtable[] = {
         SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetIconName", "sb", NULL, method_set_icon_name, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetChassis", "sb", NULL, method_set_chassis, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("SetDeployment", "sb", NULL, method_set_deployment, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END,
 };