minor, clarify "dbus signal" vs. "unix signal" in comments
[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>
fd8852d9 19
d15318db 20#include <sys/param.h>
28cac2f0 21#include <string.h>
d15318db 22
4c04b514 23#include <glib/gprintf.h>
8caf1f61 24#include <glib-unix.h>
4c04b514 25
1e8c7c88 26#include "hostnamed-gen.h"
a35a69c5 27
fd8852d9 28GPtrArray *hostnamed_freeable;
1be94ede 29Hostname1 *hostnamed_interf;
3d53b501 30
1ce41045 31/* --- begin method/property/dbus signal code --- */
76b67a18 32
3d53b501 33static gboolean
1be94ede 34on_handle_set_hostname(Hostname1 *hn1_passed_interf,
3d53b501 35 GDBusMethodInvocation *invoc,
36 const gchar *greet,
37 gpointer data) {
38 return FALSE;
39}
40
41static gboolean
1be94ede 42on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 43 GDBusMethodInvocation *invoc,
44 const gchar *greet,
45 gpointer data) {
46 return FALSE;
47}
48
49static gboolean
1be94ede 50on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 51 GDBusMethodInvocation *invoc,
52 const gchar *greet,
53 gpointer data) {
54 return FALSE;
55}
56
57static gboolean
1be94ede 58on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 59 GDBusMethodInvocation *invoc,
60 const gchar *greet,
61 gpointer data) {
62 return FALSE;
63}
64
65static gboolean
1be94ede 66on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 67 GDBusMethodInvocation *invoc,
68 const gchar *greet,
69 gpointer data) {
70 return FALSE;
71}
72
76b67a18 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
79const gchar *
80our_get_hostname() {
81
28cac2f0 82 gchar *hostname_buf, *ret;
83 size_t hostname_divider;
d15318db 84
85 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
28cac2f0 86 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
7323a4e4 87 g_ptr_array_add(hostnamed_freeable, hostname_buf);
28cac2f0 88 g_ptr_array_add(hostnamed_freeable, ret);
7323a4e4 89
90 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
91 return "";
d15318db 92
28cac2f0 93 hostname_divider = strcspn(hostname_buf, ".");
94
95 return strncpy(ret, hostname_buf, hostname_divider);
76b67a18 96}
97
98const gchar *
99our_get_static_hostname() {
100
101 return "TODO";
102}
103
104const gchar *
105our_get_pretty_hostname() {
106
107 return "TODO";
108}
109
110const gchar *
111our_get_chassis() {
112
113 return "TODO";
114}
115
116const gchar *
117our_get_icon_name() {
118
119 return "TODO";
120}
121
122const gchar *
123our_get_kernel_name() {
124
125 return "TODO";
126}
127
128const gchar *
129our_get_kernel_version() {
130
131 return "TODO";
132}
133
134const gchar *
135our_get_kernel_release() {
136
137 return "TODO";
138}
139
140const gchar *
141our_get_os_cpename() {
142
143 return "TODO";
144}
145
146const gchar *
147our_get_os_pretty_name() {
148
149 return "TODO";
150}
151
1ce41045 152/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 153
5b005882 154static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 155 const gchar *name,
156 gpointer user_data) {
d1e1db9e 157
1cd5e6fe 158 g_print("got bus, name: %s\n", name);
d1e1db9e 159
ea207ed3 160}
161
5b005882 162static void hostnamed_on_name_acquired(GDBusConnection *conn,
1be94ede 163 const gchar *name,
164 gpointer user_data) {
80043b36 165
3d53b501 166 g_print("got '%s' on system bus\n", name);
167
1be94ede 168 hostnamed_interf = hostname1_skeleton_new();
3d53b501 169
76b67a18 170 /* attach function pointers to generated struct's method handlers */
3d53b501 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
76b67a18 177 /* set our properties before export */
1be94ede 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());
76b67a18 188
3d53b501 189 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 190 conn,
191 "/org/freedesktop/hostname1",
192 NULL)) {
3d53b501 193
1be94ede 194 g_printf("Failed to export Hostname1's interface!");
3d53b501 195 }
196
ea207ed3 197}
198
7323a4e4 199/* --- end bus/name handlers, begin misc functions --- */
200
1e8c7c88 201/* free()'s */
202void hostnamed_mem_clean() {
203
c62bceb7 204 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
205 g_ptr_array_free(hostnamed_freeable, TRUE);
1e8c7c88 206}
207
5b005882 208static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 209 const gchar *name,
210 gpointer user_data) {
80043b36 211
1cd5e6fe 212 g_print("lost name %s, exiting...", name);
fd8852d9 213
1cd5e6fe 214 hostnamed_mem_clean();
3d53b501 215 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
fd8852d9 216
ea207ed3 217}
218
c62bceb7 219int main() {
220
221 guint bus_descriptor;
222 GMainLoop *hostnamed_loop;
387173cb 223
28cac2f0 224 hostnamed_loop = g_main_loop_new(NULL, TRUE);
225 hostnamed_freeable = g_ptr_array_new();
fd8852d9 226
c62bceb7 227 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1cd5e6fe 228 "org.freedesktop.hostname1",
229 G_BUS_NAME_OWNER_FLAGS_NONE,
5b005882 230 hostnamed_on_bus_acquired,
231 hostnamed_on_name_acquired,
232 hostnamed_on_name_lost,
1cd5e6fe 233 NULL,
234 NULL);
496f5d66 235
1e8c7c88 236 g_main_loop_run(hostnamed_loop);
237 g_main_loop_unref(hostnamed_loop);
238
c62bceb7 239 g_bus_unown_name(bus_descriptor);
240
7323a4e4 241 hostnamed_mem_clean();
242
1e8c7c88 243 return 0;
a35a69c5 244}
245
a6f11205 246/* TODO figure out DMI variables on obsd */
a35a69c5 247/*static gchar *guess_icon_name() {
248
249 gchar *filebuf = NULL;
250 gchar *ret = NULL;
251
1cd5e6fe 252 #if defined(__i386__) || defined(__x86_64__)
39df6847 253
a35a69c5 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
39df6847 261
a35a69c5 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 }
1cd5e6fe 285 #endif
a35a69c5 286 ret = g_strdup ("computer");
287 out:
288 g_free (filebuf);
289 return ret;
290}*/
1e8c7c88 291