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 free some strings here */
117 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
118 GDBusMethodInvocation
*invoc
,
122 gchar
*proposed_hostname
, *valid_hostname_buf
;
123 const gchar
*bus_name
;
124 gboolean policykit_auth
, ret
, try_to_set
;
126 check_auth_result is_authed
;
128 proposed_hostname
= NULL
;
129 ret
= try_to_set
= FALSE
;
131 params
= g_dbus_method_invocation_get_parameters(invoc
);
132 g_variant_get(params
, "(sb)", &proposed_hostname
, &policykit_auth
);
133 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
135 /* verify caller has correct permissions via polkit */
136 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.SetHostname", policykit_auth
);
140 case AUTHORIZED_NATIVELY
:
141 case AUTHORIZED_BY_PROMPT
:
145 case UNAUTHORIZED_NATIVELY
:
146 case UNAUTHORIZED_FAILED_PROMPT
:
147 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set hostname.");
151 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
154 case ERROR_BADACTION
:
155 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
160 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
164 /* verify passed hostname's validity */
165 if(try_to_set
&& proposed_hostname
&& (valid_hostname_buf
= g_hostname_to_ascii(proposed_hostname
))) {
167 check_length
= strnlen(valid_hostname_buf
, MAXHOSTNAMELEN
+ 1);
169 if(check_length
> MAXHOSTNAMELEN
) {
171 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
172 g_free(valid_hostname_buf
);
174 } else if(sethostname(proposed_hostname
, check_length
)) {
176 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
177 g_free(valid_hostname_buf
);
181 HOSTNAME
= valid_hostname_buf
;
182 hostname1_set_hostname(hn1_passed_interf
, HOSTNAME
);
183 g_ptr_array_add(hostnamed_freeable
, valid_hostname_buf
);
185 hostname1_complete_set_hostname(hn1_passed_interf
, invoc
);
193 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
194 GDBusMethodInvocation
*invoc
,
201 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
202 GDBusMethodInvocation
*invoc
,
209 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
210 GDBusMethodInvocation
*invoc
,
217 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
218 GDBusMethodInvocation
*invoc
,
224 /* note: all hostnamed/hostname1's properties are read-only,
225 * and do not need set_ functions, gdbus-codegen realized
226 * this from the XML and handled the to-be error of trying
227 * to set a read-only property's value
234 hostname_buf
= (gchar
*)g_malloc0(MAXHOSTNAMELEN
);
236 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
237 return "localhost.home.network"; /* TODO bomb out here probably */
239 else if(!g_strcmp0(HOSTNAME
, hostname_buf
)) {
241 g_free(hostname_buf
);
245 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
246 HOSTNAME
= hostname_buf
;
247 hostname1_set_hostname(hostnamed_interf
, HOSTNAME
);
253 our_get_static_hostname() {
255 if(STATIC_HOSTNAME
&& g_strcmp0(STATIC_HOSTNAME
, ""))
256 return STATIC_HOSTNAME
;
260 return "localhost.home.network";
264 our_get_pretty_hostname() {
267 return PRETTY_HOSTNAME
;
278 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
282 our_get_icon_name() {
291 our_get_kernel_name() {
300 our_get_kernel_version() {
309 our_get_kernel_release() {
318 our_get_os_cpename() {
324 our_get_os_pretty_name() {
329 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
331 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
333 gpointer user_data
) {
335 g_printf("got bus/name, exporting %s's interface...\n", name
);
337 hostnamed_interf
= hostname1_skeleton_new();
339 /* attach function pointers to generated struct's method handlers */
340 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
341 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
342 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
343 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
344 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
346 /* set our properties before export */
347 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
348 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
349 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
350 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
351 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
352 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
353 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
354 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
355 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
356 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
358 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
360 "/org/freedesktop/hostname1",
363 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
364 hostnamed_mem_clean();
368 dbus_interface_exported
= TRUE
;
369 g_printf("exported %s's interface on the system bus...\n", name
);
373 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
375 gpointer user_data
) {
377 g_printf("success!\n");
380 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
382 gpointer user_data
) {
386 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
);
387 hostnamed_mem_clean();
390 g_printf("lost name %s, exiting...\n", name
);
392 hostnamed_mem_clean();
395 /* --- end bus/name handlers, begin misc unix functions --- */
397 /* safe call to clean and then exit
398 * this stops our GMainLoop safely before letting main() return */
399 void hostnamed_mem_clean() {
401 g_printf("exiting...\n");
403 if(dbus_interface_exported
)
404 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
406 if(g_main_loop_is_running(hostnamed_loop
))
407 g_main_loop_quit(hostnamed_loop
);
411 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
412 gboolean
unix_sig_terminate_handler(gpointer data
) {
414 g_printf("caught SIGINT/HUP/TERM, exiting\n");
416 hostnamed_mem_clean();
417 return G_SOURCE_REMOVE
;
420 void set_signal_handlers() {
422 /* we don't care about its descriptor, we never need to unregister these */
423 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
424 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
425 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
427 /* TODO: the "only once" guarantee only counts towards specific signals.
428 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
429 * to be called twice */
434 hostnamed_freeable
= g_ptr_array_new();
436 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
438 CHASSIS
= ICON
= OS_CPENAME
= 0;
439 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
440 HOSTNAME
= STATIC_HOSTNAME
= PRETTY_HOSTNAME
= NULL
;
442 set_signal_handlers();
444 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
447 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
449 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
450 "org.freedesktop.hostname1",
451 G_BUS_NAME_OWNER_FLAGS_NONE
,
452 hostnamed_on_bus_acquired
,
453 hostnamed_on_name_acquired
,
454 hostnamed_on_name_lost
,
458 g_main_loop_run(hostnamed_loop
);
459 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
460 g_main_loop_unref(hostnamed_loop
);
462 /* guaranteed unownable */
463 g_bus_unown_name(bus_descriptor
);
465 /* at this point no operations can occur with our data, it is safe to free it + its container */
466 g_ptr_array_free(hostnamed_freeable
, TRUE
);
471 gboolean
set_names() {
474 gchar
*hostname_buf
, *static_hostname_buf
, *pretty_hostname_buf
;
476 size_t hostname_divider
;
478 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
479 static_hostname_buf
= (gchar
*) g_malloc0(4096);
480 pretty_hostname_buf
= (gchar
*) g_malloc0(4096);
482 config
= g_key_file_new();
484 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
485 g_ptr_array_add(hostnamed_freeable
, static_hostname_buf
);
486 g_ptr_array_add(hostnamed_freeable
, pretty_hostname_buf
);
488 /* (2) set HOSTNAME */
489 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || !g_strcmp0(hostname_buf
, ""))
490 HOSTNAME
= "localhost";
492 HOSTNAME
= hostname_buf
;
494 /* this bit gets you the /etc/myname style hostname
495 hostname_divider = strcspn(hostname_buf, ".");
496 strncpy(ret, hostname_buf, hostname_divider); */
498 /* (3) set PRETTY_HOSTNAME */
499 if(g_key_file_load_from_file(config
, "/etc/systemd_compat.conf", G_KEY_FILE_NONE
, NULL
)
500 && (pretty_hostname_buf
= g_key_file_get_value(config
, "hostnamed", "PrettyHostname", NULL
)))
501 PRETTY_HOSTNAME
= pretty_hostname_buf
;
503 PRETTY_HOSTNAME
= "";
506 g_key_file_unref(config
);
508 /* (4) set STATIC_HOSTNAME */
509 if(!g_strcmp0(PRETTY_HOSTNAME
, ""))
510 STATIC_HOSTNAME
= HOSTNAME
;
512 else if((static_hostname_buf
= g_hostname_to_ascii(PRETTY_HOSTNAME
)))
513 STATIC_HOSTNAME
= static_hostname_buf
;
515 return (HOSTNAME
&& STATIC_HOSTNAME
&& PRETTY_HOSTNAME
) ? TRUE
: FALSE
;
519 gboolean
set_uname_properties() {
526 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
527 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
528 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
530 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
531 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
532 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
534 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
535 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
536 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
541 gboolean
determine_chassis_and_icon() {
543 const size_t bufsize
= 4096;
545 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
546 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
547 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
549 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
551 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
552 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
555 hwproduct
= (char*)g_malloc0(4096);
556 hwmodel
= (char*)g_malloc0(4096);
557 hwvendor
= (char*)g_malloc0(4096);
558 hwmachine
= (char*)g_malloc0(4096);
560 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
561 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
562 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
563 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
565 hwproduct_name
[0] = CTL_HW
;
566 hwproduct_name
[1] = HW_PRODUCT
;
568 hwmodel_name
[0] = CTL_HW
;
569 hwmodel_name
[1] = HW_MODEL
;
571 hwvendor_name
[0] = CTL_HW
;
572 hwvendor_name
[1] = HW_VENDOR
;
574 hwmachine_name
[0] = CTL_HW
;
575 hwmachine_name
[1] = HW_MACHINE
;
577 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
578 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
581 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
584 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
587 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
590 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
591 * probably move this code to vm test func and set a global after running it early, once */
593 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
594 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
595 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
596 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
598 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
600 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
601 CHASSIS
= chassis_indicator_table
[i
].chassis
;
604 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
606 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
607 ICON
= chassis_indicator_table
[i
].icon
;
612 if(up_native_is_laptop()) {
617 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
619 } else if(is_server(hwmachine
)) {
624 ICON
= "uninterruptible-power-supply";
626 } else if(!CHASSIS
|| !ICON
) {
634 return (CHASSIS
&& ICON
);
637 gboolean
is_server(gchar
*arch
) {
641 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
642 if(strcasestr(arch
, server_archs
[i
]))
648 gboolean
up_native_is_laptop() {
650 struct apm_power_info bstate
;
651 struct sensordev acpiac
;
653 if (up_native_get_sensordev("acpiac0", &acpiac
))
656 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
657 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
659 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
662 int up_apm_get_fd() {
664 static int apm_fd
= 0;
668 g_debug("apm_fd is not initialized yet, opening");
671 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
672 if(errno
!= ENXIO
&& errno
!= ENOENT
)
673 g_error("cannot open device file");
680 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
683 size_t sdlen
= sizeof(struct sensordev
);
684 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
686 for (devn
= 0 ; ; devn
++) {
688 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
695 if (!strcmp(snsrdev
->xname
, id
))