change buffer size for gethostname() to HOST_NAME_MAX to avoid accidently truncating...
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
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
17 #include <unistd.h>
18 #include <limits.h>
19
20 #include <glib.h>
21 #include <gio/gio.h>
22
23 #include "hostnamed.h"
24 #include "hostnamed-gen.c"
25
26 GPtrArray *hostnamed_freeable;
27 GDBusNodeInfo *spect_data;
28
29 static void on_bus_acquired(GDBusConnection *conn,
30 const gchar *name,
31 gpointer user_data) {
32
33 GError *err;
34
35 g_print("got bus, name: %s\n", name);
36
37 }
38
39 static void on_name_acquired(GDBusConnection *conn,
40 const gchar *name,
41 gpointer user_data) {
42
43 g_print("got name %s\n", name);
44 }
45
46 static void on_name_lost(GDBusConnection *conn,
47 const gchar *name,
48 gpointer user_data) {
49
50 g_print("lost name %s, exiting...", name);
51
52 hostnamed_mem_clean();
53
54 //TODO exit through g_main_loop properly...
55 }
56
57 /* safe call to try and start hostnamed */
58 GError *hostnamed_init() {
59
60 guint bus_descriptor;
61 GError *err = NULL;
62 gchar **hostnamed_ispect_xml;
63 gchar *hostnamed_joined_xml;
64
65 hostnamed_freeable = g_ptr_array_new();
66 hostnamed_ispect_xml = g_malloc(3000);
67
68 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, NULL);
69 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
70 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
71
72 g_free(hostnamed_ispect_xml);
73 g_ptr_array_add(hostnamed_freeable, hostnamed_joined_xml);
74
75 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
76 "org.freedesktop.hostname1",
77 G_BUS_NAME_OWNER_FLAGS_NONE,
78 on_bus_acquired,
79 on_name_acquired,
80 on_name_lost,
81 NULL,
82 NULL);
83
84 //TODO: malloc and return reference as if a main() closed
85 return err;
86 }
87
88 /* free()'s */
89 void hostnamed_mem_clean() {
90
91 g_ptr_array_foreach(hostnamed_freeable, (GFunc) g_free, NULL);
92 }
93
94 //TODO figure out DMI variables on obsd
95 /*static gchar *guess_icon_name() {
96
97 gchar *filebuf = NULL;
98 gchar *ret = NULL;
99
100 //TODO vm check
101
102 #if defined(__i386__) || defined(__x86_64__)
103
104 Taken with a few minor changes from systemd's hostnamed.c,
105 copyright 2011 Lennart Poettering.
106
107 See the SMBIOS Specification 2.7.1 section 7.4.1 for
108 details about the values listed here:
109
110 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
111
112
113 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
114 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
115 case 0x3:
116 case 0x4:
117 case 0x5:
118 case 0x6:
119 case 0x7:
120 ret = g_strdup ("computer-desktop");
121 goto out;
122 case 0x9:
123 case 0xA:
124 case 0xE:
125 ret = g_strdup ("computer-laptop");
126 goto out;
127 case 0x11:
128 case 0x17:
129 case 0x1C:
130 case 0x1D:
131 ret = g_strdup ("computer-server");
132 goto out;
133 }
134 }
135 #endif
136 ret = g_strdup ("computer");
137 out:
138 g_free (filebuf);
139 return ret;
140 }*/