2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
25 #include <glib/gprintf.h>
26 #include <glib-unix.h>
27 /* #include <gtk/gtk.h> */
29 #include "hostnamed-gen.h"
30 #include "hostnamed.h"
32 /* add any sysctl strings that suggest virtualization here */
34 * (1) string to be matched against runtime machine's sysctl output.
35 * can be either the exact string or a substring contained
36 * within sysctl strings. no "guesses" here, a match should
37 * reliably indicate the chassis/icon.
39 * (2) string describing chassis type divulged by (1).
40 * must be one of "desktop", "laptop", "server",
41 * "tablet", "handset", "vm", "container" or NULL
42 * if only icon string can be ascertained. "vm" refers
43 * to operating systems running on baremetal hypervisors
44 * (hardware virtualization, like XEN) while "container"
45 * refers to OSs running on shared hypervisors like
46 * virtualbox or VMware. consider the distinction carefully
47 * as common virtualization software like KVM may share
48 * characteristics of both "vm" and "container" types.
50 * (3) string specifying icon to use. follows XDG icon spec.
51 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
52 * for allowed strings.
54 * (4) chassis precedence bit. TRUE if (2) is defined and
55 * we're certain it is the proper string. FALSE in the
56 * circumstance (2) may be the correct string, unless
57 * a match with this bit set to TRUE overrides it.
58 * if (2) is NULL, this bit is inconsequential.
60 * (5) icon precedence bit. see previous definition.
62 struct SYSCTL_LOOKUP_TABLE
{
66 gboolean chassis_precedence
;
67 gboolean icon_precedence
;
70 GPtrArray
*hostnamed_freeable
;
71 Hostname1
*hostnamed_interf
;
73 GMainLoop
*hostnamed_loop
;
76 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
78 gchar
*CHASSIS
, *ICON
;
80 /* TODO no specific vm or laptop icon in gnome
81 * NOTE paravirtualization on xen is only available for linuxes right now
82 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
83 * but we will worry about those later */
84 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table
[] =
86 { "QEMU Virtual CPU", "container", "drive-optical", FALSE
, FALSE
}, /* could be QEMU running in userspace or as part of KVM */
87 { "SmartDC HVM", "vm", "drive-multidisk", TRUE
, TRUE
}, /* oracle solaris kvm */
88 { "VirtualBox", "container", "drive-optical", TRUE
, TRUE
},
89 { "VMware, Inc.", "container", "drive-optical", TRUE
, TRUE
},
90 { "VMware Virtual Platform", "container", "drive-optical", TRUE
, TRUE
},
91 { "Parallels", "container", "drive-optical", TRUE
, TRUE
} /* need verification */
94 /*const gchar* vmstring_list[] = {
101 /* --- begin method/property/dbus signal code --- */
104 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
105 GDBusMethodInvocation
*invoc
,
112 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
113 GDBusMethodInvocation
*invoc
,
120 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
121 GDBusMethodInvocation
*invoc
,
128 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
129 GDBusMethodInvocation
*invoc
,
136 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
137 GDBusMethodInvocation
*invoc
,
143 /* note: all hostnamed/hostname1's properties are read-only,
144 * and do not need set_ functions, gdbus-codegen realized
145 * this from the XML and handled the to-be error of trying
146 * to set a read-only property's value
152 gchar
*hostname_buf
, *ret
;
153 size_t hostname_divider
;
155 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
156 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
158 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
159 g_ptr_array_add(hostnamed_freeable
, ret
);
161 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || g_strcmp0(hostname_buf
, "") == 0)
164 hostname_divider
= strcspn(hostname_buf
, ".");
166 return strncpy(ret
, hostname_buf
, hostname_divider
);
170 our_get_static_hostname() {
172 const gchar
*pretty_hostname
;
175 pretty_hostname
= our_get_pretty_hostname();
177 if(g_strcmp0(pretty_hostname
, "") == 0)
178 ret
= our_get_hostname();
180 else if((ret
= g_hostname_to_ascii(pretty_hostname
))) {
182 g_ptr_array_add(hostnamed_freeable
, (gpointer
)ret
);
190 our_get_pretty_hostname() {
195 config
= g_key_file_new();
197 if(g_key_file_load_from_file(config
, "/etc/systemd_compat.conf", G_KEY_FILE_NONE
, NULL
)
198 && (ret
= g_key_file_get_value(config
, "hostnamed", "PrettyHostname", NULL
))) { /* ret might need to be freed, docs dont specify but i am suspicious */
200 g_key_file_unref(config
);
218 our_get_icon_name() {
224 our_get_kernel_name() {
230 our_get_kernel_version() {
236 our_get_kernel_release() {
242 our_get_os_cpename() {
248 our_get_os_pretty_name() {
253 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
255 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
257 gpointer user_data
) {
259 g_printf("got bus/name, exporting %s's interface...\n", name
);
261 hostnamed_interf
= hostname1_skeleton_new();
263 /* attach function pointers to generated struct's method handlers */
264 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
265 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
266 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
267 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
268 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
270 /* set our properties before export */
271 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
272 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
273 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
274 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
275 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
276 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
277 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
278 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
279 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
280 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
282 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
284 "/org/freedesktop/hostname1",
287 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
288 hostnamed_mem_clean();
292 dbus_interface_exported
= TRUE
;
293 g_printf("exported %s's interface on the system bus...\n", name
);
297 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
299 gpointer user_data
) {
301 g_printf("success!\n");
304 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
306 gpointer user_data
) {
310 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name
);
311 hostnamed_mem_clean();
314 g_printf("lost name %s, exiting...\n", name
);
316 hostnamed_mem_clean();
319 /* --- end bus/name handlers, begin misc unix functions --- */
321 /* safe call to clean and then exit
322 * this stops our GMainLoop safely before letting main() return */
323 void hostnamed_mem_clean() {
325 g_printf("exiting...\n");
327 if(dbus_interface_exported
)
328 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
330 if(g_main_loop_is_running(hostnamed_loop
))
331 g_main_loop_quit(hostnamed_loop
);
335 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
336 gboolean
unix_sig_terminate_handler(gpointer data
) {
338 g_printf("caught SIGINT/HUP/TERM, exiting\n");
340 hostnamed_mem_clean();
341 return G_SOURCE_REMOVE
;
344 void set_signal_handlers() {
346 /* we don't care about its descriptor, we never need to unregister these */
347 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
348 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
349 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
351 /* TODO: the "only once" guarantee only counts towards specific signals.
352 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
353 * to be called twice */
358 set_signal_handlers();
360 if(!build_chassis_table() || !determine_chassis_and_icon())
363 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
364 hostnamed_freeable
= g_ptr_array_new();
366 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
367 "org.freedesktop.hostname1",
368 G_BUS_NAME_OWNER_FLAGS_NONE
,
369 hostnamed_on_bus_acquired
,
370 hostnamed_on_name_acquired
,
371 hostnamed_on_name_lost
,
375 g_main_loop_run(hostnamed_loop
);
376 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
377 g_main_loop_unref(hostnamed_loop
);
379 /* guaranteed unownable */
380 g_bus_unown_name(bus_descriptor
);
382 /* at this point no operations can occur with our data, it is safe to free it + its container */
383 g_ptr_array_free(hostnamed_freeable
, TRUE
);
388 static gboolean
build_chassis_table() {
392 gboolean
determine_chassis_and_icon() {
394 char *hwproduct
, *hwmodel
, *hwvendor
;
395 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
;
396 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2];
399 hwproduct_name
[0] = CTL_HW
;
400 hwproduct_name
[1] = HW_PRODUCT
;
402 hwmodel_name
[0] = CTL_HW
;
403 hwmodel_name
[1] = HW_MODEL
;
405 hwvendor_name
[0] = CTL_HW
;
406 hwvendor_name
[1] = HW_VENDOR
;
408 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
409 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
412 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
415 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
418 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
419 * probably move this code to vm test func and set a global after running it early, once */
421 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++)
423 /* if(strcasestr(sysctl_string, vmstring_list[i]))
424 return (CHASSIS = ) ? TRUE : FALSE; */
428 return TRUE
; /* temp */
431 /* TODO figure out DMI variables on obsd */
432 /*static gchar *guess_icon_name() {
434 gchar *filebuf = NULL;
437 #if defined(__i386__) || defined(__x86_64__)
439 Taken with a few minor changes from systemd's hostnamed.c,
440 copyright 2011 Lennart Poettering.
442 See the SMBIOS Specification 2.7.1 section 7.4.1 for
443 details about the values listed here:
445 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
448 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
449 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
455 ret = g_strdup ("computer-desktop");
460 ret = g_strdup ("computer-laptop");
466 ret = g_strdup ("computer-server");
471 ret = g_strdup ("computer");