6 GDBusNodeInfo
*spect_data
;
8 static gchar
*hostname
;
9 static gchar
*pretty_hostname
;
10 static gchar
*static_hostname
;
11 static gchar
*icon_name
;
12 static gchar
*chassis
;
13 static gchar
*kernel_name
;
14 static gchar
*kernel_release
;
15 static gchar
*kernel_version
;
16 static gchar
*os_prettyname
;
17 static gchar
*os_cpe
; //common platform enumeration string
19 static void handle_method_call(GDBusConnection
*conn
,
21 const gchar
*obj_path
,
22 const gchar
*interf_name
,
23 const gchar
*method_name
,
25 GDBusMethodInvocation
*invc
,
28 if(g_strcmp0(interf_name
, "org.freedesktop.DBus.Introspectable") == 0
29 && g_strcmp0(method_name
, "Introspect") == 0) {
31 GVariant
*xml_ret_gvar
;
34 g_dbus_interface_info_generate_xml(spect_data
->interfaces
[0], (guint
)0, xml_ret
);
35 xml_ret_gvar
= g_variant_new_string(xml_ret
->str
);
36 g_dbus_method_invocation_return_value(invc
, xml_ret_gvar
);
41 static GVariant
* handle_get_property(GDBusConnection
*conn
,
43 const gchar
*obj_path
,
44 const gchar
*interf_name
,
45 const gchar
*prop_name
,
49 const gchar
*our_interf_name
= "org.freedesktop.hostname1";
50 const gchar
*our_obj_path
= "/org/freedesktop/hostname1";
52 if(g_strcmp0(interf_name
, our_interf_name
) != 0
53 || g_strcmp0(obj_path
, our_obj_path
) != 0) {
55 return NULL
; //TODO error
58 if(g_strcmp0(prop_name
, "Hostname") == 0)
59 return g_variant_new_string(hostname
);
61 else if(g_strcmp0(prop_name
, "StaticHostname") == 0)
62 return g_variant_new_string(static_hostname
);
64 else if(g_strcmp0(prop_name
, "PrettyHostname") == 0)
65 return g_variant_new_string(pretty_hostname
);
67 else if(g_strcmp0(prop_name
, "IconName") == 0)
68 return g_variant_new_string(icon_name
);
71 return NULL
; //TODO error
75 static gboolean
handle_set_property(GDBusConnection
*conn
,
77 const gchar
*obj_path
,
78 const gchar
*interf_name
,
79 const gchar
*prop_name
,
84 g_dbus_connection_emit_signal(conn
,
87 "org.freedesktop.DBus.Properties",
95 /* "hot" functions initially passed to gdbus */
96 static const GDBusInterfaceVTable interface_vtable
=
103 /* end method/property functions, begin bus name handlers
104 * TODO: these should be intertwined as to handle edge cases
105 * for when the system cannot immediately grab the name, as
106 * well as cases where the system unintendedly loses the name
108 static void on_bus_acquired(GDBusConnection
*conn
,
110 gpointer user_data
) {
114 g_print("got bus, name: %s\n", name
);
116 //GDBusObjectSkeleton *hostnamed_dbobj = g_dbus_object_skeleton_new("/org/freedesktop/hostname1");
118 g_dbus_connection_register_object(conn
,
119 "/org/freedesktop/hostname1",
120 spect_data
->interfaces
[0],
125 static void on_name_acquired(GDBusConnection
*conn
,
127 gpointer user_data
) {
129 g_print("got name %s\n", name
);
132 static void on_name_lost(GDBusConnection
*conn
,
134 gpointer user_data
) {
136 g_print("lost name %s, exiting...", name
);
137 //TODO exit through g_main_loop properly...
141 /* safe call to try and start hostnamed */
142 GError
* hostnamed_init() {
144 guint bus_descriptor
;
146 gchar
**hostnamed_ispect_xml
= g_malloc(3000);
147 gchar
*hostnamed_joined_xml
= g_malloc(3000);
149 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml
, NULL
, err
);
150 hostnamed_joined_xml
= g_strjoinv("\n", hostnamed_ispect_xml
);
151 spect_data
= g_dbus_node_info_new_for_xml(hostnamed_joined_xml
, NULL
);
154 return err
; //TODO error
156 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
157 "org.freedesktop.hostname1",
158 G_BUS_NAME_OWNER_FLAGS_NONE
,
165 //TODO: malloc and return reference as if a main() closed
169 gboolean
init_props() {
172 && init_static_hostname()
173 && init_pretty_hostname()
176 && init_kernel_name()
177 && init_kernel_version()
185 //POSIX, for future ports try_hostname should be checked for null-termination
186 gboolean
init_hostname() {
188 gchar try_hostname
[HOST_NAME_MAX
];
190 if(!gethostname(try_hostname
, HOST_NAME_MAX
)) {
191 hostname
= try_hostname
;
198 gboolean
init_static_hostname() {
203 gboolean
init_pretty_hostname() {
208 gboolean
init_icon_name() {
213 gboolean
init_chassis() {
218 gboolean
init_kernel_name() {
223 gboolean
init_kernel_version() {
228 gboolean
init_os_name() {
233 gboolean
init_os_cpe() {
238 //TODO figure out DMI variables on obsd
239 /*static gchar *guess_icon_name() {
241 gchar *filebuf = NULL;
246 #if defined(__i386__) || defined(__x86_64__)
248 Taken with a few minor changes from systemd's hostnamed.c,
249 copyright 2011 Lennart Poettering.
251 See the SMBIOS Specification 2.7.1 section 7.4.1 for
252 details about the values listed here:
254 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
257 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
258 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
264 ret = g_strdup ("computer-desktop");
269 ret = g_strdup ("computer-laptop");
275 ret = g_strdup ("computer-server");
280 ret = g_strdup ("computer");