finish migrating old code to work with gdbus-codegen types, cleaned up config IO...
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
a35a69c5 1#include <unistd.h>
2#include <limits.h>
fd8852d9 3
4#include <glib.h>
ea207ed3 5#include <gio/gio.h>
6
fd8852d9 7#include "hostnamed.h"
8#include "hostnamed-gen.c"
a35a69c5 9
fd8852d9 10GPtrArray *hostnamed_freeable;
ff1030d4 11GDBusNodeInfo *spect_data;
0df0018d 12
80043b36 13static void on_bus_acquired(GDBusConnection *conn,
14 const gchar *name,
15 gpointer user_data) {
d1e1db9e 16
17 GError *err;
18
19 g_print("got bus, name: %s\n", name);
20
ea207ed3 21}
22
80043b36 23static void on_name_acquired(GDBusConnection *conn,
24 const gchar *name,
25 gpointer user_data) {
26
ea207ed3 27 g_print("got name %s\n", name);
ea207ed3 28}
29
80043b36 30static void on_name_lost(GDBusConnection *conn,
31 const gchar *name,
32 gpointer user_data) {
33
faab2eee 34 g_print("lost name %s, exiting...", name);
fd8852d9 35
36 hostnamed_mem_clean();
37
1cd91c9c 38 //TODO exit through g_main_loop properly...
ea207ed3 39}
40
0df0018d 41/* safe call to try and start hostnamed */
39df6847 42GError *hostnamed_init() {
387173cb 43
ea207ed3 44 guint bus_descriptor;
d1e1db9e 45 GError *err = NULL;
fd8852d9 46 gchar **hostnamed_ispect_xml;
47 gchar *hostnamed_joined_xml;
d1e1db9e 48
fd8852d9 49 hostnamed_freeable = g_ptr_array_new();
39df6847 50 hostnamed_ispect_xml = g_malloc(3000);
fd8852d9 51
39df6847 52 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
d1e1db9e 53 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
54 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
a35a69c5 55
fd8852d9 56 g_free(hostnamed_ispect_xml);
57 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
58
6de394d4 59 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
d1e1db9e 60 "org.freedesktop.hostname1",
ea207ed3 61 G_BUS_NAME_OWNER_FLAGS_NONE,
62 on_bus_acquired,
63 on_name_acquired,
64 on_name_lost,
65 NULL,
66 NULL);
496f5d66 67
39df6847 68 //TODO: malloc and return reference as if a main() closed
6de394d4 69 return err;
780e1f19 70}
a35a69c5 71
fd8852d9 72/* free()'s */
73void hostnamed_mem_clean() {
36575bff 74
fd8852d9 75 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
a35a69c5 76}
77
78//TODO figure out DMI variables on obsd
79/*static gchar *guess_icon_name() {
80
81 gchar *filebuf = NULL;
82 gchar *ret = NULL;
83
84 //TODO vm check
85
86 #if defined(__i386__) || defined(__x86_64__)
39df6847 87
a35a69c5 88 Taken with a few minor changes from systemd's hostnamed.c,
89 copyright 2011 Lennart Poettering.
90
91 See the SMBIOS Specification 2.7.1 section 7.4.1 for
92 details about the values listed here:
93
94 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
39df6847 95
a35a69c5 96
97 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
98 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
99 case 0x3:
100 case 0x4:
101 case 0x5:
102 case 0x6:
103 case 0x7:
104 ret = g_strdup ("computer-desktop");
105 goto out;
106 case 0x9:
107 case 0xA:
108 case 0xE:
109 ret = g_strdup ("computer-laptop");
110 goto out;
111 case 0x11:
112 case 0x17:
113 case 0x1C:
114 case 0x1D:
115 ret = g_strdup ("computer-server");
116 goto out;
117 }
118 }
119 #endif
120 ret = g_strdup ("computer");
121 out:
122 g_free (filebuf);
123 return ret;
124}*/