revamp icon/chassis determination method
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
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
17 #include <unistd.h>
18 #include <limits.h>
19 #include <signal.h>
20
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include <string.h>
24
25 #include <glib/gprintf.h>
26 #include <glib-unix.h>
27 /* #include <gtk/gtk.h> */
28
29 #include "hostnamed-gen.h"
30 #include "hostnamed.h"
31
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
70 GPtrArray *hostnamed_freeable;
71 Hostname1 *hostnamed_interf;
72
73 GMainLoop *hostnamed_loop;
74
75 guint bus_descriptor;
76 gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
77
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 };
93
94 /*const gchar* vmstring_list[] = {
95 "QEMU Virtual CPU",
96 "SmartDC HVM",
97 "KVM",
98 "VirtualBox"
99 };*/
100
101 /* --- begin method/property/dbus signal code --- */
102
103 static gboolean
104 on_handle_set_hostname(Hostname1 *hn1_passed_interf,
105 GDBusMethodInvocation *invoc,
106 const gchar *greet,
107 gpointer data) {
108 return FALSE;
109 }
110
111 static gboolean
112 on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
113 GDBusMethodInvocation *invoc,
114 const gchar *greet,
115 gpointer data) {
116 return FALSE;
117 }
118
119 static gboolean
120 on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
121 GDBusMethodInvocation *invoc,
122 const gchar *greet,
123 gpointer data) {
124 return FALSE;
125 }
126
127 static gboolean
128 on_handle_set_chassis(Hostname1 *hn1_passed_interf,
129 GDBusMethodInvocation *invoc,
130 const gchar *greet,
131 gpointer data) {
132 return FALSE;
133 }
134
135 static gboolean
136 on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
137 GDBusMethodInvocation *invoc,
138 const gchar *greet,
139 gpointer data) {
140 return FALSE;
141 }
142
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
152 gchar *hostname_buf, *ret;
153 size_t hostname_divider;
154
155 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
156 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
157
158 g_ptr_array_add(hostnamed_freeable, hostname_buf);
159 g_ptr_array_add(hostnamed_freeable, ret);
160
161 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || g_strcmp0(hostname_buf, "") == 0)
162 return "localhost";
163
164 hostname_divider = strcspn(hostname_buf, ".");
165
166 return strncpy(ret, hostname_buf, hostname_divider);
167 }
168
169 const gchar *
170 our_get_static_hostname() {
171
172 const gchar *pretty_hostname;
173 const gchar *ret;
174
175 pretty_hostname = our_get_pretty_hostname();
176
177 if(g_strcmp0(pretty_hostname, "") == 0)
178 ret = our_get_hostname();
179
180 else if((ret = g_hostname_to_ascii(pretty_hostname))) {
181
182 g_ptr_array_add(hostnamed_freeable, (gpointer)ret);
183 return ret;
184 }
185
186 return ret;
187 }
188
189 const gchar *
190 our_get_pretty_hostname() {
191
192 GKeyFile *config;
193 gchar *ret;
194
195 config = g_key_file_new();
196
197 if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)
198 && (ret = g_key_file_get_value(config, "hostnamed", "PrettyHostname", NULL))) { /* ret might need to be freed, docs dont specify but i am suspicious */
199
200 g_key_file_unref(config);
201 return ret;
202 }
203
204 if(config)
205 g_free(config);
206
207 return "";
208 }
209
210 const gchar *
211 our_get_chassis() {
212
213 return "asdf";
214
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
253 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
254
255 static void hostnamed_on_bus_acquired(GDBusConnection *conn,
256 const gchar *name,
257 gpointer user_data) {
258
259 g_printf("got bus/name, exporting %s's interface...\n", name);
260
261 hostnamed_interf = hostname1_skeleton_new();
262
263 /* attach function pointers to generated struct's method handlers */
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
270 /* set our properties before export */
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());
281
282 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
283 conn,
284 "/org/freedesktop/hostname1",
285 NULL)) {
286
287 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
288 hostnamed_mem_clean();
289
290 } else {
291
292 dbus_interface_exported = TRUE;
293 g_printf("exported %s's interface on the system bus...\n", name);
294 }
295 }
296
297 static void hostnamed_on_name_acquired(GDBusConnection *conn,
298 const gchar *name,
299 gpointer user_data) {
300
301 g_printf("success!\n");
302 }
303
304 static void hostnamed_on_name_lost(GDBusConnection *conn,
305 const gchar *name,
306 gpointer user_data) {
307
308 if(!conn) {
309
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 }
313
314 g_printf("lost name %s, exiting...\n", name);
315
316 hostnamed_mem_clean();
317 }
318
319 /* --- end bus/name handlers, begin misc unix functions --- */
320
321 /* safe call to clean and then exit
322 * this stops our GMainLoop safely before letting main() return */
323 void hostnamed_mem_clean() {
324
325 g_printf("exiting...\n");
326
327 if(dbus_interface_exported)
328 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
329
330 if(g_main_loop_is_running(hostnamed_loop))
331 g_main_loop_quit(hostnamed_loop);
332
333 }
334
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) {
337
338 g_printf("caught SIGINT/HUP/TERM, exiting\n");
339
340 hostnamed_mem_clean();
341 return G_SOURCE_REMOVE;
342 }
343
344 void set_signal_handlers() {
345
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);
350
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 */
354 }
355
356 int main() {
357
358 set_signal_handlers();
359
360 if(!build_chassis_table() || !determine_chassis_and_icon())
361 return 1;
362
363 hostnamed_loop = g_main_loop_new(NULL, TRUE);
364 hostnamed_freeable = g_ptr_array_new();
365
366 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
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);
374
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);
378
379 /* guaranteed unownable */
380 g_bus_unown_name(bus_descriptor);
381
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);
384
385 return 0;
386 }
387
388 static gboolean build_chassis_table() {
389 return TRUE;
390 }
391
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];
397 unsigned int i;
398
399 hwproduct_name[0] = CTL_HW;
400 hwproduct_name[1] = HW_PRODUCT;
401
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; */
425
426 return FALSE;
427
428 return TRUE; /* temp */
429 }
430
431 /* TODO figure out DMI variables on obsd */
432 /*static gchar *guess_icon_name() {
433
434 gchar *filebuf = NULL;
435 gchar *ret = NULL;
436
437 #if defined(__i386__) || defined(__x86_64__)
438
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
446
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 }
470 #endif
471 ret = g_strdup ("computer");
472 out:
473 g_free (filebuf);
474 return ret;
475 }*/
476