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"
38 * (1) string to be matched against runtime machine's sysctl output.
39 * can be either the exact string or a substring contained
40 * within sysctl strings. no "guesses" here, a match should
41 * reliably indicate the chassis/icon.
43 * (2) string describing chassis type divulged by (1).
44 * must be one of "desktop", "laptop", "server",
45 * "tablet", "handset", "vm", "container" or NULL
46 * if only icon string can be ascertained. "vm" refers
47 * to operating systems running on baremetal hypervisors
48 * (hardware virtualization, like XEN) while "container"
49 * refers to OSs running on shared hypervisors like
50 * virtualbox or VMware. consider the distinction carefully
51 * as common virtualization software like KVM may share
52 * characteristics of both "vm" and "container" types.
54 * (3) string specifying icon to use. follows XDG icon spec.
55 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
56 * for allowed strings.
58 * (4) chassis precedence bit. TRUE if (2) is defined and
59 * we're certain it is the proper string. FALSE in the
60 * circumstance (2) may be the correct string, unless
61 * a match with this bit set to TRUE overrides it.
62 * if (2) is NULL, this bit is inconsequential.
64 * (5) icon precedence bit. see previous definition.
66 struct SYSCTL_LOOKUP_TABLE
{
70 gboolean chassis_precedence
;
71 gboolean icon_precedence
;
74 GPtrArray
*hostnamed_freeable
;
75 Hostname1
*hostnamed_interf
;
77 GMainLoop
*hostnamed_loop
;
80 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
82 gchar
*HOSTNAME
, *STATIC_HOSTNAME
, *PRETTY_HOSTNAME
;
83 gchar
*CHASSIS
, *ICON
;
84 gchar
*KERN_NAME
, *KERN_RELEASE
, *KERN_VERS
, *OS_CPENAME
;
86 /* TODO no specific vm or laptop icon in gnome
87 * NOTE paravirtualization on xen is only available for linuxes right now
88 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
89 * but we will worry about those later */
91 /* add any sysctl strings that suggest virtualization here */
92 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table
[] =
94 { "QEMU Virtual CPU", "vm", NULL
, FALSE
, FALSE
}, /* could be QEMU running in userspace or as part of KVM */
95 { "KVM", "vm", "drive-multidisk", FALSE
, FALSE
},
96 { "SmartDC HVM", "vm", "drive-multidisk", TRUE
, TRUE
}, /* illumos-joyent kvm */
97 { "VirtualBox", "vm", "drive-multidisk", TRUE
, TRUE
},
98 { "VMware, Inc.", "vm", "drive-multidisk", TRUE
, TRUE
},
99 { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE
, TRUE
},
100 { "Parallels", "vm", "drive-multidisk", TRUE
, TRUE
}, /* need verification */
101 { "Xen", "vm", "drive-multidisk", FALSE
, FALSE
}
102 }; /* TODO: chroots, etc. are the actual "containers", add them */
104 /* archs to check against when determining if machine is server */
105 const gchar
*server_archs
[] = {
111 /* --- begin method/property/dbus signal code --- */
113 /* TODO the extra boolean passed to these funcs is for policykit auth */
114 /* TODO complete call with error, message, etc */
116 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
117 GDBusMethodInvocation
*invoc
,
121 gchar
*proposed_hostname
, *valid_hostname_buf
;
122 gboolean policykit_auth
, ret
;
123 size_t check_length
, bad_length
;
125 bad_length
= MAXHOSTNAMELEN
+ 1;
126 proposed_hostname
= NULL
;
129 params
= g_dbus_method_invocation_get_parameters(invoc
);
130 g_variant_get(params
, "(sb)", &proposed_hostname
, &policykit_auth
);
132 if(proposed_hostname
&& (valid_hostname_buf
= g_hostname_to_ascii(proposed_hostname
))) {
134 check_length
= strnlen(proposed_hostname
, bad_length
);
136 if(check_length
< bad_length
&& !sethostname(proposed_hostname
, check_length
))
140 hostname1_complete_set_hostname(hn1_passed_interf
, invoc
);
142 if(proposed_hostname
)
143 g_free(proposed_hostname
);
144 if(valid_hostname_buf
)
145 g_free(valid_hostname_buf
);
151 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
152 GDBusMethodInvocation
*invoc
,
159 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
160 GDBusMethodInvocation
*invoc
,
167 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
168 GDBusMethodInvocation
*invoc
,
175 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
176 GDBusMethodInvocation
*invoc
,
182 /* note: all hostnamed/hostname1's properties are read-only,
183 * and do not need set_ functions, gdbus-codegen realized
184 * this from the XML and handled the to-be error of trying
185 * to set a read-only property's value
191 /*gchar *hostname_buf, *ret;
192 size_t hostname_divider;
194 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
195 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
197 g_ptr_array_add(hostnamed_freeable, hostname_buf);
198 g_ptr_array_add(hostnamed_freeable, ret);
200 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0)
203 hostname_divider = strcspn(hostname_buf, ".");
205 return strncpy(ret, hostname_buf, hostname_divider);*/
214 our_get_static_hostname() {
216 /*const gchar *pretty_hostname;
219 pretty_hostname = our_get_pretty_hostname();
221 if(g_strcmp0(pretty_hostname, "") == 0)
222 ret = our_get_hostname();
224 else if((ret = g_hostname_to_ascii(pretty_hostname))) {
226 g_ptr_array_add(hostnamed_freeable, (gpointer)ret);
233 return STATIC_HOSTNAME
;
241 our_get_pretty_hostname() {
246 config = g_key_file_new();
248 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
249 && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { ret might need to be freed, docs dont specify but i am suspicious
251 g_key_file_unref(config);
261 return PRETTY_HOSTNAME
;
272 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
276 our_get_icon_name() {
285 our_get_kernel_name() {
294 our_get_kernel_version() {
303 our_get_kernel_release() {
312 our_get_os_cpename() {
318 our_get_os_pretty_name() {
323 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
325 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
327 gpointer user_data
) {
329 g_printf("got bus/name, exporting %s's interface...\n", name
);
331 hostnamed_interf
= hostname1_skeleton_new();
333 /* attach function pointers to generated struct's method handlers */
334 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
335 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
336 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
337 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
338 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
340 /* set our properties before export */
341 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
342 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
343 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
344 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
345 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
346 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
347 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
348 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
349 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
350 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
352 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
354 "/org/freedesktop/hostname1",
357 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
358 hostnamed_mem_clean();
362 dbus_interface_exported
= TRUE
;
363 g_printf("exported %s's interface on the system bus...\n", name
);
367 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
369 gpointer user_data
) {
371 g_printf("success!\n");
374 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
376 gpointer user_data
) {
380 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
);
381 hostnamed_mem_clean();
384 g_printf("lost name %s, exiting...\n", name
);
386 hostnamed_mem_clean();
389 /* --- end bus/name handlers, begin misc unix functions --- */
391 /* safe call to clean and then exit
392 * this stops our GMainLoop safely before letting main() return */
393 void hostnamed_mem_clean() {
395 g_printf("exiting...\n");
397 if(dbus_interface_exported
)
398 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
400 if(g_main_loop_is_running(hostnamed_loop
))
401 g_main_loop_quit(hostnamed_loop
);
405 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
406 gboolean
unix_sig_terminate_handler(gpointer data
) {
408 g_printf("caught SIGINT/HUP/TERM, exiting\n");
410 hostnamed_mem_clean();
411 return G_SOURCE_REMOVE
;
414 void set_signal_handlers() {
416 /* we don't care about its descriptor, we never need to unregister these */
417 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
418 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
419 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
421 /* TODO: the "only once" guarantee only counts towards specific signals.
422 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
423 * to be called twice */
428 hostnamed_freeable
= g_ptr_array_new();
430 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
432 CHASSIS
= ICON
= OS_CPENAME
= 0;
433 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
435 set_signal_handlers();
437 if(!determine_chassis_and_icon() || !set_uname_properties())
440 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
442 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
443 "org.freedesktop.hostname1",
444 G_BUS_NAME_OWNER_FLAGS_NONE
,
445 hostnamed_on_bus_acquired
,
446 hostnamed_on_name_acquired
,
447 hostnamed_on_name_lost
,
451 g_main_loop_run(hostnamed_loop
);
452 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
453 g_main_loop_unref(hostnamed_loop
);
455 /* guaranteed unownable */
456 g_bus_unown_name(bus_descriptor
);
458 /* at this point no operations can occur with our data, it is safe to free it + its container */
459 g_ptr_array_free(hostnamed_freeable
, TRUE
);
464 gboolean
set_uname_properties() {
471 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
472 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
473 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
475 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
476 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
477 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
479 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
480 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
481 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
486 gboolean
determine_chassis_and_icon() {
488 const size_t bufsize
= 4096;
490 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
491 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
492 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
494 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
496 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
497 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
500 hwproduct
= (char*)g_malloc0(4096);
501 hwmodel
= (char*)g_malloc0(4096);
502 hwvendor
= (char*)g_malloc0(4096);
503 hwmachine
= (char*)g_malloc0(4096);
505 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
506 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
507 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
508 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
510 hwproduct_name
[0] = CTL_HW
;
511 hwproduct_name
[1] = HW_PRODUCT
;
513 hwmodel_name
[0] = CTL_HW
;
514 hwmodel_name
[1] = HW_MODEL
;
516 hwvendor_name
[0] = CTL_HW
;
517 hwvendor_name
[1] = HW_VENDOR
;
519 hwmachine_name
[0] = CTL_HW
;
520 hwmachine_name
[1] = HW_MACHINE
;
522 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
523 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
526 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
529 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
532 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
535 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
536 * probably move this code to vm test func and set a global after running it early, once */
538 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
539 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
540 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
541 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
543 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
545 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
546 CHASSIS
= chassis_indicator_table
[i
].chassis
;
549 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
551 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
552 ICON
= chassis_indicator_table
[i
].icon
;
557 if(up_native_is_laptop()) {
562 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
564 } else if(is_server(hwmachine
)) {
569 ICON
= "uninterruptible-power-supply";
571 } else if(!CHASSIS
|| !ICON
) {
579 return (CHASSIS
&& ICON
);
582 gboolean
is_server(gchar
*arch
) {
586 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
587 if(strcasestr(arch
, server_archs
[i
]))
593 gboolean
up_native_is_laptop() {
595 struct apm_power_info bstate
;
596 struct sensordev acpiac
;
598 if (up_native_get_sensordev("acpiac0", &acpiac
))
601 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
602 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
604 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
607 int up_apm_get_fd() {
609 static int apm_fd
= 0;
613 g_debug("apm_fd is not initialized yet, opening");
616 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
617 if(errno
!= ENXIO
&& errno
!= ENOENT
)
618 g_error("cannot open device file");
625 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
628 size_t sdlen
= sizeof(struct sensordev
);
629 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
631 for (devn
= 0 ; ; devn
++) {
633 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
640 if (!strcmp(snsrdev
->xname
, id
))