ea207ed3 |
1 | #include <gio/gio.h> |
a622ef09 |
2 | #include "hostnamed.h" |
ea207ed3 |
3 | |
a622ef09 |
4 | GDBusNodeInfo *spect_data; |
5 | GMainLoop *loop; |
0df0018d |
6 | |
780e1f19 |
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 | |
a622ef09 |
18 | GString *xml_ret; |
19 | GVariant *xml_ret_gvar; |
780e1f19 |
20 | |
a622ef09 |
21 | g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret); |
22 | xml_ret_gvar = g_variant_new_string(xml_ret->str); |
23 | g_dbus_method_invocation_return_value(invc, xml_ret_gvar); |
780e1f19 |
24 | |
a622ef09 |
25 | } |
780e1f19 |
26 | |
27 | static GVariant * handle_get_property(GDBusConnection *conn, |
28 | const gchar *sender, |
29 | const gchar *obj_path, |
30 | const gchar *interf_name, |
31 | const gchar *prop_name, |
32 | GError **err, |
33 | gpointer usr_data) { |
34 | |
35 | GVariant *ret; |
780e1f19 |
36 | |
37 | return ret; |
38 | } |
39 | |
40 | static gboolean handle_set_property(GDBusConnection *conn, |
41 | const gchar *sender, |
42 | const gchar *obj_path, |
43 | const gchar *interf_name, |
44 | const gchar *prop_name, |
45 | GVariant *val, |
46 | GError **err, |
47 | gpointer usr_data) { |
48 | g_dbus_connection_emit_signal(conn, |
49 | NULL, |
50 | obj_path, |
51 | "org.freedesktop.DBus.Properties", |
52 | "PropertiesChanged", |
53 | NULL, /* incorrect */ |
54 | NULL); |
55 | |
56 | return TRUE; |
57 | } |
387173cb |
58 | |
a622ef09 |
59 | /* "hot" functions initially passed to gdbus */ |
60 | static const GDBusInterfaceVTable interface_vtable = |
61 | { |
62 | handle_method_call, |
63 | handle_get_property, |
64 | handle_set_property |
65 | }; |
66 | |
0df0018d |
67 | /* end method/property functions, begin bus name handlers |
68 | * TODO: these should be intertwined as to handle edge cases |
69 | * for when the system cannot immediately grab the name, as |
70 | * well as cases where the system unintendedly loses the name |
71 | */ |
ea207ed3 |
72 | static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
73 | g_print("got bus, name: %s\n", name); |
387173cb |
74 | |
387173cb |
75 | guint reg_id; |
ea207ed3 |
76 | |
a622ef09 |
77 | reg_id = g_dbus_connection_register_object(conn, |
78 | "/org/freedesktop/hostname1", |
79 | spect_data->interfaces[0], |
80 | &interface_vtable, |
81 | NULL, |
82 | NULL, |
83 | NULL ); |
387173cb |
84 | g_assert(reg_id > 0); |
ea207ed3 |
85 | } |
86 | |
87 | static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
88 | g_print("got name %s\n", name); |
ea207ed3 |
89 | } |
90 | |
91 | static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
780e1f19 |
92 | g_print("lost name %s, exiting...\n", name); |
a622ef09 |
93 | g_main_loop_quit(loop); |
ea207ed3 |
94 | } |
95 | |
0df0018d |
96 | /* safe call to try and start hostnamed */ |
97 | GError hostnamed_init() { |
387173cb |
98 | |
ea207ed3 |
99 | guint bus_descriptor; |
a622ef09 |
100 | GError *err = NULL; |
780e1f19 |
101 | |
a622ef09 |
102 | spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err); |
780e1f19 |
103 | |
104 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SESSION, |
ea207ed3 |
105 | (gchar *)"org.freedesktop.hostname1", |
106 | G_BUS_NAME_OWNER_FLAGS_NONE, |
107 | on_bus_acquired, |
108 | on_name_acquired, |
109 | on_name_lost, |
110 | NULL, |
111 | NULL); |
496f5d66 |
112 | |
780e1f19 |
113 | loop = g_main_loop_new(NULL, FALSE); |
114 | g_main_loop_run(loop); |
115 | |
0df0018d |
116 | /* i am not sure what the system state is once g_main_loop exits */ |
780e1f19 |
117 | |
118 | g_bus_unown_name(bus_descriptor); |
119 | g_dbus_node_info_unref(spect_data); |
120 | } |
387173cb |
121 | |