finish migrating old code to work with gdbus-codegen types, cleaned up config IO...
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 #include <unistd.h>
2 #include <limits.h>
3
4 #include <glib.h>
5 #include <gio/gio.h>
6
7 #include "hostnamed.h"
8 #include "hostnamed-gen.c"
9
10 GPtrArray *hostnamed_freeable;
11 GDBusNodeInfo *spect_data;
12
13 static void on_bus_acquired(GDBusConnection *conn,
14 const gchar *name,
15 gpointer user_data) {
16
17 GError *err;
18
19 g_print("got bus, name: %s\n", name);
20
21 }
22
23 static void on_name_acquired(GDBusConnection *conn,
24 const gchar *name,
25 gpointer user_data) {
26
27 g_print("got name %s\n", name);
28 }
29
30 static void on_name_lost(GDBusConnection *conn,
31 const gchar *name,
32 gpointer user_data) {
33
34 g_print("lost name %s, exiting...", name);
35
36 hostnamed_mem_clean();
37
38 //TODO exit through g_main_loop properly...
39 }
40
41 /* safe call to try and start hostnamed */
42 GError *hostnamed_init() {
43
44 guint bus_descriptor;
45 GError *err = NULL;
46 gchar **hostnamed_ispect_xml;
47 gchar *hostnamed_joined_xml;
48
49 hostnamed_freeable = g_ptr_array_new();
50 hostnamed_ispect_xml = g_malloc(3000);
51
52 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
53 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
54 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
55
56 g_free(hostnamed_ispect_xml);
57 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
58
59 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
60 "org.freedesktop.hostname1",
61 G_BUS_NAME_OWNER_FLAGS_NONE,
62 on_bus_acquired,
63 on_name_acquired,
64 on_name_lost,
65 NULL,
66 NULL);
67
68 //TODO: malloc and return reference as if a main() closed
69 return err;
70 }
71
72 /* free()'s */
73 void hostnamed_mem_clean() {
74
75 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
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__)
87
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
95
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 }*/