| 1 | #include <gio/gio.h> |
| 2 | #include "hostnamed.h" |
| 3 | |
| 4 | GDBusNodeInfo *spect_data; |
| 5 | GMainLoop *loop; |
| 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 | GString *xml_ret; |
| 19 | GVariant *xml_ret_gvar; |
| 20 | |
| 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); |
| 24 | |
| 25 | } |
| 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; |
| 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 | |
| 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; |
| 58 | } |
| 59 | |
| 60 | /* "hot" functions initially passed to gdbus */ |
| 61 | static const GDBusInterfaceVTable interface_vtable = |
| 62 | { |
| 63 | handle_method_call, |
| 64 | handle_get_property, |
| 65 | handle_set_property |
| 66 | }; |
| 67 | |
| 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 | */ |
| 73 | static void on_bus_acquired(GDBusConnection *conn, |
| 74 | const gchar *name, |
| 75 | gpointer user_data) { |
| 76 | g_print("got bus, name: %s\n", name); |
| 77 | |
| 78 | guint reg_id; |
| 79 | |
| 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, |
| 86 | NULL); |
| 87 | } |
| 88 | |
| 89 | static void on_name_acquired(GDBusConnection *conn, |
| 90 | const gchar *name, |
| 91 | gpointer user_data) { |
| 92 | |
| 93 | g_print("got name %s\n", name); |
| 94 | } |
| 95 | |
| 96 | static void on_name_lost(GDBusConnection *conn, |
| 97 | const gchar *name, |
| 98 | gpointer user_data) { |
| 99 | |
| 100 | g_print("lost name %s, exiting...\n", name); |
| 101 | g_print("you might need to run hacks/punch_config.sh\n"); |
| 102 | g_main_loop_quit(loop); |
| 103 | } |
| 104 | |
| 105 | /* safe call to try and start hostnamed */ |
| 106 | GError * hostnamed_init() { |
| 107 | |
| 108 | guint bus_descriptor; |
| 109 | GError *err = NULL; |
| 110 | |
| 111 | spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err); |
| 112 | |
| 113 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 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); |
| 121 | |
| 122 | loop = g_main_loop_new(NULL, FALSE); |
| 123 | g_main_loop_run(loop); |
| 124 | |
| 125 | //TODO: malloc and return reference as if a main() closed |
| 126 | return err; |
| 127 | } |