finish migrating old code to work with gdbus-codegen types, cleaned up config IO...
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
CommitLineData
71e3eef1 1#include <unistd.h>
2#include <limits.h>
ed99526e 3
4#include <glib.h>
ea207ed3 5#include <gio/gio.h>
6
ed99526e 7#include "hostnamed.h"
8#include "hostnamed-gen.c"
71e3eef1 9
ed99526e 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) {
11475670 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
f4d34761 34 g_print("lost name %s, exiting...", name);
ed99526e 35
36 hostnamed_mem_clean();
37
e8c57a5d 38 //TODO exit through g_main_loop properly...
ea207ed3 39}
40
0df0018d 41/* safe call to try and start hostnamed */
c3b84b0a 42GError *hostnamed_init() {
387173cb 43
ea207ed3 44 guint bus_descriptor;
11475670 45 GError *err = NULL;
ed99526e 46 gchar **hostnamed_ispect_xml;
47 gchar *hostnamed_joined_xml;
11475670 48
ed99526e 49 hostnamed_freeable = g_ptr_array_new();
c3b84b0a 50 hostnamed_ispect_xml = g_malloc(3000);
ed99526e 51
c3b84b0a 52 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
11475670 53 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
54 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
71e3eef1 55
ed99526e 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,
11475670 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
c3b84b0a 68 //TODO: malloc and return reference as if a main() closed
6de394d4 69 return err;
780e1f19 70}
71e3eef1 71
ed99526e 72/* free()'s */
73void hostnamed_mem_clean() {
1c38000a 74
ed99526e 75 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
71e3eef1 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__)
c3b84b0a 87
71e3eef1 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
c3b84b0a 95
71e3eef1 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}*/