add polkit auth object, compile it in makefile, add header to interfaces
[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
409 test_func();
a0ebb315 410
90f54407 411 set_signal_handlers();
387173cb 412
46835f3e 413 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
a1bcc33c 414 return 1;
415
baf05b70 416 hostnamed_loop = g_main_loop_new(NULL, TRUE);
fd8852d9 417
90f54407 418 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
0f339959 419 "org.freedesktop.hostname1",
420 G_BUS_NAME_OWNER_FLAGS_NONE,
421 hostnamed_on_bus_acquired,
422 hostnamed_on_name_acquired,
423 hostnamed_on_name_lost,
424 NULL,
425 NULL);
496f5d66 426
90f54407 427 g_main_loop_run(hostnamed_loop);
428 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
429 g_main_loop_unref(hostnamed_loop);
1e8c7c88 430
90f54407 431 /* guaranteed unownable */
432 g_bus_unown_name(bus_descriptor);
c62bceb7 433
90f54407 434 /* at this point no operations can occur with our data, it is safe to free it + its container */
435 g_ptr_array_free(hostnamed_freeable, TRUE);
7323a4e4 436
90f54407 437 return 0;
a35a69c5 438}
439
46835f3e 440gboolean set_names() {
441
baf05b70 442 /* (1) set up */
443 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
444 GKeyFile *config;
46835f3e 445 size_t hostname_divider;
446
baf05b70 447 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
448 static_hostname_buf = (gchar*) g_malloc0(4096);
449 pretty_hostname_buf = (gchar*) g_malloc0(4096);
46835f3e 450
baf05b70 451 config = g_key_file_new();
46835f3e 452
baf05b70 453 g_ptr_array_add(hostnamed_freeable, hostname_buf);
454 g_ptr_array_add(hostnamed_freeable, static_hostname_buf);
455 g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf);
46835f3e 456
baf05b70 457 /* (2) set HOSTNAME */
458 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
459 HOSTNAME = "localhost";
46835f3e 460
baf05b70 461 HOSTNAME = hostname_buf;
46835f3e 462
baf05b70 463 /* this bit gets you the /etc/myname style hostname
464 hostname_divider = strcspn(hostname_buf, ".");
465 strncpy(ret, hostname_buf, hostname_divider); */
46835f3e 466
baf05b70 467 /* (3) set PRETTY_HOSTNAME */
46835f3e 468 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
baf05b70 469 && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL)))
470 PRETTY_HOSTNAME = pretty_hostname_buf;
471 else
472 PRETTY_HOSTNAME = "";
473
46835f3e 474 if(config)
baf05b70 475 g_key_file_unref(config);
46835f3e 476
baf05b70 477 /* (4) set STATIC_HOSTNAME */
478 if(!g_strcmp0(PRETTY_HOSTNAME, ""))
479 STATIC_HOSTNAME = HOSTNAME;
46835f3e 480
baf05b70 481 else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
482 STATIC_HOSTNAME = static_hostname_buf;
46835f3e 483
baf05b70 484 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
46835f3e 485
46835f3e 486}
487
bd24ff31 488gboolean set_uname_properties() {
489
490 struct utsname un;
491
492 if(-1 == uname(&un))
493 return FALSE;
494
495 KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname));
496 g_ptr_array_add(hostnamed_freeable, KERN_NAME);
497 g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname));
498
499 KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release));
500 g_ptr_array_add(hostnamed_freeable, KERN_RELEASE);
501 g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release));
502
503 KERN_VERS = (gchar*)g_malloc0(sizeof(un.version));
504 g_ptr_array_add(hostnamed_freeable, KERN_VERS);
505 g_strlcpy(KERN_VERS, un.version, sizeof(un.version));
506
507 return TRUE;
508}
509
a1bcc33c 510gboolean determine_chassis_and_icon() {
511
b81ab32a 512 const size_t bufsize = 4096;
513
f8257c5d 514 char *hwproduct, *hwmodel, *hwvendor, *hwmachine;
515 size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size;
516 int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2];
f1ad9351 517 unsigned int i;
f8257c5d 518 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
519
b81ab32a 520 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
68e777c6 521 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
5ac7f542 522 i = 0;
f1ad9351 523
b81ab32a 524 hwproduct = (char*)g_malloc0(4096);
525 hwmodel = (char*)g_malloc0(4096);
526 hwvendor = (char*)g_malloc0(4096);
527 hwmachine = (char*)g_malloc0(4096);
528
bd24ff31 529 g_ptr_array_add(hostnamed_freeable, hwproduct);
530 g_ptr_array_add(hostnamed_freeable, hwmodel);
531 g_ptr_array_add(hostnamed_freeable, hwvendor);
532 g_ptr_array_add(hostnamed_freeable, hwmachine);
533
a1bcc33c 534 hwproduct_name[0] = CTL_HW;
535 hwproduct_name[1] = HW_PRODUCT;
f1ad9351 536
a1bcc33c 537 hwmodel_name[0] = CTL_HW;
538 hwmodel_name[1] = HW_MODEL;
539
540 hwvendor_name[0] = CTL_HW;
541 hwvendor_name[1] = HW_VENDOR;
542
f8257c5d 543 hwmachine_name[0] = CTL_HW;
544 hwmachine_name[1] = HW_MACHINE;
545
a1bcc33c 546 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
547 if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0))
548 return FALSE;
549
550 if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
551 return FALSE;
552
553 if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
554 return FALSE;
555
f8257c5d 556 if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0))
557 return FALSE;
558
a1bcc33c 559 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
560 * probably move this code to vm test func and set a global after running it early, once */
561
f8257c5d 562 for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) {
563 if(strcasestr(hwproduct, chassis_indicator_table[i].match_string)
564 || strcasestr(hwmodel, chassis_indicator_table[i].match_string)
565 || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) {
566
567 if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) {
568
569 UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence;
570 CHASSIS = chassis_indicator_table[i].chassis;
571 }
572
573 if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) {
574
575 UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence;
576 ICON = chassis_indicator_table[i].icon;
577 }
578 }
579 }
580
581 if(up_native_is_laptop()) {
582
583 if(!CHASSIS)
584 CHASSIS = "laptop";
585 if(!ICON)
586 ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
587
588 } else if(is_server(hwmachine)) {
589
590 if(!CHASSIS)
591 CHASSIS = "server";
592 if(!ICON)
593 ICON = "uninterruptible-power-supply";
594
595 } else if(!CHASSIS || !ICON) {
f1ad9351 596
f8257c5d 597 if(!CHASSIS)
598 CHASSIS = "desktop";
599 if(!ICON)
600 ICON = "computer";
601 }
602
603 return (CHASSIS && ICON);
604}
605
606gboolean is_server(gchar *arch) {
607
608 unsigned int i;
a1bcc33c 609
f8257c5d 610 for(; i < G_N_ELEMENTS(server_archs); i++)
611 if(strcasestr(arch, server_archs[i]))
612 return TRUE;
613
614 return FALSE;
615}
616
617gboolean up_native_is_laptop() {
618
619 struct apm_power_info bstate;
620 struct sensordev acpiac;
621
622 if (up_native_get_sensordev("acpiac0", &acpiac))
623 return TRUE;
624
625 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
626 g_error("ioctl on apm fd failed : %s", g_strerror(errno));
627
628 return bstate.ac_state != APM_AC_UNKNOWN;
629}
630
631int up_apm_get_fd() {
632
633 static int apm_fd = 0;
634
635 if(apm_fd == 0) {
636
637 g_debug("apm_fd is not initialized yet, opening");
638
639 /* open /dev/apm */
640 if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
641 if(errno != ENXIO && errno != ENOENT)
642 g_error("cannot open device file");
643 }
644 }
645
646 return apm_fd;
647}
648
649gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
650
651 int devn;
652 size_t sdlen = sizeof(struct sensordev);
653 int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
654
655 for (devn = 0 ; ; devn++) {
656 mib[2] = devn;
657 if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
658 if(errno == ENXIO)
659 continue;
660 if(errno == ENOENT)
661 break;
662 }
663
664 if (!strcmp(snsrdev->xname, id))
665 return TRUE;
666 }
667
668 return FALSE;
f1ad9351 669}