hostnamed.c revision 9bcbce4201afada1c0ad8ada0cbfbbf58a52a6a7
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier This file is part of systemd.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier Copyright 2011 Lennart Poettering
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier systemd is free software; you can redistribute it and/or modify it
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier under the terms of the GNU Lesser General Public License as published by
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier the Free Software Foundation; either version 2.1 of the License, or
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier (at your option) any later version.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier systemd is distributed in the hope that it will be useful, but
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier WITHOUT ANY WARRANTY; without even the implied warranty of
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier Lesser General Public License for more details.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier You should have received a copy of the GNU Lesser General Public License
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevaliertypedef struct Context {
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier for (p = 0; p < _PROP_MAX; p++) {
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poetteringstatic void context_free(Context *c, sd_bus *bus) {
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering bus_verify_polkit_async_registry_free(bus, c->polkit_registry);
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poetteringstatic int context_read_data(Context *c) {
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering c->data[PROP_HOSTNAME] = gethostname_malloc();
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering r = read_one_line_file("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]);
cda134ab1eac84f874aacf8e885a07112a7fd5ceLennart Poettering if (r < 0 && r != -ENOENT)
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier r = parse_env_file("/etc/machine-info", NEWLINE,
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier if (r < 0 && r != -ENOENT)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic bool check_nss(void) {
4c1fc3e404d648c70bd2f50ac50aeac6ece8872eDaniel Mack dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY);
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier return false;
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalierstatic bool valid_chassis(const char *chassis) {
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier "container\0"
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalierstatic const char* fallback_chassis(void) {
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering return "container";
641d1f99b8c4c5427a1fedcb4740586a130ac6cfRonny Chevalier r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
4f36d4004c407c16508001a20450c5f14f7d4d31Dimitri John Ledkov /* We only list the really obvious cases here as the ACPI data
4f36d4004c407c16508001a20450c5f14f7d4d31Dimitri John Ledkov * is not really super reliable.
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek return "desktop";
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek return "laptop";
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek return "server";
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek return "tablet";
ad5ecc113821fbfa33f6fd43cdaee9c538cdff78Zbigniew Jędrzejewski-Szmek r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
return NULL;
const char *chassis;
assert(c);
if (chassis)
const char *hn;
assert(c);
return -errno;
assert(c);
char **l = NULL;
assert(c);
if (r < 0 && r != -ENOENT)
strv_free(l);
return -ENOMEM;
u = strv_env_set(l, t);
free(t);
strv_free(l);
return -ENOMEM;
if (strv_isempty(l)) {
strv_free(l);
static int property_get_icon_name(
const char *path,
const char *interface,
const char *property,
void *userdata) {
const char *name;
if (!name)
return -ENOMEM;
static int property_get_chassis(
const char *path,
const char *interface,
const char *property,
void *userdata) {
const char *name;
const char *name;
unsigned interactive;
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-hostname", interactive, &error, method_set_hostname, c);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
return log_oom();
r = context_write_data_hostname(c);
sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
const char *name;
unsigned interactive;
r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-static-hostname", interactive, &error, method_set_static_hostname, c);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
return log_oom();
sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL);
static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop, sd_bus_message_handler_t cb) {
unsigned interactive;
const char *name;
assert(c);
assert(m);
"org.freedesktop.hostname1.set-static-hostname" :
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
return log_oom();
r = context_write_data_other(c);
SD_BUS_PROPERTY("StaticHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_STATIC_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
assert(c);
r = sd_bus_add_object_vtable(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
return -EEXIST;
log_open();
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 = -EINVAL;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;