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