[systemd-devel] [PATCH 2/4] Add ENVIRONMENT to hostnamed

Jóhann B. Guðmundsson johannbg at gmail.com
Mon Jul 7 17:38:50 PDT 2014


---
 src/hostname/hostnamed.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 514554d..b5ed3e9 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -41,6 +41,7 @@ enum {
         PROP_PRETTY_HOSTNAME,
         PROP_ICON_NAME,
         PROP_CHASSIS,
+        PROP_ENVIRONMENT,
         PROP_KERNEL_NAME,
         PROP_KERNEL_RELEASE,
         PROP_KERNEL_VERSION,
@@ -100,6 +101,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],
+                           "ENVIRONMENT", &c->data[PROP_ENVIRONMENT],
                            NULL);
         if (r < 0 && r != -ENOENT)
                 return r;
@@ -148,6 +150,18 @@ static bool valid_chassis(const char *chassis) {
                         chassis);
 }
 
+static bool valid_environment(const char *environment) {
+
+        assert(environment);
+
+        return nulstr_contains(
+                        "development\0"
+                        "staging\0"
+                        "production\0",
+                        environment);
+}
+
+
 static const char* fallback_chassis(void) {
         int r;
         char *type;
@@ -257,6 +271,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);
 }
@@ -311,7 +326,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_ENVIRONMENT] = "ENVIRONMENT",
         };
 
         _cleanup_strv_free_ char **l = NULL;
@@ -323,7 +339,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_ENVIRONMENT; p++) {
                 char *t, **u;
 
                 assert(name[p]);
@@ -401,6 +417,23 @@ static int property_get_chassis(
         return sd_bus_message_append(reply, "s", name);
 }
 
+static int property_get_environment(
+                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_ENVIRONMENT];
+
+        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;
@@ -554,6 +587,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_ENVIRONMENT && !valid_environment(name))
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment '%s'", name);
 
                 h = strdup(name);
                 if (!h)
@@ -571,11 +606,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_ENVIRONMENT ? "environment" :
                  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_ENVIRONMENT ? "Environment" :
+                                       prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL);
 
         return sd_bus_reply_method_return(m, NULL);
 }
@@ -592,6 +629,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_environment(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        return set_machine_info(userdata, bus, m, PROP_ENVIRONMENT, method_set_environment, 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),
@@ -599,6 +640,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("Environment", "s", property_get_environment, 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),
@@ -609,6 +651,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("SetEnvironment", "sb", NULL, method_set_environment, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END,
 };
 
-- 
1.9.3



More information about the systemd-devel mailing list