remove includes in non-gen'd interf files, they're linked properly now
[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>
21
1e8c7c88 22#include "hostnamed-gen.h"
a35a69c5 23
fd8852d9 24GPtrArray *hostnamed_freeable;
ff1030d4 25GDBusNodeInfo *spect_data;
a06e63a1 26hostnamedHostname1 *hostnamed_interf;
3d53b501 27
76b67a18 28/* begin method/property/signal code */
29
30/* TODO make sure these guys only work if called by root */
3d53b501 31static gboolean
a06e63a1 32on_handle_set_hostname(hostnamedHostname1 *hn1_passed_interf,
3d53b501 33 GDBusMethodInvocation *invoc,
34 const gchar *greet,
35 gpointer data) {
36 return FALSE;
37}
38
39static gboolean
a06e63a1 40on_handle_set_static_hostname(hostnamedHostname1 *hn1_passed_interf,
3d53b501 41 GDBusMethodInvocation *invoc,
42 const gchar *greet,
43 gpointer data) {
44 return FALSE;
45}
46
47static gboolean
a06e63a1 48on_handle_set_pretty_hostname(hostnamedHostname1 *hn1_passed_interf,
3d53b501 49 GDBusMethodInvocation *invoc,
50 const gchar *greet,
51 gpointer data) {
52 return FALSE;
53}
54
55static gboolean
a06e63a1 56on_handle_set_chassis(hostnamedHostname1 *hn1_passed_interf,
3d53b501 57 GDBusMethodInvocation *invoc,
58 const gchar *greet,
59 gpointer data) {
60 return FALSE;
61}
62
63static gboolean
a06e63a1 64on_handle_set_icon_name(hostnamedHostname1 *hn1_passed_interf,
3d53b501 65 GDBusMethodInvocation *invoc,
66 const gchar *greet,
67 gpointer data) {
68 return FALSE;
69}
70
76b67a18 71/* note: all hostnamed/hostname1's properties are read-only,
72 * and do not need set_ functions, gdbus-codegen realized
73 * this from the XML and handled the to-be error of trying
74 * to set a read-only property's value
75 */
76
77const gchar *
78our_get_hostname() {
79
d15318db 80 int hostname_try;
81 gchar *hostname_buf;
82
83 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
84 hostname_try = gethostname(hostname_buf, MAXHOSTNAMELEN);
85
86 return hostname_buf;
76b67a18 87}
88
89const gchar *
90our_get_static_hostname() {
91
92 return "TODO";
93}
94
95const gchar *
96our_get_pretty_hostname() {
97
98 return "TODO";
99}
100
101const gchar *
102our_get_chassis() {
103
104 return "TODO";
105}
106
107const gchar *
108our_get_icon_name() {
109
110 return "TODO";
111}
112
113const gchar *
114our_get_kernel_name() {
115
116 return "TODO";
117}
118
119const gchar *
120our_get_kernel_version() {
121
122 return "TODO";
123}
124
125const gchar *
126our_get_kernel_release() {
127
128 return "TODO";
129}
130
131const gchar *
132our_get_os_cpename() {
133
134 return "TODO";
135}
136
137const gchar *
138our_get_os_pretty_name() {
139
140 return "TODO";
141}
142
3d53b501 143/* end method/property/signal code, begin bus/name handlers */
0df0018d 144
5b005882 145static void hostnamed_on_bus_acquired(GDBusConnection *conn,
1cd5e6fe 146 const gchar *name,
147 gpointer user_data) {
d1e1db9e 148
1cd5e6fe 149 g_print("got bus, name: %s\n", name);
d1e1db9e 150
ea207ed3 151}
152
5b005882 153static void hostnamed_on_name_acquired(GDBusConnection *conn,
1cd5e6fe 154 const gchar *name,
155 gpointer user_data) {
80043b36 156
3d53b501 157 g_print("got '%s' on system bus\n", name);
158
a06e63a1 159 hostnamed_interf = hostnamed_hostname1_skeleton_new();
3d53b501 160
76b67a18 161 /* attach function pointers to generated struct's method handlers */
3d53b501 162 g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
163 g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
164 g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
165 g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
166 g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
167
76b67a18 168 /* set our properties before export */
a06e63a1 169 hostnamed_hostname1_set_hostname(hostnamed_interf, our_get_hostname());
170 hostnamed_hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
171 hostnamed_hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
172 hostnamed_hostname1_set_chassis(hostnamed_interf, our_get_chassis());
173 hostnamed_hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
174 hostnamed_hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
175 hostnamed_hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
176 hostnamed_hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
177 hostnamed_hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
178 hostnamed_hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
76b67a18 179
3d53b501 180 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
181 conn,
182 "/org/freedesktop/hostname1",
183 NULL)) {
184
185 g_printf("failed to export hostname1's interface on system bus!");
186 }
187
ea207ed3 188}
189
1e8c7c88 190/* free()'s */
191void hostnamed_mem_clean() {
192
193 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
194}
195
5b005882 196static void hostnamed_on_name_lost(GDBusConnection *conn,
1cd5e6fe 197 const gchar *name,
198 gpointer user_data) {
80043b36 199
1cd5e6fe 200 g_print("lost name %s, exiting...", name);
fd8852d9 201
1cd5e6fe 202 hostnamed_mem_clean();
3d53b501 203 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
fd8852d9 204
a6f11205 205 /* TODO exit through g_main_loop properly... */
ea207ed3 206}
207
0df0018d 208/* safe call to try and start hostnamed */
7f0a0212 209void hostnamed_init() {
387173cb 210
1cd5e6fe 211 guint bus_descriptor;
fd8852d9 212
1cd5e6fe 213 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
214 "org.freedesktop.hostname1",
215 G_BUS_NAME_OWNER_FLAGS_NONE,
5b005882 216 hostnamed_on_bus_acquired,
217 hostnamed_on_name_acquired,
218 hostnamed_on_name_lost,
1cd5e6fe 219 NULL,
220 NULL);
496f5d66 221
a6f11205 222 /* TODO: malloc and return reference as if a main() closed */
780e1f19 223}
a35a69c5 224
1e8c7c88 225int main() {
36575bff 226
1e8c7c88 227 GMainLoop *hostnamed_loop;
228 hostnamed_loop = g_main_loop_new(NULL, TRUE);
229
230 /* config stuff here */
231
232
233 hostnamed_init();
234 g_main_loop_run(hostnamed_loop);
235 g_main_loop_unref(hostnamed_loop);
236
237 return 0;
a35a69c5 238}
239
a6f11205 240/* TODO figure out DMI variables on obsd */
a35a69c5 241/*static gchar *guess_icon_name() {
242
243 gchar *filebuf = NULL;
244 gchar *ret = NULL;
245
1cd5e6fe 246 #if defined(__i386__) || defined(__x86_64__)
39df6847 247
a35a69c5 248 Taken with a few minor changes from systemd's hostnamed.c,
249 copyright 2011 Lennart Poettering.
250
251 See the SMBIOS Specification 2.7.1 section 7.4.1 for
252 details about the values listed here:
253
254 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
39df6847 255
a35a69c5 256
257 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
258 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
259 case 0x3:
260 case 0x4:
261 case 0x5:
262 case 0x6:
263 case 0x7:
264 ret = g_strdup ("computer-desktop");
265 goto out;
266 case 0x9:
267 case 0xA:
268 case 0xE:
269 ret = g_strdup ("computer-laptop");
270 goto out;
271 case 0x11:
272 case 0x17:
273 case 0x1C:
274 case 0x1D:
275 ret = g_strdup ("computer-server");
276 goto out;
277 }
278 }
1cd5e6fe 279 #endif
a35a69c5 280 ret = g_strdup ("computer");
281 out:
282 g_free (filebuf);
283 return ret;
284}*/
1e8c7c88 285