33ea79ead417772803d6eb5c7f7ee73ebbb8b2e0
[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 <sys/param.h>
21 #include <string.h>
22
23 #include <glib/gprintf.h>
24
25 #include "hostnamed-gen.h"
26
27 GPtrArray *hostnamed_freeable;
28 Hostname1 *hostnamed_interf;
29
30 /* --- begin method/property/signal code --- */
31
32 static gboolean
33 on_handle_set_hostname(Hostname1 *hn1_passed_interf,
34 GDBusMethodInvocation *invoc,
35 const gchar *greet,
36 gpointer data) {
37 return FALSE;
38 }
39
40 static gboolean
41 on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
42 GDBusMethodInvocation *invoc,
43 const gchar *greet,
44 gpointer data) {
45 return FALSE;
46 }
47
48 static gboolean
49 on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
50 GDBusMethodInvocation *invoc,
51 const gchar *greet,
52 gpointer data) {
53 return FALSE;
54 }
55
56 static gboolean
57 on_handle_set_chassis(Hostname1 *hn1_passed_interf,
58 GDBusMethodInvocation *invoc,
59 const gchar *greet,
60 gpointer data) {
61 return FALSE;
62 }
63
64 static gboolean
65 on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
66 GDBusMethodInvocation *invoc,
67 const gchar *greet,
68 gpointer data) {
69 return FALSE;
70 }
71
72 /* note: all hostnamed/hostname1's properties are read-only,
73 * and do not need set_ functions, gdbus-codegen realized
74 * this from the XML and handled the to-be error of trying
75 * to set a read-only property's value
76 */
77
78 const gchar *
79 our_get_hostname() {
80
81 gchar *hostname_buf, *ret;
82 size_t hostname_divider;
83
84 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
85 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
86 g_ptr_array_add(hostnamed_freeable, hostname_buf);
87 g_ptr_array_add(hostnamed_freeable, ret);
88
89 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
90 return "";
91
92 hostname_divider = strcspn(hostname_buf, ".");
93
94 return strncpy(ret, hostname_buf, hostname_divider);
95 }
96
97 const gchar *
98 our_get_static_hostname() {
99
100 return "TODO";
101 }
102
103 const gchar *
104 our_get_pretty_hostname() {
105
106 return "TODO";
107 }
108
109 const gchar *
110 our_get_chassis() {
111
112 return "TODO";
113 }
114
115 const gchar *
116 our_get_icon_name() {
117
118 return "TODO";
119 }
120
121 const gchar *
122 our_get_kernel_name() {
123
124 return "TODO";
125 }
126
127 const gchar *
128 our_get_kernel_version() {
129
130 return "TODO";
131 }
132
133 const gchar *
134 our_get_kernel_release() {
135
136 return "TODO";
137 }
138
139 const gchar *
140 our_get_os_cpename() {
141
142 return "TODO";
143 }
144
145 const gchar *
146 our_get_os_pretty_name() {
147
148 return "TODO";
149 }
150
151 /* --- end method/property/signal code, begin bus/name handlers --- */
152
153 static void hostnamed_on_bus_acquired(GDBusConnection *conn,
154 const gchar *name,
155 gpointer user_data) {
156
157 g_print("got bus, name: %s\n", name);
158
159 }
160
161 static void hostnamed_on_name_acquired(GDBusConnection *conn,
162 const gchar *name,
163 gpointer user_data) {
164
165 g_print("got '%s' on system bus\n", name);
166
167 hostnamed_interf = hostname1_skeleton_new();
168
169 /* attach function pointers to generated struct's method handlers */
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
176 /* set our properties before export */
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());
187
188 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
189 conn,
190 "/org/freedesktop/hostname1",
191 NULL)) {
192
193 g_printf("Failed to export Hostname1's interface!");
194 }
195
196 }
197
198 /* --- end bus/name handlers, begin misc functions --- */
199
200 /* free()'s */
201 void hostnamed_mem_clean() {
202
203 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
204 g_ptr_array_free(hostnamed_freeable, TRUE);
205 }
206
207 static void hostnamed_on_name_lost(GDBusConnection *conn,
208 const gchar *name,
209 gpointer user_data) {
210
211 g_print("lost name %s, exiting...", name);
212
213 hostnamed_mem_clean();
214 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
215
216 }
217
218 int main() {
219
220 guint bus_descriptor;
221 GMainLoop *hostnamed_loop;
222
223 hostnamed_loop = g_main_loop_new(NULL, TRUE);
224 hostnamed_freeable = g_ptr_array_new();
225
226 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
227 "org.freedesktop.hostname1",
228 G_BUS_NAME_OWNER_FLAGS_NONE,
229 hostnamed_on_bus_acquired,
230 hostnamed_on_name_acquired,
231 hostnamed_on_name_lost,
232 NULL,
233 NULL);
234
235 g_main_loop_run(hostnamed_loop);
236 g_main_loop_unref(hostnamed_loop);
237
238 g_bus_unown_name(bus_descriptor);
239
240 hostnamed_mem_clean();
241
242 return 0;
243 }
244
245 /* TODO figure out DMI variables on obsd */
246 /*static gchar *guess_icon_name() {
247
248 gchar *filebuf = NULL;
249 gchar *ret = NULL;
250
251 #if defined(__i386__) || defined(__x86_64__)
252
253 Taken with a few minor changes from systemd's hostnamed.c,
254 copyright 2011 Lennart Poettering.
255
256 See the SMBIOS Specification 2.7.1 section 7.4.1 for
257 details about the values listed here:
258
259 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
260
261
262 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
263 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
264 case 0x3:
265 case 0x4:
266 case 0x5:
267 case 0x6:
268 case 0x7:
269 ret = g_strdup ("computer-desktop");
270 goto out;
271 case 0x9:
272 case 0xA:
273 case 0xE:
274 ret = g_strdup ("computer-laptop");
275 goto out;
276 case 0x11:
277 case 0x17:
278 case 0x1C:
279 case 0x1D:
280 ret = g_strdup ("computer-server");
281 goto out;
282 }
283 }
284 #endif
285 ret = g_strdup ("computer");
286 out:
287 g_free (filebuf);
288 return ret;
289 }*/
290