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