include polkit.h in all interfaces, edit makefile accordingly
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
3b82e3c1 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
a35a69c5 17#include <unistd.h>
18#include <limits.h>
0f339959 19#include <signal.h>
f8257c5d 20#include <string.h>
fd8852d9 21
d15318db 22#include <sys/param.h>
f1ad9351 23#include <sys/sysctl.h>
f8257c5d 24#include <sys/sensors.h>
25#include <sys/ioctl.h>
bd24ff31 26#include <sys/utsname.h>
f8257c5d 27
28#include <machine/apmvar.h>
d15318db 29
4c04b514 30#include <glib/gprintf.h>
8caf1f61 31#include <glib-unix.h>
904d744d 32#include <polkit/polkit.h>
4c04b514 33
1e8c7c88 34#include "hostnamed-gen.h"
0f339959 35#include "hostnamed.h"
a35a69c5 36
a1bcc33c 37/* format: {
38 * (1) string to be matched against runtime machine's sysctl output.
39 * can be either the exact string or a substring contained
40 * within sysctl strings. no "guesses" here, a match should
41 * reliably indicate the chassis/icon.
42 *
43 * (2) string describing chassis type divulged by (1).
44 * must be one of "desktop", "laptop", "server",
45 * "tablet", "handset", "vm", "container" or NULL
46 * if only icon string can be ascertained. "vm" refers
47 * to operating systems running on baremetal hypervisors
48 * (hardware virtualization, like XEN) while "container"
49 * refers to OSs running on shared hypervisors like
50 * virtualbox or VMware. consider the distinction carefully
51 * as common virtualization software like KVM may share
52 * characteristics of both "vm" and "container" types.
53 *
54 * (3) string specifying icon to use. follows XDG icon spec.
55 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
56 * for allowed strings.
57 *
58 * (4) chassis precedence bit. TRUE if (2) is defined and
59 * we're certain it is the proper string. FALSE in the
60 * circumstance (2) may be the correct string, unless
61 * a match with this bit set to TRUE overrides it.
62 * if (2) is NULL, this bit is inconsequential.
63 *
64 * (5) icon precedence bit. see previous definition.
65 * } */
66struct SYSCTL_LOOKUP_TABLE {
67 gchar *match_string;
68 gchar *chassis;
69 gchar *icon;
70 gboolean chassis_precedence;
71 gboolean icon_precedence;
72};
73
fd8852d9 74GPtrArray *hostnamed_freeable;
1be94ede 75Hostname1 *hostnamed_interf;
3d53b501 76
0f339959 77GMainLoop *hostnamed_loop;
78
79guint bus_descriptor;
80gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
81
19c6b83d 82gchar *HOSTNAME, *STATIC_HOSTNAME, *PRETTY_HOSTNAME;
a1bcc33c 83gchar *CHASSIS, *ICON;
19c6b83d 84gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS, *OS_CPENAME;
a1bcc33c 85
86/* TODO no specific vm or laptop icon in gnome
87 * NOTE paravirtualization on xen is only available for linuxes right now
88 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
89 * but we will worry about those later */
c7028b11 90
91/* add any sysctl strings that suggest virtualization here */
a1bcc33c 92const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] =
93{
87df323f 94 { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */
6edc347a 95 { "KVM", "vm", "drive-multidisk", FALSE, FALSE },
b21074ae 96 { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */
87df323f 97 { "VirtualBox", "vm", "drive-multidisk", TRUE, TRUE },
98 { "VMware, Inc.", "vm", "drive-multidisk", TRUE, TRUE },
99 { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE, TRUE },
100 { "Parallels", "vm", "drive-multidisk", TRUE, TRUE }, /* need verification */
c7028b11 101 { "Xen", "vm", "drive-multidisk", FALSE, FALSE }
87df323f 102}; /* TODO: chroots, etc. are the actual "containers", add them */
76b67a18 103
f8257c5d 104/* archs to check against when determining if machine is server */
105const gchar *server_archs[] = {
106 "hppa",
107 "sparc",
108 "sparc64"
109};
f1ad9351 110
a1bcc33c 111/* --- begin method/property/dbus signal code --- */
f1ad9351 112
5b70f403 113/* TODO the extra boolean passed to these funcs is for policykit auth */
114/* TODO complete call with error, message, etc */
3d53b501 115static gboolean
1be94ede 116on_handle_set_hostname(Hostname1 *hn1_passed_interf,
3d53b501 117 GDBusMethodInvocation *invoc,
118 const gchar *greet,
119 gpointer data) {
5b70f403 120 GVariant *params;
121 gchar *proposed_hostname, *valid_hostname_buf;
122 gboolean policykit_auth, ret;
123 size_t check_length, bad_length;
124
125 bad_length = MAXHOSTNAMELEN + 1;
126 proposed_hostname = NULL;
127 ret = FALSE;
128
129 params = g_dbus_method_invocation_get_parameters(invoc);
130 g_variant_get(params, "(sb)", &proposed_hostname, &policykit_auth);
131
132 if(proposed_hostname && (valid_hostname_buf = g_hostname_to_ascii(proposed_hostname))) {
133
134 check_length = strnlen(proposed_hostname, bad_length);
135
136 if(check_length < bad_length && !sethostname(proposed_hostname, check_length))
137 ret = TRUE;
138 }
139
904d744d 140 if(ret)
141 hostname1_complete_set_hostname(hn1_passed_interf, invoc);
5b70f403 142
143 if(proposed_hostname)
144 g_free(proposed_hostname);
145 if(valid_hostname_buf)
146 g_free(valid_hostname_buf);
147
148 return ret;
3d53b501 149}
150
151static gboolean
1be94ede 152on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 153 GDBusMethodInvocation *invoc,
154 const gchar *greet,
155 gpointer data) {
156 return FALSE;
157}
158
159static gboolean
1be94ede 160on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 161 GDBusMethodInvocation *invoc,
162 const gchar *greet,
163 gpointer data) {
164 return FALSE;
165}
166
167static gboolean
1be94ede 168on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 169 GDBusMethodInvocation *invoc,
170 const gchar *greet,
171 gpointer data) {
172 return FALSE;
173}
174
175static gboolean
1be94ede 176on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 177 GDBusMethodInvocation *invoc,
178 const gchar *greet,
179 gpointer data) {
180 return FALSE;
181}
182
76b67a18 183/* note: all hostnamed/hostname1's properties are read-only,
184 * and do not need set_ functions, gdbus-codegen realized
185 * this from the XML and handled the to-be error of trying
186 * to set a read-only property's value
187 */
188
189const gchar *
190our_get_hostname() {
191
19c6b83d 192 if(HOSTNAME)
193 return HOSTNAME;
194
195 return "localhost";
76b67a18 196}
197
198const gchar *
199our_get_static_hostname() {
200
19c6b83d 201 if(STATIC_HOSTNAME)
202 return STATIC_HOSTNAME;
203 else if(HOSTNAME)
204 return HOSTNAME;
205
206 return "localhost";
76b67a18 207}
208
209const gchar *
210our_get_pretty_hostname() {
211
19c6b83d 212 if(PRETTY_HOSTNAME)
213 return PRETTY_HOSTNAME;
214
a2fffc07 215 return "";
76b67a18 216}
217
218const gchar *
219our_get_chassis() {
220
f8257c5d 221 if(CHASSIS)
222 return CHASSIS;
f1ad9351 223
19c6b83d 224 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
76b67a18 225}
226
227const gchar *
228our_get_icon_name() {
229
f8257c5d 230 if(ICON)
231 return ICON;
232
233 return "";
76b67a18 234}
235
236const gchar *
237our_get_kernel_name() {
238
bd24ff31 239 if(KERN_NAME)
240 return KERN_NAME;
241
242 return "";
76b67a18 243}
244
245const gchar *
246our_get_kernel_version() {
247
bd24ff31 248 if(KERN_VERS)
249 return KERN_VERS;
250
251 return "";
76b67a18 252}
253
254const gchar *
255our_get_kernel_release() {
256
bd24ff31 257 if(KERN_RELEASE)
258 return KERN_RELEASE;
259
260 return "";
76b67a18 261}
262
263const gchar *
264our_get_os_cpename() {
265
bd24ff31 266 return "ONEDAY";
76b67a18 267}
268
269const gchar *
270our_get_os_pretty_name() {
271
bd24ff31 272 return "OpenBSD";
76b67a18 273}
274
1ce41045 275/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 276
5b005882 277static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 278 const gchar *name,
279 gpointer user_data) {
d1e1db9e 280
0f339959 281 g_printf("got bus/name, exporting %s's interface...\n", name);
d1e1db9e 282
90f54407 283 hostnamed_interf = hostname1_skeleton_new();
3d53b501 284
76b67a18 285 /* attach function pointers to generated struct's method handlers */
3d53b501 286 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
287 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
288 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
289 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
290 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
291
76b67a18 292 /* set our properties before export */
1be94ede 293 hostname1_set_hostname(hostnamed_interf, our_get_hostname());
294 hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
295 hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
296 hostname1_set_chassis(hostnamed_interf, our_get_chassis());
297 hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
298 hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
299 hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
300 hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
301 hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
302 hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
76b67a18 303
3d53b501 304 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 305 conn,
306 "/org/freedesktop/hostname1",
307 NULL)) {
3d53b501 308
0f339959 309 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
90f54407 310 hostnamed_mem_clean();
3d53b501 311
0f339959 312 } else {
ea207ed3 313
90f54407 314 dbus_interface_exported = TRUE;
315 g_printf("exported %s's interface on the system bus...\n", name);
316 }
0f339959 317}
1e8c7c88 318
0f339959 319static void hostnamed_on_name_acquired(GDBusConnection *conn,
90f54407 320 const gchar *name,
0f339959 321 gpointer user_data) {
322
323 g_printf("success!\n");
1e8c7c88 324}
325
5b005882 326static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 327 const gchar *name,
328 gpointer user_data) {
80043b36 329
90f54407 330 if(!conn) {
0f339959 331
90f54407 332 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);
333 hostnamed_mem_clean();
334 }
0f339959 335
509599f0 336 g_printf("lost name %s, exiting...\n", name);
fd8852d9 337
1cd5e6fe 338 hostnamed_mem_clean();
0f339959 339}
340
341/* --- end bus/name handlers, begin misc unix functions --- */
342
343/* safe call to clean and then exit
509599f0 344 * this stops our GMainLoop safely before letting main() return */
0f339959 345void hostnamed_mem_clean() {
346
90f54407 347 g_printf("exiting...\n");
0f339959 348
90f54407 349 if(dbus_interface_exported)
350 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
0f339959 351
90f54407 352 if(g_main_loop_is_running(hostnamed_loop))
353 g_main_loop_quit(hostnamed_loop);
fd8852d9 354
ea207ed3 355}
356
0f339959 357/* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
358gboolean unix_sig_terminate_handler(gpointer data) {
c62bceb7 359
90f54407 360 g_printf("caught SIGINT/HUP/TERM, exiting\n");
0f339959 361
90f54407 362 hostnamed_mem_clean();
363 return G_SOURCE_REMOVE;
0f339959 364}
365
366void set_signal_handlers() {
367
90f54407 368 /* we don't care about its descriptor, we never need to unregister these */
369 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
370 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
371 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
04cc16f2 372
90f54407 373 /* TODO: the "only once" guarantee only counts towards specific signals.
374 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
375 * to be called twice */
0f339959 376}
377
378int main() {
bd24ff31 379
380 hostnamed_freeable = g_ptr_array_new();
381
a0ebb315 382 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
bd24ff31 383
384 CHASSIS = ICON = OS_CPENAME = 0;
385 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
baf05b70 386 HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL;
a0ebb315 387
90f54407 388 set_signal_handlers();
387173cb 389
46835f3e 390 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
a1bcc33c 391 return 1;
392
baf05b70 393 hostnamed_loop = g_main_loop_new(NULL, TRUE);
fd8852d9 394
90f54407 395 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
0f339959 396 "org.freedesktop.hostname1",
397 G_BUS_NAME_OWNER_FLAGS_NONE,
398 hostnamed_on_bus_acquired,
399 hostnamed_on_name_acquired,
400 hostnamed_on_name_lost,
401 NULL,
402 NULL);
496f5d66 403
90f54407 404 g_main_loop_run(hostnamed_loop);
405 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
406 g_main_loop_unref(hostnamed_loop);
1e8c7c88 407
90f54407 408 /* guaranteed unownable */
409 g_bus_unown_name(bus_descriptor);
c62bceb7 410
90f54407 411 /* at this point no operations can occur with our data, it is safe to free it + its container */
412 g_ptr_array_free(hostnamed_freeable, TRUE);
7323a4e4 413
90f54407 414 return 0;
a35a69c5 415}
416
46835f3e 417gboolean set_names() {
418
baf05b70 419 /* (1) set up */
420 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
421 GKeyFile *config;
46835f3e 422 size_t hostname_divider;
423
baf05b70 424 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
425 static_hostname_buf = (gchar*) g_malloc0(4096);
426 pretty_hostname_buf = (gchar*) g_malloc0(4096);
46835f3e 427
baf05b70 428 config = g_key_file_new();
46835f3e 429
baf05b70 430 g_ptr_array_add(hostnamed_freeable, hostname_buf);
431 g_ptr_array_add(hostnamed_freeable, static_hostname_buf);
432 g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf);
46835f3e 433
baf05b70 434 /* (2) set HOSTNAME */
435 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
436 HOSTNAME = "localhost";
46835f3e 437
baf05b70 438 HOSTNAME = hostname_buf;
46835f3e 439
baf05b70 440 /* this bit gets you the /etc/myname style hostname
441 hostname_divider = strcspn(hostname_buf, ".");
442 strncpy(ret, hostname_buf, hostname_divider); */
46835f3e 443
baf05b70 444 /* (3) set PRETTY_HOSTNAME */
46835f3e 445 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
baf05b70 446 && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL)))
447 PRETTY_HOSTNAME = pretty_hostname_buf;
448 else
449 PRETTY_HOSTNAME = "";
450
46835f3e 451 if(config)
baf05b70 452 g_key_file_unref(config);
46835f3e 453
baf05b70 454 /* (4) set STATIC_HOSTNAME */
455 if(!g_strcmp0(PRETTY_HOSTNAME, ""))
456 STATIC_HOSTNAME = HOSTNAME;
46835f3e 457
baf05b70 458 else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
459 STATIC_HOSTNAME = static_hostname_buf;
46835f3e 460
baf05b70 461 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
46835f3e 462
46835f3e 463}
464
bd24ff31 465gboolean set_uname_properties() {
466
467 struct utsname un;
468
469 if(-1 == uname(&un))
470 return FALSE;
471
472 KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname));
473 g_ptr_array_add(hostnamed_freeable, KERN_NAME);
474 g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname));
475
476 KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release));
477 g_ptr_array_add(hostnamed_freeable, KERN_RELEASE);
478 g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release));
479
480 KERN_VERS = (gchar*)g_malloc0(sizeof(un.version));
481 g_ptr_array_add(hostnamed_freeable, KERN_VERS);
482 g_strlcpy(KERN_VERS, un.version, sizeof(un.version));
483
484 return TRUE;
485}
486
a1bcc33c 487gboolean determine_chassis_and_icon() {
488
b81ab32a 489 const size_t bufsize = 4096;
490
f8257c5d 491 char *hwproduct, *hwmodel, *hwvendor, *hwmachine;
492 size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size;
493 int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2];
f1ad9351 494 unsigned int i;
f8257c5d 495 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
496
b81ab32a 497 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
68e777c6 498 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
5ac7f542 499 i = 0;
f1ad9351 500
b81ab32a 501 hwproduct = (char*)g_malloc0(4096);
502 hwmodel = (char*)g_malloc0(4096);
503 hwvendor = (char*)g_malloc0(4096);
504 hwmachine = (char*)g_malloc0(4096);
505
bd24ff31 506 g_ptr_array_add(hostnamed_freeable, hwproduct);
507 g_ptr_array_add(hostnamed_freeable, hwmodel);
508 g_ptr_array_add(hostnamed_freeable, hwvendor);
509 g_ptr_array_add(hostnamed_freeable, hwmachine);
510
a1bcc33c 511 hwproduct_name[0] = CTL_HW;
512 hwproduct_name[1] = HW_PRODUCT;
f1ad9351 513
a1bcc33c 514 hwmodel_name[0] = CTL_HW;
515 hwmodel_name[1] = HW_MODEL;
516
517 hwvendor_name[0] = CTL_HW;
518 hwvendor_name[1] = HW_VENDOR;
519
f8257c5d 520 hwmachine_name[0] = CTL_HW;
521 hwmachine_name[1] = HW_MACHINE;
522
a1bcc33c 523 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
524 if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0))
525 return FALSE;
526
527 if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
528 return FALSE;
529
530 if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
531 return FALSE;
532
f8257c5d 533 if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0))
534 return FALSE;
535
a1bcc33c 536 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
537 * probably move this code to vm test func and set a global after running it early, once */
538
f8257c5d 539 for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) {
540 if(strcasestr(hwproduct, chassis_indicator_table[i].match_string)
541 || strcasestr(hwmodel, chassis_indicator_table[i].match_string)
542 || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) {
543
544 if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) {
545
546 UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence;
547 CHASSIS = chassis_indicator_table[i].chassis;
548 }
549
550 if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) {
551
552 UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence;
553 ICON = chassis_indicator_table[i].icon;
554 }
555 }
556 }
557
558 if(up_native_is_laptop()) {
559
560 if(!CHASSIS)
561 CHASSIS = "laptop";
562 if(!ICON)
563 ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
564
565 } else if(is_server(hwmachine)) {
566
567 if(!CHASSIS)
568 CHASSIS = "server";
569 if(!ICON)
570 ICON = "uninterruptible-power-supply";
571
572 } else if(!CHASSIS || !ICON) {
f1ad9351 573
f8257c5d 574 if(!CHASSIS)
575 CHASSIS = "desktop";
576 if(!ICON)
577 ICON = "computer";
578 }
579
580 return (CHASSIS && ICON);
581}
582
583gboolean is_server(gchar *arch) {
584
585 unsigned int i;
a1bcc33c 586
f8257c5d 587 for(; i < G_N_ELEMENTS(server_archs); i++)
588 if(strcasestr(arch, server_archs[i]))
589 return TRUE;
590
591 return FALSE;
592}
593
594gboolean up_native_is_laptop() {
595
596 struct apm_power_info bstate;
597 struct sensordev acpiac;
598
599 if (up_native_get_sensordev("acpiac0", &acpiac))
600 return TRUE;
601
602 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
603 g_error("ioctl on apm fd failed : %s", g_strerror(errno));
604
605 return bstate.ac_state != APM_AC_UNKNOWN;
606}
607
608int up_apm_get_fd() {
609
610 static int apm_fd = 0;
611
612 if(apm_fd == 0) {
613
614 g_debug("apm_fd is not initialized yet, opening");
615
616 /* open /dev/apm */
617 if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
618 if(errno != ENXIO && errno != ENOENT)
619 g_error("cannot open device file");
620 }
621 }
622
623 return apm_fd;
624}
625
626gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
627
628 int devn;
629 size_t sdlen = sizeof(struct sensordev);
630 int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
631
632 for (devn = 0 ; ; devn++) {
633 mib[2] = devn;
634 if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
635 if(errno == ENXIO)
636 continue;
637 if(errno == ENOENT)
638 break;
639 }
640
641 if (!strcmp(snsrdev->xname, id))
642 return TRUE;
643 }
644
645 return FALSE;
f1ad9351 646}