2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
20 #include <sys/param.h>
25 #include "hostnamed.h"
26 #include "hostnamed-gen.c"
28 GPtrArray
*hostnamed_freeable
;
29 GDBusNodeInfo
*spect_data
;
30 hostnamedHostname1
*hostnamed_interf
;
32 /* begin method/property/signal code */
34 /* TODO make sure these guys only work if called by root */
36 on_handle_set_hostname(hostnamedHostname1
*hn1_passed_interf
,
37 GDBusMethodInvocation
*invoc
,
44 on_handle_set_static_hostname(hostnamedHostname1
*hn1_passed_interf
,
45 GDBusMethodInvocation
*invoc
,
52 on_handle_set_pretty_hostname(hostnamedHostname1
*hn1_passed_interf
,
53 GDBusMethodInvocation
*invoc
,
60 on_handle_set_chassis(hostnamedHostname1
*hn1_passed_interf
,
61 GDBusMethodInvocation
*invoc
,
68 on_handle_set_icon_name(hostnamedHostname1
*hn1_passed_interf
,
69 GDBusMethodInvocation
*invoc
,
75 /* note: all hostnamed/hostname1's properties are read-only,
76 * and do not need set_ functions, gdbus-codegen realized
77 * this from the XML and handled the to-be error of trying
78 * to set a read-only property's value
87 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
88 hostname_try
= gethostname(hostname_buf
, MAXHOSTNAMELEN
);
94 our_get_static_hostname() {
100 our_get_pretty_hostname() {
112 our_get_icon_name() {
118 our_get_kernel_name() {
124 our_get_kernel_version() {
130 our_get_kernel_release() {
136 our_get_os_cpename() {
142 our_get_os_pretty_name() {
147 /* end method/property/signal code, begin bus/name handlers */
149 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
151 gpointer user_data
) {
153 g_print("got bus, name: %s\n", name
);
157 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
159 gpointer user_data
) {
161 g_print("got '%s' on system bus\n", name
);
163 hostnamed_interf
= hostnamed_hostname1_skeleton_new();
165 /* attach function pointers to generated struct's method handlers */
166 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
167 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
168 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
169 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
170 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
172 /* set our properties before export */
173 hostnamed_hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
174 hostnamed_hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
175 hostnamed_hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
176 hostnamed_hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
177 hostnamed_hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
178 hostnamed_hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
179 hostnamed_hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
180 hostnamed_hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
181 hostnamed_hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
182 hostnamed_hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
184 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
186 "/org/freedesktop/hostname1",
189 g_printf("failed to export hostname1's interface on system bus!");
194 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
196 gpointer user_data
) {
198 g_print("lost name %s, exiting...", name
);
200 hostnamed_mem_clean();
201 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
203 /* TODO exit through g_main_loop properly... */
206 /* safe call to try and start hostnamed */
207 void hostnamed_init() {
209 guint bus_descriptor
;
211 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
212 "org.freedesktop.hostname1",
213 G_BUS_NAME_OWNER_FLAGS_NONE
,
214 hostnamed_on_bus_acquired
,
215 hostnamed_on_name_acquired
,
216 hostnamed_on_name_lost
,
220 /* TODO: malloc and return reference as if a main() closed */
224 void hostnamed_mem_clean() {
226 g_ptr_array_foreach(hostnamed_freeable
, (GFunc
) g_free
, NULL
);
229 /* TODO figure out DMI variables on obsd */
230 /*static gchar *guess_icon_name() {
232 gchar *filebuf = NULL;
235 #if defined(__i386__) || defined(__x86_64__)
237 Taken with a few minor changes from systemd's hostnamed.c,
238 copyright 2011 Lennart Poettering.
240 See the SMBIOS Specification 2.7.1 section 7.4.1 for
241 details about the values listed here:
243 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
246 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
247 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
253 ret = g_strdup ("computer-desktop");
258 ret = g_strdup ("computer-laptop");
264 ret = g_strdup ("computer-server");
269 ret = g_strdup ("computer");