furthering hostnamed, begining configuration..
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 #include <unistd.h>
2 #include <limits.h>
3 #include <gio/gio.h>
4
5
6 GDBusNodeInfo *spect_data;
7
8 static gchar *hostname;
9 static gchar *pretty_hostname;
10 static gchar *static_hostname;
11 static gchar *icon_name;
12 static gchar *chassis;
13 static gchar *kernel_name;
14 static gchar *kernel_release;
15 static gchar *kernel_version;
16 static gchar *os_prettyname;
17 static gchar *os_cpe; //common platform enumeration string
18
19 static void handle_method_call(GDBusConnection *conn,
20 const gchar *sender,
21 const gchar *obj_path,
22 const gchar *interf_name,
23 const gchar *method_name,
24 GVariant *params,
25 GDBusMethodInvocation *invc,
26 gpointer usrdat) {
27
28 if(g_strcmp0(interf_name, "org.freedesktop.DBus.Introspectable") == 0
29 && g_strcmp0(method_name, "Introspect") == 0) {
30
31 GVariant *xml_ret_gvar;
32 GString *xml_ret;
33
34 g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret);
35 xml_ret_gvar = g_variant_new_string(xml_ret->str);
36 g_dbus_method_invocation_return_value(invc, xml_ret_gvar);
37 }
38
39 }
40
41 static GVariant * handle_get_property(GDBusConnection *conn,
42 const gchar *sender,
43 const gchar *obj_path,
44 const gchar *interf_name,
45 const gchar *prop_name,
46 GError **err,
47 gpointer usr_data) {
48
49 const gchar *our_interf_name = "org.freedesktop.hostname1";
50 const gchar *our_obj_path = "/org/freedesktop/hostname1";
51
52 if(g_strcmp0(interf_name, our_interf_name) != 0
53 || g_strcmp0(obj_path, our_obj_path) != 0) {
54
55 return NULL; //TODO error
56 }
57
58 if(g_strcmp0(prop_name, "Hostname") == 0)
59 return g_variant_new_string(hostname);
60
61 else if(g_strcmp0(prop_name, "StaticHostname") == 0)
62 return g_variant_new_string(static_hostname);
63
64 else if(g_strcmp0(prop_name, "PrettyHostname") == 0)
65 return g_variant_new_string(pretty_hostname);
66
67 else if(g_strcmp0(prop_name, "IconName") == 0)
68 return g_variant_new_string(icon_name);
69
70 else
71 return NULL; //TODO error
72
73 }
74
75 static gboolean handle_set_property(GDBusConnection *conn,
76 const gchar *sender,
77 const gchar *obj_path,
78 const gchar *interf_name,
79 const gchar *prop_name,
80 GVariant *val,
81 GError **err,
82 gpointer usr_data) {
83
84 g_dbus_connection_emit_signal(conn,
85 NULL,
86 obj_path,
87 "org.freedesktop.DBus.Properties",
88 "PropertiesChanged",
89 NULL, /* incorrect */
90 NULL);
91
92 return TRUE;
93 }
94
95 /* "hot" functions initially passed to gdbus */
96 static const GDBusInterfaceVTable interface_vtable =
97 {
98 handle_method_call,
99 handle_get_property,
100 handle_set_property
101 };
102
103 /* end method/property functions, begin bus name handlers
104 * TODO: these should be intertwined as to handle edge cases
105 * for when the system cannot immediately grab the name, as
106 * well as cases where the system unintendedly loses the name
107 */
108 static void on_bus_acquired(GDBusConnection *conn,
109 const gchar *name,
110 gpointer user_data) {
111
112 GError *err;
113
114 g_print("got bus, name: %s\n", name);
115
116 //GDBusObjectSkeleton *hostnamed_dbobj = g_dbus_object_skeleton_new("/org/freedesktop/hostname1");
117
118 g_dbus_connection_register_object(conn,
119 "/org/freedesktop/hostname1",
120 spect_data->interfaces[0],
121 &interface_vtable,
122 NULL, NULL, NULL);
123 }
124
125 static void on_name_acquired(GDBusConnection *conn,
126 const gchar *name,
127 gpointer user_data) {
128
129 g_print("got name %s\n", name);
130 }
131
132 static void on_name_lost(GDBusConnection *conn,
133 const gchar *name,
134 gpointer user_data) {
135
136 g_print("lost name %s, exiting...", name);
137 //TODO exit through g_main_loop properly...
138 exit(0);
139 }
140
141 /* safe call to try and start hostnamed */
142 GError * hostnamed_init() {
143
144 guint bus_descriptor;
145 GError *err = NULL;
146 gchar **hostnamed_ispect_xml = g_malloc(3000);
147 gchar *hostnamed_joined_xml = g_malloc(3000);
148
149 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, err);
150 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
151 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
152
153 if(!init_props())
154 return err; //TODO error
155
156 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
157 "org.freedesktop.hostname1",
158 G_BUS_NAME_OWNER_FLAGS_NONE,
159 on_bus_acquired,
160 on_name_acquired,
161 on_name_lost,
162 NULL,
163 NULL);
164
165 //TODO: malloc and return reference as if a main() closed
166 return err;
167 }
168
169 static gboolean init_props() {
170
171 if(init_hostname()
172 && init_static_hostname()
173 && init_pretty_hostname()
174 && init_icon_name()
175 && init_chassis()
176 && init_kernel_name()
177 && init_kernel_version()
178 && init_os_name()
179 && init_os_cpe() )
180 return TRUE;
181
182 return FALSE;
183 }
184
185 //POSIX, for future ports try_hostname should be checked for null-termination
186 static gboolean init_hostname() {
187
188 gchar try_hostname[MAX_HOSTNAME];
189
190 if(!get_hostname(try_hostname, MAX_HOSTNAME)) {
191 hostname = try_hostname;
192 return TRUE;
193 }
194
195 return FALSE;
196 }
197
198 static gboolean init_pretty_hostname() {
199
200
201
202 }
203
204 //TODO figure out DMI variables on obsd
205 /*static gchar *guess_icon_name() {
206
207 gchar *filebuf = NULL;
208 gchar *ret = NULL;
209
210 //TODO vm check
211
212 #if defined(__i386__) || defined(__x86_64__)
213 /*
214 Taken with a few minor changes from systemd's hostnamed.c,
215 copyright 2011 Lennart Poettering.
216
217 See the SMBIOS Specification 2.7.1 section 7.4.1 for
218 details about the values listed here:
219
220 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
221 */ /*
222
223 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
224 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
225 case 0x3:
226 case 0x4:
227 case 0x5:
228 case 0x6:
229 case 0x7:
230 ret = g_strdup ("computer-desktop");
231 goto out;
232 case 0x9:
233 case 0xA:
234 case 0xE:
235 ret = g_strdup ("computer-laptop");
236 goto out;
237 case 0x11:
238 case 0x17:
239 case 0x1C:
240 case 0x1D:
241 ret = g_strdup ("computer-server");
242 goto out;
243 }
244 }
245 #endif
246 ret = g_strdup ("computer");
247 out:
248 g_free (filebuf);
249 return ret;
250 }*/