(1) regenerate leaner genfiles, we're not using the object manager functionality...
[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
20 #include <glib.h>
21 #include <gio/gio.h>
22
23 #include "hostnamed.h"
24 #include "hostnamed-gen.c"
25
26 GPtrArray *hostnamed_freeable;
27 GDBusNodeInfo *spect_data;
28 hostnamedHostname1 *hostnamed_interf;
29
30 /* begin method/property/signal code */
31
32 /* TODO make sure these guys only work if called by root */
33 static gboolean
34 on_handle_set_hostname(hostnamedHostname1 *hn1_passed_interf,
35 GDBusMethodInvocation *invoc,
36 const gchar *greet,
37 gpointer data) {
38 return FALSE;
39 }
40
41 static gboolean
42 on_handle_set_static_hostname(hostnamedHostname1 *hn1_passed_interf,
43 GDBusMethodInvocation *invoc,
44 const gchar *greet,
45 gpointer data) {
46 return FALSE;
47 }
48
49 static gboolean
50 on_handle_set_pretty_hostname(hostnamedHostname1 *hn1_passed_interf,
51 GDBusMethodInvocation *invoc,
52 const gchar *greet,
53 gpointer data) {
54 return FALSE;
55 }
56
57 static gboolean
58 on_handle_set_chassis(hostnamedHostname1 *hn1_passed_interf,
59 GDBusMethodInvocation *invoc,
60 const gchar *greet,
61 gpointer data) {
62 return FALSE;
63 }
64
65 static gboolean
66 on_handle_set_icon_name(hostnamedHostname1 *hn1_passed_interf,
67 GDBusMethodInvocation *invoc,
68 const gchar *greet,
69 gpointer data) {
70 return FALSE;
71 }
72
73 /* note: all hostnamed/hostname1's properties are read-only,
74 * and do not need set_ functions, gdbus-codegen realized
75 * this from the XML and handled the to-be error of trying
76 * to set a read-only property's value
77 */
78
79 const gchar *
80 our_get_hostname() {
81
82 return "TODO";
83 }
84
85 const gchar *
86 our_get_static_hostname() {
87
88 return "TODO";
89 }
90
91 const gchar *
92 our_get_pretty_hostname() {
93
94 return "TODO";
95 }
96
97 const gchar *
98 our_get_chassis() {
99
100 return "TODO";
101 }
102
103 const gchar *
104 our_get_icon_name() {
105
106 return "TODO";
107 }
108
109 const gchar *
110 our_get_kernel_name() {
111
112 return "TODO";
113 }
114
115 const gchar *
116 our_get_kernel_version() {
117
118 return "TODO";
119 }
120
121 const gchar *
122 our_get_kernel_release() {
123
124 return "TODO";
125 }
126
127 const gchar *
128 our_get_os_cpename() {
129
130 return "TODO";
131 }
132
133 const gchar *
134 our_get_os_pretty_name() {
135
136 return "TODO";
137 }
138
139 /* end method/property/signal code, begin bus/name handlers */
140
141 static void hostnamed_on_bus_acquired(GDBusConnection *conn,
142 const gchar *name,
143 gpointer user_data) {
144
145 g_print("got bus, name: %s\n", name);
146
147 }
148
149 static void hostnamed_on_name_acquired(GDBusConnection *conn,
150 const gchar *name,
151 gpointer user_data) {
152
153 g_print("got '%s' on system bus\n", name);
154
155 hostnamed_interf = hostnamed_hostname1_skeleton_new();
156
157 /* attach function pointers to generated struct's method handlers */
158 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
159 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
160 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
161 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
162 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
163
164 /* set our properties before export */
165 hostnamed_hostname1_set_hostname(hostnamed_interf, our_get_hostname());
166 hostnamed_hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
167 hostnamed_hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
168 hostnamed_hostname1_set_chassis(hostnamed_interf, our_get_chassis());
169 hostnamed_hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
170 hostnamed_hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
171 hostnamed_hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
172 hostnamed_hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
173 hostnamed_hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
174 hostnamed_hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
175
176 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
177 conn,
178 "/org/freedesktop/hostname1",
179 NULL)) {
180
181 g_printf("failed to export hostname1's interface on system bus!");
182 }
183
184 }
185
186 static void hostnamed_on_name_lost(GDBusConnection *conn,
187 const gchar *name,
188 gpointer user_data) {
189
190 g_print("lost name %s, exiting...", name);
191
192 hostnamed_mem_clean();
193 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
194
195 /* TODO exit through g_main_loop properly... */
196 }
197
198 /* safe call to try and start hostnamed */
199 GError *hostnamed_init() {
200
201 guint bus_descriptor;
202 GError *err = NULL;
203 gchar **hostnamed_ispect_xml;
204 gchar *hostnamed_joined_xml;
205
206 hostnamed_freeable = g_ptr_array_new();
207 hostnamed_ispect_xml = g_malloc(3000);
208
209 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
210 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
211 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
212
213 g_free(hostnamed_ispect_xml);
214 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
215
216 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
217 "org.freedesktop.hostname1",
218 G_BUS_NAME_OWNER_FLAGS_NONE,
219 hostnamed_on_bus_acquired,
220 hostnamed_on_name_acquired,
221 hostnamed_on_name_lost,
222 NULL,
223 NULL);
224
225 /* TODO: malloc and return reference as if a main() closed */
226 return err;
227 }
228
229 /* free()'s */
230 void hostnamed_mem_clean() {
231
232 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
233 }
234
235 /* TODO figure out DMI variables on obsd */
236 /*static gchar *guess_icon_name() {
237
238 gchar *filebuf = NULL;
239 gchar *ret = NULL;
240
241 #if defined(__i386__) || defined(__x86_64__)
242
243 Taken with a few minor changes from systemd's hostnamed.c,
244 copyright 2011 Lennart Poettering.
245
246 See the SMBIOS Specification 2.7.1 section 7.4.1 for
247 details about the values listed here:
248
249 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
250
251
252 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
253 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
254 case 0x3:
255 case 0x4:
256 case 0x5:
257 case 0x6:
258 case 0x7:
259 ret = g_strdup ("computer-desktop");
260 goto out;
261 case 0x9:
262 case 0xA:
263 case 0xE:
264 ret = g_strdup ("computer-laptop");
265 goto out;
266 case 0x11:
267 case 0x17:
268 case 0x1C:
269 case 0x1D:
270 ret = g_strdup ("computer-server");
271 goto out;
272 }
273 }
274 #endif
275 ret = g_strdup ("computer");
276 out:
277 g_free (filebuf);
278 return ret;
279 }*/