X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?p=systembsd.git;a=blobdiff_plain;f=src%2Finterfaces%2Ftimedated%2Ftimedated.c;h=3df5fc6e3e1fd956ef15894741f2896eb154680b;hp=0c9d4302d78680bc598669647f379f7824ba724d;hb=509599f043fd77c5eee595ebce117ff9e11a5324;hpb=1e8c7c889a1c3b92c58fb1590b2f1192dd86623b diff --git a/src/interfaces/timedated/timedated.c b/src/interfaces/timedated/timedated.c index 0c9d430..3df5fc6 100644 --- a/src/interfaces/timedated/timedated.c +++ b/src/interfaces/timedated/timedated.c @@ -14,8 +14,139 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include +#include + +#include +#include + +#include +#include + #include "timedated-gen.h" +#include "timedated.h" + +GPtrArray *timedated_freeable; +Timedate1 *timedated_interf; + +GMainLoop *timedated_loop; + +guint bus_descriptor; +gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ + +/* --- begin method/property/dbus signal code --- */ + +/*static gboolean +on_handle_set_hostname(Timedate1 *hn1_passed_interf, + GDBusMethodInvocation *invoc, + const gchar *greet, + gpointer data) { + return FALSE; +}*/ + +/*const gchar * +our_get_hostname() { + + gchar *hostname_buf, *ret; + size_t hostname_divider; + + hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); + ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); + g_ptr_array_add(timedated_freeable, hostname_buf); + g_ptr_array_add(timedated_freeable, ret); + + if(gethostname(hostname_buf, MAXHOSTNAMELEN)) + return ""; + + hostname_divider = strcspn(hostname_buf, "."); + + return strncpy(ret, hostname_buf, hostname_divider); +}*/ + +/* --- end method/property/dbus signal code, begin bus/name handlers --- */ + +static void timedated_on_bus_acquired(GDBusConnection *conn, + const gchar *name, + gpointer user_data) { + + g_print("got bus/name, exporting %s's interface...\n", name); + + timedated_interf = timedate1_skeleton_new(); + + /* attach function pointers to generated struct's method handlers + g_signal_connect(timedated_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);*/ + + /* set our properties before export + timedate1_set_hostname(timedated_interf, our_get_hostname()); */ + + if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), + conn, + "/org/freedesktop/timedate1", + NULL)) { + + g_printf("failed to export %s's interface!\n", name); + timedated_mem_clean(); + + } else { + + dbus_interface_exported = TRUE; + g_printf("exported %s's interface on the system bus...\n", name); + } +} + +static void timedated_on_name_acquired(GDBusConnection *conn, + const gchar *name, + gpointer user_data) { + + g_printf("success!\n"); +} + +static void timedated_on_name_lost(GDBusConnection *conn, + const gchar *name, + gpointer user_data) { + + if(!conn) { + + g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name); + timedated_mem_clean(); + } + + g_print("lost name %s, exiting...\n", name); + + timedated_mem_clean(); +} + +/* --- end bus/name handlers, begin misc unix functions --- */ + +/* safe call to clean and then exit + * this stops our GMainLoop safely before letting main() return */ +void timedated_mem_clean() { + + g_ptr_array_foreach(timedated_freeable, (GFunc) g_free, NULL); + g_ptr_array_free(timedated_freeable, TRUE); +} int main() { + + timedated_loop = g_main_loop_new(NULL, TRUE); + timedated_freeable = g_ptr_array_new(); + + bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, + "org.freedesktop.timedate1", + G_BUS_NAME_OWNER_FLAGS_NONE, + timedated_on_bus_acquired, + timedated_on_name_acquired, + timedated_on_name_lost, + NULL, + NULL); + + g_main_loop_run(timedated_loop); + g_main_loop_unref(timedated_loop); + + g_bus_unown_name(bus_descriptor); + + timedated_mem_clean(); + return 0; }