tighten up polkit-auth, implement in hostnamed.c
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 /*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
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.
7 *
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.
15 */
16
17 #include <unistd.h>
18 #include <limits.h>
19 #include <signal.h>
20 #include <string.h>
21
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>
27
28 #include <machine/apmvar.h>
29
30 #include <glib/gprintf.h>
31 #include <glib-unix.h>
32 #include <polkit/polkit.h>
33
34 #include "hostnamed-gen.h"
35 #include "hostnamed.h"
36
37 #include "../../polkit-auth.h"
38
39 /* format: {
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.
44 *
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.
55 *
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.
59 *
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.
65 *
66 * (5) icon precedence bit. see previous definition.
67 * } */
68 struct SYSCTL_LOOKUP_TABLE {
69 gchar *match_string;
70 gchar *chassis;
71 gchar *icon;
72 gboolean chassis_precedence;
73 gboolean icon_precedence;
74 };
75
76 GPtrArray *hostnamed_freeable;
77 Hostname1 *hostnamed_interf;
78
79 GMainLoop *hostnamed_loop;
80
81 guint bus_descriptor;
82 gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
83
84 gchar *HOSTNAME, *STATIC_HOSTNAME, *PRETTY_HOSTNAME;
85 gchar *CHASSIS, *ICON;
86 gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS, *OS_CPENAME;
87
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 */
92
93 /* add any sysctl strings that suggest virtualization here */
94 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] =
95 {
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 */
105
106 /* archs to check against when determining if machine is server */
107 const gchar *server_archs[] = {
108 "hppa",
109 "sparc",
110 "sparc64"
111 };
112
113 /* --- begin method/property/dbus signal code --- */
114
115 /* TODO free some strings here */
116 static gboolean
117 on_handle_set_hostname(Hostname1 *hn1_passed_interf,
118 GDBusMethodInvocation *invoc,
119 const gchar *greet,
120 gpointer data) {
121 GVariant *params;
122 gchar *proposed_hostname, *valid_hostname_buf;
123 const gchar *bus_name;
124 gboolean policykit_auth, ret, try_to_set;
125 size_t check_length;
126 check_auth_result is_authed;
127
128 proposed_hostname = NULL;
129 ret = try_to_set = FALSE;
130
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);
134
135 /* verify caller has correct permissions via polkit */
136 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.SetHostname", policykit_auth);
137
138 switch(is_authed) {
139
140 case AUTHORIZED_NATIVELY:
141 case AUTHORIZED_BY_PROMPT:
142 try_to_set = TRUE;
143 break;
144
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.");
148 break;
149
150 case ERROR_BADBUS:
151 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
152 break;
153
154 case ERROR_BADACTION:
155 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
156 break;
157
158 case ERROR_GENERIC:
159 default:
160 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
161 break;
162 }
163
164 /* verify passed hostname's validity */
165 if(try_to_set && proposed_hostname && (valid_hostname_buf = g_hostname_to_ascii(proposed_hostname))) {
166
167 check_length = strnlen(valid_hostname_buf, MAXHOSTNAMELEN + 1);
168
169 if(check_length > MAXHOSTNAMELEN) {
170
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);
173
174 } else if(sethostname(proposed_hostname, check_length)) {
175
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);
178
179 } else {
180
181 HOSTNAME = valid_hostname_buf;
182 hostname1_set_hostname(hn1_passed_interf, HOSTNAME);
183 g_ptr_array_add(hostnamed_freeable, valid_hostname_buf);
184 ret = TRUE;
185 hostname1_complete_set_hostname(hn1_passed_interf, invoc);
186 }
187 }
188
189 return ret;
190 }
191
192 static gboolean
193 on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
194 GDBusMethodInvocation *invoc,
195 const gchar *greet,
196 gpointer data) {
197 return FALSE;
198 }
199
200 static gboolean
201 on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
202 GDBusMethodInvocation *invoc,
203 const gchar *greet,
204 gpointer data) {
205 return FALSE;
206 }
207
208 static gboolean
209 on_handle_set_chassis(Hostname1 *hn1_passed_interf,
210 GDBusMethodInvocation *invoc,
211 const gchar *greet,
212 gpointer data) {
213 return FALSE;
214 }
215
216 static gboolean
217 on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
218 GDBusMethodInvocation *invoc,
219 const gchar *greet,
220 gpointer data) {
221 return FALSE;
222 }
223
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
228 */
229
230 const gchar *
231 our_get_hostname() {
232
233 gchar *hostname_buf;
234 hostname_buf = (gchar *)g_malloc0(MAXHOSTNAMELEN);
235
236 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
237 return "localhost.home.network"; /* TODO bomb out here probably */
238
239 else if(!g_strcmp0(HOSTNAME, hostname_buf)) {
240
241 g_free(hostname_buf);
242 return HOSTNAME;
243 }
244
245 g_ptr_array_add(hostnamed_freeable, hostname_buf);
246 HOSTNAME = hostname_buf;
247 hostname1_set_hostname(hostnamed_interf, HOSTNAME);
248
249 return HOSTNAME;
250 }
251
252 const gchar *
253 our_get_static_hostname() {
254
255 if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, ""))
256 return STATIC_HOSTNAME;
257 else if(HOSTNAME)
258 return HOSTNAME;
259
260 return "localhost.home.network";
261 }
262
263 const gchar *
264 our_get_pretty_hostname() {
265
266 if(PRETTY_HOSTNAME)
267 return PRETTY_HOSTNAME;
268
269 return "";
270 }
271
272 const gchar *
273 our_get_chassis() {
274
275 if(CHASSIS)
276 return CHASSIS;
277
278 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
279 }
280
281 const gchar *
282 our_get_icon_name() {
283
284 if(ICON)
285 return ICON;
286
287 return "";
288 }
289
290 const gchar *
291 our_get_kernel_name() {
292
293 if(KERN_NAME)
294 return KERN_NAME;
295
296 return "";
297 }
298
299 const gchar *
300 our_get_kernel_version() {
301
302 if(KERN_VERS)
303 return KERN_VERS;
304
305 return "";
306 }
307
308 const gchar *
309 our_get_kernel_release() {
310
311 if(KERN_RELEASE)
312 return KERN_RELEASE;
313
314 return "";
315 }
316
317 const gchar *
318 our_get_os_cpename() {
319
320 return "ONEDAY";
321 }
322
323 const gchar *
324 our_get_os_pretty_name() {
325
326 return "OpenBSD";
327 }
328
329 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
330
331 static void hostnamed_on_bus_acquired(GDBusConnection *conn,
332 const gchar *name,
333 gpointer user_data) {
334
335 g_printf("got bus/name, exporting %s's interface...\n", name);
336
337 hostnamed_interf = hostname1_skeleton_new();
338
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);
345
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());
357
358 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
359 conn,
360 "/org/freedesktop/hostname1",
361 NULL)) {
362
363 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
364 hostnamed_mem_clean();
365
366 } else {
367
368 dbus_interface_exported = TRUE;
369 g_printf("exported %s's interface on the system bus...\n", name);
370 }
371 }
372
373 static void hostnamed_on_name_acquired(GDBusConnection *conn,
374 const gchar *name,
375 gpointer user_data) {
376
377 g_printf("success!\n");
378 }
379
380 static void hostnamed_on_name_lost(GDBusConnection *conn,
381 const gchar *name,
382 gpointer user_data) {
383
384 if(!conn) {
385
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();
388 }
389
390 g_printf("lost name %s, exiting...\n", name);
391
392 hostnamed_mem_clean();
393 }
394
395 /* --- end bus/name handlers, begin misc unix functions --- */
396
397 /* safe call to clean and then exit
398 * this stops our GMainLoop safely before letting main() return */
399 void hostnamed_mem_clean() {
400
401 g_printf("exiting...\n");
402
403 if(dbus_interface_exported)
404 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
405
406 if(g_main_loop_is_running(hostnamed_loop))
407 g_main_loop_quit(hostnamed_loop);
408
409 }
410
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) {
413
414 g_printf("caught SIGINT/HUP/TERM, exiting\n");
415
416 hostnamed_mem_clean();
417 return G_SOURCE_REMOVE;
418 }
419
420 void set_signal_handlers() {
421
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);
426
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 */
430 }
431
432 int main() {
433
434 hostnamed_freeable = g_ptr_array_new();
435
436 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
437
438 CHASSIS = ICON = OS_CPENAME = 0;
439 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
440 HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL;
441
442 set_signal_handlers();
443
444 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
445 return 1;
446
447 hostnamed_loop = g_main_loop_new(NULL, TRUE);
448
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,
455 NULL,
456 NULL);
457
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);
461
462 /* guaranteed unownable */
463 g_bus_unown_name(bus_descriptor);
464
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);
467
468 return 0;
469 }
470
471 gboolean set_names() {
472
473 /* (1) set up */
474 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
475 GKeyFile *config;
476 size_t hostname_divider;
477
478 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
479 static_hostname_buf = (gchar*) g_malloc0(4096);
480 pretty_hostname_buf = (gchar*) g_malloc0(4096);
481
482 config = g_key_file_new();
483
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);
487
488 /* (2) set HOSTNAME */
489 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
490 HOSTNAME = "localhost";
491
492 HOSTNAME = hostname_buf;
493
494 /* this bit gets you the /etc/myname style hostname
495 hostname_divider = strcspn(hostname_buf, ".");
496 strncpy(ret, hostname_buf, hostname_divider); */
497
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;
502 else
503 PRETTY_HOSTNAME = "";
504
505 if(config)
506 g_key_file_unref(config);
507
508 /* (4) set STATIC_HOSTNAME */
509 if(!g_strcmp0(PRETTY_HOSTNAME, ""))
510 STATIC_HOSTNAME = HOSTNAME;
511
512 else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
513 STATIC_HOSTNAME = static_hostname_buf;
514
515 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
516
517 }
518
519 gboolean set_uname_properties() {
520
521 struct utsname un;
522
523 if(-1 == uname(&un))
524 return FALSE;
525
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));
529
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));
533
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));
537
538 return TRUE;
539 }
540
541 gboolean determine_chassis_and_icon() {
542
543 const size_t bufsize = 4096;
544
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];
548 unsigned int i;
549 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
550
551 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
552 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
553 i = 0;
554
555 hwproduct = (char*)g_malloc0(4096);
556 hwmodel = (char*)g_malloc0(4096);
557 hwvendor = (char*)g_malloc0(4096);
558 hwmachine = (char*)g_malloc0(4096);
559
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);
564
565 hwproduct_name[0] = CTL_HW;
566 hwproduct_name[1] = HW_PRODUCT;
567
568 hwmodel_name[0] = CTL_HW;
569 hwmodel_name[1] = HW_MODEL;
570
571 hwvendor_name[0] = CTL_HW;
572 hwvendor_name[1] = HW_VENDOR;
573
574 hwmachine_name[0] = CTL_HW;
575 hwmachine_name[1] = HW_MACHINE;
576
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))
579 return FALSE;
580
581 if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
582 return FALSE;
583
584 if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
585 return FALSE;
586
587 if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0))
588 return FALSE;
589
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 */
592
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)) {
597
598 if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) {
599
600 UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence;
601 CHASSIS = chassis_indicator_table[i].chassis;
602 }
603
604 if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) {
605
606 UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence;
607 ICON = chassis_indicator_table[i].icon;
608 }
609 }
610 }
611
612 if(up_native_is_laptop()) {
613
614 if(!CHASSIS)
615 CHASSIS = "laptop";
616 if(!ICON)
617 ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
618
619 } else if(is_server(hwmachine)) {
620
621 if(!CHASSIS)
622 CHASSIS = "server";
623 if(!ICON)
624 ICON = "uninterruptible-power-supply";
625
626 } else if(!CHASSIS || !ICON) {
627
628 if(!CHASSIS)
629 CHASSIS = "desktop";
630 if(!ICON)
631 ICON = "computer";
632 }
633
634 return (CHASSIS && ICON);
635 }
636
637 gboolean is_server(gchar *arch) {
638
639 unsigned int i;
640
641 for(; i < G_N_ELEMENTS(server_archs); i++)
642 if(strcasestr(arch, server_archs[i]))
643 return TRUE;
644
645 return FALSE;
646 }
647
648 gboolean up_native_is_laptop() {
649
650 struct apm_power_info bstate;
651 struct sensordev acpiac;
652
653 if (up_native_get_sensordev("acpiac0", &acpiac))
654 return TRUE;
655
656 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
657 g_error("ioctl on apm fd failed : %s", g_strerror(errno));
658
659 return bstate.ac_state != APM_AC_UNKNOWN;
660 }
661
662 int up_apm_get_fd() {
663
664 static int apm_fd = 0;
665
666 if(apm_fd == 0) {
667
668 g_debug("apm_fd is not initialized yet, opening");
669
670 /* open /dev/apm */
671 if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
672 if(errno != ENXIO && errno != ENOENT)
673 g_error("cannot open device file");
674 }
675 }
676
677 return apm_fd;
678 }
679
680 gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
681
682 int devn;
683 size_t sdlen = sizeof(struct sensordev);
684 int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
685
686 for (devn = 0 ; ; devn++) {
687 mib[2] = devn;
688 if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
689 if(errno == ENXIO)
690 continue;
691 if(errno == ENOENT)
692 break;
693 }
694
695 if (!strcmp(snsrdev->xname, id))
696 return TRUE;
697 }
698
699 return FALSE;
700 }