(1) updated ispect xml to reflect recent undocumented changes in hostnamed
[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 Hostname1 *hostnamed_interf;
29
30 static gboolean
31 on_handle_set_hostname(Hostname1 *hn1_passed_interf,
32 GDBusMethodInvocation *invoc,
33 const gchar *greet,
34 gpointer data) {
35 return FALSE;
36 }
37
38 static gboolean
39 on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
40 GDBusMethodInvocation *invoc,
41 const gchar *greet,
42 gpointer data) {
43 return FALSE;
44 }
45
46 static gboolean
47 on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
48 GDBusMethodInvocation *invoc,
49 const gchar *greet,
50 gpointer data) {
51 return FALSE;
52 }
53
54 static gboolean
55 on_handle_set_chassis(Hostname1 *hn1_passed_interf,
56 GDBusMethodInvocation *invoc,
57 const gchar *greet,
58 gpointer data) {
59 return FALSE;
60 }
61
62 static gboolean
63 on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
64 GDBusMethodInvocation *invoc,
65 const gchar *greet,
66 gpointer data) {
67 return FALSE;
68 }
69
70 /* end method/property/signal code, begin bus/name handlers */
71
72 static void on_bus_acquired(GDBusConnection *conn,
73 const gchar *name,
74 gpointer user_data) {
75
76 g_print("got bus, name: %s\n", name);
77
78 }
79
80 static void on_name_acquired(GDBusConnection *conn,
81 const gchar *name,
82 gpointer user_data) {
83
84 g_print("got '%s' on system bus\n", name);
85
86 hostnamed_interf = hostname1_skeleton_new();
87
88 /* attach function pointers to generated vfunc table struct */
89 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
90 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
91 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
92 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
93 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
94
95 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
96 conn,
97 "/org/freedesktop/hostname1",
98 NULL)) {
99
100 g_printf("failed to export hostname1's interface on system bus!");
101 }
102
103 }
104
105 static void on_name_lost(GDBusConnection *conn,
106 const gchar *name,
107 gpointer user_data) {
108
109 g_print("lost name %s, exiting...", name);
110
111 hostnamed_mem_clean();
112 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
113
114 /* TODO exit through g_main_loop properly... */
115 }
116
117 /* safe call to try and start hostnamed */
118 GError *hostnamed_init() {
119
120 guint bus_descriptor;
121 GError *err = NULL;
122 gchar **hostnamed_ispect_xml;
123 gchar *hostnamed_joined_xml;
124
125 hostnamed_freeable = g_ptr_array_new();
126 hostnamed_ispect_xml = g_malloc(3000);
127
128 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
129 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
130 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
131
132 g_free(hostnamed_ispect_xml);
133 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
134
135 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
136 "org.freedesktop.hostname1",
137 G_BUS_NAME_OWNER_FLAGS_NONE,
138 on_bus_acquired,
139 on_name_acquired,
140 on_name_lost,
141 NULL,
142 NULL);
143
144 /* TODO: malloc and return reference as if a main() closed */
145 return err;
146 }
147
148 /* free()'s */
149 void hostnamed_mem_clean() {
150
151 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
152 }
153
154 /* TODO figure out DMI variables on obsd */
155 /*static gchar *guess_icon_name() {
156
157 gchar *filebuf = NULL;
158 gchar *ret = NULL;
159
160 #if defined(__i386__) || defined(__x86_64__)
161
162 Taken with a few minor changes from systemd's hostnamed.c,
163 copyright 2011 Lennart Poettering.
164
165 See the SMBIOS Specification 2.7.1 section 7.4.1 for
166 details about the values listed here:
167
168 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
169
170
171 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
172 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
173 case 0x3:
174 case 0x4:
175 case 0x5:
176 case 0x6:
177 case 0x7:
178 ret = g_strdup ("computer-desktop");
179 goto out;
180 case 0x9:
181 case 0xA:
182 case 0xE:
183 ret = g_strdup ("computer-laptop");
184 goto out;
185 case 0x11:
186 case 0x17:
187 case 0x1C:
188 case 0x1D:
189 ret = g_strdup ("computer-server");
190 goto out;
191 }
192 }
193 #endif
194 ret = g_strdup ("computer");
195 out:
196 g_free (filebuf);
197 return ret;
198 }*/