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 <polkit/polkit.h>
34 #include "hostnamed-gen.h"
35 #include "hostnamed.h"
37 #include "../../polkit-auth.h"
40 * (1) string to be matched against runtime machine's sysctl output.
41 * can be either the exact string or a substring contained
42 * within sysctl strings. no "guesses" here, a match should
43 * reliably indicate the chassis/icon.
45 * (2) string describing chassis type divulged by (1).
46 * must be one of "desktop", "laptop", "server",
47 * "tablet", "handset", "vm", "container" or NULL
48 * if only icon string can be ascertained. "vm" refers
49 * to operating systems running on baremetal hypervisors
50 * (hardware virtualization, like XEN) while "container"
51 * refers to OSs running on shared hypervisors like
52 * virtualbox or VMware. consider the distinction carefully
53 * as common virtualization software like KVM may share
54 * characteristics of both "vm" and "container" types.
56 * (3) string specifying icon to use. follows XDG icon spec.
57 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
58 * for allowed strings.
60 * (4) chassis precedence bit. TRUE if (2) is defined and
61 * we're certain it is the proper string. FALSE in the
62 * circumstance (2) may be the correct string, unless
63 * a match with this bit set to TRUE overrides it.
64 * if (2) is NULL, this bit is inconsequential.
66 * (5) icon precedence bit. see previous definition.
68 struct SYSCTL_LOOKUP_TABLE
{
72 gboolean chassis_precedence
;
73 gboolean icon_precedence
;
76 GPtrArray
*hostnamed_freeable
;
77 Hostname1
*hostnamed_interf
;
79 GMainLoop
*hostnamed_loop
;
82 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
84 gchar
*HOSTNAME
, *STATIC_HOSTNAME
, *PRETTY_HOSTNAME
;
85 gchar
*CHASSIS
, *ICON
;
86 gchar
*KERN_NAME
, *KERN_RELEASE
, *KERN_VERS
, *OS_CPENAME
;
88 /* TODO no specific vm or laptop icon in gnome
89 * NOTE paravirtualization on xen is only available for linuxes right now
90 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
91 * but we will worry about those later */
93 /* add any sysctl strings that suggest virtualization here */
94 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table
[] =
96 { "QEMU Virtual CPU", "vm", NULL
, FALSE
, FALSE
}, /* could be QEMU running in userspace or as part of KVM */
97 { "KVM", "vm", "drive-multidisk", FALSE
, FALSE
},
98 { "SmartDC HVM", "vm", "drive-multidisk", TRUE
, TRUE
}, /* illumos-joyent kvm */
99 { "VirtualBox", "vm", "drive-multidisk", TRUE
, TRUE
},
100 { "VMware, Inc.", "vm", "drive-multidisk", TRUE
, TRUE
},
101 { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE
, TRUE
},
102 { "Parallels", "vm", "drive-multidisk", TRUE
, TRUE
}, /* need verification */
103 { "Xen", "vm", "drive-multidisk", FALSE
, FALSE
}
104 }; /* TODO: chroots, etc. are the actual "containers", add them */
106 /* archs to check against when determining if machine is server */
107 const gchar
*server_archs
[] = {
113 /* --- begin method/property/dbus signal code --- */
115 /* TODO the extra boolean passed to these funcs is for policykit auth */
116 /* TODO complete call with error, message, etc */
118 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
119 GDBusMethodInvocation
*invoc
,
123 gchar
*proposed_hostname
, *valid_hostname_buf
;
124 gboolean policykit_auth
, ret
;
127 proposed_hostname
= NULL
;
130 params
= g_dbus_method_invocation_get_parameters(invoc
);
131 g_variant_get(params
, "(sb)", &proposed_hostname
, &policykit_auth
);
133 if(proposed_hostname
&& (valid_hostname_buf
= g_hostname_to_ascii(proposed_hostname
))) {
135 check_length
= strnlen(proposed_hostname
, MAXHOSTNAMELEN
+ 1);
137 if(check_length
> MAXHOSTNAMELEN
)
138 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
140 else if(sethostname(proposed_hostname
, check_length
))
141 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to change hostname.");
144 HOSTNAME
= proposed_hostname
;
145 hostname1_set_hostname(hn1_passed_interf
, HOSTNAME
);
147 hostname1_complete_set_hostname(hn1_passed_interf
, invoc
);
151 if(proposed_hostname
)
152 g_free(proposed_hostname
);
153 if(valid_hostname_buf
)
154 g_free(valid_hostname_buf
);
160 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
161 GDBusMethodInvocation
*invoc
,
168 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
169 GDBusMethodInvocation
*invoc
,
176 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
177 GDBusMethodInvocation
*invoc
,
184 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
185 GDBusMethodInvocation
*invoc
,
191 /* note: all hostnamed/hostname1's properties are read-only,
192 * and do not need set_ functions, gdbus-codegen realized
193 * this from the XML and handled the to-be error of trying
194 * to set a read-only property's value
201 hostname_buf
= (gchar
*)g_malloc0(MAXHOSTNAMELEN
);
203 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
204 return "localhost.home.network"; /* TODO bomb out here probably */
206 else if(!g_strcmp0(HOSTNAME
, hostname_buf
)) {
208 g_free(hostname_buf
);
212 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
213 HOSTNAME
= hostname_buf
;
214 hostname1_set_hostname(hostnamed_interf
, HOSTNAME
);
220 our_get_static_hostname() {
222 if(STATIC_HOSTNAME
&& g_strcmp0(STATIC_HOSTNAME
, ""))
223 return STATIC_HOSTNAME
;
227 return "localhost.home.network";
231 our_get_pretty_hostname() {
234 return PRETTY_HOSTNAME
;
245 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
249 our_get_icon_name() {
258 our_get_kernel_name() {
267 our_get_kernel_version() {
276 our_get_kernel_release() {
285 our_get_os_cpename() {
291 our_get_os_pretty_name() {
296 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
298 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
300 gpointer user_data
) {
302 g_printf("got bus/name, exporting %s's interface...\n", name
);
304 hostnamed_interf
= hostname1_skeleton_new();
306 /* attach function pointers to generated struct's method handlers */
307 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
308 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
309 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
310 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
311 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
313 /* set our properties before export */
314 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
315 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
316 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
317 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
318 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
319 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
320 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
321 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
322 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
323 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
325 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
327 "/org/freedesktop/hostname1",
330 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
331 hostnamed_mem_clean();
335 dbus_interface_exported
= TRUE
;
336 g_printf("exported %s's interface on the system bus...\n", name
);
340 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
342 gpointer user_data
) {
344 g_printf("success!\n");
347 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
349 gpointer user_data
) {
353 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
);
354 hostnamed_mem_clean();
357 g_printf("lost name %s, exiting...\n", name
);
359 hostnamed_mem_clean();
362 /* --- end bus/name handlers, begin misc unix functions --- */
364 /* safe call to clean and then exit
365 * this stops our GMainLoop safely before letting main() return */
366 void hostnamed_mem_clean() {
368 g_printf("exiting...\n");
370 if(dbus_interface_exported
)
371 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
373 if(g_main_loop_is_running(hostnamed_loop
))
374 g_main_loop_quit(hostnamed_loop
);
378 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
379 gboolean
unix_sig_terminate_handler(gpointer data
) {
381 g_printf("caught SIGINT/HUP/TERM, exiting\n");
383 hostnamed_mem_clean();
384 return G_SOURCE_REMOVE
;
387 void set_signal_handlers() {
389 /* we don't care about its descriptor, we never need to unregister these */
390 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
391 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
392 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
394 /* TODO: the "only once" guarantee only counts towards specific signals.
395 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
396 * to be called twice */
401 hostnamed_freeable
= g_ptr_array_new();
403 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
405 CHASSIS
= ICON
= OS_CPENAME
= 0;
406 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
407 HOSTNAME
= STATIC_HOSTNAME
= PRETTY_HOSTNAME
= NULL
;
411 set_signal_handlers();
413 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
416 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
418 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
419 "org.freedesktop.hostname1",
420 G_BUS_NAME_OWNER_FLAGS_NONE
,
421 hostnamed_on_bus_acquired
,
422 hostnamed_on_name_acquired
,
423 hostnamed_on_name_lost
,
427 g_main_loop_run(hostnamed_loop
);
428 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
429 g_main_loop_unref(hostnamed_loop
);
431 /* guaranteed unownable */
432 g_bus_unown_name(bus_descriptor
);
434 /* at this point no operations can occur with our data, it is safe to free it + its container */
435 g_ptr_array_free(hostnamed_freeable
, TRUE
);
440 gboolean
set_names() {
443 gchar
*hostname_buf
, *static_hostname_buf
, *pretty_hostname_buf
;
445 size_t hostname_divider
;
447 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
448 static_hostname_buf
= (gchar
*) g_malloc0(4096);
449 pretty_hostname_buf
= (gchar
*) g_malloc0(4096);
451 config
= g_key_file_new();
453 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
454 g_ptr_array_add(hostnamed_freeable
, static_hostname_buf
);
455 g_ptr_array_add(hostnamed_freeable
, pretty_hostname_buf
);
457 /* (2) set HOSTNAME */
458 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || !g_strcmp0(hostname_buf
, ""))
459 HOSTNAME
= "localhost";
461 HOSTNAME
= hostname_buf
;
463 /* this bit gets you the /etc/myname style hostname
464 hostname_divider = strcspn(hostname_buf, ".");
465 strncpy(ret, hostname_buf, hostname_divider); */
467 /* (3) set PRETTY_HOSTNAME */
468 if(g_key_file_load_from_file(config
, "/etc/systemd_compat.conf", G_KEY_FILE_NONE
, NULL
)
469 && (pretty_hostname_buf
= g_key_file_get_value(config
, "hostnamed", "PrettyHostname", NULL
)))
470 PRETTY_HOSTNAME
= pretty_hostname_buf
;
472 PRETTY_HOSTNAME
= "";
475 g_key_file_unref(config
);
477 /* (4) set STATIC_HOSTNAME */
478 if(!g_strcmp0(PRETTY_HOSTNAME
, ""))
479 STATIC_HOSTNAME
= HOSTNAME
;
481 else if((static_hostname_buf
= g_hostname_to_ascii(PRETTY_HOSTNAME
)))
482 STATIC_HOSTNAME
= static_hostname_buf
;
484 return (HOSTNAME
&& STATIC_HOSTNAME
&& PRETTY_HOSTNAME
) ? TRUE
: FALSE
;
488 gboolean
set_uname_properties() {
495 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
496 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
497 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
499 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
500 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
501 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
503 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
504 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
505 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
510 gboolean
determine_chassis_and_icon() {
512 const size_t bufsize
= 4096;
514 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
515 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
516 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
518 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
520 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
521 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
524 hwproduct
= (char*)g_malloc0(4096);
525 hwmodel
= (char*)g_malloc0(4096);
526 hwvendor
= (char*)g_malloc0(4096);
527 hwmachine
= (char*)g_malloc0(4096);
529 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
530 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
531 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
532 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
534 hwproduct_name
[0] = CTL_HW
;
535 hwproduct_name
[1] = HW_PRODUCT
;
537 hwmodel_name
[0] = CTL_HW
;
538 hwmodel_name
[1] = HW_MODEL
;
540 hwvendor_name
[0] = CTL_HW
;
541 hwvendor_name
[1] = HW_VENDOR
;
543 hwmachine_name
[0] = CTL_HW
;
544 hwmachine_name
[1] = HW_MACHINE
;
546 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
547 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
550 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
553 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
556 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
559 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
560 * probably move this code to vm test func and set a global after running it early, once */
562 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
563 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
564 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
565 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
567 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
569 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
570 CHASSIS
= chassis_indicator_table
[i
].chassis
;
573 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
575 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
576 ICON
= chassis_indicator_table
[i
].icon
;
581 if(up_native_is_laptop()) {
586 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
588 } else if(is_server(hwmachine
)) {
593 ICON
= "uninterruptible-power-supply";
595 } else if(!CHASSIS
|| !ICON
) {
603 return (CHASSIS
&& ICON
);
606 gboolean
is_server(gchar
*arch
) {
610 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
611 if(strcasestr(arch
, server_archs
[i
]))
617 gboolean
up_native_is_laptop() {
619 struct apm_power_info bstate
;
620 struct sensordev acpiac
;
622 if (up_native_get_sensordev("acpiac0", &acpiac
))
625 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
626 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
628 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
631 int up_apm_get_fd() {
633 static int apm_fd
= 0;
637 g_debug("apm_fd is not initialized yet, opening");
640 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
641 if(errno
!= ENXIO
&& errno
!= ENOENT
)
642 g_error("cannot open device file");
649 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
652 size_t sdlen
= sizeof(struct sensordev
);
653 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
655 for (devn
= 0 ; ; devn
++) {
657 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
664 if (!strcmp(snsrdev
->xname
, id
))