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.
21 #include <sys/param.h>
24 #include <glib/gprintf.h>
25 #include <glib-unix.h>
27 #include "hostnamed-gen.h"
28 #include "hostnamed.h"
30 GPtrArray
*hostnamed_freeable
;
31 Hostname1
*hostnamed_interf
;
33 GMainLoop
*hostnamed_loop
;
36 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
38 /* --- begin method/property/dbus signal code --- */
41 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
42 GDBusMethodInvocation
*invoc
,
49 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
50 GDBusMethodInvocation
*invoc
,
57 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
58 GDBusMethodInvocation
*invoc
,
65 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
66 GDBusMethodInvocation
*invoc
,
73 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
74 GDBusMethodInvocation
*invoc
,
80 /* note: all hostnamed/hostname1's properties are read-only,
81 * and do not need set_ functions, gdbus-codegen realized
82 * this from the XML and handled the to-be error of trying
83 * to set a read-only property's value
89 gchar
*hostname_buf
, *ret
;
90 size_t hostname_divider
;
92 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
); /* todo check & free */
93 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
94 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
95 g_ptr_array_add(hostnamed_freeable
, ret
);
97 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
100 hostname_divider
= strcspn(hostname_buf
, ".");
102 return strncpy(ret
, hostname_buf
, hostname_divider
);
106 our_get_static_hostname() {
112 our_get_pretty_hostname() {
124 our_get_icon_name() {
130 our_get_kernel_name() {
136 our_get_kernel_version() {
142 our_get_kernel_release() {
148 our_get_os_cpename() {
154 our_get_os_pretty_name() {
159 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
161 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
163 gpointer user_data
) {
165 g_printf("got bus/name, exporting %s's interface...\n", name
);
167 hostnamed_interf
= hostname1_skeleton_new();
169 /* attach function pointers to generated struct's method handlers */
170 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
171 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
172 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
173 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
174 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
176 /* set our properties before export */
177 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
178 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
179 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
180 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
181 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
182 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
183 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
184 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
185 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
186 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
188 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
190 "/org/freedesktop/hostname1",
193 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
194 hostnamed_mem_clean();
198 dbus_interface_exported
= TRUE
;
199 g_printf("exported %s's interface on the system bus...\n", name
);
203 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
205 gpointer user_data
) {
207 g_printf("success!\n");
210 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
212 gpointer user_data
) {
216 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name
);
217 hostnamed_mem_clean();
220 g_printf("lost name %s, exiting...\n", name
);
222 hostnamed_mem_clean();
225 /* --- end bus/name handlers, begin misc unix functions --- */
227 /* safe call to clean and then exit
228 * this stops our GMainLoop safely before letting main() return */
229 void hostnamed_mem_clean() {
231 g_printf("exiting...\n");
233 if(dbus_interface_exported
)
234 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
236 if(g_main_loop_is_running(hostnamed_loop
))
237 g_main_loop_quit(hostnamed_loop
);
241 /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
242 gboolean
unix_sig_terminate_handler(gpointer data
) {
244 g_printf("caught SIGINT/HUP/TERM, exiting\n");
246 hostnamed_mem_clean();
247 return G_SOURCE_REMOVE
;
250 void set_signal_handlers() {
252 /* we don't care about its descriptor, we never need to unregister these */
253 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
254 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
255 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
260 set_signal_handlers();
262 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
263 hostnamed_freeable
= g_ptr_array_new();
265 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
266 "org.freedesktop.hostname1",
267 G_BUS_NAME_OWNER_FLAGS_NONE
,
268 hostnamed_on_bus_acquired
,
269 hostnamed_on_name_acquired
,
270 hostnamed_on_name_lost
,
274 g_main_loop_run(hostnamed_loop
);
275 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
276 g_main_loop_unref(hostnamed_loop
);
278 /* guaranteed unownable */
279 g_bus_unown_name(bus_descriptor
);
281 /* at this point no operations can occur with our data, it is safe to free it + its container */
282 g_ptr_array_free(hostnamed_freeable
, TRUE
);
287 /* TODO figure out DMI variables on obsd */
288 /*static gchar *guess_icon_name() {
290 gchar *filebuf = NULL;
293 #if defined(__i386__) || defined(__x86_64__)
295 Taken with a few minor changes from systemd's hostnamed.c,
296 copyright 2011 Lennart Poettering.
298 See the SMBIOS Specification 2.7.1 section 7.4.1 for
299 details about the values listed here:
301 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
304 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
305 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
311 ret = g_strdup ("computer-desktop");
316 ret = g_strdup ("computer-laptop");
322 ret = g_strdup ("computer-server");
327 ret = g_strdup ("computer");