/***
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 "alloc-util.h"
#include "bus-util.h"
#include "def.h"
#include "env-util.h"
#include "fileio-label.h"
#include "hostname-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "selinux-util.h"
#include "strv.h"
#include "user-util.h"
#include "util.h"
#include "virt.h"
enum {
};
typedef struct Context {
} Context;
int p;
assert(c);
for (p = 0; p < _PROP_MAX; p++)
}
assert(c);
context_reset(c);
}
int r;
struct utsname u;
assert(c);
context_reset(c);
!c->data[PROP_KERNEL_VERSION])
return -ENOMEM;
if (!c->data[PROP_HOSTNAME])
return -ENOMEM;
if (r < 0 && r != -ENOENT)
return r;
NULL);
if (r < 0 && r != -ENOENT)
return r;
NULL);
if (r == -ENOENT)
NULL);
if (r < 0 && r != -ENOENT)
return r;
return 0;
}
return nulstr_contains(
"vm\0"
"container\0"
"desktop\0"
"laptop\0"
"server\0"
"tablet\0"
"handset\0"
"watch\0"
"embedded\0",
chassis);
}
}
static const char* fallback_chassis(void) {
int r;
char *type;
unsigned t;
int v;
v = detect_virtualization();
if (VIRTUALIZATION_IS_VM(v))
return "vm";
if (VIRTUALIZATION_IS_CONTAINER(v))
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 3.0 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:
case 0x1D:
return "server";
case 0x1E:
return "tablet";
}
return NULL;
}
const char *chassis;
assert(c);
chassis = fallback_chassis();
if (chassis)
return strdup("computer");
}
}
const char *static_hn;
const char *hn;
assert(c);
* has the highest preference ... */
if (hostname_is_useful(static_hn))
/* ... the transient host name, (ie: DHCP) comes next ... */
/* ... fallback to static "localhost.*" ignored above ... */
/* ... and the ultimate fallback */
else
hn = "localhost";
if (sethostname_idempotent(hn) < 0)
return -errno;
return 0;
}
assert(c);
return 0;
}
}
[PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
[PROP_ICON_NAME] = "ICON_NAME",
[PROP_CHASSIS] = "CHASSIS",
[PROP_DEPLOYMENT] = "DEPLOYMENT",
[PROP_LOCATION] = "LOCATION",
};
_cleanup_strv_free_ char **l = NULL;
int r, p;
assert(c);
if (r < 0 && r != -ENOENT)
return r;
for (p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) {
_cleanup_free_ char *t = NULL;
char **u;
strv_env_unset(l, name[p]);
continue;
}
if (!t)
return -ENOMEM;
u = strv_env_set(l, t);
if (!u)
return -ENOMEM;
strv_free(l);
l = u;
}
if (strv_isempty(l)) {
if (unlink("/etc/machine-info") < 0)
return 0;
}
return write_env_file_label("/etc/machine-info", l);
}
static int property_get_icon_name(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *n = NULL;
const char *name;
name = n = context_fallback_icon_name(c);
else
if (!name)
return -ENOMEM;
}
static int property_get_chassis(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
const char *name;
name = fallback_chassis();
else
}
const char *name;
int interactive;
char *h;
int r;
assert(m);
assert(c);
if (r < 0)
return r;
name = "localhost";
if (!hostname_is_valid(name, false))
return sd_bus_reply_method_return(m, NULL);
m,
"org.freedesktop.hostname1.set-hostname",
NULL,
&c->polkit_registry,
error);
if (r < 0)
return r;
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 -ENOMEM;
c->data[PROP_HOSTNAME] = h;
r = context_update_kernel_hostname(c);
if (r < 0) {
log_error_errno(r, "Failed to set host name: %m");
}
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
return sd_bus_reply_method_return(m, NULL);
}
const char *name;
int interactive;
int r;
assert(m);
assert(c);
if (r < 0)
return r;
return sd_bus_reply_method_return(m, NULL);
m,
"org.freedesktop.hostname1.set-static-hostname",
NULL,
&c->polkit_registry,
error);
if (r < 0)
return r;
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, false))
if (!h)
return -ENOMEM;
c->data[PROP_STATIC_HOSTNAME] = h;
}
r = context_update_kernel_hostname(c);
if (r < 0) {
log_error_errno(r, "Failed to set host name: %m");
}
if (r < 0) {
log_error_errno(r, "Failed to write static host name: %m");
}
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL);
return sd_bus_reply_method_return(m, NULL);
}
static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) {
int interactive;
const char *name;
int r;
assert(c);
assert(m);
if (r < 0)
return r;
return sd_bus_reply_method_return(m, NULL);
/* Since the pretty hostname should always be changed at the
* same time as the static one, use the same policy action for
* both... */
m,
prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
NULL,
&c->polkit_registry,
error);
if (r < 0)
return r;
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 */
if (!h)
return -ENOMEM;
}
r = context_write_data_machine_info(c);
if (r < 0) {
log_error_errno(r, "Failed to write machine info: %m");
}
log_info("Changed %s to '%s'",
(void) sd_bus_emit_properties_changed(
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
return sd_bus_reply_method_return(m, NULL);
}
}
}
}
}
}
SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
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),
SD_BUS_PROPERTY("Deployment", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Location", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("KernelName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KernelRelease", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_RELEASE, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_METHOD("SetStaticHostname", "sb", NULL, method_set_static_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
};
int r;
assert(c);
r = sd_bus_default_system(&bus);
if (r < 0)
return log_error_errno(r, "Failed to get system bus connection: %m");
r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
if (r < 0)
return log_error_errno(r, "Failed to register object: %m");
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
return 0;
}
int r;
log_open();
umask(0022);
mac_selinux_init("/etc");
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
r = sd_event_default(&event);
if (r < 0) {
log_error_errno(r, "Failed to allocate event loop: %m");
goto finish;
}
sd_event_set_watchdog(event, true);
if (r < 0)
goto finish;
r = context_read_data(&context);
if (r < 0) {
log_error_errno(r, "Failed to read hostname and machine information: %m");
goto finish;
}
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}