Add basic mainloop and D-Bus setup to supplicant test tool
authorMarcel Holtmann <marcel@holtmann.org>
Wed, 23 Dec 2009 11:39:00 +0000 (03:39 -0800)
committerMarcel Holtmann <marcel@holtmann.org>
Wed, 23 Dec 2009 11:39:00 +0000 (03:39 -0800)
Makefile.am
tools/supplicant-test.c

index df54367..1242705 100644 (file)
@@ -117,6 +117,8 @@ noinst_PROGRAMS += tools/wifi-scan tools/supplicant-test \
 
 tools_wifi_scan_LDADD = @GLIB_LIBS@ @NETLINK_LIBS@
 
+tools_supplicant_test_SOURCES = $(gdbus_sources) \
+                               tools/supplicant-test.c
 tools_supplicant_test_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 
 tools_polkit_test_LDADD = @DBUS_LIBS@
index 6f110c1..24fd5d8 100644 (file)
 #include <config.h>
 #endif
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <syslog.h>
+
+#include <gdbus.h>
+
+#define DBG(fmt, arg...) do { \
+       syslog(LOG_DEBUG, "%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg); \
+} while (0)
+
+static GMainLoop *main_loop = NULL;
+
+static void sig_term(int sig)
+{
+       syslog(LOG_INFO, "Terminating");
+
+       g_main_loop_quit(main_loop);
+}
+
+static void disconnect_callback(DBusConnection *conn, void *user_data)
+{
+       syslog(LOG_ERR, "D-Bus disconnect");
+
+       g_main_loop_quit(main_loop);
+}
+
 int main(int argc, char *argv[])
 {
+       DBusConnection *conn;
+       DBusError err;
+       struct sigaction sa;
+
+       main_loop = g_main_loop_new(NULL, FALSE);
+
+       dbus_error_init(&err);
+
+       conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
+       if (conn == NULL) {
+               if (dbus_error_is_set(&err) == TRUE) {
+                       fprintf(stderr, "%s\n", err.message);
+                       dbus_error_free(&err);
+               } else
+                       fprintf(stderr, "Can't register with system bus\n");
+               exit(1);
+       }
+
+       openlog("supplicant", LOG_NDELAY | LOG_PERROR, LOG_USER);
+
+       g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = sig_term;
+       sigaction(SIGINT, &sa, NULL);
+       sigaction(SIGTERM, &sa, NULL);
+
+       syslog(LOG_INFO, "Startup");
+
+       g_main_loop_run(main_loop);
+
+       syslog(LOG_INFO, "Exit");
+
+       dbus_connection_unref(conn);
+
+       g_main_loop_unref(main_loop);
+
+       closelog();
+
        return 0;
 }