231bc4e2cd854def7e2f29c652d4ecfc622cbc37
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 #include <gio/gio.h>
2
3 GDBusNodeInfo *spect_data;
4
5 static void handle_method_call(GDBusConnection *conn,
6 const gchar *sender,
7 const gchar *obj_path,
8 const gchar *interf_name,
9 const gchar *method_name,
10 GVariant *params,
11 GDBusMethodInvocation *invc,
12 gpointer usrdat) {
13
14 GVariant *xml_ret_gvar;
15 GString *xml_ret;
16
17 g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret);
18 xml_ret_gvar = g_variant_new_string(xml_ret->str);
19 g_dbus_method_invocation_return_value(invc, xml_ret_gvar);
20
21 }
22
23 static GVariant * handle_get_property(GDBusConnection *conn,
24 const gchar *sender,
25 const gchar *obj_path,
26 const gchar *interf_name,
27 const gchar *prop_name,
28 GError **err,
29 gpointer usr_data) {
30
31 GVariant *ret;
32
33 return ret;
34 }
35
36 static gboolean handle_set_property(GDBusConnection *conn,
37 const gchar *sender,
38 const gchar *obj_path,
39 const gchar *interf_name,
40 const gchar *prop_name,
41 GVariant *val,
42 GError **err,
43 gpointer usr_data) {
44
45 g_dbus_connection_emit_signal(conn,
46 NULL,
47 obj_path,
48 "org.freedesktop.DBus.Properties",
49 "PropertiesChanged",
50 NULL, /* incorrect */
51 NULL);
52
53 return TRUE;
54 }
55
56 /* "hot" functions initially passed to gdbus */
57 static const GDBusInterfaceVTable interface_vtable =
58 {
59 handle_method_call,
60 handle_get_property,
61 handle_set_property
62 };
63
64 /* end method/property functions, begin bus name handlers
65 * TODO: these should be intertwined as to handle edge cases
66 * for when the system cannot immediately grab the name, as
67 * well as cases where the system unintendedly loses the name
68 */
69 static void on_bus_acquired(GDBusConnection *conn,
70 const gchar *name,
71 gpointer user_data) {
72
73 GError *err;
74
75 g_print("got bus, name: %s\n", name);
76
77 //GDBusObjectSkeleton *hostnamed_dbobj = g_dbus_object_skeleton_new("/org/freedesktop/hostname1");
78
79 g_dbus_connection_register_object(conn,
80 "/org/freedesktop/hostname1",
81 spect_data->interfaces[0],
82 &interface_vtable,
83 NULL, NULL, NULL);
84 }
85
86 static void on_name_acquired(GDBusConnection *conn,
87 const gchar *name,
88 gpointer user_data) {
89
90 g_print("got name %s\n", name);
91 }
92
93 static void on_name_lost(GDBusConnection *conn,
94 const gchar *name,
95 gpointer user_data) {
96
97 g_print("lost name %s, exiting...", name);
98 //TODO exit through g_main_loop properly...
99 exit(0);
100 }
101
102 /* safe call to try and start hostnamed */
103 GError * hostnamed_init() {
104
105 guint bus_descriptor;
106 GError *err = NULL;
107 gchar **hostnamed_ispect_xml = g_malloc(3000);
108 gchar *hostnamed_joined_xml = g_malloc(3000);
109
110 g_file_get_contents("conf/hostnamed-ispect.xml", hostnamed_ispect_xml, NULL, err);
111 hostnamed_joined_xml = g_strjoinv("\n", hostnamed_ispect_xml);
112 spect_data = g_dbus_node_info_new_for_xml(hostnamed_joined_xml, NULL);
113
114 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
115 "org.freedesktop.hostname1",
116 G_BUS_NAME_OWNER_FLAGS_NONE,
117 on_bus_acquired,
118 on_name_acquired,
119 on_name_lost,
120 NULL,
121 NULL);
122
123 //TODO: malloc and return reference as if a main() closed
124 return err;
125 }