minor, add a TODO to hostnamed.c i caught
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
3b82e3c1 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
a35a69c5 17#include <unistd.h>
18#include <limits.h>
0f339959 19#include <signal.h>
fd8852d9 20
d15318db 21#include <sys/param.h>
28cac2f0 22#include <string.h>
d15318db 23
4c04b514 24#include <glib/gprintf.h>
8caf1f61 25#include <glib-unix.h>
4c04b514 26
1e8c7c88 27#include "hostnamed-gen.h"
0f339959 28#include "hostnamed.h"
a35a69c5 29
fd8852d9 30GPtrArray *hostnamed_freeable;
1be94ede 31Hostname1 *hostnamed_interf;
3d53b501 32
0f339959 33GMainLoop *hostnamed_loop;
34
35guint bus_descriptor;
36gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
37
1ce41045 38/* --- begin method/property/dbus signal code --- */
76b67a18 39
3d53b501 40static gboolean
1be94ede 41on_handle_set_hostname(Hostname1 *hn1_passed_interf,
3d53b501 42 GDBusMethodInvocation *invoc,
43 const gchar *greet,
44 gpointer data) {
45 return FALSE;
46}
47
48static gboolean
1be94ede 49on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 50 GDBusMethodInvocation *invoc,
51 const gchar *greet,
52 gpointer data) {
53 return FALSE;
54}
55
56static gboolean
1be94ede 57on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 58 GDBusMethodInvocation *invoc,
59 const gchar *greet,
60 gpointer data) {
61 return FALSE;
62}
63
64static gboolean
1be94ede 65on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 66 GDBusMethodInvocation *invoc,
67 const gchar *greet,
68 gpointer data) {
69 return FALSE;
70}
71
72static gboolean
1be94ede 73on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 74 GDBusMethodInvocation *invoc,
75 const gchar *greet,
76 gpointer data) {
77 return FALSE;
78}
79
76b67a18 80/* note: all hostnamed/hostname1's properties are read-only,
81 * and do not need set_ functions, gdbus-codegen realized
82 * this from the XML and handled the to-be error of trying
83 * to set a read-only property's value
84 */
85
86const gchar *
87our_get_hostname() {
88
28cac2f0 89 gchar *hostname_buf, *ret;
90 size_t hostname_divider;
d15318db 91
0f339959 92 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); /* todo check & free */
28cac2f0 93 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
7323a4e4 94 g_ptr_array_add(hostnamed_freeable, hostname_buf);
28cac2f0 95 g_ptr_array_add(hostnamed_freeable, ret);
7323a4e4 96
97 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
98 return "";
d15318db 99
28cac2f0 100 hostname_divider = strcspn(hostname_buf, ".");
101
102 return strncpy(ret, hostname_buf, hostname_divider);
76b67a18 103}
104
105const gchar *
106our_get_static_hostname() {
107
108 return "TODO";
109}
110
111const gchar *
112our_get_pretty_hostname() {
113
114 return "TODO";
115}
116
117const gchar *
118our_get_chassis() {
119
120 return "TODO";
121}
122
123const gchar *
124our_get_icon_name() {
125
126 return "TODO";
127}
128
129const gchar *
130our_get_kernel_name() {
131
132 return "TODO";
133}
134
135const gchar *
136our_get_kernel_version() {
137
138 return "TODO";
139}
140
141const gchar *
142our_get_kernel_release() {
143
144 return "TODO";
145}
146
147const gchar *
148our_get_os_cpename() {
149
150 return "TODO";
151}
152
153const gchar *
154our_get_os_pretty_name() {
155
156 return "TODO";
157}
158
1ce41045 159/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 160
5b005882 161static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 162 const gchar *name,
163 gpointer user_data) {
d1e1db9e 164
0f339959 165 g_printf("got bus/name, exporting %s's interface...\n", name);
d1e1db9e 166
0f339959 167 hostnamed_interf = hostname1_skeleton_new();
3d53b501 168
76b67a18 169 /* attach function pointers to generated struct's method handlers */
3d53b501 170 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
171 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
172 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
173 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
174 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
175
76b67a18 176 /* set our properties before export */
1be94ede 177 hostname1_set_hostname(hostnamed_interf, our_get_hostname());
178 hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
179 hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
180 hostname1_set_chassis(hostnamed_interf, our_get_chassis());
181 hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
182 hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
183 hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
184 hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
185 hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
186 hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
76b67a18 187
3d53b501 188 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 189 conn,
190 "/org/freedesktop/hostname1",
191 NULL)) {
3d53b501 192
0f339959 193 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
194 hostnamed_mem_clean();
3d53b501 195
0f339959 196 } else {
ea207ed3 197
0f339959 198 dbus_interface_exported = TRUE;
509599f0 199 g_printf("exported %s's interface on the system bus...\n", name);
0f339959 200 }
0f339959 201}
1e8c7c88 202
0f339959 203static void hostnamed_on_name_acquired(GDBusConnection *conn,
204 const gchar *name,
205 gpointer user_data) {
206
207 g_printf("success!\n");
1e8c7c88 208}
209
5b005882 210static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 211 const gchar *name,
212 gpointer user_data) {
80043b36 213
0f339959 214 if(!conn) {
215
509599f0 216 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);
0f339959 217 hostnamed_mem_clean();
218 }
219
509599f0 220 g_printf("lost name %s, exiting...\n", name);
fd8852d9 221
1cd5e6fe 222 hostnamed_mem_clean();
0f339959 223}
224
225/* --- end bus/name handlers, begin misc unix functions --- */
226
227/* safe call to clean and then exit
509599f0 228 * this stops our GMainLoop safely before letting main() return */
0f339959 229void hostnamed_mem_clean() {
230
231 g_printf("exiting...\n");
232
233 if(dbus_interface_exported)
234 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
235
236 if(g_main_loop_is_running(hostnamed_loop))
237 g_main_loop_quit(hostnamed_loop);
fd8852d9 238
ea207ed3 239}
240
0f339959 241/* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
242gboolean unix_sig_terminate_handler(gpointer data) {
c62bceb7 243
0f339959 244 g_printf("caught SIGINT/HUP/TERM, exiting\n");
245
246 hostnamed_mem_clean();
247 return G_SOURCE_REMOVE;
248}
249
250void set_signal_handlers() {
251
252 /* we don't care about its descriptor, we never need to unregister these */
253 g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL);
254 g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL);
255 g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL);
04cc16f2 256
257 /* TODO: the "only once" guarantee only counts towards specific signals.
258 * make sure calling a SIGINT and SIGHUP doesn't cause term_handler()
259 * to be called twice */
0f339959 260}
261
262int main() {
263
264 set_signal_handlers();
387173cb 265
28cac2f0 266 hostnamed_loop = g_main_loop_new(NULL, TRUE);
267 hostnamed_freeable = g_ptr_array_new();
fd8852d9 268
0f339959 269 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
270 "org.freedesktop.hostname1",
271 G_BUS_NAME_OWNER_FLAGS_NONE,
272 hostnamed_on_bus_acquired,
273 hostnamed_on_name_acquired,
274 hostnamed_on_name_lost,
275 NULL,
276 NULL);
496f5d66 277
1e8c7c88 278 g_main_loop_run(hostnamed_loop);
0f339959 279 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
1e8c7c88 280 g_main_loop_unref(hostnamed_loop);
281
0f339959 282 /* guaranteed unownable */
c62bceb7 283 g_bus_unown_name(bus_descriptor);
284
0f339959 285 /* at this point no operations can occur with our data, it is safe to free it + its container */
286 g_ptr_array_free(hostnamed_freeable, TRUE);
7323a4e4 287
1e8c7c88 288 return 0;
a35a69c5 289}
290
a6f11205 291/* TODO figure out DMI variables on obsd */
a35a69c5 292/*static gchar *guess_icon_name() {
293
294 gchar *filebuf = NULL;
295 gchar *ret = NULL;
296
1cd5e6fe 297 #if defined(__i386__) || defined(__x86_64__)
39df6847 298
a35a69c5 299 Taken with a few minor changes from systemd's hostnamed.c,
300 copyright 2011 Lennart Poettering.
301
302 See the SMBIOS Specification 2.7.1 section 7.4.1 for
303 details about the values listed here:
304
305 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
39df6847 306
a35a69c5 307
308 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
309 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
310 case 0x3:
311 case 0x4:
312 case 0x5:
313 case 0x6:
314 case 0x7:
315 ret = g_strdup ("computer-desktop");
316 goto out;
317 case 0x9:
318 case 0xA:
319 case 0xE:
320 ret = g_strdup ("computer-laptop");
321 goto out;
322 case 0x11:
323 case 0x17:
324 case 0x1C:
325 case 0x1D:
326 ret = g_strdup ("computer-server");
327 goto out;
328 }
329 }
1cd5e6fe 330 #endif
a35a69c5 331 ret = g_strdup ("computer");
332 out:
333 g_free (filebuf);
334 return ret;
335}*/
1e8c7c88 336