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