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