hostnamed.c revision 6c8ea34fd945103e2f998403fea68bb0006e8057
/*-*- 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 "def.h"
#include "virt.h"
#include "env-util.h"
#include "fileio-label.h"
#include "label.h"
#include "bus-util.h"
#include "event-util.h"
enum {
};
typedef struct Context {
} Context;
static void context_reset(Context *c) {
int p;
assert(c);
for (p = 0; p < _PROP_MAX; p++) {
}
}
assert(c);
context_reset(c);
}
static int context_read_data(Context *c) {
int r;
assert(c);
context_reset(c);
if (!c->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* context_fallback_icon_name(Context *c) {
const char *chassis;
assert(c);
chassis = fallback_chassis();
if (chassis)
return strdup("computer");
}
static int context_write_data_hostname(Context *c) {
const char *hn;
assert(c);
hn = "localhost";
else
return -errno;
return 0;
}
static int context_write_data_static_hostname(Context *c) {
assert(c);
return 0;
}
}
static int context_write_data_other(Context *c) {
[PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
[PROP_ICON_NAME] = "ICON_NAME",
[PROP_CHASSIS] = "CHASSIS"
};
char **l = NULL;
int r, p;
assert(c);
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 property_get_icon_name(
const char *path,
const char *interface,
const char *property,
void *userdata) {
_cleanup_free_ char *n = NULL;
const char *name;
int r;
name = n = context_fallback_icon_name(c);
else
if (!name)
return -ENOMEM;
if (r < 0)
return r;
return 1;
}
static int property_get_chassis(
const char *path,
const char *interface,
const char *property,
void *userdata) {
const char *name;
int r;
name = fallback_chassis();
else
if (r < 0)
return r;
return 1;
}
const char *name;
unsigned interactive;
char *h;
int r;
if (r < 0)
name = "localhost";
if (!hostname_is_valid(name))
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);
if (r < 0)
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (!h)
return log_oom();
c->data[PROP_HOSTNAME] = h;
r = context_write_data_hostname(c);
if (r < 0) {
}
sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
}
const char *name;
unsigned interactive;
int r;
if (r < 0)
r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-static-hostname", interactive, &error, method_set_static_hostname, c);
if (r < 0)
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
} else {
char *h;
if (!hostname_is_valid(name))
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
if (!h)
return log_oom();
c->data[PROP_STATIC_HOSTNAME] = h;
}
if (r < 0) {
}
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;
int r;
assert(c);
assert(m);
if (r < 0)
/* 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)
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
} else {
char *h;
/* The icon name might ultimately be used as file
* name, so better be safe than sorry */
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
if (prop == PROP_PRETTY_HOSTNAME &&
return sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
if (!h)
return log_oom();
}
r = context_write_data_other(c);
if (r < 0) {
}
log_info("Changed %s to '%s'",
}
}
}
}
static const sd_bus_vtable hostname_vtable[] = {
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),
};
int r;
assert(c);
r = sd_bus_open_system(&bus);
if (r < 0) {
return r;
}
r = sd_bus_add_object_vtable(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r != SD_BUS_NAME_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
return -EEXIST;
}
if (r < 0) {
return r;
}
return 0;
}
int r;
log_open();
umask(0022);
label_init("/etc");
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!");
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
r = sd_event_new(&event);
if (r < 0) {
goto finish;
}
if (r < 0)
goto finish;
r = context_read_data(&context);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
r = 0;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}