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.
22 #include <sys/param.h>
23 #include <sys/sysctl.h>
24 #include <sys/sensors.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
28 #include <machine/apmvar.h>
30 #include <glib/gprintf.h>
31 #include <glib-unix.h>
32 /* #include <gtk/gtk.h> */
34 #include "hostnamed-gen.h"
35 #include "hostnamed.h"
37 /* add any sysctl strings that suggest virtualization here */
39 * (1) string to be matched against runtime machine's sysctl output.
40 * can be either the exact string or a substring contained
41 * within sysctl strings. no "guesses" here, a match should
42 * reliably indicate the chassis/icon.
44 * (2) string describing chassis type divulged by (1).
45 * must be one of "desktop", "laptop", "server",
46 * "tablet", "handset", "vm", "container" or NULL
47 * if only icon string can be ascertained. "vm" refers
48 * to operating systems running on baremetal hypervisors
49 * (hardware virtualization, like XEN) while "container"
50 * refers to OSs running on shared hypervisors like
51 * virtualbox or VMware. consider the distinction carefully
52 * as common virtualization software like KVM may share
53 * characteristics of both "vm" and "container" types.
55 * (3) string specifying icon to use. follows XDG icon spec.
56 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
57 * for allowed strings.
59 * (4) chassis precedence bit. TRUE if (2) is defined and
60 * we're certain it is the proper string. FALSE in the
61 * circumstance (2) may be the correct string, unless
62 * a match with this bit set to TRUE overrides it.
63 * if (2) is NULL, this bit is inconsequential.
65 * (5) icon precedence bit. see previous definition.
67 struct SYSCTL_LOOKUP_TABLE
{
71 gboolean chassis_precedence
;
72 gboolean icon_precedence
;
75 GPtrArray
*hostnamed_freeable
;
76 Hostname1
*hostnamed_interf
;
78 GMainLoop
*hostnamed_loop
;
81 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
83 gchar
*CHASSIS
, *ICON
;
85 gchar
*KERN_NAME
, *KERN_RELEASE
, *KERN_VERS
;
87 /* TODO no specific vm or laptop icon in gnome
88 * NOTE paravirtualization on xen is only available for linuxes right now
89 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
90 * but we will worry about those later */
91 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table
[] =
93 { "QEMU Virtual CPU", "vm", NULL
, FALSE
, FALSE
}, /* could be QEMU running in userspace or as part of KVM */
94 { "KVM", "vm", "drive-multidisk", FALSE
, FALSE
},
95 { "SmartDC HVM", "vm", "drive-multidisk", TRUE
, TRUE
}, /* illumos-joyent kvm */
96 { "VirtualBox", "vm", "drive-multidisk", TRUE
, TRUE
},
97 { "VMware, Inc.", "vm", "drive-multidisk", TRUE
, TRUE
},
98 { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE
, TRUE
},
99 { "Parallels", "vm", "drive-multidisk", TRUE
, TRUE
}, /* need verification */
100 { "Xen", "vm", "drive-multidisk", TRUE
, TRUE
}
101 }; /* TODO: chroots, etc. are the actual "containers", add them */
103 /* archs to check against when determining if machine is server */
104 const gchar
*server_archs
[] = {
110 /* --- begin method/property/dbus signal code --- */
113 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
114 GDBusMethodInvocation
*invoc
,
121 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
122 GDBusMethodInvocation
*invoc
,
129 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
130 GDBusMethodInvocation
*invoc
,
137 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
138 GDBusMethodInvocation
*invoc
,
145 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
146 GDBusMethodInvocation
*invoc
,
152 /* note: all hostnamed/hostname1's properties are read-only,
153 * and do not need set_ functions, gdbus-codegen realized
154 * this from the XML and handled the to-be error of trying
155 * to set a read-only property's value
161 gchar
*hostname_buf
, *ret
;
162 size_t hostname_divider
;
164 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
165 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
167 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
168 g_ptr_array_add(hostnamed_freeable
, ret
);
170 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || g_strcmp0(hostname_buf
, "") == 0)
173 hostname_divider
= strcspn(hostname_buf
, ".");
175 return strncpy(ret
, hostname_buf
, hostname_divider
);
179 our_get_static_hostname() {
181 const gchar
*pretty_hostname
;
184 pretty_hostname
= our_get_pretty_hostname();
186 if(g_strcmp0(pretty_hostname
, "") == 0)
187 ret
= our_get_hostname();
189 else if((ret
= g_hostname_to_ascii(pretty_hostname
))) {
191 g_ptr_array_add(hostnamed_freeable
, (gpointer
)ret
);
199 our_get_pretty_hostname() {
204 config
= g_key_file_new();
206 if(g_key_file_load_from_file(config
, "/etc/systemd_compat.conf", G_KEY_FILE_NONE
, NULL
)
207 && (ret
= g_key_file_get_value(config
, "hostnamed", "PrettyHostname", NULL
))) { /* ret might need to be freed, docs dont specify but i am suspicious */
209 g_key_file_unref(config
);
229 our_get_icon_name() {
238 our_get_kernel_name() {
247 our_get_kernel_version() {
256 our_get_kernel_release() {
265 our_get_os_cpename() {
271 our_get_os_pretty_name() {
276 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
278 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
280 gpointer user_data
) {
282 g_printf("got bus/name, exporting %s's interface...\n", name
);
284 hostnamed_interf
= hostname1_skeleton_new();
286 /* attach function pointers to generated struct's method handlers */
287 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
288 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
289 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
290 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
291 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
293 /* set our properties before export */
294 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
295 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
296 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
297 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
298 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
299 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
300 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
301 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
302 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
303 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
305 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
307 "/org/freedesktop/hostname1",
310 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
311 hostnamed_mem_clean();
315 dbus_interface_exported
= TRUE
;
316 g_printf("exported %s's interface on the system bus...\n", name
);
320 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
322 gpointer user_data
) {
324 g_printf("success!\n");
327 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
329 gpointer user_data
) {
333 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
);
334 hostnamed_mem_clean();
337 g_printf("lost name %s, exiting...\n", name
);
339 hostnamed_mem_clean();
342 /* --- end bus/name handlers, begin misc unix functions --- */
344 /* safe call to clean and then exit
345 * this stops our GMainLoop safely before letting main() return */
346 void hostnamed_mem_clean() {
348 g_printf("exiting...\n");
350 if(dbus_interface_exported
)
351 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
353 if(g_main_loop_is_running(hostnamed_loop
))
354 g_main_loop_quit(hostnamed_loop
);
358 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
359 gboolean
unix_sig_terminate_handler(gpointer data
) {
361 g_printf("caught SIGINT/HUP/TERM, exiting\n");
363 hostnamed_mem_clean();
364 return G_SOURCE_REMOVE
;
367 void set_signal_handlers() {
369 /* we don't care about its descriptor, we never need to unregister these */
370 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
371 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
372 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
374 /* TODO: the "only once" guarantee only counts towards specific signals.
375 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
376 * to be called twice */
381 hostnamed_freeable
= g_ptr_array_new();
383 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
385 CHASSIS
= ICON
= OS_CPENAME
= 0;
386 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
388 set_signal_handlers();
390 if(!determine_chassis_and_icon() || !set_uname_properties())
393 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
395 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
396 "org.freedesktop.hostname1",
397 G_BUS_NAME_OWNER_FLAGS_NONE
,
398 hostnamed_on_bus_acquired
,
399 hostnamed_on_name_acquired
,
400 hostnamed_on_name_lost
,
404 g_main_loop_run(hostnamed_loop
);
405 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
406 g_main_loop_unref(hostnamed_loop
);
408 /* guaranteed unownable */
409 g_bus_unown_name(bus_descriptor
);
411 /* at this point no operations can occur with our data, it is safe to free it + its container */
412 g_ptr_array_free(hostnamed_freeable
, TRUE
);
417 gboolean
set_uname_properties() {
424 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
425 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
426 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
428 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
429 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
430 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
432 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
433 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
434 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
439 gboolean
determine_chassis_and_icon() {
441 const size_t bufsize
= 4096;
443 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
444 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
445 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
447 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
449 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
450 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
453 hwproduct
= (char*)g_malloc0(4096);
454 hwmodel
= (char*)g_malloc0(4096);
455 hwvendor
= (char*)g_malloc0(4096);
456 hwmachine
= (char*)g_malloc0(4096);
458 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
459 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
460 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
461 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
463 hwproduct_name
[0] = CTL_HW
;
464 hwproduct_name
[1] = HW_PRODUCT
;
466 hwmodel_name
[0] = CTL_HW
;
467 hwmodel_name
[1] = HW_MODEL
;
469 hwvendor_name
[0] = CTL_HW
;
470 hwvendor_name
[1] = HW_VENDOR
;
472 hwmachine_name
[0] = CTL_HW
;
473 hwmachine_name
[1] = HW_MACHINE
;
475 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
476 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
479 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
482 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
485 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
488 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
489 * probably move this code to vm test func and set a global after running it early, once */
491 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
492 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
493 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
494 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
496 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
498 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
499 CHASSIS
= chassis_indicator_table
[i
].chassis
;
502 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
504 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
505 ICON
= chassis_indicator_table
[i
].icon
;
510 if(up_native_is_laptop()) {
515 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
517 } else if(is_server(hwmachine
)) {
522 ICON
= "uninterruptible-power-supply";
524 } else if(!CHASSIS
|| !ICON
) {
532 return (CHASSIS
&& ICON
);
535 gboolean
is_server(gchar
*arch
) {
539 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
540 if(strcasestr(arch
, server_archs
[i
]))
546 gboolean
up_native_is_laptop() {
548 struct apm_power_info bstate
;
549 struct sensordev acpiac
;
551 if (up_native_get_sensordev("acpiac0", &acpiac
))
554 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
555 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
557 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
560 int up_apm_get_fd() {
562 static int apm_fd
= 0;
566 g_debug("apm_fd is not initialized yet, opening");
569 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
570 if(errno
!= ENXIO
&& errno
!= ENOENT
)
571 g_error("cannot open device file");
578 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
581 size_t sdlen
= sizeof(struct sensordev
);
582 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
584 for (devn
= 0 ; ; devn
++) {
586 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
593 if (!strcmp(snsrdev
->xname
, id
))