Add D-Bus disconnect callback
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <sys/stat.h>
33 #include <net/if.h>
34
35 #include <gdbus.h>
36
37 #include "connman.h"
38
39 static GMainLoop *main_loop = NULL;
40
41 static void sig_term(int sig)
42 {
43         g_main_loop_quit(main_loop);
44 }
45
46 static void disconnect_callback(void *user_data)
47 {
48         DBG("D-Bus disconnect");
49 }
50
51 static gchar *option_interface = NULL;
52 static gboolean option_detach = TRUE;
53 static gboolean option_compat = FALSE;
54 static gboolean option_debug = FALSE;
55
56 static GOptionEntry options[] = {
57         { "interface", 'i', 0, G_OPTION_ARG_STRING, &option_interface,
58                                 "Specify network interface", "IFACE" },
59         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
60                                 G_OPTION_ARG_NONE, &option_detach,
61                                 "Don't fork daemon to background" },
62         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
63                                 "Enable Network Manager compatibility" },
64         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
65                                 "Enable debug information output" },
66         { NULL },
67 };
68
69 int main(int argc, char *argv[])
70 {
71         GOptionContext *context;
72         GError *error = NULL;
73         DBusConnection *conn;
74         DBusError err;
75         struct sigaction sa;
76
77         if (g_thread_supported() == FALSE)
78                 g_thread_init(NULL);
79
80         context = g_option_context_new(NULL);
81         g_option_context_add_main_entries(context, options, NULL);
82
83         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
84                 if (error != NULL) {
85                         g_printerr("%s\n", error->message);
86                         g_error_free(error);
87                 } else
88                         g_printerr("An unknown error occurred\n");
89                 exit(1);
90         }
91
92         g_option_context_free(context);
93
94         if (option_detach == TRUE) {
95                 if (daemon(0, 0)) {
96                         perror("Can't start daemon");
97                         exit(1);
98                 }
99         }
100
101         mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
102                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
103
104         mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
105                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
106
107         main_loop = g_main_loop_new(NULL, FALSE);
108
109         if (dbus_threads_init_default() == FALSE) {
110                 fprintf(stderr, "Can't init usage of threads\n");
111                 exit(1);
112         }
113
114         dbus_error_init(&err);
115
116         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
117         if (conn == NULL) {
118                 if (dbus_error_is_set(&err) == TRUE) {
119                         fprintf(stderr, "%s\n", err.message);
120                         dbus_error_free(&err);
121                 } else
122                         fprintf(stderr, "Can't register with system bus\n");
123                 exit(1);
124         }
125
126         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
127
128         if (option_compat == TRUE) {
129                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
130                         fprintf(stderr, "Can't register compat service\n");
131                         option_compat = FALSE;
132                 }
133         }
134
135         __connman_log_init(option_detach, option_debug);
136
137         __connman_storage_init();
138
139         __connman_element_init(conn);
140
141         __connman_agent_init(conn);
142
143         __connman_manager_init(conn, option_compat);
144
145         __connman_plugin_init();
146
147         g_free(option_interface);
148
149         memset(&sa, 0, sizeof(sa));
150         sa.sa_handler = sig_term;
151         sigaction(SIGINT, &sa, NULL);
152         sigaction(SIGTERM, &sa, NULL);
153
154         g_main_loop_run(main_loop);
155
156         __connman_agent_cleanup();
157
158         __connman_element_cleanup();
159
160         __connman_manager_cleanup();
161
162         __connman_storage_cleanup();
163
164         __connman_plugin_cleanup();
165
166         __connman_log_cleanup();
167
168         g_dbus_cleanup_connection(conn);
169
170         g_main_loop_unref(main_loop);
171
172         rmdir(STORAGEDIR);
173
174         rmdir(STATEDIR);
175
176         return 0;
177 }