hostnamed.c revision 89f134406af6a4b4c7493f624a89dcd654b48e81
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering#include <dbus/dbus.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <dlfcn.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "dbus-common.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "polkit.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "def.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "virt.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define INTERFACE \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen " <interface name=\"org.freedesktop.hostname1\">\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <property name=\"Hostname\" type=\"s\" access=\"read\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <property name=\"StaticHostname\" type=\"s\" access=\"read\"/>\n" \
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering " <property name=\"PrettyHostname\" type=\"s\" access=\"read\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <property name=\"IconName\" type=\"s\" access=\"read\"/>\n" \
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering " <method name=\"SetHostname\">\n" \
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " </method>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <method name=\"SetStaticHostname\">\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " </method>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <method name=\"SetPrettyHostname\">\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " </method>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <method name=\"SetIconName\">\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " </method>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " </interface>\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define INTROSPECTION \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "<node>\n" \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering INTERFACE \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen BUS_PROPERTIES_INTERFACE \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen BUS_INTROSPECTABLE_INTERFACE \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen BUS_PEER_INTERFACE \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "</node>\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define INTERFACES_LIST \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering BUS_GENERIC_INTERFACES_LIST \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.hostname1\0"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenconst char hostname_interface[] _introspect_("hostname1") = INTERFACE;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenenum {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen PROP_HOSTNAME,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen PROP_STATIC_HOSTNAME,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen PROP_PRETTY_HOSTNAME,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen PROP_ICON_NAME,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen _PROP_MAX
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen};
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic char *data[_PROP_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic usec_t remain_until = 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poetteringstatic void free_data(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int p;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (p = 0; p < _PROP_MAX; p++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(data[p]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering data[p] = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int read_data(void) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering free_data();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering data[PROP_HOSTNAME] = gethostname_malloc();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!data[PROP_HOSTNAME])
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -ENOMEM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = read_one_line_file("/etc/hostname", &data[PROP_STATIC_HOSTNAME]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0 && r != -ENOENT)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = parse_env_file("/etc/machine-info", NEWLINE,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering "PRETTY_HOSTNAME", &data[PROP_PRETTY_HOSTNAME],
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering "ICON_NAME", &data[PROP_ICON_NAME],
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering NULL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0 && r != -ENOENT)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return r;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 0;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic bool check_nss(void) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering void *dl;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if ((dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY))) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering dlclose(dl);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return true;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return false;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering}
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic const char* fallback_icon_name(void) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering#if defined(__i386__) || defined(__x86_64__)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering int r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char *type;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering unsigned t;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering#endif
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (detect_virtualization(NULL) > 0)
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering return "computer-vm";
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering#if defined(__i386__) || defined(__x86_64__)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (r < 0)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return NULL;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering r = safe_atou(type, &t);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering free(type);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* We only list the really obvious cases here. The DMI data is
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering unreliable enough, so let's not do any additional guesswork
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering on top of that.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering See the SMBIOS Specification 2.7.1 section 7.4.1 for
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering details about the values listed here:
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (t) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x3:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers case 0x4:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers case 0x6:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers case 0x7:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return "computer-desktop";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x9:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0xA:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0xE:
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return "computer-laptop";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x11:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x1C:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return "computer-server";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int write_data_hostname(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *hn;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (isempty(data[PROP_HOSTNAME]))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hn = "localhost";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hn = data[PROP_HOSTNAME];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sethostname(hn, strlen(hn)) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int write_data_static_hostname(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (isempty(data[PROP_STATIC_HOSTNAME])) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (unlink("/etc/hostname") < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return errno == ENOENT ? 0 : -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return write_one_line_file_atomic("/etc/hostname", data[PROP_STATIC_HOSTNAME]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int write_data_other(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering static const char * const name[_PROP_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [PROP_ICON_NAME] = "ICON_NAME"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering };
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char **l = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering int r, p;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = load_env_file("/etc/machine-info", &l);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0 && r != -ENOENT)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers for (p = 2; p < _PROP_MAX; p++) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers char *t, **u;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(name[p]);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (isempty(data[p])) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers strv_env_unset(l, name[p]);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers continue;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (asprintf(&t, "%s=%s", name[p], strempty(data[p])) < 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers strv_free(l);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return -ENOMEM;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering u = strv_env_set(l, t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(t);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers strv_free(l);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (!u)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return -ENOMEM;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers l = u;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (strv_isempty(l)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (unlink("/etc/machine-info") < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return errno == ENOENT ? 0 : -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = write_env_file("/etc/machine-info", l);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers strv_free(l);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int bus_hostname_append_icon_name(DBusMessageIter *i, const char *property, void *userdata) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers const char *name;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(i);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(property);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (isempty(data[PROP_ICON_NAME]))
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers name = fallback_icon_name();
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name = data[PROP_ICON_NAME];
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return bus_property_append_string(i, property, (void*) name);
27e72d6b22890ba4a8cbc05c49667cd1cccf1461Simon Peeters}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic const BusProperty bus_hostname_properties[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Hostname", bus_property_append_string, "s", sizeof(data[0])*PROP_HOSTNAME, true },
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering { "StaticHostname", bus_property_append_string, "s", sizeof(data[0])*PROP_STATIC_HOSTNAME, true },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "PrettyHostname", bus_property_append_string, "s", sizeof(data[0])*PROP_PRETTY_HOSTNAME, true },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "IconName", bus_hostname_append_icon_name, "s", sizeof(data[0])*PROP_ICON_NAME, true },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { NULL, }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen};
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic const BusBoundProperties bps[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "org.freedesktop.hostname1", bus_hostname_properties, data },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers { NULL, }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic DBusHandlerResult hostname_message_handler(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering DBusConnection *connection,
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers DBusMessage *message,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers DBusMessage *reply = NULL, *changed = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen DBusError error;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(connection);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(message);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers dbus_error_init(&error);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetHostname")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *name;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dbus_bool_t interactive;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!dbus_message_get_args(
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen message,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &error,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen DBUS_TYPE_STRING, &name,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen DBUS_TYPE_BOOLEAN, &interactive,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen DBUS_TYPE_INVALID))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return bus_send_error_reply(connection, message, &error, -EINVAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (isempty(name))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen name = data[PROP_STATIC_HOSTNAME];
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (isempty(name))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen name = "localhost";
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!hostname_is_valid(name))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return bus_send_error_reply(connection, message, NULL, -EINVAL);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (!streq_ptr(name, data[PROP_HOSTNAME])) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *h;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-hostname", interactive, NULL, &error);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (r < 0)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return bus_send_error_reply(connection, message, &error, r);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering h = strdup(name);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (!h)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto oom;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(data[PROP_HOSTNAME]);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen data[PROP_HOSTNAME] = h;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = write_data_hostname();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to set host name: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_send_error_reply(connection, message, NULL, r);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_info("Changed host name to '%s'", strempty(data[PROP_HOSTNAME]));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering changed = bus_properties_changed_new(
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "/org/freedesktop/hostname1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.hostname1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "Hostname\0");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!changed)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto oom;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetStaticHostname")) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *name;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen dbus_bool_t interactive;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!dbus_message_get_args(
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen message,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &error,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering DBUS_TYPE_STRING, &name,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering DBUS_TYPE_BOOLEAN, &interactive,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering DBUS_TYPE_INVALID))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (isempty(name))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!streq_ptr(name, data[PROP_STATIC_HOSTNAME])) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-static-hostname", interactive, NULL, &error);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_send_error_reply(connection, message, &error, r);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (isempty(name)) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(data[PROP_STATIC_HOSTNAME]);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen data[PROP_STATIC_HOSTNAME] = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } else {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char *h;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!hostname_is_valid(name))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return bus_send_error_reply(connection, message, NULL, -EINVAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen h = strdup(name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!h)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto oom;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(data[PROP_STATIC_HOSTNAME]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering data[PROP_STATIC_HOSTNAME] = h;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = write_data_static_hostname();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r < 0) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to write static host name: %s", strerror(-r));
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_send_error_reply(connection, message, NULL, r);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_info("Changed static host name to '%s'", strempty(data[PROP_STATIC_HOSTNAME]));
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek changed = bus_properties_changed_new(
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek "/org/freedesktop/hostname1",
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek "org.freedesktop.hostname1",
34a6778fb9d1065f3fbb8e2243b9f0f25d1d18f1Zbigniew Jędrzejewski-Szmek "StaticHostname\0");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!changed)
f69157a66ffe413b4cf8bd79057487fc8921e78bThomas Hindoe Paaboel Andersen goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetPrettyHostname") ||
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetIconName")) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering const char *name;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_bool_t interactive;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering int k;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!dbus_message_get_args(
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering message,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering &error,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBUS_TYPE_STRING, &name,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBUS_TYPE_BOOLEAN, &interactive,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBUS_TYPE_INVALID))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (isempty(name))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering name = NULL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering k = streq(dbus_message_get_member(message), "SetPrettyHostname") ? PROP_PRETTY_HOSTNAME : PROP_ICON_NAME;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!streq_ptr(name, data[k])) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering /* Since the pretty hostname should always be
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * changed at the same time as the static one,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * use the same policy action for both... */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = verify_polkit(connection, message, k == PROP_PRETTY_HOSTNAME ?
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "org.freedesktop.hostname1.set-static-hostname" :
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "org.freedesktop.hostname1.set-machine-info", interactive, NULL, &error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r < 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_send_error_reply(connection, message, &error, r);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (isempty(name)) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering free(data[k]);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering data[k] = NULL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering } else {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering char *h;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering h = strdup(name);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!h)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering free(data[k]);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering data[k] = h;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = write_data_other();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r < 0) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to write machine info: %s", strerror(-r));
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_send_error_reply(connection, message, NULL, r);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_info("Changed %s to '%s'", k == PROP_PRETTY_HOSTNAME ? "pretty host name" : "icon name", strempty(data[k]));
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering changed = bus_properties_changed_new(
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "/org/freedesktop/hostname1",
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "org.freedesktop.hostname1",
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering k == PROP_PRETTY_HOSTNAME ? "PrettyHostname\0" : "IconName\0");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!changed)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering } else
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!(reply = dbus_message_new_method_return(message)))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!dbus_connection_send(connection, reply, NULL))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_message_unref(reply);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering reply = NULL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (changed) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!dbus_connection_send(connection, changed, NULL))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto oom;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_message_unref(changed);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return DBUS_HANDLER_RESULT_HANDLED;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringoom:
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (reply)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_message_unref(reply);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (changed)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_message_unref(changed);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_error_free(&error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return DBUS_HANDLER_RESULT_NEED_MEMORY;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering}
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringstatic int connect_bus(DBusConnection **_bus) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering static const DBusObjectPathVTable hostname_vtable = {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering .message_function = hostname_message_handler
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering };
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBusError error;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBusConnection *bus = NULL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering int r;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert(_bus);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_error_init(&error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!bus) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = -ECONNREFUSED;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto fail;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_connection_set_exit_on_disconnect(bus, FALSE);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL) ||
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Not enough memory");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = -ENOMEM;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto fail;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (dbus_error_is_set(&error)) {
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering log_error("Failed to register name on bus: %s", bus_error_message(&error));
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering r = -EEXIST;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto fail;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to acquire name.");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = -EEXIST;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto fail;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (_bus)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering *_bus = bus;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return 0;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringfail:
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_connection_close(bus);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering dbus_connection_unref(bus);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_error_free(&error);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return r;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering}
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint main(int argc, char *argv[]) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering int r;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering DBusConnection *bus = NULL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering bool exiting = false;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_set_target(LOG_TARGET_AUTO);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_parse_environment();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_open();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering umask(0022);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (argc == 2 && streq(argv[1], "--introspect")) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "<node>\n", stdout);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering fputs(hostname_interface, stdout);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering fputs("</node>\n", stdout);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return 0;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (argc != 1) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("This program takes no arguments.");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = -EINVAL;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto finish;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!check_nss())
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = read_data();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r < 0) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to read hostname data: %s", strerror(-r));
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto finish;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = connect_bus(&bus);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (r < 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering goto finish;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering for (;;) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering break;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering exiting = true;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = 0;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringfinish:
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering free_data();
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering if (bus) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_connection_flush(bus);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_connection_close(bus);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering dbus_connection_unref(bus);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering