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>
23 #include <glib/gprintf.h>
25 #include "hostnamed-gen.h"
27 GPtrArray
*hostnamed_freeable
;
28 Hostname1
*hostnamed_interf
;
30 /* --- begin method/property/signal code --- */
33 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
34 GDBusMethodInvocation
*invoc
,
41 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
42 GDBusMethodInvocation
*invoc
,
49 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
50 GDBusMethodInvocation
*invoc
,
57 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
58 GDBusMethodInvocation
*invoc
,
65 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
66 GDBusMethodInvocation
*invoc
,
72 /* note: all hostnamed/hostname1's properties are read-only,
73 * and do not need set_ functions, gdbus-codegen realized
74 * this from the XML and handled the to-be error of trying
75 * to set a read-only property's value
81 gchar
*hostname_buf
, *ret
;
82 size_t hostname_divider
;
84 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
85 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
86 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
87 g_ptr_array_add(hostnamed_freeable
, ret
);
89 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
92 hostname_divider
= strcspn(hostname_buf
, ".");
94 return strncpy(ret
, hostname_buf
, hostname_divider
);
98 our_get_static_hostname() {
104 our_get_pretty_hostname() {
116 our_get_icon_name() {
122 our_get_kernel_name() {
128 our_get_kernel_version() {
134 our_get_kernel_release() {
140 our_get_os_cpename() {
146 our_get_os_pretty_name() {
151 /* --- end method/property/signal code, begin bus/name handlers --- */
153 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
155 gpointer user_data
) {
157 g_print("got bus, name: %s\n", name
);
161 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
163 gpointer user_data
) {
165 g_print("got '%s' on system bus\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 Hostname1's interface!");
198 /* --- end bus/name handlers, begin misc functions --- */
201 void hostnamed_mem_clean() {
203 g_ptr_array_foreach(hostnamed_freeable
, (GFunc
) g_free
, NULL
);
204 g_ptr_array_free(hostnamed_freeable
, TRUE
);
207 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
209 gpointer user_data
) {
211 g_print("lost name %s, exiting...", name
);
213 hostnamed_mem_clean();
214 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
220 guint bus_descriptor
;
221 GMainLoop
*hostnamed_loop
;
223 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
224 hostnamed_freeable
= g_ptr_array_new();
226 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
227 "org.freedesktop.hostname1",
228 G_BUS_NAME_OWNER_FLAGS_NONE
,
229 hostnamed_on_bus_acquired
,
230 hostnamed_on_name_acquired
,
231 hostnamed_on_name_lost
,
235 g_main_loop_run(hostnamed_loop
);
236 g_main_loop_unref(hostnamed_loop
);
238 g_bus_unown_name(bus_descriptor
);
240 hostnamed_mem_clean();
245 /* TODO figure out DMI variables on obsd */
246 /*static gchar *guess_icon_name() {
248 gchar *filebuf = NULL;
251 #if defined(__i386__) || defined(__x86_64__)
253 Taken with a few minor changes from systemd's hostnamed.c,
254 copyright 2011 Lennart Poettering.
256 See the SMBIOS Specification 2.7.1 section 7.4.1 for
257 details about the values listed here:
259 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
262 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
263 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
269 ret = g_strdup ("computer-desktop");
274 ret = g_strdup ("computer-laptop");
280 ret = g_strdup ("computer-server");
285 ret = g_strdup ("computer");