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