following last commit, freshen up all ispect xml files with a on an updated archlinu...
[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
20#include <glib.h>
ea207ed3 21#include <gio/gio.h>
22
fd8852d9 23#include "hostnamed.h"
24#include "hostnamed-gen.c"
a35a69c5 25
fd8852d9 26GPtrArray *hostnamed_freeable;
ff1030d4 27GDBusNodeInfo *spect_data;
3d53b501 28Hostname1 *hostnamed_interf;
29
30static gboolean
31on_handle_set_hostname(Hostname1 *hn1_passed_interf,
32 GDBusMethodInvocation *invoc,
33 const gchar *greet,
34 gpointer data) {
35 return FALSE;
36}
37
38static gboolean
39on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
40 GDBusMethodInvocation *invoc,
41 const gchar *greet,
42 gpointer data) {
43 return FALSE;
44}
45
46static gboolean
47on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
48 GDBusMethodInvocation *invoc,
49 const gchar *greet,
50 gpointer data) {
51 return FALSE;
52}
53
54static gboolean
55on_handle_set_chassis(Hostname1 *hn1_passed_interf,
56 GDBusMethodInvocation *invoc,
57 const gchar *greet,
58 gpointer data) {
59 return FALSE;
60}
61
62static gboolean
63on_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 */
0df0018d 71
80043b36 72static void on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 73 const gchar *name,
74 gpointer user_data) {
d1e1db9e 75
1cd5e6fe 76 g_print("got bus, name: %s\n", name);
d1e1db9e 77
ea207ed3 78}
79
80043b36 80static void on_name_acquired(GDBusConnection *conn,
1cd5e6fe 81 const gchar *name,
82 gpointer user_data) {
80043b36 83
3d53b501 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
ea207ed3 103}
104
80043b36 105static void on_name_lost(GDBusConnection *conn,
1cd5e6fe 106 const gchar *name,
107 gpointer user_data) {
80043b36 108
1cd5e6fe 109 g_print("lost name %s, exiting...", name);
fd8852d9 110
1cd5e6fe 111 hostnamed_mem_clean();
3d53b501 112 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
fd8852d9 113
a6f11205 114 /* TODO exit through g_main_loop properly... */
ea207ed3 115}
116
0df0018d 117/* safe call to try and start hostnamed */
39df6847 118GError *hostnamed_init() {
387173cb 119
1cd5e6fe 120 guint bus_descriptor;
121 GError *err = NULL;
122 gchar **hostnamed_ispect_xml;
123 gchar *hostnamed_joined_xml;
d1e1db9e 124
1cd5e6fe 125 hostnamed_freeable = g_ptr_array_new();
126 hostnamed_ispect_xml = g_malloc(3000);
fd8852d9 127
1cd5e6fe 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);
a35a69c5 131
1cd5e6fe 132 g_free(hostnamed_ispect_xml);
133 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
fd8852d9 134
1cd5e6fe 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);
496f5d66 143
a6f11205 144 /* TODO: malloc and return reference as if a main() closed */
1cd5e6fe 145 return err;
780e1f19 146}
a35a69c5 147
fd8852d9 148/* free()'s */
149void hostnamed_mem_clean() {
36575bff 150
1cd5e6fe 151 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
a35a69c5 152}
153
a6f11205 154/* TODO figure out DMI variables on obsd */
a35a69c5 155/*static gchar *guess_icon_name() {
156
157 gchar *filebuf = NULL;
158 gchar *ret = NULL;
159
1cd5e6fe 160 #if defined(__i386__) || defined(__x86_64__)
39df6847 161
a35a69c5 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
39df6847 169
a35a69c5 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 }
1cd5e6fe 193 #endif
a35a69c5 194 ret = g_strdup ("computer");
195 out:
196 g_free (filebuf);
197 return ret;
198}*/