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> |
26 | |
27 | #include <machine/apmvar.h> |
d15318db |
28 | |
4c04b514 |
29 | #include <glib/gprintf.h> |
8caf1f61 |
30 | #include <glib-unix.h> |
a1bcc33c |
31 | /* #include <gtk/gtk.h> */ |
4c04b514 |
32 | |
1e8c7c88 |
33 | #include "hostnamed-gen.h" |
0f339959 |
34 | #include "hostnamed.h" |
a35a69c5 |
35 | |
a1bcc33c |
36 | /* add any sysctl strings that suggest virtualization here */ |
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 | * } */ |
66 | struct SYSCTL_LOOKUP_TABLE { |
67 | gchar *match_string; |
68 | gchar *chassis; |
69 | gchar *icon; |
70 | gboolean chassis_precedence; |
71 | gboolean icon_precedence; |
72 | }; |
73 | |
fd8852d9 |
74 | GPtrArray *hostnamed_freeable; |
1be94ede |
75 | Hostname1 *hostnamed_interf; |
3d53b501 |
76 | |
0f339959 |
77 | GMainLoop *hostnamed_loop; |
78 | |
79 | guint bus_descriptor; |
80 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
81 | |
a1bcc33c |
82 | gchar *CHASSIS, *ICON; |
83 | |
84 | /* TODO no specific vm or laptop icon in gnome |
85 | * NOTE paravirtualization on xen is only available for linuxes right now |
86 | * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) |
87 | * but we will worry about those later */ |
88 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
89 | { |
f8257c5d |
90 | { "QEMU Virtual CPU", "container", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ |
a1bcc33c |
91 | { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* oracle solaris kvm */ |
92 | { "VirtualBox", "container", "drive-optical", TRUE, TRUE }, |
93 | { "VMware, Inc.", "container", "drive-optical", TRUE, TRUE }, |
94 | { "VMware Virtual Platform", "container", "drive-optical", TRUE, TRUE }, |
95 | { "Parallels", "container", "drive-optical", TRUE, TRUE } /* need verification */ |
96 | }; |
76b67a18 |
97 | |
f8257c5d |
98 | /* archs to check against when determining if machine is server */ |
99 | const gchar *server_archs[] = { |
100 | "hppa", |
101 | "sparc", |
102 | "sparc64" |
103 | }; |
f1ad9351 |
104 | |
a1bcc33c |
105 | /* --- begin method/property/dbus signal code --- */ |
f1ad9351 |
106 | |
3d53b501 |
107 | static gboolean |
1be94ede |
108 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
109 | GDBusMethodInvocation *invoc, |
110 | const gchar *greet, |
111 | gpointer data) { |
112 | return FALSE; |
113 | } |
114 | |
115 | static gboolean |
1be94ede |
116 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
117 | GDBusMethodInvocation *invoc, |
118 | const gchar *greet, |
119 | gpointer data) { |
120 | return FALSE; |
121 | } |
122 | |
123 | static gboolean |
1be94ede |
124 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
125 | GDBusMethodInvocation *invoc, |
126 | const gchar *greet, |
127 | gpointer data) { |
128 | return FALSE; |
129 | } |
130 | |
131 | static gboolean |
1be94ede |
132 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 |
133 | GDBusMethodInvocation *invoc, |
134 | const gchar *greet, |
135 | gpointer data) { |
136 | return FALSE; |
137 | } |
138 | |
139 | static gboolean |
1be94ede |
140 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 |
141 | GDBusMethodInvocation *invoc, |
142 | const gchar *greet, |
143 | gpointer data) { |
144 | return FALSE; |
145 | } |
146 | |
76b67a18 |
147 | /* note: all hostnamed/hostname1's properties are read-only, |
148 | * and do not need set_ functions, gdbus-codegen realized |
149 | * this from the XML and handled the to-be error of trying |
150 | * to set a read-only property's value |
151 | */ |
152 | |
153 | const gchar * |
154 | our_get_hostname() { |
155 | |
90f54407 |
156 | gchar *hostname_buf, *ret; |
157 | size_t hostname_divider; |
d15318db |
158 | |
a2fffc07 |
159 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
90f54407 |
160 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
a2fffc07 |
161 | |
90f54407 |
162 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
163 | g_ptr_array_add(hostnamed_freeable, ret); |
7323a4e4 |
164 | |
a2fffc07 |
165 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0) |
166 | return "localhost"; |
d15318db |
167 | |
90f54407 |
168 | hostname_divider = strcspn(hostname_buf, "."); |
28cac2f0 |
169 | |
90f54407 |
170 | return strncpy(ret, hostname_buf, hostname_divider); |
76b67a18 |
171 | } |
172 | |
173 | const gchar * |
174 | our_get_static_hostname() { |
175 | |
af14252f |
176 | const gchar *pretty_hostname; |
177 | const gchar *ret; |
a2fffc07 |
178 | |
179 | pretty_hostname = our_get_pretty_hostname(); |
180 | |
181 | if(g_strcmp0(pretty_hostname, "") == 0) |
182 | ret = our_get_hostname(); |
183 | |
af14252f |
184 | else if((ret = g_hostname_to_ascii(pretty_hostname))) { |
a2fffc07 |
185 | |
af14252f |
186 | g_ptr_array_add(hostnamed_freeable, (gpointer)ret); |
a2fffc07 |
187 | return ret; |
188 | } |
189 | |
190 | return ret; |
76b67a18 |
191 | } |
192 | |
193 | const gchar * |
194 | our_get_pretty_hostname() { |
195 | |
a2fffc07 |
196 | GKeyFile *config; |
197 | gchar *ret; |
198 | |
f1ad9351 |
199 | config = g_key_file_new(); |
200 | |
a2fffc07 |
201 | if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL) |
af14252f |
202 | && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */ |
a2fffc07 |
203 | |
f1ad9351 |
204 | g_key_file_unref(config); |
a2fffc07 |
205 | return ret; |
206 | } |
207 | |
208 | if(config) |
209 | g_free(config); |
210 | |
211 | return ""; |
76b67a18 |
212 | } |
213 | |
214 | const gchar * |
215 | our_get_chassis() { |
216 | |
f8257c5d |
217 | if(CHASSIS) |
218 | return CHASSIS; |
f1ad9351 |
219 | |
f8257c5d |
220 | return "desktop"; |
76b67a18 |
221 | } |
222 | |
223 | const gchar * |
224 | our_get_icon_name() { |
225 | |
f8257c5d |
226 | if(ICON) |
227 | return ICON; |
228 | |
229 | return ""; |
76b67a18 |
230 | } |
231 | |
232 | const gchar * |
233 | our_get_kernel_name() { |
234 | |
235 | return "TODO"; |
236 | } |
237 | |
238 | const gchar * |
239 | our_get_kernel_version() { |
240 | |
241 | return "TODO"; |
242 | } |
243 | |
244 | const gchar * |
245 | our_get_kernel_release() { |
246 | |
247 | return "TODO"; |
248 | } |
249 | |
250 | const gchar * |
251 | our_get_os_cpename() { |
252 | |
253 | return "TODO"; |
254 | } |
255 | |
256 | const gchar * |
257 | our_get_os_pretty_name() { |
258 | |
259 | return "TODO"; |
260 | } |
261 | |
1ce41045 |
262 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
0df0018d |
263 | |
5b005882 |
264 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
1cd5e6fe |
265 | const gchar *name, |
266 | gpointer user_data) { |
d1e1db9e |
267 | |
0f339959 |
268 | g_printf("got bus/name, exporting %s's interface...\n", name); |
d1e1db9e |
269 | |
90f54407 |
270 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 |
271 | |
76b67a18 |
272 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 |
273 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
274 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
275 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
276 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
277 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
278 | |
76b67a18 |
279 | /* set our properties before export */ |
1be94ede |
280 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
281 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
282 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
283 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
284 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
285 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
286 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
287 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
288 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
289 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
76b67a18 |
290 | |
3d53b501 |
291 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede |
292 | conn, |
293 | "/org/freedesktop/hostname1", |
294 | NULL)) { |
3d53b501 |
295 | |
0f339959 |
296 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
90f54407 |
297 | hostnamed_mem_clean(); |
3d53b501 |
298 | |
0f339959 |
299 | } else { |
ea207ed3 |
300 | |
90f54407 |
301 | dbus_interface_exported = TRUE; |
302 | g_printf("exported %s's interface on the system bus...\n", name); |
303 | } |
0f339959 |
304 | } |
1e8c7c88 |
305 | |
0f339959 |
306 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
90f54407 |
307 | const gchar *name, |
0f339959 |
308 | gpointer user_data) { |
309 | |
310 | g_printf("success!\n"); |
1e8c7c88 |
311 | } |
312 | |
5b005882 |
313 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede |
314 | const gchar *name, |
315 | gpointer user_data) { |
80043b36 |
316 | |
90f54407 |
317 | if(!conn) { |
0f339959 |
318 | |
90f54407 |
319 | 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); |
320 | hostnamed_mem_clean(); |
321 | } |
0f339959 |
322 | |
509599f0 |
323 | g_printf("lost name %s, exiting...\n", name); |
fd8852d9 |
324 | |
1cd5e6fe |
325 | hostnamed_mem_clean(); |
0f339959 |
326 | } |
327 | |
328 | /* --- end bus/name handlers, begin misc unix functions --- */ |
329 | |
330 | /* safe call to clean and then exit |
509599f0 |
331 | * this stops our GMainLoop safely before letting main() return */ |
0f339959 |
332 | void hostnamed_mem_clean() { |
333 | |
90f54407 |
334 | g_printf("exiting...\n"); |
0f339959 |
335 | |
90f54407 |
336 | if(dbus_interface_exported) |
337 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
0f339959 |
338 | |
90f54407 |
339 | if(g_main_loop_is_running(hostnamed_loop)) |
340 | g_main_loop_quit(hostnamed_loop); |
fd8852d9 |
341 | |
ea207ed3 |
342 | } |
343 | |
0f339959 |
344 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
345 | gboolean unix_sig_terminate_handler(gpointer data) { |
c62bceb7 |
346 | |
90f54407 |
347 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
0f339959 |
348 | |
90f54407 |
349 | hostnamed_mem_clean(); |
350 | return G_SOURCE_REMOVE; |
0f339959 |
351 | } |
352 | |
353 | void set_signal_handlers() { |
354 | |
90f54407 |
355 | /* we don't care about its descriptor, we never need to unregister these */ |
356 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
357 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
358 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
04cc16f2 |
359 | |
90f54407 |
360 | /* TODO: the "only once" guarantee only counts towards specific signals. |
361 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() |
362 | * to be called twice */ |
0f339959 |
363 | } |
364 | |
365 | int main() { |
90f54407 |
366 | |
367 | set_signal_handlers(); |
387173cb |
368 | |
f8257c5d |
369 | if(!determine_chassis_and_icon()) |
a1bcc33c |
370 | return 1; |
371 | |
372 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
90f54407 |
373 | hostnamed_freeable = g_ptr_array_new(); |
fd8852d9 |
374 | |
90f54407 |
375 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
0f339959 |
376 | "org.freedesktop.hostname1", |
377 | G_BUS_NAME_OWNER_FLAGS_NONE, |
378 | hostnamed_on_bus_acquired, |
379 | hostnamed_on_name_acquired, |
380 | hostnamed_on_name_lost, |
381 | NULL, |
382 | NULL); |
496f5d66 |
383 | |
90f54407 |
384 | g_main_loop_run(hostnamed_loop); |
385 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
386 | g_main_loop_unref(hostnamed_loop); |
1e8c7c88 |
387 | |
90f54407 |
388 | /* guaranteed unownable */ |
389 | g_bus_unown_name(bus_descriptor); |
c62bceb7 |
390 | |
90f54407 |
391 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
392 | g_ptr_array_free(hostnamed_freeable, TRUE); |
7323a4e4 |
393 | |
90f54407 |
394 | return 0; |
a35a69c5 |
395 | } |
396 | |
a1bcc33c |
397 | gboolean determine_chassis_and_icon() { |
398 | |
f8257c5d |
399 | char *hwproduct, *hwmodel, *hwvendor, *hwmachine; |
400 | size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size; |
401 | int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2]; |
f1ad9351 |
402 | unsigned int i; |
f8257c5d |
403 | gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG; |
404 | |
405 | hwproduct = hwmodel = hwvendor = hwmachine = NULL; |
f1ad9351 |
406 | |
a1bcc33c |
407 | hwproduct_name[0] = CTL_HW; |
408 | hwproduct_name[1] = HW_PRODUCT; |
f1ad9351 |
409 | |
a1bcc33c |
410 | hwmodel_name[0] = CTL_HW; |
411 | hwmodel_name[1] = HW_MODEL; |
412 | |
413 | hwvendor_name[0] = CTL_HW; |
414 | hwvendor_name[1] = HW_VENDOR; |
415 | |
f8257c5d |
416 | hwmachine_name[0] = CTL_HW; |
417 | hwmachine_name[1] = HW_MACHINE; |
418 | |
a1bcc33c |
419 | /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ |
420 | if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) |
421 | return FALSE; |
422 | |
423 | if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) |
424 | return FALSE; |
425 | |
426 | if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) |
427 | return FALSE; |
428 | |
f8257c5d |
429 | if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0)) |
430 | return FALSE; |
431 | |
a1bcc33c |
432 | /* TODO: test for laptop, if not, dmidecode for desktop vs. server |
433 | * probably move this code to vm test func and set a global after running it early, once */ |
434 | |
f8257c5d |
435 | for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) { |
436 | if(strcasestr(hwproduct, chassis_indicator_table[i].match_string) |
437 | || strcasestr(hwmodel, chassis_indicator_table[i].match_string) |
438 | || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) { |
439 | |
440 | if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) { |
441 | |
442 | UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence; |
443 | CHASSIS = chassis_indicator_table[i].chassis; |
444 | } |
445 | |
446 | if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) { |
447 | |
448 | UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence; |
449 | ICON = chassis_indicator_table[i].icon; |
450 | } |
451 | } |
452 | } |
453 | |
454 | if(up_native_is_laptop()) { |
455 | |
456 | if(!CHASSIS) |
457 | CHASSIS = "laptop"; |
458 | if(!ICON) |
459 | ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */ |
460 | |
461 | } else if(is_server(hwmachine)) { |
462 | |
463 | if(!CHASSIS) |
464 | CHASSIS = "server"; |
465 | if(!ICON) |
466 | ICON = "uninterruptible-power-supply"; |
467 | |
468 | } else if(!CHASSIS || !ICON) { |
f1ad9351 |
469 | |
f8257c5d |
470 | if(!CHASSIS) |
471 | CHASSIS = "desktop"; |
472 | if(!ICON) |
473 | ICON = "computer"; |
474 | } |
475 | |
476 | return (CHASSIS && ICON); |
477 | } |
478 | |
479 | gboolean is_server(gchar *arch) { |
480 | |
481 | unsigned int i; |
a1bcc33c |
482 | |
f8257c5d |
483 | for(; i < G_N_ELEMENTS(server_archs); i++) |
484 | if(strcasestr(arch, server_archs[i])) |
485 | return TRUE; |
486 | |
487 | return FALSE; |
488 | } |
489 | |
490 | gboolean up_native_is_laptop() { |
491 | |
492 | struct apm_power_info bstate; |
493 | struct sensordev acpiac; |
494 | |
495 | if (up_native_get_sensordev("acpiac0", &acpiac)) |
496 | return TRUE; |
497 | |
498 | if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate)) |
499 | g_error("ioctl on apm fd failed : %s", g_strerror(errno)); |
500 | |
501 | return bstate.ac_state != APM_AC_UNKNOWN; |
502 | } |
503 | |
504 | int up_apm_get_fd() { |
505 | |
506 | static int apm_fd = 0; |
507 | |
508 | if(apm_fd == 0) { |
509 | |
510 | g_debug("apm_fd is not initialized yet, opening"); |
511 | |
512 | /* open /dev/apm */ |
513 | if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { |
514 | if(errno != ENXIO && errno != ENOENT) |
515 | g_error("cannot open device file"); |
516 | } |
517 | } |
518 | |
519 | return apm_fd; |
520 | } |
521 | |
522 | gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) { |
523 | |
524 | int devn; |
525 | size_t sdlen = sizeof(struct sensordev); |
526 | int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0}; |
527 | |
528 | for (devn = 0 ; ; devn++) { |
529 | mib[2] = devn; |
530 | if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) { |
531 | if(errno == ENXIO) |
532 | continue; |
533 | if(errno == ENOENT) |
534 | break; |
535 | } |
536 | |
537 | if (!strcmp(snsrdev->xname, id)) |
538 | return TRUE; |
539 | } |
540 | |
541 | return FALSE; |
f1ad9351 |
542 | } |
543 | |
a6f11205 |
544 | /* TODO figure out DMI variables on obsd */ |
a35a69c5 |
545 | /*static gchar *guess_icon_name() { |
546 | |
547 | gchar *filebuf = NULL; |
548 | gchar *ret = NULL; |
549 | |
1cd5e6fe |
550 | #if defined(__i386__) || defined(__x86_64__) |
39df6847 |
551 | |
a35a69c5 |
552 | Taken with a few minor changes from systemd's hostnamed.c, |
553 | copyright 2011 Lennart Poettering. |
554 | |
555 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
556 | details about the values listed here: |
557 | |
558 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
39df6847 |
559 | |
a35a69c5 |
560 | |
561 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
562 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
563 | case 0x3: |
564 | case 0x4: |
565 | case 0x5: |
566 | case 0x6: |
567 | case 0x7: |
568 | ret = g_strdup ("computer-desktop"); |
569 | goto out; |
570 | case 0x9: |
571 | case 0xA: |
572 | case 0xE: |
573 | ret = g_strdup ("computer-laptop"); |
574 | goto out; |
575 | case 0x11: |
576 | case 0x17: |
577 | case 0x1C: |
578 | case 0x1D: |
579 | ret = g_strdup ("computer-server"); |
580 | goto out; |
581 | } |
582 | } |
1cd5e6fe |
583 | #endif |
a35a69c5 |
584 | ret = g_strdup ("computer"); |
585 | out: |
586 | g_free (filebuf); |
587 | return ret; |
588 | }*/ |
1e8c7c88 |
589 | |