ea207ed3 |
1 | #include <gio/gio.h> |
a622ef09 |
2 | #include "hostnamed.h" |
ea207ed3 |
3 | |
a622ef09 |
4 | GMainLoop *loop; |
ff1030d4 |
5 | GDBusNodeInfo *spect_data; |
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 | |
ff1030d4 |
16 | //g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name); |
780e1f19 |
17 | |
ff1030d4 |
18 | if(g_strcmp0(method_name, "Introspect" |
780e1f19 |
19 | |
ff1030d4 |
20 | GVariant *xml_ret_gvar; |
21 | GString *xml_ret; |
22 | |
80043b36 |
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); |
780e1f19 |
26 | |
a622ef09 |
27 | } |
780e1f19 |
28 | |
29 | static GVariant * handle_get_property(GDBusConnection *conn, |
80043b36 |
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) { |
780e1f19 |
36 | |
80043b36 |
37 | GVariant *ret; |
780e1f19 |
38 | |
80043b36 |
39 | return ret; |
780e1f19 |
40 | } |
41 | |
42 | static gboolean handle_set_property(GDBusConnection *conn, |
80043b36 |
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; |
780e1f19 |
60 | } |
387173cb |
61 | |
a622ef09 |
62 | /* "hot" functions initially passed to gdbus */ |
63 | static const GDBusInterfaceVTable interface_vtable = |
64 | { |
80043b36 |
65 | handle_method_call, |
66 | handle_get_property, |
67 | handle_set_property |
a622ef09 |
68 | }; |
69 | |
0df0018d |
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 | */ |
80043b36 |
75 | static void on_bus_acquired(GDBusConnection *conn, |
76 | const gchar *name, |
77 | gpointer user_data) { |
ea207ed3 |
78 | g_print("got bus, name: %s\n", name); |
387173cb |
79 | |
387173cb |
80 | guint reg_id; |
ea207ed3 |
81 | |
a622ef09 |
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, |
80043b36 |
88 | NULL); |
ea207ed3 |
89 | } |
90 | |
80043b36 |
91 | static void on_name_acquired(GDBusConnection *conn, |
92 | const gchar *name, |
93 | gpointer user_data) { |
94 | |
ea207ed3 |
95 | g_print("got name %s\n", name); |
ea207ed3 |
96 | } |
97 | |
80043b36 |
98 | static void on_name_lost(GDBusConnection *conn, |
99 | const gchar *name, |
100 | gpointer user_data) { |
101 | |
780e1f19 |
102 | g_print("lost name %s, exiting...\n", name); |
ff1030d4 |
103 | //g_print("you might need to run hacks/punch_config.sh\n"); |
a622ef09 |
104 | g_main_loop_quit(loop); |
ea207ed3 |
105 | } |
106 | |
0df0018d |
107 | /* safe call to try and start hostnamed */ |
6de394d4 |
108 | GError * hostnamed_init() { |
387173cb |
109 | |
ea207ed3 |
110 | guint bus_descriptor; |
a622ef09 |
111 | GError *err = NULL; |
780e1f19 |
112 | |
a622ef09 |
113 | spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err); |
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 | } |