hostnamed.c revision 102d8f8169427cb68cdebf5ee0f0e07788e9c2b2
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2011 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <errno.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <string.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <unistd.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <dlfcn.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "strv.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "def.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "virt.h"
b4bbcaa9c44260e88402cb8f9a5fb8ac7f35e123Thomas Hindoe Paaboel Andersen#include "env-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "fileio-label.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "label.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "bus-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "event-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poetteringenum {
430f0182b72373145c839dbfe99d2382855cb8f8Lennart Poettering PROP_HOSTNAME,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek PROP_STATIC_HOSTNAME,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek PROP_PRETTY_HOSTNAME,
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering PROP_ICON_NAME,
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen PROP_CHASSIS,
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering _PROP_MAX
33d52ab92f2f0bfd706e6f343d172618d1e03f3dLennart Poettering};
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poetteringtypedef struct Context {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering char *data[_PROP_MAX];
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering Hashmap *polkit_registry;
afc5dbf37fd2399d37976388d9dd9ab470ecf446Lennart Poettering} Context;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void context_reset(Context *c) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (p = 0; p < _PROP_MAX; p++) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(c->data[p]);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering c->data[p] = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void context_free(Context *c, sd_bus *bus) {
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering assert(c);
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering context_reset(c);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering bus_verify_polkit_async_registry_free(bus, c->polkit_registry);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering}
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic int context_read_data(Context *c) {
32917e33882778cf2ec6dd54b1e1082266fb072eZbigniew Jędrzejewski-Szmek int r;
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert(c);
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek context_reset(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->data[PROP_HOSTNAME] = gethostname_malloc();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!c->data[PROP_HOSTNAME])
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return -ENOMEM;
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering r = read_one_line_file("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]);
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz if (r < 0 && r != -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = parse_env_file("/etc/machine-info", NEWLINE,
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering "ICON_NAME", &c->data[PROP_ICON_NAME],
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo "CHASSIS", &c->data[PROP_CHASSIS],
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo NULL);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (r < 0 && r != -ENOENT)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic bool check_nss(void) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering void *dl;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (dl) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dlclose(dl);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic bool valid_chassis(const char *chassis) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(chassis);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return nulstr_contains(
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "vm\0"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "container\0"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "desktop\0"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "laptop\0"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "server\0"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "tablet\0"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "handset\0",
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering chassis);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic const char* fallback_chassis(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering char *type;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Virtualization v;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek v = detect_virtualization(NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (v == VIRTUALIZATION_VM)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "vm";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (v == VIRTUALIZATION_CONTAINER)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "container";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto try_dmi;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = safe_atou(type, &t);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(type);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering goto try_dmi;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* We only list the really obvious cases here as the ACPI data
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * is not really super reliable.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering switch(t) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 1:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 3:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case 6:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "desktop";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek case 2:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "laptop";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek case 4:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 5:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 7:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "server";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek case 8:
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return "tablet";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmektry_dmi:
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (r < 0)
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return NULL;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = safe_atou(type, &t);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek free(type);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (r < 0)
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return NULL;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek /* We only list the really obvious cases here. The DMI data is
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek unreliable enough, so let's not do any additional guesswork
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek on top of that.
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek See the SMBIOS Specification 2.7.1 section 7.4.1 for
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek details about the values listed here:
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek */
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek switch (t) {
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek case 0x3:
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek case 0x4:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x6:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x7:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "desktop";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x8:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x9:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0xA:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0xE:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "laptop";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0xB:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "handset";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x11:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case 0x1C:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "server";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic char* context_fallback_icon_name(Context *c) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *chassis;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(c);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!isempty(c->data[PROP_CHASSIS]))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return strappend("computer-", c->data[PROP_CHASSIS]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek chassis = fallback_chassis();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (chassis)
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek return strappend("computer-", chassis);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return strdup("computer");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poetteringstatic int context_write_data_hostname(Context *c) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *hn;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek assert(c);
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek if (isempty(c->data[PROP_HOSTNAME]))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek hn = "localhost";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek hn = c->data[PROP_HOSTNAME];
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (sethostname(hn, strlen(hn)) < 0)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return -errno;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return 0;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo}
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputostatic int context_write_data_static_hostname(Context *c) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering assert(c);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (unlink("/etc/hostname") < 0)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return errno == ENOENT ? 0 : -errno;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo return 0;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo }
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo}
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputostatic int context_write_data_other(Context *c) {
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo static const char * const name[_PROP_MAX] = {
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo [PROP_ICON_NAME] = "ICON_NAME",
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo [PROP_CHASSIS] = "CHASSIS"
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo };
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo char **l = NULL;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo int r, p;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(c);
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = load_env_file("/etc/machine-info", NULL, &l);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (p = 2; p < _PROP_MAX; p++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *t, **u;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(name[p]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (isempty(c->data[p])) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek strv_env_unset(l, name[p]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering if (asprintf(&t, "%s=%s", name[p], strempty(c->data[p])) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek strv_free(l);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u = strv_env_set(l, t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(t);
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering strv_free(l);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek l = u;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strv_isempty(l)) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (unlink("/etc/machine-info") < 0)
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return errno == ENOENT ? 0 : -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo r = write_env_file_label("/etc/machine-info", l);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek strv_free(l);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poetteringstatic int property_get_icon_name(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus *bus,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *interface,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *property,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *reply,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_error *error,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering _cleanup_free_ char *n = NULL;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering Context *c = userdata;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering const char *name;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering int r;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (isempty(c->data[PROP_ICON_NAME]))
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering name = n = context_fallback_icon_name(c);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek else
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek name = c->data[PROP_ICON_NAME];
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (!name)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -ENOMEM;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "s", name);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return 1;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering}
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmekstatic int property_get_chassis(
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering sd_bus *bus,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek const char *path,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek const char *interface,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek const char *property,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_message *reply,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_bus_error *error,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Context *c = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *name;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (isempty(c->data[PROP_CHASSIS]))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering name = fallback_chassis();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt name = c->data[PROP_CHASSIS];
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "s", name);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk Context *c = userdata;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk const char *name;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int interactive;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk char *h;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = sd_bus_message_read(m, "sb", &name, &interactive);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return sd_bus_reply_method_errno(bus, m, r, NULL);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
65c1d46b0923771955519329160a1e4c7cd027b0Lennart Poettering if (isempty(name))
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk name = c->data[PROP_STATIC_HOSTNAME];
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering if (isempty(name))
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk name = "localhost";
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (!hostname_is_valid(name))
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (streq_ptr(name, c->data[PROP_HOSTNAME]))
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return sd_bus_reply_method_return(bus, m, NULL);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-hostname", interactive, &error, method_set_hostname, c);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return sd_bus_reply_method_errno(bus, m, r, &error);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (r == 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering h = strdup(name);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!h)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return log_oom();
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(c->data[PROP_HOSTNAME]);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering c->data[PROP_HOSTNAME] = h;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = context_write_data_hostname(c);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (r < 0) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek log_error("Failed to set host name: %s", strerror(-r));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return sd_bus_reply_method_errnof(bus, m, r, "Failed to set hostname: %s", strerror(-r));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_info("Changed host name to '%s'", strna(c->data[PROP_HOSTNAME]));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(bus, m, NULL);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *userdata) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering Context *c = userdata;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *name;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek int interactive;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(m, "sb", &name, &interactive);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return sd_bus_reply_method_errno(bus, m, r, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (isempty(name))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek name = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return sd_bus_reply_method_return(bus, m, NULL);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-static-hostname", interactive, &error, method_set_static_hostname, c);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return sd_bus_reply_method_errno(bus, m, r, &error);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r == 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (isempty(name)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(c->data[PROP_STATIC_HOSTNAME]);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering c->data[PROP_STATIC_HOSTNAME] = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering } else {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *h;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!hostname_is_valid(name))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering h = strdup(name);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!h)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return log_oom();
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering free(c->data[PROP_STATIC_HOSTNAME]);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering c->data[PROP_STATIC_HOSTNAME] = h;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = context_write_data_static_hostname(c);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_error("Failed to write static host name: %s", strerror(-r));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return sd_bus_reply_method_errnof(bus, m, r, "Failed to set static hostname: %s", strerror(-r));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_info("Changed static host name to '%s'", strna(c->data[PROP_STATIC_HOSTNAME]));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return sd_bus_reply_method_return(bus, m, NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop, sd_bus_message_handler_t cb) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int interactive;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *name;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(c);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(bus);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(m);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return sd_bus_reply_method_errno(bus, m, r, NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (isempty(name))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering name = NULL;
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq_ptr(name, c->data[prop]))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(bus, m, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Since the pretty hostname should always be changed at the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * same time as the static one, use the same policy action for
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering * both... */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = bus_verify_polkit_async(bus, &c->polkit_registry, m, prop == PROP_PRETTY_HOSTNAME ?
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "org.freedesktop.hostname1.set-static-hostname" :
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "org.freedesktop.hostname1.set-machine-info", interactive, &error, cb, c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering return sd_bus_reply_method_errno(bus, m, r, &error);
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (r == 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (isempty(name)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(c->data[prop]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->data[prop] = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *h;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* The icon name might ultimately be used as file
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * name, so better be safe than sorry */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering if (prop == PROP_ICON_NAME && !filename_is_safe(name))
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (prop == PROP_PRETTY_HOSTNAME &&
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering (string_has_cc(name) || chars_intersect(name, "\t")))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (prop == PROP_CHASSIS && !valid_chassis(name))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek h = strdup(name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!h)
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return log_oom();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(c->data[prop]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c->data[prop] = h;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = context_write_data_other(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to write machine info: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errnof(bus, m, r, "Failed to write machine info: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Changed %s to '%s'",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prop == PROP_PRETTY_HOSTNAME ? "pretty host name" :
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prop == PROP_CHASSIS ? "Chassis" : "IconName", NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(bus, m, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int method_set_pretty_hostname(sd_bus *bus, sd_bus_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return set_machine_info(userdata, bus, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int method_set_icon_name(sd_bus *bus, sd_bus_message *m, void *userdata) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return set_machine_info(userdata, bus, m, PROP_ICON_NAME, method_set_icon_name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poetteringstatic int method_set_chassis(sd_bus *bus, sd_bus_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return set_machine_info(userdata, bus, m, PROP_CHASSIS, method_set_chassis);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const sd_bus_vtable hostname_vtable[] = {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering SD_BUS_VTABLE_START(0),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, 0),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_PROPERTY("StaticHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_STATIC_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_METHOD("SetHostname", "sb", NULL, method_set_hostname, 0),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SD_BUS_METHOD("SetStaticHostname", "sb", NULL, method_set_static_hostname, 0),
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, 0),
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering SD_BUS_METHOD("SetIconName", "sb", NULL, method_set_icon_name, 0),
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering SD_BUS_METHOD("SetChassis", "sb", NULL, method_set_chassis, 0),
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering SD_BUS_VTABLE_END,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek};
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_bus_unref_ sd_bus *bus = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering assert(c);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(event);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(_bus);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_bus_open_system(&bus);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to get system bus connection: %s", strerror(-r));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler r = sd_bus_add_object_vtable(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to register object: %s", strerror(-r));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_bus_request_name(bus, "org.freedesktop.hostname1", SD_BUS_NAME_DO_NOT_QUEUE);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to register name: %s", strerror(-r));
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering return r;
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r != SD_BUS_NAME_PRIMARY_OWNER) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to acquire name.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EEXIST;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = sd_bus_attach_event(bus, event, 0);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r < 0) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to attach bus to event loop: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *_bus = bus;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek bus = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther Context context = {};
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek _cleanup_event_unref_ sd_event *event = NULL;
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther _cleanup_bus_unref_ sd_bus *bus = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther log_set_target(LOG_TARGET_AUTO);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_parse_environment();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_open();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering umask(0022);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek label_init("/etc");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (argc != 1) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("This program takes no arguments.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (!check_nss())
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn if (argc != 1) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("This program takes no arguments.");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering r = -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden }
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden r = sd_event_new(&event);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (r < 0) {
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden log_error("Failed to allocate event loop: %s", strerror(-r));
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering goto finish;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = connect_bus(&context, event, &bus);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = context_read_data(&context);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (r < 0) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to read timezone data: %s", strerror(-r));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (r < 0) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to run event loop: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering r = 0;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekfinish:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek context_free(&context, bus);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering