consolidate name setting functionality into set_name()
[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>
a1bcc33c 32/* #include <gtk/gtk.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
140 hostname1_complete_set_hostname(hn1_passed_interf, invoc);
141
142 if(proposed_hostname)
143 g_free(proposed_hostname);
144 if(valid_hostname_buf)
145 g_free(valid_hostname_buf);
146
147 return ret;
3d53b501 148}
149
150static gboolean
1be94ede 151on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 152 GDBusMethodInvocation *invoc,
153 const gchar *greet,
154 gpointer data) {
155 return FALSE;
156}
157
158static gboolean
1be94ede 159on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 160 GDBusMethodInvocation *invoc,
161 const gchar *greet,
162 gpointer data) {
163 return FALSE;
164}
165
166static gboolean
1be94ede 167on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 168 GDBusMethodInvocation *invoc,
169 const gchar *greet,
170 gpointer data) {
171 return FALSE;
172}
173
174static gboolean
1be94ede 175on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 176 GDBusMethodInvocation *invoc,
177 const gchar *greet,
178 gpointer data) {
179 return FALSE;
180}
181
76b67a18 182/* note: all hostnamed/hostname1's properties are read-only,
183 * and do not need set_ functions, gdbus-codegen realized
184 * this from the XML and handled the to-be error of trying
185 * to set a read-only property's value
186 */
187
188const gchar *
189our_get_hostname() {
190
19c6b83d 191 if(HOSTNAME)
192 return HOSTNAME;
193
194 return "localhost";
76b67a18 195}
196
197const gchar *
198our_get_static_hostname() {
199
19c6b83d 200 if(STATIC_HOSTNAME)
201 return STATIC_HOSTNAME;
202 else if(HOSTNAME)
203 return HOSTNAME;
204
205 return "localhost";
76b67a18 206}
207
208const gchar *
209our_get_pretty_hostname() {
210
19c6b83d 211 if(PRETTY_HOSTNAME)
212 return PRETTY_HOSTNAME;
213
a2fffc07 214 return "";
76b67a18 215}
216
217const gchar *
218our_get_chassis() {
219
f8257c5d 220 if(CHASSIS)
221 return CHASSIS;
f1ad9351 222
19c6b83d 223 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
76b67a18 224}
225
226const gchar *
227our_get_icon_name() {
228
f8257c5d 229 if(ICON)
230 return ICON;
231
232 return "";
76b67a18 233}
234
235const gchar *
236our_get_kernel_name() {
237
bd24ff31 238 if(KERN_NAME)
239 return KERN_NAME;
240
241 return "";
76b67a18 242}
243
244const gchar *
245our_get_kernel_version() {
246
bd24ff31 247 if(KERN_VERS)
248 return KERN_VERS;
249
250 return "";
76b67a18 251}
252
253const gchar *
254our_get_kernel_release() {
255
bd24ff31 256 if(KERN_RELEASE)
257 return KERN_RELEASE;
258
259 return "";
76b67a18 260}
261
262const gchar *
263our_get_os_cpename() {
264
bd24ff31 265 return "ONEDAY";
76b67a18 266}
267
268const gchar *
269our_get_os_pretty_name() {
270
bd24ff31 271 return "OpenBSD";
76b67a18 272}
273
1ce41045 274/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 275
5b005882 276static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 277 const gchar *name,
278 gpointer user_data) {
d1e1db9e 279
0f339959 280 g_printf("got bus/name, exporting %s's interface...\n", name);
d1e1db9e 281
90f54407 282 hostnamed_interf = hostname1_skeleton_new();
3d53b501 283
76b67a18 284 /* attach function pointers to generated struct's method handlers */
3d53b501 285 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
286 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
287 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
288 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
289 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
290
76b67a18 291 /* set our properties before export */
1be94ede 292 hostname1_set_hostname(hostnamed_interf, our_get_hostname());
293 hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
294 hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
295 hostname1_set_chassis(hostnamed_interf, our_get_chassis());
296 hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
297 hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
298 hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
299 hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
300 hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
301 hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
76b67a18 302
3d53b501 303 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 304 conn,
305 "/org/freedesktop/hostname1",
306 NULL)) {
3d53b501 307
0f339959 308 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
90f54407 309 hostnamed_mem_clean();
3d53b501 310
0f339959 311 } else {
ea207ed3 312
90f54407 313 dbus_interface_exported = TRUE;
314 g_printf("exported %s's interface on the system bus...\n", name);
315 }
0f339959 316}
1e8c7c88 317
0f339959 318static void hostnamed_on_name_acquired(GDBusConnection *conn,
90f54407 319 const gchar *name,
0f339959 320 gpointer user_data) {
321
322 g_printf("success!\n");
1e8c7c88 323}
324
5b005882 325static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 326 const gchar *name,
327 gpointer user_data) {
80043b36 328
90f54407 329 if(!conn) {
0f339959 330
90f54407 331 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);
332 hostnamed_mem_clean();
333 }
0f339959 334
509599f0 335 g_printf("lost name %s, exiting...\n", name);
fd8852d9 336
1cd5e6fe 337 hostnamed_mem_clean();
0f339959 338}
339
340/* --- end bus/name handlers, begin misc unix functions --- */
341
342/* safe call to clean and then exit
509599f0 343 * this stops our GMainLoop safely before letting main() return */
0f339959 344void hostnamed_mem_clean() {
345
90f54407 346 g_printf("exiting...\n");
0f339959 347
90f54407 348 if(dbus_interface_exported)
349 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
0f339959 350
90f54407 351 if(g_main_loop_is_running(hostnamed_loop))
352 g_main_loop_quit(hostnamed_loop);
fd8852d9 353
ea207ed3 354}
355
0f339959 356/* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
357gboolean unix_sig_terminate_handler(gpointer data) {
c62bceb7 358
90f54407 359 g_printf("caught SIGINT/HUP/TERM, exiting\n");
0f339959 360
90f54407 361 hostnamed_mem_clean();
362 return G_SOURCE_REMOVE;
0f339959 363}
364
365void set_signal_handlers() {
366
90f54407 367 /* we don't care about its descriptor, we never need to unregister these */
368 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
369 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
370 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
04cc16f2 371
90f54407 372 /* TODO: the "only once" guarantee only counts towards specific signals.
373 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
374 * to be called twice */
0f339959 375}
376
377int main() {
bd24ff31 378
379 hostnamed_freeable = g_ptr_array_new();
380
a0ebb315 381 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
bd24ff31 382
383 CHASSIS = ICON = OS_CPENAME = 0;
384 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
baf05b70 385 HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL;
a0ebb315 386
90f54407 387 set_signal_handlers();
387173cb 388
46835f3e 389 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
a1bcc33c 390 return 1;
391
baf05b70 392 hostnamed_loop = g_main_loop_new(NULL, TRUE);
fd8852d9 393
90f54407 394 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
0f339959 395 "org.freedesktop.hostname1",
396 G_BUS_NAME_OWNER_FLAGS_NONE,
397 hostnamed_on_bus_acquired,
398 hostnamed_on_name_acquired,
399 hostnamed_on_name_lost,
400 NULL,
401 NULL);
496f5d66 402
90f54407 403 g_main_loop_run(hostnamed_loop);
404 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
405 g_main_loop_unref(hostnamed_loop);
1e8c7c88 406
90f54407 407 /* guaranteed unownable */
408 g_bus_unown_name(bus_descriptor);
c62bceb7 409
90f54407 410 /* at this point no operations can occur with our data, it is safe to free it + its container */
411 g_ptr_array_free(hostnamed_freeable, TRUE);
7323a4e4 412
90f54407 413 return 0;
a35a69c5 414}
415
46835f3e 416gboolean set_names() {
417
baf05b70 418 /* (1) set up */
419 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
420 GKeyFile *config;
46835f3e 421 size_t hostname_divider;
422
baf05b70 423 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
424 static_hostname_buf = (gchar*) g_malloc0(4096);
425 pretty_hostname_buf = (gchar*) g_malloc0(4096);
46835f3e 426
baf05b70 427 config = g_key_file_new();
46835f3e 428
baf05b70 429 g_ptr_array_add(hostnamed_freeable, hostname_buf);
430 g_ptr_array_add(hostnamed_freeable, static_hostname_buf);
431 g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf);
46835f3e 432
baf05b70 433 /* (2) set HOSTNAME */
434 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
435 HOSTNAME = "localhost";
46835f3e 436
baf05b70 437 HOSTNAME = hostname_buf;
46835f3e 438
baf05b70 439 /* this bit gets you the /etc/myname style hostname
440 hostname_divider = strcspn(hostname_buf, ".");
441 strncpy(ret, hostname_buf, hostname_divider); */
46835f3e 442
baf05b70 443 /* (3) set PRETTY_HOSTNAME */
46835f3e 444 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
baf05b70 445 && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL)))
446 PRETTY_HOSTNAME = pretty_hostname_buf;
447 else
448 PRETTY_HOSTNAME = "";
449
46835f3e 450 if(config)
baf05b70 451 g_key_file_unref(config);
46835f3e 452
baf05b70 453 /* (4) set STATIC_HOSTNAME */
454 if(!g_strcmp0(PRETTY_HOSTNAME, ""))
455 STATIC_HOSTNAME = HOSTNAME;
46835f3e 456
baf05b70 457 else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
458 STATIC_HOSTNAME = static_hostname_buf;
46835f3e 459
baf05b70 460 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
46835f3e 461
46835f3e 462}
463
bd24ff31 464gboolean set_uname_properties() {
465
466 struct utsname un;
467
468 if(-1 == uname(&un))
469 return FALSE;
470
471 KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname));
472 g_ptr_array_add(hostnamed_freeable, KERN_NAME);
473 g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname));
474
475 KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release));
476 g_ptr_array_add(hostnamed_freeable, KERN_RELEASE);
477 g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release));
478
479 KERN_VERS = (gchar*)g_malloc0(sizeof(un.version));
480 g_ptr_array_add(hostnamed_freeable, KERN_VERS);
481 g_strlcpy(KERN_VERS, un.version, sizeof(un.version));
482
483 return TRUE;
484}
485
a1bcc33c 486gboolean determine_chassis_and_icon() {
487
b81ab32a 488 const size_t bufsize = 4096;
489
f8257c5d 490 char *hwproduct, *hwmodel, *hwvendor, *hwmachine;
491 size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size;
492 int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2];
f1ad9351 493 unsigned int i;
f8257c5d 494 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
495
b81ab32a 496 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
68e777c6 497 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
5ac7f542 498 i = 0;
f1ad9351 499
b81ab32a 500 hwproduct = (char*)g_malloc0(4096);
501 hwmodel = (char*)g_malloc0(4096);
502 hwvendor = (char*)g_malloc0(4096);
503 hwmachine = (char*)g_malloc0(4096);
504
bd24ff31 505 g_ptr_array_add(hostnamed_freeable, hwproduct);
506 g_ptr_array_add(hostnamed_freeable, hwmodel);
507 g_ptr_array_add(hostnamed_freeable, hwvendor);
508 g_ptr_array_add(hostnamed_freeable, hwmachine);
509
a1bcc33c 510 hwproduct_name[0] = CTL_HW;
511 hwproduct_name[1] = HW_PRODUCT;
f1ad9351 512
a1bcc33c 513 hwmodel_name[0] = CTL_HW;
514 hwmodel_name[1] = HW_MODEL;
515
516 hwvendor_name[0] = CTL_HW;
517 hwvendor_name[1] = HW_VENDOR;
518
f8257c5d 519 hwmachine_name[0] = CTL_HW;
520 hwmachine_name[1] = HW_MACHINE;
521
a1bcc33c 522 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
523 if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0))
524 return FALSE;
525
526 if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
527 return FALSE;
528
529 if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
530 return FALSE;
531
f8257c5d 532 if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0))
533 return FALSE;
534
a1bcc33c 535 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
536 * probably move this code to vm test func and set a global after running it early, once */
537
f8257c5d 538 for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) {
539 if(strcasestr(hwproduct, chassis_indicator_table[i].match_string)
540 || strcasestr(hwmodel, chassis_indicator_table[i].match_string)
541 || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) {
542
543 if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) {
544
545 UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence;
546 CHASSIS = chassis_indicator_table[i].chassis;
547 }
548
549 if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) {
550
551 UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence;
552 ICON = chassis_indicator_table[i].icon;
553 }
554 }
555 }
556
557 if(up_native_is_laptop()) {
558
559 if(!CHASSIS)
560 CHASSIS = "laptop";
561 if(!ICON)
562 ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
563
564 } else if(is_server(hwmachine)) {
565
566 if(!CHASSIS)
567 CHASSIS = "server";
568 if(!ICON)
569 ICON = "uninterruptible-power-supply";
570
571 } else if(!CHASSIS || !ICON) {
f1ad9351 572
f8257c5d 573 if(!CHASSIS)
574 CHASSIS = "desktop";
575 if(!ICON)
576 ICON = "computer";
577 }
578
579 return (CHASSIS && ICON);
580}
581
582gboolean is_server(gchar *arch) {
583
584 unsigned int i;
a1bcc33c 585
f8257c5d 586 for(; i < G_N_ELEMENTS(server_archs); i++)
587 if(strcasestr(arch, server_archs[i]))
588 return TRUE;
589
590 return FALSE;
591}
592
593gboolean up_native_is_laptop() {
594
595 struct apm_power_info bstate;
596 struct sensordev acpiac;
597
598 if (up_native_get_sensordev("acpiac0", &acpiac))
599 return TRUE;
600
601 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
602 g_error("ioctl on apm fd failed : %s", g_strerror(errno));
603
604 return bstate.ac_state != APM_AC_UNKNOWN;
605}
606
607int up_apm_get_fd() {
608
609 static int apm_fd = 0;
610
611 if(apm_fd == 0) {
612
613 g_debug("apm_fd is not initialized yet, opening");
614
615 /* open /dev/apm */
616 if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
617 if(errno != ENXIO && errno != ENOENT)
618 g_error("cannot open device file");
619 }
620 }
621
622 return apm_fd;
623}
624
625gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
626
627 int devn;
628 size_t sdlen = sizeof(struct sensordev);
629 int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
630
631 for (devn = 0 ; ; devn++) {
632 mib[2] = devn;
633 if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
634 if(errno == ENXIO)
635 continue;
636 if(errno == ENOENT)
637 break;
638 }
639
640 if (!strcmp(snsrdev->xname, id))
641 return TRUE;
642 }
643
644 return FALSE;
f1ad9351 645}