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