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