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>
22 #include <glib/gprintf.h>
24 #include "hostnamed-gen.h"
26 GPtrArray
*hostnamed_freeable
;
27 Hostname1
*hostnamed_interf
;
29 /* --- begin method/property/signal code --- */
32 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
33 GDBusMethodInvocation
*invoc
,
40 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
41 GDBusMethodInvocation
*invoc
,
48 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
49 GDBusMethodInvocation
*invoc
,
56 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
57 GDBusMethodInvocation
*invoc
,
64 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
65 GDBusMethodInvocation
*invoc
,
71 /* note: all hostnamed/hostname1's properties are read-only,
72 * and do not need set_ functions, gdbus-codegen realized
73 * this from the XML and handled the to-be error of trying
74 * to set a read-only property's value
82 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
83 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
85 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
92 our_get_static_hostname() {
98 our_get_pretty_hostname() {
110 our_get_icon_name() {
116 our_get_kernel_name() {
122 our_get_kernel_version() {
128 our_get_kernel_release() {
134 our_get_os_cpename() {
140 our_get_os_pretty_name() {
145 /* --- end method/property/signal code, begin bus/name handlers --- */
147 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
149 gpointer user_data
) {
151 g_print("got bus, name: %s\n", name
);
155 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
157 gpointer user_data
) {
159 g_print("got '%s' on system bus\n", name
);
161 hostnamed_interf
= hostname1_skeleton_new();
163 /* attach function pointers to generated struct's method handlers */
164 g_signal_connect(hostnamed_interf
, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname
), NULL
);
165 g_signal_connect(hostnamed_interf
, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname
), NULL
);
166 g_signal_connect(hostnamed_interf
, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname
), NULL
);
167 g_signal_connect(hostnamed_interf
, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis
), NULL
);
168 g_signal_connect(hostnamed_interf
, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name
), NULL
);
170 /* set our properties before export */
171 hostname1_set_hostname(hostnamed_interf
, our_get_hostname());
172 hostname1_set_static_hostname(hostnamed_interf
, our_get_static_hostname());
173 hostname1_set_pretty_hostname(hostnamed_interf
, our_get_pretty_hostname());
174 hostname1_set_chassis(hostnamed_interf
, our_get_chassis());
175 hostname1_set_icon_name(hostnamed_interf
, our_get_icon_name());
176 hostname1_set_kernel_name(hostnamed_interf
, our_get_kernel_name());
177 hostname1_set_kernel_version(hostnamed_interf
, our_get_kernel_version());
178 hostname1_set_kernel_release(hostnamed_interf
, our_get_kernel_release());
179 hostname1_set_operating_system_cpename(hostnamed_interf
, our_get_os_cpename());
180 hostname1_set_operating_system_pretty_name(hostnamed_interf
, our_get_os_pretty_name());
182 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
184 "/org/freedesktop/hostname1",
187 g_printf("Failed to export Hostname1's interface!");
192 /* --- end bus/name handlers, begin misc functions --- */
195 void hostnamed_mem_clean() {
197 ddg_ptr_array_foreach(hostnamed_freeable
, (GFunc
) g_free
, NULL
);
198 g_ptr_array_free(hostnamed_freeable
);
201 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
203 gpointer user_data
) {
205 g_print("lost name %s, exiting...", name
);
207 hostnamed_mem_clean();
208 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
212 /* safe call to try and start hostnamed */
213 void hostnamed_init() {
215 guint bus_descriptor
;
217 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
218 "org.freedesktop.hostname1",
219 G_BUS_NAME_OWNER_FLAGS_NONE
,
220 hostnamed_on_bus_acquired
,
221 hostnamed_on_name_acquired
,
222 hostnamed_on_name_lost
,
230 GMainLoop
*hostnamed_loop
;
231 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
233 /* config stuff here */
236 g_main_loop_run(hostnamed_loop
);
237 g_main_loop_unref(hostnamed_loop
);
239 hostnamed_mem_clean();
244 /* TODO figure out DMI variables on obsd */
245 /*static gchar *guess_icon_name() {
247 gchar *filebuf = NULL;
250 #if defined(__i386__) || defined(__x86_64__)
252 Taken with a few minor changes from systemd's hostnamed.c,
253 copyright 2011 Lennart Poettering.
255 See the SMBIOS Specification 2.7.1 section 7.4.1 for
256 details about the values listed here:
258 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
261 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
262 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
268 ret = g_strdup ("computer-desktop");
273 ret = g_strdup ("computer-laptop");
279 ret = g_strdup ("computer-server");
284 ret = g_strdup ("computer");