d54fb379dc4de93a6a47253fc70508c8ba573527
[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 #include <glib-unix.h>
25
26 #include "hostnamed-gen.h"
27
28 GPtrArray *hostnamed_freeable;
29 Hostname1 *hostnamed_interf;
30
31 /* --- begin method/property/signal code --- */
32
33 static gboolean
34 on_handle_set_hostname(Hostname1 *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(Hostname1 *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(Hostname1 *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(Hostname1 *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(Hostname1 *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 gchar *hostname_buf, *ret;
83 size_t hostname_divider;
84
85 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
86 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
87 g_ptr_array_add(hostnamed_freeable, hostname_buf);
88 g_ptr_array_add(hostnamed_freeable, ret);
89
90 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
91 return "";
92
93 hostname_divider = strcspn(hostname_buf, ".");
94
95 return strncpy(ret, hostname_buf, hostname_divider);
96 }
97
98 const gchar *
99 our_get_static_hostname() {
100
101 return "TODO";
102 }
103
104 const gchar *
105 our_get_pretty_hostname() {
106
107 return "TODO";
108 }
109
110 const gchar *
111 our_get_chassis() {
112
113 return "TODO";
114 }
115
116 const gchar *
117 our_get_icon_name() {
118
119 return "TODO";
120 }
121
122 const gchar *
123 our_get_kernel_name() {
124
125 return "TODO";
126 }
127
128 const gchar *
129 our_get_kernel_version() {
130
131 return "TODO";
132 }
133
134 const gchar *
135 our_get_kernel_release() {
136
137 return "TODO";
138 }
139
140 const gchar *
141 our_get_os_cpename() {
142
143 return "TODO";
144 }
145
146 const gchar *
147 our_get_os_pretty_name() {
148
149 return "TODO";
150 }
151
152 /* --- end method/property/signal code, begin bus/name handlers --- */
153
154 static void hostnamed_on_bus_acquired(GDBusConnection *conn,
155 const gchar *name,
156 gpointer user_data) {
157
158 g_print("got bus, name: %s\n", name);
159
160 }
161
162 static void hostnamed_on_name_acquired(GDBusConnection *conn,
163 const gchar *name,
164 gpointer user_data) {
165
166 g_print("got '%s' on system bus\n", name);
167
168 hostnamed_interf = hostname1_skeleton_new();
169
170 /* attach function pointers to generated struct's method handlers */
171 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
172 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
173 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
174 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
175 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
176
177 /* set our properties before export */
178 hostname1_set_hostname(hostnamed_interf, our_get_hostname());
179 hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
180 hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
181 hostname1_set_chassis(hostnamed_interf, our_get_chassis());
182 hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
183 hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
184 hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
185 hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
186 hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
187 hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
188
189 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
190 conn,
191 "/org/freedesktop/hostname1",
192 NULL)) {
193
194 g_printf("Failed to export Hostname1's interface!");
195 }
196
197 }
198
199 /* --- end bus/name handlers, begin misc functions --- */
200
201 /* free()'s */
202 void hostnamed_mem_clean() {
203
204 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
205 g_ptr_array_free(hostnamed_freeable, TRUE);
206 }
207
208 static void hostnamed_on_name_lost(GDBusConnection *conn,
209 const gchar *name,
210 gpointer user_data) {
211
212 g_print("lost name %s, exiting...", name);
213
214 hostnamed_mem_clean();
215 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
216
217 }
218
219 int main() {
220
221 guint bus_descriptor;
222 GMainLoop *hostnamed_loop;
223
224 hostnamed_loop = g_main_loop_new(NULL, TRUE);
225 hostnamed_freeable = g_ptr_array_new();
226
227 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
228 "org.freedesktop.hostname1",
229 G_BUS_NAME_OWNER_FLAGS_NONE,
230 hostnamed_on_bus_acquired,
231 hostnamed_on_name_acquired,
232 hostnamed_on_name_lost,
233 NULL,
234 NULL);
235
236 g_main_loop_run(hostnamed_loop);
237 g_main_loop_unref(hostnamed_loop);
238
239 g_bus_unown_name(bus_descriptor);
240
241 hostnamed_mem_clean();
242
243 return 0;
244 }
245
246 /* TODO figure out DMI variables on obsd */
247 /*static gchar *guess_icon_name() {
248
249 gchar *filebuf = NULL;
250 gchar *ret = NULL;
251
252 #if defined(__i386__) || defined(__x86_64__)
253
254 Taken with a few minor changes from systemd's hostnamed.c,
255 copyright 2011 Lennart Poettering.
256
257 See the SMBIOS Specification 2.7.1 section 7.4.1 for
258 details about the values listed here:
259
260 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
261
262
263 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
264 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
265 case 0x3:
266 case 0x4:
267 case 0x5:
268 case 0x6:
269 case 0x7:
270 ret = g_strdup ("computer-desktop");
271 goto out;
272 case 0x9:
273 case 0xA:
274 case 0xE:
275 ret = g_strdup ("computer-laptop");
276 goto out;
277 case 0x11:
278 case 0x17:
279 case 0x1C:
280 case 0x1D:
281 ret = g_strdup ("computer-server");
282 goto out;
283 }
284 }
285 #endif
286 ret = g_strdup ("computer");
287 out:
288 g_free (filebuf);
289 return ret;
290 }*/
291