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.set-hostname", 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
,
199 gchar
*proposed_static_hostname
, *valid_static_hostname_buf
;
200 const gchar
*bus_name
;
201 gboolean policykit_auth
, ret
, try_to_set
;
203 check_auth_result is_authed
;
205 proposed_static_hostname
= NULL
;
206 ret
= try_to_set
= FALSE
;
208 params
= g_dbus_method_invocation_get_parameters(invoc
);
209 g_variant_get(params
, "(sb)", &proposed_static_hostname
, &policykit_auth
);
210 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
212 /* verify caller has correct permissions via polkit */
213 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-static-hostname", policykit_auth
);
217 case AUTHORIZED_NATIVELY
:
218 case AUTHORIZED_BY_PROMPT
:
222 case UNAUTHORIZED_NATIVELY
:
223 case UNAUTHORIZED_FAILED_PROMPT
:
224 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set static hostname.");
228 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
231 case ERROR_BADACTION
:
232 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
237 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason.");
241 /* verify passed hostname's validity */
242 if(try_to_set
&& proposed_static_hostname
&& (valid_static_hostname_buf
= g_hostname_to_ascii(proposed_static_hostname
))) {
244 check_length
= strnlen(valid_static_hostname_buf
, MAXHOSTNAMELEN
+ 1);
246 if(check_length
> MAXHOSTNAMELEN
) {
248 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length.");
249 g_free(valid_static_hostname_buf
);
251 } else if(!(STATIC_HOSTNAME
= valid_static_hostname_buf
)) {
253 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason.");
254 g_free(valid_static_hostname_buf
);
258 g_strdelimit(STATIC_HOSTNAME
, " ", '-');
259 hostname1_set_static_hostname(hn1_passed_interf
, STATIC_HOSTNAME
);
260 g_ptr_array_add(hostnamed_freeable
, valid_static_hostname_buf
);
262 hostname1_complete_set_static_hostname(hn1_passed_interf
, invoc
);
270 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
271 GDBusMethodInvocation
*invoc
,
276 gchar
*proposed_pretty_hostname
, *valid_pretty_hostname_buf
, *computed_static_hostname
;
277 const gchar
*bus_name
;
278 gboolean policykit_auth
, ret
, try_to_set
;
280 check_auth_result is_authed
;
283 config
= g_key_file_new();
284 proposed_pretty_hostname
= NULL
;
285 ret
= try_to_set
= FALSE
;
287 params
= g_dbus_method_invocation_get_parameters(invoc
);
288 g_variant_get(params
, "(sb)", &proposed_pretty_hostname
, &policykit_auth
);
289 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
291 /* verify caller has correct permissions via polkit */
292 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth
);
296 case AUTHORIZED_NATIVELY
:
297 case AUTHORIZED_BY_PROMPT
:
301 case UNAUTHORIZED_NATIVELY
:
302 case UNAUTHORIZED_FAILED_PROMPT
:
303 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set pretty hostname.");
307 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
310 case ERROR_BADACTION
:
311 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
316 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
320 /* verify passed hostname's validity */
321 if(try_to_set
&& proposed_pretty_hostname
&& (valid_pretty_hostname_buf
= g_locale_to_utf8(proposed_pretty_hostname
, -1, 0, 0, NULL
))) {
323 check_length
= strnlen(valid_pretty_hostname_buf
, MAXHOSTNAMELEN
+ 1);
325 if(check_length
> MAXHOSTNAMELEN
) {
327 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length.");
328 g_free(valid_pretty_hostname_buf
);
330 } else if(!(PRETTY_HOSTNAME
= valid_pretty_hostname_buf
)) {
332 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
333 g_free(valid_pretty_hostname_buf
);
337 hostname1_set_pretty_hostname(hn1_passed_interf
, PRETTY_HOSTNAME
);
338 g_ptr_array_add(hostnamed_freeable
, valid_pretty_hostname_buf
);
339 hostname1_complete_set_pretty_hostname(hn1_passed_interf
, invoc
);
342 if(g_key_file_load_from_file(config
, "/etc/machine-info", G_KEY_FILE_NONE
, NULL
)) {
344 g_key_file_set_string(config
, "hostnamed", "PRETTY_HOSTNAME", valid_pretty_hostname_buf
);
346 if((computed_static_hostname
= g_hostname_to_ascii(PRETTY_HOSTNAME
))) {
348 g_strdelimit(computed_static_hostname
, " ", '-');
349 hostname1_set_static_hostname(hn1_passed_interf
, computed_static_hostname
);
350 STATIC_HOSTNAME
= computed_static_hostname
;
351 g_ptr_array_add(hostnamed_freeable
, computed_static_hostname
);
352 g_key_file_set_string(config
, "hostnamed", "StaticHostname", computed_static_hostname
);
355 g_free(computed_static_hostname
);
361 g_key_file_save_to_file(config
, "/etc/machine-info", NULL
);
362 g_key_file_unref(config
);
368 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
369 GDBusMethodInvocation
*invoc
,
374 gchar
*proposed_chassis_name
, *valid_chassis_name_buf
;
375 const gchar
*bus_name
;
376 gboolean policykit_auth
, ret
, try_to_set
;
377 check_auth_result is_authed
;
380 config
= g_key_file_new();
381 proposed_chassis_name
= NULL
;
382 ret
= try_to_set
= FALSE
;
383 valid_chassis_name_buf
= (gchar
*)g_malloc0(8192);
385 params
= g_dbus_method_invocation_get_parameters(invoc
);
386 g_variant_get(params
, "(sb)", &proposed_chassis_name
, &policykit_auth
);
387 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
389 g_strlcpy(valid_chassis_name_buf
, proposed_chassis_name
, (gsize
)64);
391 /* verify caller has correct permissions via polkit */
392 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-chassis", policykit_auth
);
396 case AUTHORIZED_NATIVELY
:
397 case AUTHORIZED_BY_PROMPT
:
401 case UNAUTHORIZED_NATIVELY
:
402 case UNAUTHORIZED_FAILED_PROMPT
:
403 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set chassis type.");
407 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
410 case ERROR_BADACTION
:
411 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
416 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
420 /* verify passed chassis type's validity */
421 if(try_to_set
&& proposed_chassis_name
) {
423 if(!is_valid_chassis_type(proposed_chassis_name
)) {
425 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Chassis type must be 'desktop', 'laptop', 'server', 'tablet', 'handset', 'vm', or 'container'.");
426 g_free(valid_chassis_name_buf
);
428 } else if(!(CHASSIS
= valid_chassis_name_buf
)) {
430 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
431 g_free(valid_chassis_name_buf
);
435 hostname1_set_chassis(hn1_passed_interf
, CHASSIS
);
436 g_ptr_array_add(hostnamed_freeable
, valid_chassis_name_buf
);
437 hostname1_complete_set_chassis(hn1_passed_interf
, invoc
);
439 if(g_key_file_load_from_file(config
, "/etc/machine-info", G_KEY_FILE_NONE
, NULL
)) {
442 g_key_file_set_string(config
, "hostnamed", "ChassisType", valid_chassis_name_buf
);
448 g_key_file_save_to_file(config
, "/etc/machine-info", NULL
);
449 g_key_file_unref(config
);
455 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
456 GDBusMethodInvocation
*invoc
,
461 gchar
*proposed_icon_name
, *valid_icon_name_buf
;
462 const gchar
*bus_name
;
463 gboolean policykit_auth
, ret
, try_to_set
;
464 check_auth_result is_authed
;
467 config
= g_key_file_new();
468 proposed_icon_name
= NULL
;
469 ret
= try_to_set
= FALSE
;
471 params
= g_dbus_method_invocation_get_parameters(invoc
);
472 g_variant_get(params
, "(sb)", &proposed_icon_name
, &policykit_auth
);
473 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
475 /* verify caller has correct permissions via polkit */
476 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-icon-name", policykit_auth
);
480 case AUTHORIZED_NATIVELY
:
481 case AUTHORIZED_BY_PROMPT
:
485 case UNAUTHORIZED_NATIVELY
:
486 case UNAUTHORIZED_FAILED_PROMPT
:
487 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set icon name.");
491 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
494 case ERROR_BADACTION
:
495 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
500 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
504 /* verify passed chassis type's validity */
505 if(try_to_set
&& proposed_icon_name
) {
507 g_strlcpy(valid_icon_name_buf
, proposed_icon_name
, (gsize
)64);
509 if(!(ICON
= valid_icon_name_buf
)) {
511 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
512 g_free(valid_icon_name_buf
);
516 hostname1_set_icon_name(hn1_passed_interf
, ICON
);
517 g_ptr_array_add(hostnamed_freeable
, valid_icon_name_buf
);
518 hostname1_complete_set_icon_name(hn1_passed_interf
, invoc
);
520 if(g_key_file_load_from_file(config
, "/etc/machine-info", G_KEY_FILE_NONE
, NULL
)) {
523 g_key_file_set_string(config
, "hostnamed", "IconName", valid_icon_name_buf
);
529 g_key_file_save_to_file(config
, "/etc/machine-info", NULL
);
530 g_key_file_unref(config
);
535 /* note: all hostnamed/hostname1's properties are read-only,
536 * and do not need set_ functions, gdbus-codegen realized
537 * this from the XML and handled the to-be error of trying
538 * to set a read-only property's value
545 hostname_buf
= (gchar
*)g_malloc0(MAXHOSTNAMELEN
);
547 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
548 return "localhost.home.network"; /* TODO bomb out here probably */
550 else if(!g_strcmp0(HOSTNAME
, hostname_buf
)) {
552 g_free(hostname_buf
);
556 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
557 HOSTNAME
= hostname_buf
;
558 hostname1_set_hostname(hostnamed_interf
, HOSTNAME
);
564 our_get_static_hostname() {
566 if(STATIC_HOSTNAME
&& g_strcmp0(STATIC_HOSTNAME
, ""))
567 return STATIC_HOSTNAME
;
571 return "localhost.home.network";
575 our_get_pretty_hostname() {
578 return PRETTY_HOSTNAME
;
589 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
593 our_get_icon_name() {
602 our_get_kernel_name() {
611 our_get_kernel_version() {
620 our_get_kernel_release() {
629 our_get_os_cpename() {
631 /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */
636 our_get_os_pretty_name() {
641 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
643 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
645 gpointer user_data
) {
647 g_printf("got bus/name, exporting %s's interface...\n", name
);
649 hostnamed_interf
= hostname1_skeleton_new();
651 /* attach function pointers to generated struct's method handlers */
652 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
653 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
654 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
655 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
656 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
658 /* set our properties before export */
659 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
660 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
661 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
662 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
663 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
664 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
665 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
666 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
667 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
668 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
670 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
672 "/org/freedesktop/hostname1",
675 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
676 hostnamed_mem_clean();
680 dbus_interface_exported
= TRUE
;
681 g_printf("exported %s's interface on the system bus...\n", name
);
685 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
687 gpointer user_data
) {
689 g_printf("success!\n");
692 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
694 gpointer user_data
) {
698 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
);
699 hostnamed_mem_clean();
702 g_printf("lost name %s, exiting...\n", name
);
704 hostnamed_mem_clean();
707 /* --- end bus/name handlers, begin misc unix functions --- */
709 /* safe call to clean and then exit
710 * this stops our GMainLoop safely before letting main() return */
711 void hostnamed_mem_clean() {
713 g_printf("exiting...\n");
715 if(dbus_interface_exported
)
716 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
718 if(g_main_loop_is_running(hostnamed_loop
))
719 g_main_loop_quit(hostnamed_loop
);
723 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
724 gboolean
unix_sig_terminate_handler(gpointer data
) {
726 g_printf("caught SIGINT/HUP/TERM, exiting\n");
728 hostnamed_mem_clean();
729 return G_SOURCE_REMOVE
;
732 void set_signal_handlers() {
734 /* we don't care about its descriptor, we never need to unregister these */
735 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
736 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
737 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
739 /* TODO: the "only once" guarantee only counts towards specific signals.
740 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
741 * to be called twice */
746 hostnamed_freeable
= g_ptr_array_new();
748 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
750 CHASSIS
= ICON
= OS_CPENAME
= 0;
751 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
752 HOSTNAME
= STATIC_HOSTNAME
= PRETTY_HOSTNAME
= NULL
;
754 set_signal_handlers();
756 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
759 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
761 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
762 "org.freedesktop.hostname1",
763 G_BUS_NAME_OWNER_FLAGS_NONE
,
764 hostnamed_on_bus_acquired
,
765 hostnamed_on_name_acquired
,
766 hostnamed_on_name_lost
,
770 g_main_loop_run(hostnamed_loop
);
771 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
772 g_main_loop_unref(hostnamed_loop
);
774 /* guaranteed unownable */
775 g_bus_unown_name(bus_descriptor
);
777 /* at this point no operations can occur with our data, it is safe to free it + its container */
778 g_ptr_array_free(hostnamed_freeable
, TRUE
);
783 gboolean
set_names() {
786 gchar
*hostname_buf
, *static_hostname_buf
, *pretty_hostname_buf
;
788 size_t hostname_divider
;
790 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
791 static_hostname_buf
= (gchar
*) g_malloc0(4096);
792 pretty_hostname_buf
= (gchar
*) g_malloc0(4096);
794 config
= g_key_file_new();
796 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
797 g_ptr_array_add(hostnamed_freeable
, static_hostname_buf
);
798 g_ptr_array_add(hostnamed_freeable
, pretty_hostname_buf
);
800 /* (2) set HOSTNAME */
801 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || !g_strcmp0(hostname_buf
, ""))
802 HOSTNAME
= "localhost";
804 HOSTNAME
= hostname_buf
;
806 /* this bit gets you the /etc/myname style hostname
807 hostname_divider = strcspn(hostname_buf, ".");
808 strncpy(ret, hostname_buf, hostname_divider); */
810 /* (3) set PRETTY_HOSTNAME */
811 if(g_key_file_load_from_file(config
, "/etc/machine-info", G_KEY_FILE_NONE
, NULL
)
812 && (pretty_hostname_buf
= g_key_file_get_value(config
, "hostnamed", "PRETTY_HOSTNAME", NULL
)))
813 PRETTY_HOSTNAME
= pretty_hostname_buf
;
815 PRETTY_HOSTNAME
= "";
818 g_key_file_unref(config
);
820 /* (4) set STATIC_HOSTNAME */
821 if(!g_strcmp0(PRETTY_HOSTNAME
, ""))
822 STATIC_HOSTNAME
= HOSTNAME
;
824 else if((static_hostname_buf
= g_hostname_to_ascii(PRETTY_HOSTNAME
)))
825 STATIC_HOSTNAME
= static_hostname_buf
;
827 return (HOSTNAME
&& STATIC_HOSTNAME
&& PRETTY_HOSTNAME
) ? TRUE
: FALSE
;
831 gboolean
set_uname_properties() {
838 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
839 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
840 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
842 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
843 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
844 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
846 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
847 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
848 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
853 gboolean
determine_chassis_and_icon() {
855 const size_t bufsize
= 4096;
857 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
858 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
859 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
861 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
863 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
864 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
867 hwproduct
= (char*)g_malloc0(4096);
868 hwmodel
= (char*)g_malloc0(4096);
869 hwvendor
= (char*)g_malloc0(4096);
870 hwmachine
= (char*)g_malloc0(4096);
872 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
873 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
874 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
875 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
877 hwproduct_name
[0] = CTL_HW
;
878 hwproduct_name
[1] = HW_PRODUCT
;
880 hwmodel_name
[0] = CTL_HW
;
881 hwmodel_name
[1] = HW_MODEL
;
883 hwvendor_name
[0] = CTL_HW
;
884 hwvendor_name
[1] = HW_VENDOR
;
886 hwmachine_name
[0] = CTL_HW
;
887 hwmachine_name
[1] = HW_MACHINE
;
889 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
890 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
893 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
896 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
899 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
902 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
903 * probably move this code to vm test func and set a global after running it early, once */
905 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
906 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
907 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
908 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
910 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
912 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
913 CHASSIS
= chassis_indicator_table
[i
].chassis
;
916 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
918 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
919 ICON
= chassis_indicator_table
[i
].icon
;
924 if(up_native_is_laptop()) {
929 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
931 } else if(is_server(hwmachine
)) {
936 ICON
= "uninterruptible-power-supply";
938 } else if(!CHASSIS
|| !ICON
) {
946 return (CHASSIS
&& ICON
);
949 gboolean
is_server(gchar
*arch
) {
953 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
954 if(strcasestr(arch
, server_archs
[i
]))
960 gboolean
up_native_is_laptop() {
962 struct apm_power_info bstate
;
963 struct sensordev acpiac
;
965 if (up_native_get_sensordev("acpiac0", &acpiac
))
968 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
969 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
971 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
974 int up_apm_get_fd() {
976 static int apm_fd
= 0;
980 g_debug("apm_fd is not initialized yet, opening");
983 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
984 if(errno
!= ENXIO
&& errno
!= ENOENT
)
985 g_error("cannot open device file");
992 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
995 size_t sdlen
= sizeof(struct sensordev
);
996 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
998 for (devn
= 0 ; ; devn
++) {
1000 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
1007 if (!strcmp(snsrdev
->xname
, id
))
1014 static gboolean
is_valid_chassis_type(gchar
*test
) {
1016 if(!g_strcmp0(test
, "desktop") ||
1017 !g_strcmp0(test
, "laptop") ||
1018 !g_strcmp0(test
, "server") ||
1019 !g_strcmp0(test
, "tablet") ||
1020 !g_strcmp0(test
, "handset") ||
1021 !g_strcmp0(test
, "vm") ||
1022 !g_strcmp0(test
, "container") ||
1023 !g_strcmp0(test
, ""))