ea207ed3 |
1 | #include <gio/gio.h> |
2 | |
a622ef09 |
3 | GMainLoop *loop; |
ff1030d4 |
4 | GDBusNodeInfo *spect_data; |
0df0018d |
5 | |
780e1f19 |
6 | static void handle_method_call(GDBusConnection *conn, |
80043b36 |
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) { |
780e1f19 |
14 | |
ff1030d4 |
15 | //g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name); |
780e1f19 |
16 | |
ff1030d4 |
17 | if(g_strcmp0(method_name, "Introspect" |
780e1f19 |
18 | |
ff1030d4 |
19 | GVariant *xml_ret_gvar; |
20 | GString *xml_ret; |
21 | |
80043b36 |
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); |
780e1f19 |
25 | |
a622ef09 |
26 | } |
780e1f19 |
27 | |
28 | static GVariant * handle_get_property(GDBusConnection *conn, |
80043b36 |
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) { |
780e1f19 |
35 | |
80043b36 |
36 | GVariant *ret; |
780e1f19 |
37 | |
80043b36 |
38 | return ret; |
780e1f19 |
39 | } |
40 | |
41 | static gboolean handle_set_property(GDBusConnection *conn, |
80043b36 |
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; |
780e1f19 |
59 | } |
387173cb |
60 | |
a622ef09 |
61 | /* "hot" functions initially passed to gdbus */ |
62 | static const GDBusInterfaceVTable interface_vtable = |
63 | { |
80043b36 |
64 | handle_method_call, |
65 | handle_get_property, |
66 | handle_set_property |
a622ef09 |
67 | }; |
68 | |
0df0018d |
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 | */ |
80043b36 |
74 | static void on_bus_acquired(GDBusConnection *conn, |
75 | const gchar *name, |
76 | gpointer user_data) { |
ea207ed3 |
77 | g_print("got bus, name: %s\n", name); |
387173cb |
78 | |
387173cb |
79 | guint reg_id; |
ea207ed3 |
80 | |
a622ef09 |
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, |
80043b36 |
87 | NULL); |
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); |
ff1030d4 |
102 | //g_print("you might need to run hacks/punch_config.sh\n"); |
a622ef09 |
103 | g_main_loop_quit(loop); |
ea207ed3 |
104 | } |
105 | |
0df0018d |
106 | /* safe call to try and start hostnamed */ |
6de394d4 |
107 | GError * hostnamed_init() { |
387173cb |
108 | |
ea207ed3 |
109 | guint bus_descriptor; |
a622ef09 |
110 | GError *err = NULL; |
780e1f19 |
111 | |
a622ef09 |
112 | spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err); |
15c779be |
113 | |
6de394d4 |
114 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
ea207ed3 |
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); |
496f5d66 |
122 | |
780e1f19 |
123 | loop = g_main_loop_new(NULL, FALSE); |
124 | g_main_loop_run(loop); |
6de394d4 |
125 | |
126 | //TODO: malloc and return reference as if a main() closed |
127 | return err; |
780e1f19 |
128 | } |