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