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