hostnamed.c revision fda2c5d28bdf5c6ac1ff1ba504fa884c48ed1439
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include "util.h"
#include "strv.h"
#include "dbus-common.h"
#include "polkit.h"
#include "def.h"
#include "virt.h"
#include "env-util.h"
#include "fileio-label.h"
#include "label.h"
#define INTERFACE \
" <interface name=\"org.freedesktop.hostname1\">\n" \
" <property name=\"Hostname\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"StaticHostname\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"PrettyHostname\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"IconName\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"Chassis\" type=\"s\" access=\"read\"/>\n" \
" <method name=\"SetHostname\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetStaticHostname\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetPrettyHostname\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetIconName\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetChassis\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" </interface>\n"
#define INTROSPECTION \
"<node>\n" \
"</node>\n"
#define INTERFACES_LIST \
"org.freedesktop.hostname1\0"
enum {
};
NULL,
NULL,
NULL,
NULL,
};
static usec_t remain_until = 0;
static void free_data(void) {
int p;
for (p = 0; p < _PROP_MAX; p++) {
}
}
static int read_data(void) {
int r;
free_data();
if (!data[PROP_HOSTNAME])
return -ENOMEM;
if (r < 0 && r != -ENOENT)
return r;
NULL);
if (r < 0 && r != -ENOENT)
return r;
return 0;
}
static bool check_nss(void) {
void *dl;
if (dl) {
return true;
}
return false;
}
static bool valid_chassis(const char *chassis) {
return nulstr_contains(
"vm\0"
"container\0"
"desktop\0"
"laptop\0"
"server\0"
"tablet\0"
"handset\0",
chassis);
}
static const char* fallback_chassis(void) {
int r;
char *type;
unsigned t;
v = detect_virtualization(NULL);
if (v == VIRTUALIZATION_VM)
return "vm";
if (v == VIRTUALIZATION_CONTAINER)
return "container";
if (r < 0)
goto try_dmi;
if (r < 0)
goto try_dmi;
/* We only list the really obvious cases here as the ACPI data
* is not really super reliable.
*
* See the ACPI 5.0 Spec Section 5.2.9.1 for details:
*
*/
switch(t) {
case 1:
case 3:
case 6:
return "desktop";
case 2:
return "laptop";
case 4:
case 5:
case 7:
return "server";
case 8:
return "tablet";
}
if (r < 0)
return NULL;
if (r < 0)
return NULL;
/* We only list the really obvious cases here. The DMI data is
unreliable enough, so let's not do any additional guesswork
on top of that.
See the SMBIOS Specification 2.7.1 section 7.4.1 for
details about the values listed here:
*/
switch (t) {
case 0x3:
case 0x4:
case 0x6:
case 0x7:
return "desktop";
case 0x8:
case 0x9:
case 0xA:
case 0xE:
return "laptop";
case 0xB:
return "handset";
case 0x11:
case 0x1C:
return "server";
}
return NULL;
}
static char* fallback_icon_name(void) {
const char *chassis;
chassis = fallback_chassis();
if (chassis)
return strdup("computer");
}
static int write_data_hostname(void) {
const char *hn;
hn = "localhost";
else
return -errno;
return 0;
}
static int write_data_static_hostname(void) {
return 0;
}
}
static int write_data_other(void) {
[PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
[PROP_ICON_NAME] = "ICON_NAME",
[PROP_CHASSIS] = "CHASSIS"
};
char **l = NULL;
int r, p;
if (r < 0 && r != -ENOENT)
return r;
for (p = 2; p < _PROP_MAX; p++) {
char *t, **u;
strv_env_unset(l, name[p]);
continue;
}
strv_free(l);
return -ENOMEM;
}
u = strv_env_set(l, t);
free(t);
strv_free(l);
if (!u)
return -ENOMEM;
l = u;
}
if (strv_isempty(l)) {
if (unlink("/etc/machine-info") < 0)
return 0;
}
r = write_env_file_label("/etc/machine-info", l);
strv_free(l);
return r;
}
static int bus_hostname_append_icon_name(DBusMessageIter *i, const char *property, void *userdata) {
const char *name;
_cleanup_free_ char *n = NULL;
assert(i);
name = n = fallback_icon_name();
else
}
const char *name;
assert(i);
name = fallback_chassis();
else
}
static const BusProperty bus_hostname_properties[] = {
{ NULL, }
};
static const BusBoundProperties bps[] = {
{ NULL, }
};
void *userdata) {
int r;
const char *name;
if (!dbus_message_get_args(
&error,
name = "localhost";
if (!hostname_is_valid(name))
char *h;
r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-hostname", interactive, NULL, &error);
if (r < 0)
if (!h)
goto oom;
data[PROP_HOSTNAME] = h;
r = write_data_hostname();
if (r < 0) {
}
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"Hostname\0");
if (!changed)
goto oom;
}
} else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetStaticHostname")) {
const char *name;
if (!dbus_message_get_args(
&error,
r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-static-hostname", interactive, NULL, &error);
if (r < 0)
} else {
char *h;
if (!hostname_is_valid(name))
if (!h)
goto oom;
data[PROP_STATIC_HOSTNAME] = h;
}
r = write_data_static_hostname();
if (r < 0) {
}
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"StaticHostname\0");
if (!changed)
goto oom;
}
} else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetPrettyHostname") ||
const char *name;
int k;
if (!dbus_message_get_args(
&error,
/* Since the pretty hostname should always be
* changed at the same time as the static one,
* use the same policy action for both... */
"org.freedesktop.hostname1.set-static-hostname" :
if (r < 0)
} else {
char *h;
/* The icon name might ultimately be
* used as file name, so better be
* safe than sorry */
if (!h)
goto oom;
data[k] = h;
}
r = write_data_other();
if (r < 0) {
}
log_info("Changed %s to '%s'",
k == PROP_PRETTY_HOSTNAME ? "pretty host name" :
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
k == PROP_PRETTY_HOSTNAME ? "PrettyHostname\0" :
if (!changed)
goto oom;
}
} else
if (!reply)
goto oom;
goto oom;
if (changed) {
goto oom;
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
if (changed)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static const DBusObjectPathVTable hostname_vtable = {
};
int r;
if (!bus) {
r = -ECONNREFUSED;
goto fail;
}
if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL) ||
r = log_oom();
goto fail;
}
if (dbus_error_is_set(&error)) {
r = -EEXIST;
goto fail;
}
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
r = -EEXIST;
goto fail;
}
if (_bus)
return 0;
fail:
return r;
}
int r;
bool exiting = false;
log_open();
umask(0022);
label_init("/etc");
"<node>\n", stdout);
return 0;
}
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
if (!check_nss())
log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!");
r = read_data();
if (r < 0) {
goto finish;
}
r = connect_bus(&bus);
if (r < 0)
goto finish;
for (;;) {
if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
break;
exiting = true;
}
}
r = 0;
free_data();
if (bus) {
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}