add SetHostname functionality
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
3b82e3c1 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
a35a69c5 17#include <unistd.h>
18#include <limits.h>
0f339959 19#include <signal.h>
f8257c5d 20#include <string.h>
fd8852d9 21
d15318db 22#include <sys/param.h>
f1ad9351 23#include <sys/sysctl.h>
f8257c5d 24#include <sys/sensors.h>
25#include <sys/ioctl.h>
bd24ff31 26#include <sys/utsname.h>
f8257c5d 27
28#include <machine/apmvar.h>
d15318db 29
4c04b514 30#include <glib/gprintf.h>
8caf1f61 31#include <glib-unix.h>
a1bcc33c 32/* #include <gtk/gtk.h> */
4c04b514 33
1e8c7c88 34#include "hostnamed-gen.h"
0f339959 35#include "hostnamed.h"
a35a69c5 36
a1bcc33c 37/* format: {
38 * (1) string to be matched against runtime machine's sysctl output.
39 * can be either the exact string or a substring contained
40 * within sysctl strings. no "guesses" here, a match should
41 * reliably indicate the chassis/icon.
42 *
43 * (2) string describing chassis type divulged by (1).
44 * must be one of "desktop", "laptop", "server",
45 * "tablet", "handset", "vm", "container" or NULL
46 * if only icon string can be ascertained. "vm" refers
47 * to operating systems running on baremetal hypervisors
48 * (hardware virtualization, like XEN) while "container"
49 * refers to OSs running on shared hypervisors like
50 * virtualbox or VMware. consider the distinction carefully
51 * as common virtualization software like KVM may share
52 * characteristics of both "vm" and "container" types.
53 *
54 * (3) string specifying icon to use. follows XDG icon spec.
55 * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
56 * for allowed strings.
57 *
58 * (4) chassis precedence bit. TRUE if (2) is defined and
59 * we're certain it is the proper string. FALSE in the
60 * circumstance (2) may be the correct string, unless
61 * a match with this bit set to TRUE overrides it.
62 * if (2) is NULL, this bit is inconsequential.
63 *
64 * (5) icon precedence bit. see previous definition.
65 * } */
66struct SYSCTL_LOOKUP_TABLE {
67 gchar *match_string;
68 gchar *chassis;
69 gchar *icon;
70 gboolean chassis_precedence;
71 gboolean icon_precedence;
72};
73
fd8852d9 74GPtrArray *hostnamed_freeable;
1be94ede 75Hostname1 *hostnamed_interf;
3d53b501 76
0f339959 77GMainLoop *hostnamed_loop;
78
79guint bus_descriptor;
80gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
81
a1bcc33c 82gchar *CHASSIS, *ICON;
bd24ff31 83gchar *OS_CPENAME;
84gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS;
a1bcc33c 85
86/* TODO no specific vm or laptop icon in gnome
87 * NOTE paravirtualization on xen is only available for linuxes right now
88 * dmesg on linux systems reveals xen and virtualization method (HVM or PVM)
89 * but we will worry about those later */
c7028b11 90
91/* add any sysctl strings that suggest virtualization here */
a1bcc33c 92const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] =
93{
87df323f 94 { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */
6edc347a 95 { "KVM", "vm", "drive-multidisk", FALSE, FALSE },
b21074ae 96 { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */
87df323f 97 { "VirtualBox", "vm", "drive-multidisk", TRUE, TRUE },
98 { "VMware, Inc.", "vm", "drive-multidisk", TRUE, TRUE },
99 { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE, TRUE },
100 { "Parallels", "vm", "drive-multidisk", TRUE, TRUE }, /* need verification */
c7028b11 101 { "Xen", "vm", "drive-multidisk", FALSE, FALSE }
87df323f 102}; /* TODO: chroots, etc. are the actual "containers", add them */
76b67a18 103
f8257c5d 104/* archs to check against when determining if machine is server */
105const gchar *server_archs[] = {
106 "hppa",
107 "sparc",
108 "sparc64"
109};
f1ad9351 110
a1bcc33c 111/* --- begin method/property/dbus signal code --- */
f1ad9351 112
5b70f403 113/* TODO the extra boolean passed to these funcs is for policykit auth */
114/* TODO complete call with error, message, etc */
3d53b501 115static gboolean
1be94ede 116on_handle_set_hostname(Hostname1 *hn1_passed_interf,
3d53b501 117 GDBusMethodInvocation *invoc,
118 const gchar *greet,
119 gpointer data) {
5b70f403 120 GVariant *params;
121 gchar *proposed_hostname, *valid_hostname_buf;
122 gboolean policykit_auth, ret;
123 size_t check_length, bad_length;
124
125 bad_length = MAXHOSTNAMELEN + 1;
126 proposed_hostname = NULL;
127 ret = FALSE;
128
129 params = g_dbus_method_invocation_get_parameters(invoc);
130 g_variant_get(params, "(sb)", &proposed_hostname, &policykit_auth);
131
132 if(proposed_hostname && (valid_hostname_buf = g_hostname_to_ascii(proposed_hostname))) {
133
134 check_length = strnlen(proposed_hostname, bad_length);
135
136 if(check_length < bad_length && !sethostname(proposed_hostname, check_length))
137 ret = TRUE;
138 }
139
140 hostname1_complete_set_hostname(hn1_passed_interf, invoc);
141
142 if(proposed_hostname)
143 g_free(proposed_hostname);
144 if(valid_hostname_buf)
145 g_free(valid_hostname_buf);
146
147 return ret;
3d53b501 148}
149
150static gboolean
1be94ede 151on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 152 GDBusMethodInvocation *invoc,
153 const gchar *greet,
154 gpointer data) {
155 return FALSE;
156}
157
158static gboolean
1be94ede 159on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 160 GDBusMethodInvocation *invoc,
161 const gchar *greet,
162 gpointer data) {
163 return FALSE;
164}
165
166static gboolean
1be94ede 167on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 168 GDBusMethodInvocation *invoc,
169 const gchar *greet,
170 gpointer data) {
171 return FALSE;
172}
173
174static gboolean
1be94ede 175on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 176 GDBusMethodInvocation *invoc,
177 const gchar *greet,
178 gpointer data) {
179 return FALSE;
180}
181
76b67a18 182/* note: all hostnamed/hostname1's properties are read-only,
183 * and do not need set_ functions, gdbus-codegen realized
184 * this from the XML and handled the to-be error of trying
185 * to set a read-only property's value
186 */
187
188const gchar *
189our_get_hostname() {
190
90f54407 191 gchar *hostname_buf, *ret;
192 size_t hostname_divider;
d15318db 193
a2fffc07 194 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
90f54407 195 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
a2fffc07 196
90f54407 197 g_ptr_array_add(hostnamed_freeable, hostname_buf);
198 g_ptr_array_add(hostnamed_freeable, ret);
7323a4e4 199
a2fffc07 200 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0)
201 return "localhost";
d15318db 202
90f54407 203 hostname_divider = strcspn(hostname_buf, ".");
28cac2f0 204
90f54407 205 return strncpy(ret, hostname_buf, hostname_divider);
76b67a18 206}
207
208const gchar *
209our_get_static_hostname() {
210
af14252f 211 const gchar *pretty_hostname;
212 const gchar *ret;
a2fffc07 213
214 pretty_hostname = our_get_pretty_hostname();
215
216 if(g_strcmp0(pretty_hostname, "") == 0)
217 ret = our_get_hostname();
218
af14252f 219 else if((ret = g_hostname_to_ascii(pretty_hostname))) {
a2fffc07 220
af14252f 221 g_ptr_array_add(hostnamed_freeable, (gpointer)ret);
a2fffc07 222 return ret;
223 }
224
225 return ret;
76b67a18 226}
227
228const gchar *
229our_get_pretty_hostname() {
230
a2fffc07 231 GKeyFile *config;
232 gchar *ret;
233
f1ad9351 234 config = g_key_file_new();
235
a2fffc07 236 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
af14252f 237 && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */
a2fffc07 238
f1ad9351 239 g_key_file_unref(config);
a2fffc07 240 return ret;
241 }
242
243 if(config)
244 g_free(config);
245
246 return "";
76b67a18 247}
248
249const gchar *
250our_get_chassis() {
251
f8257c5d 252 if(CHASSIS)
253 return CHASSIS;
f1ad9351 254
f8257c5d 255 return "desktop";
76b67a18 256}
257
258const gchar *
259our_get_icon_name() {
260
f8257c5d 261 if(ICON)
262 return ICON;
263
264 return "";
76b67a18 265}
266
267const gchar *
268our_get_kernel_name() {
269
bd24ff31 270 if(KERN_NAME)
271 return KERN_NAME;
272
273 return "";
76b67a18 274}
275
276const gchar *
277our_get_kernel_version() {
278
bd24ff31 279 if(KERN_VERS)
280 return KERN_VERS;
281
282 return "";
76b67a18 283}
284
285const gchar *
286our_get_kernel_release() {
287
bd24ff31 288 if(KERN_RELEASE)
289 return KERN_RELEASE;
290
291 return "";
76b67a18 292}
293
294const gchar *
295our_get_os_cpename() {
296
bd24ff31 297 return "ONEDAY";
76b67a18 298}
299
300const gchar *
301our_get_os_pretty_name() {
302
bd24ff31 303 return "OpenBSD";
76b67a18 304}
305
1ce41045 306/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 307
5b005882 308static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 309 const gchar *name,
310 gpointer user_data) {
d1e1db9e 311
0f339959 312 g_printf("got bus/name, exporting %s's interface...\n", name);
d1e1db9e 313
90f54407 314 hostnamed_interf = hostname1_skeleton_new();
3d53b501 315
76b67a18 316 /* attach function pointers to generated struct's method handlers */
3d53b501 317 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
318 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
319 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
320 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
321 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
322
76b67a18 323 /* set our properties before export */
1be94ede 324 hostname1_set_hostname(hostnamed_interf, our_get_hostname());
325 hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
326 hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
327 hostname1_set_chassis(hostnamed_interf, our_get_chassis());
328 hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
329 hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
330 hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
331 hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
332 hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
333 hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
76b67a18 334
3d53b501 335 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 336 conn,
337 "/org/freedesktop/hostname1",
338 NULL)) {
3d53b501 339
0f339959 340 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
90f54407 341 hostnamed_mem_clean();
3d53b501 342
0f339959 343 } else {
ea207ed3 344
90f54407 345 dbus_interface_exported = TRUE;
346 g_printf("exported %s's interface on the system bus...\n", name);
347 }
0f339959 348}
1e8c7c88 349
0f339959 350static void hostnamed_on_name_acquired(GDBusConnection *conn,
90f54407 351 const gchar *name,
0f339959 352 gpointer user_data) {
353
354 g_printf("success!\n");
1e8c7c88 355}
356
5b005882 357static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 358 const gchar *name,
359 gpointer user_data) {
80043b36 360
90f54407 361 if(!conn) {
0f339959 362
90f54407 363 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);
364 hostnamed_mem_clean();
365 }
0f339959 366
509599f0 367 g_printf("lost name %s, exiting...\n", name);
fd8852d9 368
1cd5e6fe 369 hostnamed_mem_clean();
0f339959 370}
371
372/* --- end bus/name handlers, begin misc unix functions --- */
373
374/* safe call to clean and then exit
509599f0 375 * this stops our GMainLoop safely before letting main() return */
0f339959 376void hostnamed_mem_clean() {
377
90f54407 378 g_printf("exiting...\n");
0f339959 379
90f54407 380 if(dbus_interface_exported)
381 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
0f339959 382
90f54407 383 if(g_main_loop_is_running(hostnamed_loop))
384 g_main_loop_quit(hostnamed_loop);
fd8852d9 385
ea207ed3 386}
387
0f339959 388/* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
389gboolean unix_sig_terminate_handler(gpointer data) {
c62bceb7 390
90f54407 391 g_printf("caught SIGINT/HUP/TERM, exiting\n");
0f339959 392
90f54407 393 hostnamed_mem_clean();
394 return G_SOURCE_REMOVE;
0f339959 395}
396
397void set_signal_handlers() {
398
90f54407 399 /* we don't care about its descriptor, we never need to unregister these */
400 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
401 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
402 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
04cc16f2 403
90f54407 404 /* TODO: the "only once" guarantee only counts towards specific signals.
405 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
406 * to be called twice */
0f339959 407}
408
409int main() {
bd24ff31 410
411 hostnamed_freeable = g_ptr_array_new();
412
a0ebb315 413 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
bd24ff31 414
415 CHASSIS = ICON = OS_CPENAME = 0;
416 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
a0ebb315 417
90f54407 418 set_signal_handlers();
387173cb 419
bd24ff31 420 if(!determine_chassis_and_icon() || !set_uname_properties())
a1bcc33c 421 return 1;
422
423 hostnamed_loop = g_main_loop_new(NULL, TRUE);
fd8852d9 424
90f54407 425 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
0f339959 426 "org.freedesktop.hostname1",
427 G_BUS_NAME_OWNER_FLAGS_NONE,
428 hostnamed_on_bus_acquired,
429 hostnamed_on_name_acquired,
430 hostnamed_on_name_lost,
431 NULL,
432 NULL);
496f5d66 433
90f54407 434 g_main_loop_run(hostnamed_loop);
435 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
436 g_main_loop_unref(hostnamed_loop);
1e8c7c88 437
90f54407 438 /* guaranteed unownable */
439 g_bus_unown_name(bus_descriptor);
c62bceb7 440
90f54407 441 /* at this point no operations can occur with our data, it is safe to free it + its container */
442 g_ptr_array_free(hostnamed_freeable, TRUE);
7323a4e4 443
90f54407 444 return 0;
a35a69c5 445}
446
bd24ff31 447gboolean set_uname_properties() {
448
449 struct utsname un;
450
451 if(-1 == uname(&un))
452 return FALSE;
453
454 KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname));
455 g_ptr_array_add(hostnamed_freeable, KERN_NAME);
456 g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname));
457
458 KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release));
459 g_ptr_array_add(hostnamed_freeable, KERN_RELEASE);
460 g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release));
461
462 KERN_VERS = (gchar*)g_malloc0(sizeof(un.version));
463 g_ptr_array_add(hostnamed_freeable, KERN_VERS);
464 g_strlcpy(KERN_VERS, un.version, sizeof(un.version));
465
466 return TRUE;
467}
468
a1bcc33c 469gboolean determine_chassis_and_icon() {
470
b81ab32a 471 const size_t bufsize = 4096;
472
f8257c5d 473 char *hwproduct, *hwmodel, *hwvendor, *hwmachine;
474 size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size;
475 int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2];
f1ad9351 476 unsigned int i;
f8257c5d 477 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
478
b81ab32a 479 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
68e777c6 480 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
5ac7f542 481 i = 0;
f1ad9351 482
b81ab32a 483 hwproduct = (char*)g_malloc0(4096);
484 hwmodel = (char*)g_malloc0(4096);
485 hwvendor = (char*)g_malloc0(4096);
486 hwmachine = (char*)g_malloc0(4096);
487
bd24ff31 488 g_ptr_array_add(hostnamed_freeable, hwproduct);
489 g_ptr_array_add(hostnamed_freeable, hwmodel);
490 g_ptr_array_add(hostnamed_freeable, hwvendor);
491 g_ptr_array_add(hostnamed_freeable, hwmachine);
492
a1bcc33c 493 hwproduct_name[0] = CTL_HW;
494 hwproduct_name[1] = HW_PRODUCT;
f1ad9351 495
a1bcc33c 496 hwmodel_name[0] = CTL_HW;
497 hwmodel_name[1] = HW_MODEL;
498
499 hwvendor_name[0] = CTL_HW;
500 hwvendor_name[1] = HW_VENDOR;
501
f8257c5d 502 hwmachine_name[0] = CTL_HW;
503 hwmachine_name[1] = HW_MACHINE;
504
a1bcc33c 505 /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */
506 if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0))
507 return FALSE;
508
509 if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0))
510 return FALSE;
511
512 if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0))
513 return FALSE;
514
f8257c5d 515 if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0))
516 return FALSE;
517
a1bcc33c 518 /* TODO: test for laptop, if not, dmidecode for desktop vs. server
519 * probably move this code to vm test func and set a global after running it early, once */
520
f8257c5d 521 for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) {
522 if(strcasestr(hwproduct, chassis_indicator_table[i].match_string)
523 || strcasestr(hwmodel, chassis_indicator_table[i].match_string)
524 || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) {
525
526 if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) {
527
528 UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence;
529 CHASSIS = chassis_indicator_table[i].chassis;
530 }
531
532 if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) {
533
534 UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence;
535 ICON = chassis_indicator_table[i].icon;
536 }
537 }
538 }
539
540 if(up_native_is_laptop()) {
541
542 if(!CHASSIS)
543 CHASSIS = "laptop";
544 if(!ICON)
545 ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
546
547 } else if(is_server(hwmachine)) {
548
549 if(!CHASSIS)
550 CHASSIS = "server";
551 if(!ICON)
552 ICON = "uninterruptible-power-supply";
553
554 } else if(!CHASSIS || !ICON) {
f1ad9351 555
f8257c5d 556 if(!CHASSIS)
557 CHASSIS = "desktop";
558 if(!ICON)
559 ICON = "computer";
560 }
561
562 return (CHASSIS && ICON);
563}
564
565gboolean is_server(gchar *arch) {
566
567 unsigned int i;
a1bcc33c 568
f8257c5d 569 for(; i < G_N_ELEMENTS(server_archs); i++)
570 if(strcasestr(arch, server_archs[i]))
571 return TRUE;
572
573 return FALSE;
574}
575
576gboolean up_native_is_laptop() {
577
578 struct apm_power_info bstate;
579 struct sensordev acpiac;
580
581 if (up_native_get_sensordev("acpiac0", &acpiac))
582 return TRUE;
583
584 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate))
585 g_error("ioctl on apm fd failed : %s", g_strerror(errno));
586
587 return bstate.ac_state != APM_AC_UNKNOWN;
588}
589
590int up_apm_get_fd() {
591
592 static int apm_fd = 0;
593
594 if(apm_fd == 0) {
595
596 g_debug("apm_fd is not initialized yet, opening");
597
598 /* open /dev/apm */
599 if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
600 if(errno != ENXIO && errno != ENOENT)
601 g_error("cannot open device file");
602 }
603 }
604
605 return apm_fd;
606}
607
608gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) {
609
610 int devn;
611 size_t sdlen = sizeof(struct sensordev);
612 int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
613
614 for (devn = 0 ; ; devn++) {
615 mib[2] = devn;
616 if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
617 if(errno == ENXIO)
618 continue;
619 if(errno == ENOENT)
620 break;
621 }
622
623 if (!strcmp(snsrdev->xname, id))
624 return TRUE;
625 }
626
627 return FALSE;
f1ad9351 628}