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