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 "../../util.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 static const gchar
*DEFAULT_DOMAIN
= ""; /* blank domains are OK for now */
114 static const gchar
*OS_HOSTNAME_PATH
= "/etc/myname";
115 static const gchar
*OS_CONFIG_PATH
= "/etc/machine-info";
117 /* --- begin method/property/dbus signal code --- */
119 /* TODO free some strings here */
121 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
122 GDBusMethodInvocation
*invoc
,
126 gchar
*proposed_hostname
, *valid_hostname_buf
;
127 const gchar
*bus_name
;
128 gboolean policykit_auth
, ret
, try_to_set
;
130 check_auth_result is_authed
;
132 proposed_hostname
= NULL
;
133 ret
= try_to_set
= FALSE
;
135 params
= g_dbus_method_invocation_get_parameters(invoc
);
136 g_variant_get(params
, "(sb)", &proposed_hostname
, &policykit_auth
);
137 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
139 /* verify caller has correct permissions via polkit */
140 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-hostname", policykit_auth
);
144 case AUTHORIZED_NATIVELY
:
145 case AUTHORIZED_BY_PROMPT
:
149 case UNAUTHORIZED_NATIVELY
:
150 case UNAUTHORIZED_FAILED_PROMPT
:
151 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set hostname.");
155 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
158 case ERROR_BADACTION
:
159 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
164 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
168 /* verify passed hostname's validity */
169 if(try_to_set
&& proposed_hostname
&& (valid_hostname_buf
= g_hostname_to_ascii(proposed_hostname
))) {
171 check_length
= strnlen(valid_hostname_buf
, MAXHOSTNAMELEN
+ 1);
173 if(check_length
> MAXHOSTNAMELEN
) {
175 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
176 g_free(valid_hostname_buf
);
178 } else if(sethostname(proposed_hostname
, check_length
)) {
180 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
181 g_free(valid_hostname_buf
);
185 HOSTNAME
= valid_hostname_buf
;
186 hostname1_set_hostname(hn1_passed_interf
, HOSTNAME
);
187 g_ptr_array_add(hostnamed_freeable
, valid_hostname_buf
);
189 hostname1_complete_set_hostname(hn1_passed_interf
, invoc
);
197 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
198 GDBusMethodInvocation
*invoc
,
203 gchar
*proposed_static_hostname
, *valid_static_hostname_buf
, *bsd_hostname_try
;
204 const gchar
*bus_name
;
205 gboolean policykit_auth
, ret
, try_to_set
;
207 check_auth_result is_authed
;
210 proposed_static_hostname
= NULL
;
211 ret
= try_to_set
= FALSE
;
213 params
= g_dbus_method_invocation_get_parameters(invoc
);
214 g_variant_get(params
, "(sb)", &proposed_static_hostname
, &policykit_auth
);
215 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
217 /* verify caller has correct permissions via polkit */
218 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-static-hostname", policykit_auth
);
222 case AUTHORIZED_NATIVELY
:
223 case AUTHORIZED_BY_PROMPT
:
227 case UNAUTHORIZED_NATIVELY
:
228 case UNAUTHORIZED_FAILED_PROMPT
:
229 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set static hostname.");
233 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
236 case ERROR_BADACTION
:
237 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
242 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason.");
246 /* verify passed hostname's validity */
247 if(try_to_set
&& proposed_static_hostname
&& (valid_static_hostname_buf
= g_hostname_to_ascii(proposed_static_hostname
))) {
249 check_length
= strnlen(valid_static_hostname_buf
, MAXHOSTNAMELEN
+ 1);
251 if(check_length
> MAXHOSTNAMELEN
) {
253 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length.");
254 g_free(valid_static_hostname_buf
);
256 } else if(!(STATIC_HOSTNAME
= valid_static_hostname_buf
)) {
258 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason.");
259 g_free(valid_static_hostname_buf
);
263 g_strdelimit(STATIC_HOSTNAME
, " ", '-');
264 hostname1_set_static_hostname(hn1_passed_interf
, STATIC_HOSTNAME
);
265 g_ptr_array_add(hostnamed_freeable
, valid_static_hostname_buf
);
267 /* set string in OS_HOSTNAME_PATH ("/etc/myname" on bsd) */
268 bsd_hostname_try
= get_bsd_hostname(STATIC_HOSTNAME
);
270 if(!bsd_hostname_try
|| !g_file_set_contents(OS_HOSTNAME_PATH
, bsd_hostname_try
, -1, &debug_error
))
271 g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH
);
274 g_free(bsd_hostname_try
);
276 /* call sethostname(3) too */
277 ret
= (!sethostname(valid_static_hostname_buf
, MAXHOSTNAMELEN
)) ? TRUE
: FALSE
; /* TODO set /etc/myname, guarantee domain or substitue .home.network" */
278 hostname1_complete_set_static_hostname(hn1_passed_interf
, invoc
);
286 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
287 GDBusMethodInvocation
*invoc
,
292 gchar
*proposed_pretty_hostname
, *valid_pretty_hostname_buf
, *computed_static_hostname
;
293 const gchar
*bus_name
;
294 gboolean policykit_auth
, ret
, try_to_set
;
296 check_auth_result is_authed
;
298 proposed_pretty_hostname
= NULL
;
299 ret
= try_to_set
= FALSE
;
301 params
= g_dbus_method_invocation_get_parameters(invoc
);
302 g_variant_get(params
, "(sb)", &proposed_pretty_hostname
, &policykit_auth
);
303 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
305 /* verify caller has correct permissions via polkit */
306 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth
);
310 case AUTHORIZED_NATIVELY
:
311 case AUTHORIZED_BY_PROMPT
:
315 case UNAUTHORIZED_NATIVELY
:
316 case UNAUTHORIZED_FAILED_PROMPT
:
317 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set pretty hostname.");
321 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
324 case ERROR_BADACTION
:
325 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
330 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
334 /* verify passed hostname's validity */
335 if(try_to_set
&& proposed_pretty_hostname
&& (valid_pretty_hostname_buf
= g_locale_to_utf8(proposed_pretty_hostname
, -1, 0, 0, NULL
))) {
337 check_length
= strnlen(valid_pretty_hostname_buf
, MAXHOSTNAMELEN
+ 1);
339 if(check_length
> MAXHOSTNAMELEN
) {
341 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length.");
342 g_free(valid_pretty_hostname_buf
);
344 } else if(!(PRETTY_HOSTNAME
= valid_pretty_hostname_buf
)) {
346 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
347 g_free(valid_pretty_hostname_buf
);
351 hostname1_set_pretty_hostname(hn1_passed_interf
, PRETTY_HOSTNAME
);
352 g_ptr_array_add(hostnamed_freeable
, valid_pretty_hostname_buf
);
353 hostname1_complete_set_pretty_hostname(hn1_passed_interf
, invoc
);
356 if(!config_set(OS_CONFIG_PATH
, "PRETTY_HOSTNAME", PRETTY_HOSTNAME
))
357 g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH
);
365 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
366 GDBusMethodInvocation
*invoc
,
371 gchar
*proposed_chassis_name
, *valid_chassis_name_buf
;
372 const gchar
*bus_name
;
373 gboolean policykit_auth
, ret
, try_to_set
;
374 check_auth_result is_authed
;
376 proposed_chassis_name
= NULL
;
377 ret
= try_to_set
= FALSE
;
378 valid_chassis_name_buf
= (gchar
*)g_malloc0(8192);
380 params
= g_dbus_method_invocation_get_parameters(invoc
);
381 g_variant_get(params
, "(sb)", &proposed_chassis_name
, &policykit_auth
);
382 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
384 g_strlcpy(valid_chassis_name_buf
, proposed_chassis_name
, (gsize
)64);
386 /* verify caller has correct permissions via polkit */
387 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-chassis", policykit_auth
);
391 case AUTHORIZED_NATIVELY
:
392 case AUTHORIZED_BY_PROMPT
:
396 case UNAUTHORIZED_NATIVELY
:
397 case UNAUTHORIZED_FAILED_PROMPT
:
398 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set chassis type.");
402 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
405 case ERROR_BADACTION
:
406 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
411 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
415 /* verify passed chassis type's validity */
416 if(try_to_set
&& proposed_chassis_name
) {
418 if(!is_valid_chassis_type(proposed_chassis_name
)) {
420 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'.");
421 g_free(valid_chassis_name_buf
);
423 } else if(!(CHASSIS
= valid_chassis_name_buf
)) {
425 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
426 g_free(valid_chassis_name_buf
);
430 hostname1_set_chassis(hn1_passed_interf
, CHASSIS
);
431 g_ptr_array_add(hostnamed_freeable
, valid_chassis_name_buf
);
432 hostname1_complete_set_chassis(hn1_passed_interf
, invoc
);
435 if(!config_set(OS_CONFIG_PATH
, "CHASSIS", valid_chassis_name_buf
))
436 g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH
);
444 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
445 GDBusMethodInvocation
*invoc
,
450 gchar
*proposed_icon_name
, *valid_icon_name_buf
;
451 const gchar
*bus_name
;
452 gboolean policykit_auth
, ret
, try_to_set
;
453 check_auth_result is_authed
;
455 proposed_icon_name
= NULL
;
456 ret
= try_to_set
= FALSE
;
458 params
= g_dbus_method_invocation_get_parameters(invoc
);
459 g_variant_get(params
, "(sb)", &proposed_icon_name
, &policykit_auth
);
460 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
462 /* verify caller has correct permissions via polkit */
463 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-icon-name", policykit_auth
);
467 case AUTHORIZED_NATIVELY
:
468 case AUTHORIZED_BY_PROMPT
:
472 case UNAUTHORIZED_NATIVELY
:
473 case UNAUTHORIZED_FAILED_PROMPT
:
474 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set icon name.");
478 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
481 case ERROR_BADACTION
:
482 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
487 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
491 /* verify passed chassis type's validity */
492 if(try_to_set
&& proposed_icon_name
) {
494 g_strlcpy(valid_icon_name_buf
, proposed_icon_name
, (gsize
)64);
496 if(!(ICON
= valid_icon_name_buf
)) {
498 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
499 g_free(valid_icon_name_buf
);
503 hostname1_set_icon_name(hn1_passed_interf
, ICON
);
504 g_ptr_array_add(hostnamed_freeable
, valid_icon_name_buf
);
505 hostname1_complete_set_icon_name(hn1_passed_interf
, invoc
);
508 if(!config_set(OS_CONFIG_PATH
, "ICON_NAME", valid_icon_name_buf
))
509 g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH
);
517 /* note: all hostnamed/hostname1's properties are read-only,
518 * and do not need set_ functions, gdbus-codegen realized
519 * this from the XML and handled the to-be error of trying
520 * to set a read-only property's value
527 hostname_buf
= (gchar
*)g_malloc0(MAXHOSTNAMELEN
);
529 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
530 return "localhost.home.network"; /* TODO bomb out here probably */
532 else if(!g_strcmp0(HOSTNAME
, hostname_buf
)) {
534 g_free(hostname_buf
);
538 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
539 HOSTNAME
= hostname_buf
;
540 hostname1_set_hostname(hostnamed_interf
, HOSTNAME
);
546 our_get_static_hostname() {
548 if(STATIC_HOSTNAME
&& g_strcmp0(STATIC_HOSTNAME
, ""))
549 return STATIC_HOSTNAME
;
553 return "localhost.home.network";
557 our_get_pretty_hostname() {
560 return PRETTY_HOSTNAME
;
571 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
575 our_get_icon_name() {
584 our_get_kernel_name() {
593 our_get_kernel_version() {
602 our_get_kernel_release() {
611 our_get_os_cpename() {
613 /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */
618 our_get_os_pretty_name() {
623 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
625 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
627 gpointer user_data
) {
629 g_printf("got bus/name, exporting %s's interface...\n", name
);
631 hostnamed_interf
= hostname1_skeleton_new();
633 /* attach function pointers to generated struct's method handlers */
634 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
635 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
636 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
637 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
638 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
640 /* set our properties before export */
641 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
642 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
643 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
644 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
645 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
646 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
647 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
648 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
649 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
650 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
652 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
654 "/org/freedesktop/hostname1",
657 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
658 hostnamed_mem_clean();
662 dbus_interface_exported
= TRUE
;
663 g_printf("exported %s's interface on the system bus...\n", name
);
667 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
669 gpointer user_data
) {
671 g_printf("success!\n");
674 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
676 gpointer user_data
) {
680 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
);
681 hostnamed_mem_clean();
684 g_printf("lost name %s, exiting...\n", name
);
686 hostnamed_mem_clean();
689 /* --- end bus/name handlers, begin misc unix functions --- */
691 /* safe call to clean and then exit
692 * this stops our GMainLoop safely before letting main() return */
693 void hostnamed_mem_clean() {
695 g_printf("exiting...\n");
697 if(dbus_interface_exported
)
698 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
700 if(g_main_loop_is_running(hostnamed_loop
))
701 g_main_loop_quit(hostnamed_loop
);
705 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
706 gboolean
unix_sig_terminate_handler(gpointer data
) {
708 g_printf("caught SIGINT/HUP/TERM, exiting\n");
710 hostnamed_mem_clean();
711 return G_SOURCE_REMOVE
;
714 void set_signal_handlers() {
716 /* we don't care about its descriptor, we never need to unregister these */
717 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
718 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
719 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
721 /* TODO: the "only once" guarantee only counts towards specific signals.
722 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
723 * to be called twice */
728 hostnamed_freeable
= g_ptr_array_new();
730 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
732 get_bsd_hostname("adsf"); /* TODO KILL ME */
734 CHASSIS
= ICON
= OS_CPENAME
= 0;
735 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
736 HOSTNAME
= STATIC_HOSTNAME
= PRETTY_HOSTNAME
= NULL
;
738 set_signal_handlers();
740 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
743 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
745 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
746 "org.freedesktop.hostname1",
747 G_BUS_NAME_OWNER_FLAGS_NONE
,
748 hostnamed_on_bus_acquired
,
749 hostnamed_on_name_acquired
,
750 hostnamed_on_name_lost
,
754 g_main_loop_run(hostnamed_loop
);
755 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
756 g_main_loop_unref(hostnamed_loop
);
758 /* guaranteed unownable */
759 g_bus_unown_name(bus_descriptor
);
761 /* at this point no operations can occur with our data, it is safe to free it + its container */
762 g_ptr_array_free(hostnamed_freeable
, TRUE
);
767 gboolean
set_names() {
770 gchar
*hostname_buf
, *static_hostname_buf
, *pretty_hostname_buf
;
771 size_t hostname_divider
;
773 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
774 static_hostname_buf
= (gchar
*) g_malloc0(4096);
775 pretty_hostname_buf
= (gchar
*) g_malloc0(4096);
777 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
778 g_ptr_array_add(hostnamed_freeable
, static_hostname_buf
);
779 g_ptr_array_add(hostnamed_freeable
, pretty_hostname_buf
);
781 /* (2) set HOSTNAME */
782 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || !g_strcmp0(hostname_buf
, ""))
783 HOSTNAME
= "localhost";
785 HOSTNAME
= hostname_buf
;
787 /* this bit gets you the /etc/myname style hostname
788 hostname_divider = strcspn(hostname_buf, ".");
789 strncpy(ret, hostname_buf, hostname_divider); */
791 /* (3) set PRETTY_HOSTNAME */
792 if((pretty_hostname_buf
= config_get(OS_CONFIG_PATH
, "PRETTY_HOSTNAME")))
793 PRETTY_HOSTNAME
= pretty_hostname_buf
;
796 PRETTY_HOSTNAME
= "";
799 /* (4) set STATIC_HOSTNAME */
800 if(!gethostname(static_hostname_buf
, MAXHOSTNAMELEN
))
801 STATIC_HOSTNAME
= static_hostname_buf
;
804 STATIC_HOSTNAME
= "";
806 return (HOSTNAME
&& STATIC_HOSTNAME
&& PRETTY_HOSTNAME
) ? TRUE
: FALSE
;
809 gboolean
set_uname_properties() {
816 KERN_NAME
= (gchar
*)g_malloc0(sizeof(un
.sysname
));
817 g_ptr_array_add(hostnamed_freeable
, KERN_NAME
);
818 g_strlcpy(KERN_NAME
, un
.sysname
, sizeof(un
.sysname
));
820 KERN_RELEASE
= (gchar
*)g_malloc0(sizeof(un
.release
));
821 g_ptr_array_add(hostnamed_freeable
, KERN_RELEASE
);
822 g_strlcpy(KERN_RELEASE
, un
.release
, sizeof(un
.release
));
824 KERN_VERS
= (gchar
*)g_malloc0(sizeof(un
.version
));
825 g_ptr_array_add(hostnamed_freeable
, KERN_VERS
);
826 g_strlcpy(KERN_VERS
, un
.version
, sizeof(un
.version
));
831 gboolean
determine_chassis_and_icon() {
833 const size_t bufsize
= 4096;
835 char *hwproduct
, *hwmodel
, *hwvendor
, *hwmachine
;
836 size_t hwproduct_size
, hwmodel_size
, hwvendor_size
, hwmachine_size
;
837 int hwproduct_name
[2], hwmodel_name
[2], hwvendor_name
[2], hwmachine_name
[2];
839 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
841 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
842 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
845 hwproduct
= (char*)g_malloc0(4096);
846 hwmodel
= (char*)g_malloc0(4096);
847 hwvendor
= (char*)g_malloc0(4096);
848 hwmachine
= (char*)g_malloc0(4096);
850 g_ptr_array_add(hostnamed_freeable
, hwproduct
);
851 g_ptr_array_add(hostnamed_freeable
, hwmodel
);
852 g_ptr_array_add(hostnamed_freeable
, hwvendor
);
853 g_ptr_array_add(hostnamed_freeable
, hwmachine
);
855 hwproduct_name
[0] = CTL_HW
;
856 hwproduct_name
[1] = HW_PRODUCT
;
858 hwmodel_name
[0] = CTL_HW
;
859 hwmodel_name
[1] = HW_MODEL
;
861 hwvendor_name
[0] = CTL_HW
;
862 hwvendor_name
[1] = HW_VENDOR
;
864 hwmachine_name
[0] = CTL_HW
;
865 hwmachine_name
[1] = HW_MACHINE
;
867 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
868 if(-1 == sysctl(hwproduct_name
, 2, NULL
, &hwproduct_size
, NULL
, 0) || -1 == sysctl(hwproduct_name
, 2, hwproduct
, &hwproduct_size
, NULL
, 0))
871 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
874 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
877 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
880 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
881 * probably move this code to vm test func and set a global after running it early, once */
883 for(; i
< G_N_ELEMENTS(chassis_indicator_table
); i
++) {
884 if(strcasestr(hwproduct
, chassis_indicator_table
[i
].match_string
)
885 || strcasestr(hwmodel
, chassis_indicator_table
[i
].match_string
)
886 || strcasestr(hwvendor
, chassis_indicator_table
[i
].match_string
)) {
888 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
890 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
891 CHASSIS
= chassis_indicator_table
[i
].chassis
;
894 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
896 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
897 ICON
= chassis_indicator_table
[i
].icon
;
902 if(up_native_is_laptop()) {
907 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
909 } else if(is_server(hwmachine
)) {
914 ICON
= "uninterruptible-power-supply";
916 } else if(!CHASSIS
|| !ICON
) {
924 return (CHASSIS
&& ICON
);
927 gboolean
is_server(gchar
*arch
) {
931 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
932 if(strcasestr(arch
, server_archs
[i
]))
938 gboolean
up_native_is_laptop() {
940 struct apm_power_info bstate
;
941 struct sensordev acpiac
;
943 if (up_native_get_sensordev("acpiac0", &acpiac
))
946 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
947 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
949 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
952 int up_apm_get_fd() {
954 static int apm_fd
= 0;
958 g_debug("apm_fd is not initialized yet, opening");
961 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
962 if(errno
!= ENXIO
&& errno
!= ENOENT
)
963 g_error("cannot open device file");
970 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
973 size_t sdlen
= sizeof(struct sensordev
);
974 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
976 for (devn
= 0 ; ; devn
++) {
978 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
985 if (!strcmp(snsrdev
->xname
, id
))
992 static gboolean
is_valid_chassis_type(gchar
*test
) {
994 if(!g_strcmp0(test
, "desktop") ||
995 !g_strcmp0(test
, "laptop") ||
996 !g_strcmp0(test
, "server") ||
997 !g_strcmp0(test
, "tablet") ||
998 !g_strcmp0(test
, "handset") ||
999 !g_strcmp0(test
, "vm") ||
1000 !g_strcmp0(test
, "container") ||
1001 !g_strcmp0(test
, ""))
1007 /* returns a proper, bsd-style FQDN hostname safe to write to /etc/myname
1008 * if proposed_hostname does not contain an appended domain, the one in /etc/myname is substituted.
1009 * failing that, DEFAULT_DOMAIN is used. NULL if proposed_hostname is invalid
1010 * returns string that should be g_free()'d, or NULL if passed an invalid hostname */
1011 static gchar
*get_bsd_hostname(gchar
*proposed_hostname
) {
1013 gchar
*bsd_hostname
, *ascii_translated_hostname
, **myname_contents
, *passed_domain
, *temp_buf
;
1014 size_t domain_len
, check_len
;
1015 gboolean read_result
;
1017 g_strdelimit(proposed_hostname
, "`~!@#$%^&*()_=+[{]}|:;'\"\\", '-');
1019 ascii_translated_hostname
= g_hostname_to_ascii(proposed_hostname
);
1020 check_len
= strnlen(ascii_translated_hostname
, MAXHOSTNAMELEN
);
1022 if(!ascii_translated_hostname
|| !check_len
|| check_len
> MAXHOSTNAMELEN
|| !g_strcmp0("", ascii_translated_hostname
) || !g_strcmp0(".", ascii_translated_hostname
)) {
1024 bsd_hostname
= NULL
;
1025 passed_domain
= NULL
;
1026 myname_contents
= NULL
;
1028 } else if((passed_domain
= has_domain(ascii_translated_hostname
))) {
1030 bsd_hostname
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
1031 g_strlcpy(bsd_hostname
, ascii_translated_hostname
, MAXHOSTNAMELEN
);
1033 passed_domain
= NULL
;
1034 myname_contents
= NULL
;
1038 myname_contents
= (gchar
**) g_malloc0(MAXHOSTNAMELEN
* 2);
1039 read_result
= g_file_get_contents(OS_HOSTNAME_PATH
, myname_contents
, NULL
, NULL
);
1041 if(read_result
&& (passed_domain
= has_domain(myname_contents
[0]))) {
1043 domain_len
= strnlen(passed_domain
, MAXHOSTNAMELEN
);
1045 if((domain_len
+ check_len
) > MAXHOSTNAMELEN
)
1046 bsd_hostname
= NULL
;
1048 bsd_hostname
= g_strconcat(ascii_translated_hostname
, passed_domain
, NULL
);
1050 } else if(myname_contents
[0]) {
1052 g_printf("%s does not contain a proper FQDN! this is a significant error on BSD machines, otherwise OK.\nfalling back to default domain, '%s'\n", OS_HOSTNAME_PATH
, DEFAULT_DOMAIN
);
1054 domain_len
= strnlen(DEFAULT_DOMAIN
, MAXHOSTNAMELEN
);
1056 if((domain_len
+ check_len
) > MAXHOSTNAMELEN
)
1057 bsd_hostname
= NULL
;
1059 bsd_hostname
= g_strconcat(ascii_translated_hostname
, DEFAULT_DOMAIN
, NULL
);
1063 g_printf("could not read hostname at %s, this is a major error\n", OS_HOSTNAME_PATH
);
1064 bsd_hostname
= NULL
;
1065 passed_domain
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
1070 g_free(passed_domain
);
1072 g_free(myname_contents
);
1074 if(bsd_hostname
&& !strchr(bsd_hostname
, '\n')) {
1076 temp_buf
= bsd_hostname
;
1077 bsd_hostname
= g_strconcat(bsd_hostname
, "\n", NULL
);
1081 return bsd_hostname
;
1084 /* returns NULL if no domain, otherwise append-appropriate domain string you must g_free()
1085 * i.e. has_domain("foo.bar.com") returns ".bar.com"
1086 * only pass g_hostname_to_ascii'd strings */
1087 static gchar
*has_domain(const gchar
*test
) {
1089 size_t hostname_len
, full_len
;
1092 hostname_len
= strcspn(test
, ".");
1093 full_len
= strnlen(test
, MAXHOSTNAMELEN
);
1095 if(full_len
== hostname_len
)
1098 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
1099 g_strlcpy(ret
, &test
[hostname_len
], MAXHOSTNAMELEN
);