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.
23 #include "hostnamed.h"
24 #include "hostnamed-gen.c"
26 GPtrArray
*hostnamed_freeable
;
27 GDBusNodeInfo
*spect_data
;
28 Hostname1
*hostnamed_interf
;
31 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
32 GDBusMethodInvocation
*invoc
,
39 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
40 GDBusMethodInvocation
*invoc
,
47 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
48 GDBusMethodInvocation
*invoc
,
55 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
56 GDBusMethodInvocation
*invoc
,
63 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
64 GDBusMethodInvocation
*invoc
,
70 /* end method/property/signal code, begin bus/name handlers */
72 static void on_bus_acquired(GDBusConnection
*conn
,
76 g_print("got bus, name: %s\n", name
);
80 static void on_name_acquired(GDBusConnection
*conn
,
84 g_print("got '%s' on system bus\n", name
);
86 hostnamed_interf
= hostname1_skeleton_new();
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
);
95 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
97 "/org/freedesktop/hostname1",
100 g_printf("failed to export hostname1's interface on system bus!");
105 static void on_name_lost(GDBusConnection
*conn
,
107 gpointer user_data
) {
109 g_print("lost name %s, exiting...", name
);
111 hostnamed_mem_clean();
112 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
114 /* TODO exit through g_main_loop properly... */
117 /* safe call to try and start hostnamed */
118 GError
*hostnamed_init() {
120 guint bus_descriptor
;
122 gchar
**hostnamed_ispect_xml
;
123 gchar
*hostnamed_joined_xml
;
125 hostnamed_freeable
= g_ptr_array_new();
126 hostnamed_ispect_xml
= g_malloc(3000);
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
);
132 g_free(hostnamed_ispect_xml
);
133 g_ptr_array_add(hostnamed_freeable
, hostnamed_joined_xml
);
135 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
136 "org.freedesktop.hostname1",
137 G_BUS_NAME_OWNER_FLAGS_NONE
,
144 /* TODO: malloc and return reference as if a main() closed */
149 void hostnamed_mem_clean() {
151 g_ptr_array_foreach(hostnamed_freeable
, (GFunc
) g_free
, NULL
);
154 /* TODO figure out DMI variables on obsd */
155 /*static gchar *guess_icon_name() {
157 gchar *filebuf = NULL;
160 #if defined(__i386__) || defined(__x86_64__)
162 Taken with a few minor changes from systemd's hostnamed.c,
163 copyright 2011 Lennart Poettering.
165 See the SMBIOS Specification 2.7.1 section 7.4.1 for
166 details about the values listed here:
168 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
171 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
172 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
178 ret = g_strdup ("computer-desktop");
183 ret = g_strdup ("computer-laptop");
189 ret = g_strdup ("computer-server");
194 ret = g_strdup ("computer");