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> |
fd8852d9 |
20 | |
d15318db |
21 | #include <sys/param.h> |
f1ad9351 |
22 | #include <sys/sysctl.h> |
28cac2f0 |
23 | #include <string.h> |
d15318db |
24 | |
4c04b514 |
25 | #include <glib/gprintf.h> |
8caf1f61 |
26 | #include <glib-unix.h> |
a1bcc33c |
27 | /* #include <gtk/gtk.h> */ |
4c04b514 |
28 | |
1e8c7c88 |
29 | #include "hostnamed-gen.h" |
0f339959 |
30 | #include "hostnamed.h" |
a35a69c5 |
31 | |
a1bcc33c |
32 | /* add any sysctl strings that suggest virtualization here */ |
33 | /* format: { |
34 | * (1) string to be matched against runtime machine's sysctl output. |
35 | * can be either the exact string or a substring contained |
36 | * within sysctl strings. no "guesses" here, a match should |
37 | * reliably indicate the chassis/icon. |
38 | * |
39 | * (2) string describing chassis type divulged by (1). |
40 | * must be one of "desktop", "laptop", "server", |
41 | * "tablet", "handset", "vm", "container" or NULL |
42 | * if only icon string can be ascertained. "vm" refers |
43 | * to operating systems running on baremetal hypervisors |
44 | * (hardware virtualization, like XEN) while "container" |
45 | * refers to OSs running on shared hypervisors like |
46 | * virtualbox or VMware. consider the distinction carefully |
47 | * as common virtualization software like KVM may share |
48 | * characteristics of both "vm" and "container" types. |
49 | * |
50 | * (3) string specifying icon to use. follows XDG icon spec. |
51 | * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html |
52 | * for allowed strings. |
53 | * |
54 | * (4) chassis precedence bit. TRUE if (2) is defined and |
55 | * we're certain it is the proper string. FALSE in the |
56 | * circumstance (2) may be the correct string, unless |
57 | * a match with this bit set to TRUE overrides it. |
58 | * if (2) is NULL, this bit is inconsequential. |
59 | * |
60 | * (5) icon precedence bit. see previous definition. |
61 | * } */ |
62 | struct SYSCTL_LOOKUP_TABLE { |
63 | gchar *match_string; |
64 | gchar *chassis; |
65 | gchar *icon; |
66 | gboolean chassis_precedence; |
67 | gboolean icon_precedence; |
68 | }; |
69 | |
fd8852d9 |
70 | GPtrArray *hostnamed_freeable; |
1be94ede |
71 | Hostname1 *hostnamed_interf; |
3d53b501 |
72 | |
0f339959 |
73 | GMainLoop *hostnamed_loop; |
74 | |
75 | guint bus_descriptor; |
76 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
77 | |
a1bcc33c |
78 | gchar *CHASSIS, *ICON; |
79 | |
80 | /* TODO no specific vm or laptop icon in gnome |
81 | * NOTE paravirtualization on xen is only available for linuxes right now |
82 | * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) |
83 | * but we will worry about those later */ |
84 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
85 | { |
86 | { "QEMU Virtual CPU", "container", "drive-optical", FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ |
87 | { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* oracle solaris kvm */ |
88 | { "VirtualBox", "container", "drive-optical", TRUE, TRUE }, |
89 | { "VMware, Inc.", "container", "drive-optical", TRUE, TRUE }, |
90 | { "VMware Virtual Platform", "container", "drive-optical", TRUE, TRUE }, |
91 | { "Parallels", "container", "drive-optical", TRUE, TRUE } /* need verification */ |
92 | }; |
76b67a18 |
93 | |
a1bcc33c |
94 | /*const gchar* vmstring_list[] = { |
f1ad9351 |
95 | "QEMU Virtual CPU", |
96 | "SmartDC HVM", |
97 | "KVM", |
98 | "VirtualBox" |
a1bcc33c |
99 | };*/ |
f1ad9351 |
100 | |
a1bcc33c |
101 | /* --- begin method/property/dbus signal code --- */ |
f1ad9351 |
102 | |
3d53b501 |
103 | static gboolean |
1be94ede |
104 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
105 | GDBusMethodInvocation *invoc, |
106 | const gchar *greet, |
107 | gpointer data) { |
108 | return FALSE; |
109 | } |
110 | |
111 | static gboolean |
1be94ede |
112 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
113 | GDBusMethodInvocation *invoc, |
114 | const gchar *greet, |
115 | gpointer data) { |
116 | return FALSE; |
117 | } |
118 | |
119 | static gboolean |
1be94ede |
120 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 |
121 | GDBusMethodInvocation *invoc, |
122 | const gchar *greet, |
123 | gpointer data) { |
124 | return FALSE; |
125 | } |
126 | |
127 | static gboolean |
1be94ede |
128 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 |
129 | GDBusMethodInvocation *invoc, |
130 | const gchar *greet, |
131 | gpointer data) { |
132 | return FALSE; |
133 | } |
134 | |
135 | static gboolean |
1be94ede |
136 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 |
137 | GDBusMethodInvocation *invoc, |
138 | const gchar *greet, |
139 | gpointer data) { |
140 | return FALSE; |
141 | } |
142 | |
76b67a18 |
143 | /* note: all hostnamed/hostname1's properties are read-only, |
144 | * and do not need set_ functions, gdbus-codegen realized |
145 | * this from the XML and handled the to-be error of trying |
146 | * to set a read-only property's value |
147 | */ |
148 | |
149 | const gchar * |
150 | our_get_hostname() { |
151 | |
90f54407 |
152 | gchar *hostname_buf, *ret; |
153 | size_t hostname_divider; |
d15318db |
154 | |
a2fffc07 |
155 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
90f54407 |
156 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
a2fffc07 |
157 | |
90f54407 |
158 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
159 | g_ptr_array_add(hostnamed_freeable, ret); |
7323a4e4 |
160 | |
a2fffc07 |
161 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0) |
162 | return "localhost"; |
d15318db |
163 | |
90f54407 |
164 | hostname_divider = strcspn(hostname_buf, "."); |
28cac2f0 |
165 | |
90f54407 |
166 | return strncpy(ret, hostname_buf, hostname_divider); |
76b67a18 |
167 | } |
168 | |
169 | const gchar * |
170 | our_get_static_hostname() { |
171 | |
af14252f |
172 | const gchar *pretty_hostname; |
173 | const gchar *ret; |
a2fffc07 |
174 | |
175 | pretty_hostname = our_get_pretty_hostname(); |
176 | |
177 | if(g_strcmp0(pretty_hostname, "") == 0) |
178 | ret = our_get_hostname(); |
179 | |
af14252f |
180 | else if((ret = g_hostname_to_ascii(pretty_hostname))) { |
a2fffc07 |
181 | |
af14252f |
182 | g_ptr_array_add(hostnamed_freeable, (gpointer)ret); |
a2fffc07 |
183 | return ret; |
184 | } |
185 | |
186 | return ret; |
76b67a18 |
187 | } |
188 | |
189 | const gchar * |
190 | our_get_pretty_hostname() { |
191 | |
a2fffc07 |
192 | GKeyFile *config; |
193 | gchar *ret; |
194 | |
f1ad9351 |
195 | config = g_key_file_new(); |
196 | |
a2fffc07 |
197 | if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL) |
af14252f |
198 | && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */ |
a2fffc07 |
199 | |
f1ad9351 |
200 | g_key_file_unref(config); |
a2fffc07 |
201 | return ret; |
202 | } |
203 | |
204 | if(config) |
205 | g_free(config); |
206 | |
207 | return ""; |
76b67a18 |
208 | } |
209 | |
210 | const gchar * |
211 | our_get_chassis() { |
212 | |
a1bcc33c |
213 | return "asdf"; |
f1ad9351 |
214 | |
76b67a18 |
215 | } |
216 | |
217 | const gchar * |
218 | our_get_icon_name() { |
219 | |
220 | return "TODO"; |
221 | } |
222 | |
223 | const gchar * |
224 | our_get_kernel_name() { |
225 | |
226 | return "TODO"; |
227 | } |
228 | |
229 | const gchar * |
230 | our_get_kernel_version() { |
231 | |
232 | return "TODO"; |
233 | } |
234 | |
235 | const gchar * |
236 | our_get_kernel_release() { |
237 | |
238 | return "TODO"; |
239 | } |
240 | |
241 | const gchar * |
242 | our_get_os_cpename() { |
243 | |
244 | return "TODO"; |
245 | } |
246 | |
247 | const gchar * |
248 | our_get_os_pretty_name() { |
249 | |
250 | return "TODO"; |
251 | } |
252 | |
1ce41045 |
253 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
0df0018d |
254 | |
5b005882 |
255 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
1cd5e6fe |
256 | const gchar *name, |
257 | gpointer user_data) { |
d1e1db9e |
258 | |
0f339959 |
259 | g_printf("got bus/name, exporting %s's interface...\n", name); |
d1e1db9e |
260 | |
90f54407 |
261 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 |
262 | |
76b67a18 |
263 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 |
264 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
265 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); |
266 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); |
267 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); |
268 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); |
269 | |
76b67a18 |
270 | /* set our properties before export */ |
1be94ede |
271 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
272 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); |
273 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); |
274 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); |
275 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); |
276 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); |
277 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); |
278 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); |
279 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); |
280 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); |
76b67a18 |
281 | |
3d53b501 |
282 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede |
283 | conn, |
284 | "/org/freedesktop/hostname1", |
285 | NULL)) { |
3d53b501 |
286 | |
0f339959 |
287 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
90f54407 |
288 | hostnamed_mem_clean(); |
3d53b501 |
289 | |
0f339959 |
290 | } else { |
ea207ed3 |
291 | |
90f54407 |
292 | dbus_interface_exported = TRUE; |
293 | g_printf("exported %s's interface on the system bus...\n", name); |
294 | } |
0f339959 |
295 | } |
1e8c7c88 |
296 | |
0f339959 |
297 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
90f54407 |
298 | const gchar *name, |
0f339959 |
299 | gpointer user_data) { |
300 | |
301 | g_printf("success!\n"); |
1e8c7c88 |
302 | } |
303 | |
5b005882 |
304 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede |
305 | const gchar *name, |
306 | gpointer user_data) { |
80043b36 |
307 | |
90f54407 |
308 | if(!conn) { |
0f339959 |
309 | |
90f54407 |
310 | 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); |
311 | hostnamed_mem_clean(); |
312 | } |
0f339959 |
313 | |
509599f0 |
314 | g_printf("lost name %s, exiting...\n", name); |
fd8852d9 |
315 | |
1cd5e6fe |
316 | hostnamed_mem_clean(); |
0f339959 |
317 | } |
318 | |
319 | /* --- end bus/name handlers, begin misc unix functions --- */ |
320 | |
321 | /* safe call to clean and then exit |
509599f0 |
322 | * this stops our GMainLoop safely before letting main() return */ |
0f339959 |
323 | void hostnamed_mem_clean() { |
324 | |
90f54407 |
325 | g_printf("exiting...\n"); |
0f339959 |
326 | |
90f54407 |
327 | if(dbus_interface_exported) |
328 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
0f339959 |
329 | |
90f54407 |
330 | if(g_main_loop_is_running(hostnamed_loop)) |
331 | g_main_loop_quit(hostnamed_loop); |
fd8852d9 |
332 | |
ea207ed3 |
333 | } |
334 | |
0f339959 |
335 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
336 | gboolean unix_sig_terminate_handler(gpointer data) { |
c62bceb7 |
337 | |
90f54407 |
338 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
0f339959 |
339 | |
90f54407 |
340 | hostnamed_mem_clean(); |
341 | return G_SOURCE_REMOVE; |
0f339959 |
342 | } |
343 | |
344 | void set_signal_handlers() { |
345 | |
90f54407 |
346 | /* we don't care about its descriptor, we never need to unregister these */ |
347 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
348 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
349 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
04cc16f2 |
350 | |
90f54407 |
351 | /* TODO: the "only once" guarantee only counts towards specific signals. |
352 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() |
353 | * to be called twice */ |
0f339959 |
354 | } |
355 | |
356 | int main() { |
90f54407 |
357 | |
358 | set_signal_handlers(); |
387173cb |
359 | |
a1bcc33c |
360 | if(!build_chassis_table() || !determine_chassis_and_icon()) |
361 | return 1; |
362 | |
363 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
90f54407 |
364 | hostnamed_freeable = g_ptr_array_new(); |
fd8852d9 |
365 | |
90f54407 |
366 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
0f339959 |
367 | "org.freedesktop.hostname1", |
368 | G_BUS_NAME_OWNER_FLAGS_NONE, |
369 | hostnamed_on_bus_acquired, |
370 | hostnamed_on_name_acquired, |
371 | hostnamed_on_name_lost, |
372 | NULL, |
373 | NULL); |
496f5d66 |
374 | |
90f54407 |
375 | g_main_loop_run(hostnamed_loop); |
376 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
377 | g_main_loop_unref(hostnamed_loop); |
1e8c7c88 |
378 | |
90f54407 |
379 | /* guaranteed unownable */ |
380 | g_bus_unown_name(bus_descriptor); |
c62bceb7 |
381 | |
90f54407 |
382 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
383 | g_ptr_array_free(hostnamed_freeable, TRUE); |
7323a4e4 |
384 | |
90f54407 |
385 | return 0; |
a35a69c5 |
386 | } |
387 | |
a1bcc33c |
388 | static gboolean build_chassis_table() { |
389 | return TRUE; |
390 | } |
f1ad9351 |
391 | |
a1bcc33c |
392 | gboolean determine_chassis_and_icon() { |
393 | |
394 | char *hwproduct, *hwmodel, *hwvendor; |
395 | size_t hwproduct_size, hwmodel_size, hwvendor_size; |
396 | int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2]; |
f1ad9351 |
397 | unsigned int i; |
398 | |
a1bcc33c |
399 | hwproduct_name[0] = CTL_HW; |
400 | hwproduct_name[1] = HW_PRODUCT; |
f1ad9351 |
401 | |
a1bcc33c |
402 | hwmodel_name[0] = CTL_HW; |
403 | hwmodel_name[1] = HW_MODEL; |
404 | |
405 | hwvendor_name[0] = CTL_HW; |
406 | hwvendor_name[1] = HW_VENDOR; |
407 | |
408 | /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ |
409 | if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) |
410 | return FALSE; |
411 | |
412 | if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) |
413 | return FALSE; |
414 | |
415 | if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) |
416 | return FALSE; |
417 | |
418 | /* TODO: test for laptop, if not, dmidecode for desktop vs. server |
419 | * probably move this code to vm test func and set a global after running it early, once */ |
420 | |
421 | for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) |
422 | |
423 | /* if(strcasestr(sysctl_string, vmstring_list[i])) |
424 | return (CHASSIS = ) ? TRUE : FALSE; */ |
f1ad9351 |
425 | |
426 | return FALSE; |
a1bcc33c |
427 | |
428 | return TRUE; /* temp */ |
f1ad9351 |
429 | } |
430 | |
a6f11205 |
431 | /* TODO figure out DMI variables on obsd */ |
a35a69c5 |
432 | /*static gchar *guess_icon_name() { |
433 | |
434 | gchar *filebuf = NULL; |
435 | gchar *ret = NULL; |
436 | |
1cd5e6fe |
437 | #if defined(__i386__) || defined(__x86_64__) |
39df6847 |
438 | |
a35a69c5 |
439 | Taken with a few minor changes from systemd's hostnamed.c, |
440 | copyright 2011 Lennart Poettering. |
441 | |
442 | See the SMBIOS Specification 2.7.1 section 7.4.1 for |
443 | details about the values listed here: |
444 | |
445 | http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf |
39df6847 |
446 | |
a35a69c5 |
447 | |
448 | if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) { |
449 | switch (g_ascii_strtoull (filebuf, NULL, 10)) { |
450 | case 0x3: |
451 | case 0x4: |
452 | case 0x5: |
453 | case 0x6: |
454 | case 0x7: |
455 | ret = g_strdup ("computer-desktop"); |
456 | goto out; |
457 | case 0x9: |
458 | case 0xA: |
459 | case 0xE: |
460 | ret = g_strdup ("computer-laptop"); |
461 | goto out; |
462 | case 0x11: |
463 | case 0x17: |
464 | case 0x1C: |
465 | case 0x1D: |
466 | ret = g_strdup ("computer-server"); |
467 | goto out; |
468 | } |
469 | } |
1cd5e6fe |
470 | #endif |
a35a69c5 |
471 | ret = g_strdup ("computer"); |
472 | out: |
473 | g_free (filebuf); |
474 | return ret; |
475 | }*/ |
1e8c7c88 |
476 | |