5 #include "src/interfaces/hostnamed/hostnamed-gen.c"
7 GDBusNodeInfo
*spect_data
;
10 static gchar *hostname;
11 static gchar *pretty_hostname;
12 static gchar *static_hostname;
13 static gchar *icon_name;
14 static gchar *chassis;
15 static gchar *kernel_name;
16 static gchar *kernel_release;
17 static gchar *kernel_version;
18 static gchar *os_prettyname;
19 static gchar *os_cpe; //common platform enumeration string
23 static void handle_method_call(GDBusConnection *conn,
25 const gchar *obj_path,
26 const gchar *interf_name,
27 const gchar *method_name,
29 GDBusMethodInvocation *invc,
32 if(g_strcmp0(interf_name, "org.freedesktop.DBus.Introspectable") == 0
33 && g_strcmp0(method_name, "Introspect") == 0) {
35 GVariant *xml_ret_gvar;
38 g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret);
39 xml_ret_gvar = g_variant_new_string(xml_ret->str);
40 g_dbus_method_invocation_return_value(invc, xml_ret_gvar);
47 static GVariant * handle_get_property(GDBusConnection *conn,
49 const gchar *obj_path,
50 const gchar *interf_name,
51 const gchar *prop_name,
55 const gchar *our_interf_name = "org.freedesktop.hostname1";
56 const gchar *our_obj_path = "/org/freedesktop/hostname1";
58 if(g_strcmp0(interf_name, our_interf_name) != 0
59 || g_strcmp0(obj_path, our_obj_path) != 0) {
61 return NULL; //TODO error
64 if(g_strcmp0(prop_name, "Hostname") == 0)
65 return g_variant_new_string(hostname);
67 else if(g_strcmp0(prop_name, "StaticHostname") == 0)
68 return g_variant_new_string(static_hostname);
70 else if(g_strcmp0(prop_name, "PrettyHostname") == 0)
71 return g_variant_new_string(pretty_hostname);
73 else if(g_strcmp0(prop_name, "IconName") == 0)
74 return g_variant_new_string(icon_name);
77 return NULL; //TODO error
83 static gboolean handle_set_property(GDBusConnection *conn,
85 const gchar *obj_path,
86 const gchar *interf_name,
87 const gchar *prop_name,
92 g_dbus_connection_emit_signal(conn,
95 "org.freedesktop.DBus.Properties",
104 /* handled by codegen
105 static const GDBusInterfaceVTable interface_vtable =
114 static void on_bus_acquired(GDBusConnection
*conn
,
116 gpointer user_data
) {
120 g_print("got bus, name: %s\n", name
);
122 //GDBusObjectSkeleton *hostnamed_dbobj = g_dbus_object_skeleton_new("/org/freedesktop/hostname1");
124 g_dbus_connection_register_object(conn
,
125 "/org/freedesktop/hostname1",
126 spect_data
->interfaces
[0],
131 static void on_name_acquired(GDBusConnection
*conn
,
133 gpointer user_data
) {
135 g_print("got name %s\n", name
);
138 static void on_name_lost(GDBusConnection
*conn
,
140 gpointer user_data
) {
142 g_print("lost name %s, exiting...", name
);
143 //TODO exit through g_main_loop properly...
147 /* safe call to try and start hostnamed */
148 GError
* hostnamed_init() {
150 guint bus_descriptor
;
152 gchar
**hostnamed_ispect_xml
= g_malloc(3000);
153 gchar
*hostnamed_joined_xml
= g_malloc(3000);
155 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml
, NULL
, err
);
156 hostnamed_joined_xml
= g_strjoinv("\n", hostnamed_ispect_xml
);
157 spect_data
= g_dbus_node_info_new_for_xml(hostnamed_joined_xml
, NULL
);
160 return err
; //TODO error
162 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
163 "org.freedesktop.hostname1",
164 G_BUS_NAME_OWNER_FLAGS_NONE
,
171 //TODO: malloc and return reference as if a main() closed
174 /* handled by codegen
175 gboolean init_props() {
178 && init_static_hostname()
179 && init_pretty_hostname()
182 && init_kernel_name()
183 && init_kernel_version()
191 //POSIX, for future ports try_hostname should be checked for null-termination
192 gboolean init_hostname() {
194 gchar try_hostname[HOST_NAME_MAX];
196 if(!gethostname(try_hostname, HOST_NAME_MAX)) {
197 hostname = try_hostname;
205 /* handled by codegen
206 gboolean init_static_hostname() {
211 gboolean init_pretty_hostname() {
216 gboolean init_icon_name() {
221 gboolean init_chassis() {
226 gboolean init_kernel_name() {
231 gboolean init_kernel_version() {
236 gboolean init_os_name() {
241 gboolean init_os_cpe() {
246 //TODO figure out DMI variables on obsd
247 /*static gchar *guess_icon_name() {
249 gchar *filebuf = NULL;
254 #if defined(__i386__) || defined(__x86_64__)
256 Taken with a few minor changes from systemd's hostnamed.c,
257 copyright 2011 Lennart Poettering.
259 See the SMBIOS Specification 2.7.1 section 7.4.1 for
260 details about the values listed here:
262 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
265 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
266 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
272 ret = g_strdup ("computer-desktop");
277 ret = g_strdup ("computer-laptop");
283 ret = g_strdup ("computer-server");
288 ret = g_strdup ("computer");