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