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