add polkit auth object, compile it in makefile, add header to interfaces
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
baa40e28 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
71e3eef1 17#include <unistd.h>
18#include <limits.h>
3eb1ef91 19#include <signal.h>
041ce20b 20#include <string.h>
ed99526e 21
043bd2ed 22#include <sys/param.h>
962fad6d 23#include <sys/sysctl.h>
041ce20b 24#include <sys/sensors.h>
25#include <sys/ioctl.h>
8504b5df 26#include <sys/utsname.h>
041ce20b 27
28#include <machine/apmvar.h>
043bd2ed 29
cd16a588 30#include <glib/gprintf.h>
ba9914b6 31#include <glib-unix.h>
eb78abe9 32#include <polkit/polkit.h>
cd16a588 33
3c3794ac 34#include "hostnamed-gen.h"
3eb1ef91 35#include "hostnamed.h"
71e3eef1 36
06701625 37#include "../../polkit-auth.h"
38
7a9b162a 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
ed99526e 76GPtrArray *hostnamed_freeable;
9d2500b0 77Hostname1 *hostnamed_interf;
7ce16a35 78
3eb1ef91 79GMainLoop *hostnamed_loop;
80
81guint bus_descriptor;
82gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
83
5fcfaccc 84gchar *HOSTNAME, *STATIC_HOSTNAME, *PRETTY_HOSTNAME;
7a9b162a 85gchar *CHASSIS, *ICON;
5fcfaccc 86gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS, *OS_CPENAME;
7a9b162a 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 */
2e337f46 92
93/* add any sysctl strings that suggest virtualization here */
7a9b162a 94const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] =
95{
a194b156 96 { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */
7c613054 97 { "KVM", "vm", "drive-multidisk", FALSE, FALSE },
df95f94a 98 { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */
a194b156 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 */
2e337f46 103 { "Xen", "vm", "drive-multidisk", FALSE, FALSE }
a194b156 104}; /* TODO: chroots, etc. are the actual "containers", add them */
355f60ae 105
041ce20b 106/* archs to check against when determining if machine is server */
107const gchar *server_archs[] = {
108 "hppa",
109 "sparc",
110 "sparc64"
111};
962fad6d 112
7a9b162a 113/* --- begin method/property/dbus signal code --- */
962fad6d 114
3572d013 115/* TODO the extra boolean passed to these funcs is for policykit auth */
116/* TODO complete call with error, message, etc */
7ce16a35 117static gboolean
9d2500b0 118on_handle_set_hostname(Hostname1 *hn1_passed_interf,
7ce16a35 119 GDBusMethodInvocation *invoc,
120 const gchar *greet,
121 gpointer data) {
3572d013 122 GVariant *params;
123 gchar *proposed_hostname, *valid_hostname_buf;
124 gboolean policykit_auth, ret;
9e7c2b86 125 size_t check_length;
3572d013 126
3572d013 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
9e7c2b86 135 check_length = strnlen(proposed_hostname, MAXHOSTNAMELEN + 1);
3572d013 136
9e7c2b86 137 if(check_length > MAXHOSTNAMELEN)
138 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
3572d013 139
9e7c2b86 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.");
3572d013 142
9e7c2b86 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
3572d013 151 if(proposed_hostname)
152 g_free(proposed_hostname);
153 if(valid_hostname_buf)
154 g_free(valid_hostname_buf);
155
156 return ret;
7ce16a35 157}
158
159static gboolean
9d2500b0 160on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
7ce16a35 161 GDBusMethodInvocation *invoc,
162 const gchar *greet,
163 gpointer data) {
164 return FALSE;
165}
166
167static gboolean
9d2500b0 168on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
7ce16a35 169 GDBusMethodInvocation *invoc,
170 const gchar *greet,
171 gpointer data) {
172 return FALSE;
173}
174
175static gboolean
9d2500b0 176on_handle_set_chassis(Hostname1 *hn1_passed_interf,
7ce16a35 177 GDBusMethodInvocation *invoc,
178 const gchar *greet,
179 gpointer data) {
180 return FALSE;
181}
182
183static gboolean
9d2500b0 184on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
7ce16a35 185 GDBusMethodInvocation *invoc,
186 const gchar *greet,
187 gpointer data) {
188 return FALSE;
189}
190
355f60ae 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
9e7c2b86 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);
5fcfaccc 209 return HOSTNAME;
9e7c2b86 210 }
211
212 g_ptr_array_add(hostnamed_freeable, hostname_buf);
213 HOSTNAME = hostname_buf;
214 hostname1_set_hostname(hostnamed_interf, HOSTNAME);
5fcfaccc 215
9e7c2b86 216 return HOSTNAME;
355f60ae 217}
218
219const gchar *
220our_get_static_hostname() {
221
9e7c2b86 222 if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, ""))
5fcfaccc 223 return STATIC_HOSTNAME;
224 else if(HOSTNAME)
225 return HOSTNAME;
226
9e7c2b86 227 return "localhost.home.network";
355f60ae 228}
229
230const gchar *
231our_get_pretty_hostname() {
232
5fcfaccc 233 if(PRETTY_HOSTNAME)
234 return PRETTY_HOSTNAME;
235
8e18351c 236 return "";
355f60ae 237}
238
239const gchar *
240our_get_chassis() {
241
041ce20b 242 if(CHASSIS)
243 return CHASSIS;
962fad6d 244
5fcfaccc 245 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
355f60ae 246}
247
248const gchar *
249our_get_icon_name() {
250
041ce20b 251 if(ICON)
252 return ICON;
253
254 return "";
355f60ae 255}
256
257const gchar *
258our_get_kernel_name() {
259
8504b5df 260 if(KERN_NAME)
261 return KERN_NAME;
262
263 return "";
355f60ae 264}
265
266const gchar *
267our_get_kernel_version() {
268
8504b5df 269 if(KERN_VERS)
270 return KERN_VERS;
271
272 return "";
355f60ae 273}
274
275const gchar *
276our_get_kernel_release() {
277
8504b5df 278 if(KERN_RELEASE)
279 return KERN_RELEASE;
280
281 return "";
355f60ae 282}
283
284const gchar *
285our_get_os_cpename() {
286
8504b5df 287 return "ONEDAY";
355f60ae 288}
289
290const gchar *
291our_get_os_pretty_name() {
292
8504b5df 293 return "OpenBSD";
355f60ae 294}
295
ff036f75 296/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 297
828caf9a 298static void hostnamed_on_bus_acquired(GDBusConnection *conn,
9ce23618 299 const gchar *name,
300 gpointer user_data) {
11475670 301
3eb1ef91 302 g_printf("got bus/name, exporting %s's interface...\n", name);
11475670 303
99ef55dd 304 hostnamed_interf = hostname1_skeleton_new();
7ce16a35 305
355f60ae 306 /* attach function pointers to generated struct's method handlers */
7ce16a35 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
355f60ae 313 /* set our properties before export */
9d2500b0 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());
355f60ae 324
7ce16a35 325 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
9d2500b0 326 conn,
327 "/org/freedesktop/hostname1",
328 NULL)) {
7ce16a35 329
3eb1ef91 330 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
99ef55dd 331 hostnamed_mem_clean();
7ce16a35 332
3eb1ef91 333 } else {
ea207ed3 334
99ef55dd 335 dbus_interface_exported = TRUE;
336 g_printf("exported %s's interface on the system bus...\n", name);
337 }
3eb1ef91 338}
3c3794ac 339
3eb1ef91 340static void hostnamed_on_name_acquired(GDBusConnection *conn,
99ef55dd 341 const gchar *name,
3eb1ef91 342 gpointer user_data) {
343
344 g_printf("success!\n");
3c3794ac 345}
346
828caf9a 347static void hostnamed_on_name_lost(GDBusConnection *conn,
9d2500b0 348 const gchar *name,
349 gpointer user_data) {
80043b36 350
99ef55dd 351 if(!conn) {
3eb1ef91 352
99ef55dd 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 }
3eb1ef91 356
ca5a7f9f 357 g_printf("lost name %s, exiting...\n", name);
ed99526e 358
fbc0295f 359 hostnamed_mem_clean();
3eb1ef91 360}
361
362/* --- end bus/name handlers, begin misc unix functions --- */
363
364/* safe call to clean and then exit
ca5a7f9f 365 * this stops our GMainLoop safely before letting main() return */
3eb1ef91 366void hostnamed_mem_clean() {
367
99ef55dd 368 g_printf("exiting...\n");
3eb1ef91 369
99ef55dd 370 if(dbus_interface_exported)
371 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
3eb1ef91 372
99ef55dd 373 if(g_main_loop_is_running(hostnamed_loop))
374 g_main_loop_quit(hostnamed_loop);
ed99526e 375
ea207ed3 376}
377
3eb1ef91 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) {
a3041e78 380
99ef55dd 381 g_printf("caught SIGINT/HUP/TERM, exiting\n");
3eb1ef91 382
99ef55dd 383 hostnamed_mem_clean();
384 return G_SOURCE_REMOVE;
3eb1ef91 385}
386
387void set_signal_handlers() {
388
99ef55dd 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);
5c2460ab 393
99ef55dd 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 */
3eb1ef91 397}
398
399int main() {
8504b5df 400
401 hostnamed_freeable = g_ptr_array_new();
402
4babc29e 403 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
8504b5df 404
405 CHASSIS = ICON = OS_CPENAME = 0;
406 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
4a537080 407 HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL;
06701625 408
409 test_func();
4babc29e 410
99ef55dd 411 set_signal_handlers();
387173cb 412
e51864d1 413 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
7a9b162a 414 return 1;
415
4a537080 416 hostnamed_loop = g_main_loop_new(NULL, TRUE);
ed99526e 417
99ef55dd 418 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
3eb1ef91 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
99ef55dd 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);
3c3794ac 430
99ef55dd 431 /* guaranteed unownable */
432 g_bus_unown_name(bus_descriptor);
a3041e78 433
99ef55dd 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);
af9f8b50 436
99ef55dd 437 return 0;
71e3eef1 438}
439
e51864d1 440gboolean set_names() {
441
4a537080 442 /* (1) set up */
443 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
444 GKeyFile *config;
e51864d1 445 size_t hostname_divider;
446
4a537080 447 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
448 static_hostname_buf = (gchar*) g_malloc0(4096);
449 pretty_hostname_buf = (gchar*) g_malloc0(4096);
e51864d1 450
4a537080 451 config = g_key_file_new();
e51864d1 452
4a537080 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);
e51864d1 456
4a537080 457 /* (2) set HOSTNAME */
458 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
459 HOSTNAME = "localhost";
e51864d1 460
4a537080 461 HOSTNAME = hostname_buf;
e51864d1 462
4a537080 463 /* this bit gets you the /etc/myname style hostname
464 hostname_divider = strcspn(hostname_buf, ".");
465 strncpy(ret, hostname_buf, hostname_divider); */
e51864d1 466
4a537080 467 /* (3) set PRETTY_HOSTNAME */
e51864d1 468 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
4a537080 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
e51864d1 474 if(config)
4a537080 475 g_key_file_unref(config);
e51864d1 476
4a537080 477 /* (4) set STATIC_HOSTNAME */
478 if(!g_strcmp0(PRETTY_HOSTNAME, ""))
479 STATIC_HOSTNAME = HOSTNAME;
e51864d1 480
4a537080 481 else if((static_hostname_buf = g_hostname_to_ascii(PRETTY_HOSTNAME)))
482 STATIC_HOSTNAME = static_hostname_buf;
e51864d1 483
4a537080 484 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
e51864d1 485
e51864d1 486}
487
8504b5df 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
7a9b162a 510gboolean determine_chassis_and_icon() {
511
22b13250 512 const size_t bufsize = 4096;
513
041ce20b 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];
962fad6d 517 unsigned int i;
041ce20b 518 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
519
22b13250 520 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
a112db4c 521 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
1ed17375 522 i = 0;
962fad6d 523
22b13250 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
8504b5df 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
7a9b162a 534 hwproduct_name[0] = CTL_HW;
535 hwproduct_name[1] = HW_PRODUCT;
962fad6d 536
7a9b162a 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
041ce20b 543 hwmachine_name[0] = CTL_HW;
544 hwmachine_name[1] = HW_MACHINE;
545
7a9b162a 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
041ce20b 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
7a9b162a 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
041ce20b 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) {
962fad6d 596
041ce20b 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;
7a9b162a 609
041ce20b 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;
962fad6d 669}