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