Add signal handling to supplicant test program
[framework/connectivity/connman.git] / tools / supplicant-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <syslog.h>
31
32 #include <gdbus.h>
33
34 #include "supplicant.h"
35
36 #define DBG(fmt, arg...) do { \
37         syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
38 } while (0)
39
40 static void interface_added(const struct supplicant_interface *interface)
41 {
42         DBG("interface %p", interface);
43 }
44
45 static void interface_removed(const struct supplicant_interface *interface)
46 {
47         DBG("interface %p", interface);
48 }
49
50 static const struct supplicant_callbacks callbacks = {
51         .interface_added        = interface_added,
52         .interface_removed      = interface_removed,
53 };
54
55 static GMainLoop *main_loop = NULL;
56
57 static void sig_term(int sig)
58 {
59         syslog(LOG_INFO, "Terminating");
60
61         g_main_loop_quit(main_loop);
62 }
63
64 static void disconnect_callback(DBusConnection *conn, void *user_data)
65 {
66         syslog(LOG_ERR, "D-Bus disconnect");
67
68         g_main_loop_quit(main_loop);
69 }
70
71 int main(int argc, char *argv[])
72 {
73         DBusConnection *conn;
74         DBusError err;
75         struct sigaction sa;
76
77         main_loop = g_main_loop_new(NULL, FALSE);
78
79         dbus_error_init(&err);
80
81         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
82         if (conn == NULL) {
83                 if (dbus_error_is_set(&err) == TRUE) {
84                         fprintf(stderr, "%s\n", err.message);
85                         dbus_error_free(&err);
86                 } else
87                         fprintf(stderr, "Can't register with system bus\n");
88                 exit(1);
89         }
90
91         openlog("supplicant", LOG_NDELAY | LOG_PERROR, LOG_USER);
92
93         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
94
95         memset(&sa, 0, sizeof(sa));
96         sa.sa_handler = sig_term;
97         sigaction(SIGINT, &sa, NULL);
98         sigaction(SIGTERM, &sa, NULL);
99
100         syslog(LOG_INFO, "Startup");
101
102         if (supplicant_register(&callbacks) < 0) {
103                 syslog(LOG_ERR, "Failed to init supplicant");
104                 goto done;
105         }
106
107         g_main_loop_run(main_loop);
108
109         supplicant_unregister(&callbacks);
110
111 done:
112         syslog(LOG_INFO, "Exit");
113
114         dbus_connection_unref(conn);
115
116         g_main_loop_unref(main_loop);
117
118         closelog();
119
120         return 0;
121 }