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, |
80043b36 |
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) { |
780e1f19 |
15 | |
80043b36 |
16 | g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name); |
780e1f19 |
17 | |
80043b36 |
18 | GString *xml_ret; |
19 | GVariant *xml_ret_gvar; |
780e1f19 |
20 | |
80043b36 |
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, |
80043b36 |
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) { |
780e1f19 |
34 | |
80043b36 |
35 | GVariant *ret; |
780e1f19 |
36 | |
80043b36 |
37 | return ret; |
780e1f19 |
38 | } |
39 | |
40 | static gboolean handle_set_property(GDBusConnection *conn, |
80043b36 |
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 | |
49 | g_dbus_connection_emit_signal(conn, |
50 | NULL, |
51 | obj_path, |
52 | "org.freedesktop.DBus.Properties", |
53 | "PropertiesChanged", |
54 | NULL, /* incorrect */ |
55 | NULL); |
56 | |
57 | return TRUE; |
780e1f19 |
58 | } |
387173cb |
59 | |
a622ef09 |
60 | /* "hot" functions initially passed to gdbus */ |
61 | static const GDBusInterfaceVTable interface_vtable = |
62 | { |
80043b36 |
63 | handle_method_call, |
64 | handle_get_property, |
65 | handle_set_property |
a622ef09 |
66 | }; |
67 | |
0df0018d |
68 | /* end method/property functions, begin bus name handlers |
69 | * TODO: these should be intertwined as to handle edge cases |
70 | * for when the system cannot immediately grab the name, as |
71 | * well as cases where the system unintendedly loses the name |
72 | */ |
80043b36 |
73 | static void on_bus_acquired(GDBusConnection *conn, |
74 | const gchar *name, |
75 | gpointer user_data) { |
ea207ed3 |
76 | g_print("got bus, name: %s\n", name); |
387173cb |
77 | |
387173cb |
78 | guint reg_id; |
ea207ed3 |
79 | |
a622ef09 |
80 | reg_id = g_dbus_connection_register_object(conn, |
81 | "/org/freedesktop/hostname1", |
82 | spect_data->interfaces[0], |
83 | &interface_vtable, |
84 | NULL, |
85 | NULL, |
80043b36 |
86 | NULL); |
387173cb |
87 | g_assert(reg_id > 0); |
ea207ed3 |
88 | } |
89 | |
80043b36 |
90 | static void on_name_acquired(GDBusConnection *conn, |
91 | const gchar *name, |
92 | gpointer user_data) { |
93 | |
ea207ed3 |
94 | g_print("got name %s\n", name); |
ea207ed3 |
95 | } |
96 | |
80043b36 |
97 | static void on_name_lost(GDBusConnection *conn, |
98 | const gchar *name, |
99 | gpointer user_data) { |
100 | |
780e1f19 |
101 | g_print("lost name %s, exiting...\n", name); |
a622ef09 |
102 | g_main_loop_quit(loop); |
ea207ed3 |
103 | } |
104 | |
0df0018d |
105 | /* safe call to try and start hostnamed */ |
106 | GError hostnamed_init() { |
387173cb |
107 | |
ea207ed3 |
108 | guint bus_descriptor; |
a622ef09 |
109 | GError *err = NULL; |
780e1f19 |
110 | |
a622ef09 |
111 | spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err); |
780e1f19 |
112 | |
113 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SESSION, |
ea207ed3 |
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); |
496f5d66 |
121 | |
780e1f19 |
122 | loop = g_main_loop_new(NULL, FALSE); |
123 | g_main_loop_run(loop); |
780e1f19 |
124 | } |