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