ipconfig: Only save FIXED & MANUAL IP configuraiton
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 #ifdef HAVE_CAPNG
38 #include <cap-ng.h>
39 #endif
40
41 #include "connman.h"
42
43 static GMainLoop *main_loop = NULL;
44
45 static void sig_term(int sig)
46 {
47         connman_info("Terminating");
48
49         g_main_loop_quit(main_loop);
50 }
51
52 static void disconnect_callback(DBusConnection *conn, void *user_data)
53 {
54         connman_error("D-Bus disconnect");
55
56         g_main_loop_quit(main_loop);
57 }
58
59 static gchar *option_debug = NULL;
60 static gchar *option_device = NULL;
61 static gchar *option_plugin = NULL;
62 static gchar *option_nodevice = NULL;
63 static gchar *option_noplugin = NULL;
64 static gchar *option_wifi = NULL;
65 static gboolean option_detach = TRUE;
66 static gboolean option_dnsproxy = TRUE;
67 static gboolean option_compat = FALSE;
68 static gboolean option_version = FALSE;
69
70 static gboolean parse_debug(const char *key, const char *value,
71                                         gpointer user_data, GError **error)
72 {
73         if (value)
74                 option_debug = g_strdup(value);
75         else
76                 option_debug = g_strdup("*");
77
78         return TRUE;
79 }
80
81 static GOptionEntry options[] = {
82         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
83                                 G_OPTION_ARG_CALLBACK, parse_debug,
84                                 "Specify debug options to enable", "DEBUG" },
85         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
86                         "Specify networking device or interface", "DEV" },
87         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
88                         "Specify networking interface to ignore", "DEV" },
89         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
90                                 "Specify plugins to load", "NAME,..." },
91         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
92                                 "Specify plugins not to load", "NAME,..." },
93         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
94                                 "Specify driver for WiFi/Supplicant", "NAME" },
95         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
96                                 G_OPTION_ARG_NONE, &option_detach,
97                                 "Don't fork daemon to background" },
98         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
99                                 G_OPTION_ARG_NONE, &option_dnsproxy,
100                                 "Don't enable DNS Proxy" },
101         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
102                                 "(obsolete)" },
103         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
104                                 "Show version information and exit" },
105         { NULL },
106 };
107
108 const char *connman_option_get_string(const char *key)
109 {
110         if (g_strcmp0(key, "wifi") == 0) {
111                 if (option_wifi == NULL)
112                         return "nl80211,wext";
113                 else
114                         return option_wifi;
115         }
116
117         return NULL;
118 }
119
120 int main(int argc, char *argv[])
121 {
122         GOptionContext *context;
123         GError *error = NULL;
124         DBusConnection *conn;
125         DBusError err;
126         struct sigaction sa;
127         mode_t old_umask;
128
129 #ifdef HAVE_CAPNG
130         /* Drop capabilities */
131 #endif
132
133 #ifdef NEED_THREADS
134         if (g_thread_supported() == FALSE)
135                 g_thread_init(NULL);
136 #endif
137
138         context = g_option_context_new(NULL);
139         g_option_context_add_main_entries(context, options, NULL);
140
141         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
142                 if (error != NULL) {
143                         g_printerr("%s\n", error->message);
144                         g_error_free(error);
145                 } else
146                         g_printerr("An unknown error occurred\n");
147                 exit(1);
148         }
149
150         g_option_context_free(context);
151
152         if (option_version == TRUE) {
153                 printf("%s\n", VERSION);
154                 exit(0);
155         }
156
157         if (option_detach == TRUE) {
158                 if (daemon(0, 0)) {
159                         perror("Can't start daemon");
160                         exit(1);
161                 }
162         }
163
164         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
165                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
166                 if (errno != EEXIST)
167                         perror("Failed to create state directory");
168         }
169
170         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
171                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
172                 if (errno != EEXIST)
173                         perror("Failed to create storage directory");
174         }
175
176         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
177                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
178                 if (errno != EEXIST)
179                         perror("Failed to create statistics directory");
180         }
181
182         old_umask = umask(077);
183
184         main_loop = g_main_loop_new(NULL, FALSE);
185
186 #ifdef NEED_THREADS
187         if (dbus_threads_init_default() == FALSE) {
188                 fprintf(stderr, "Can't init usage of threads\n");
189                 exit(1);
190         }
191 #endif
192
193         dbus_error_init(&err);
194
195         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
196         if (conn == NULL) {
197                 if (dbus_error_is_set(&err) == TRUE) {
198                         fprintf(stderr, "%s\n", err.message);
199                         dbus_error_free(&err);
200                 } else
201                         fprintf(stderr, "Can't register with system bus\n");
202                 exit(1);
203         }
204
205         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
206
207         __connman_log_init(option_debug, option_detach);
208
209         __connman_dbus_init(conn);
210
211         __connman_storage_init();
212         __connman_element_init(option_device, option_nodevice);
213
214         __connman_agent_init();
215         __connman_iptables_init();
216         __connman_tethering_init();
217         __connman_counter_init();
218         __connman_manager_init();
219         __connman_profile_init();
220         __connman_config_init();
221         __connman_stats_init();
222
223         __connman_resolver_init(option_dnsproxy);
224         __connman_ipconfig_init();
225         __connman_rtnl_init();
226         __connman_task_init();
227         __connman_proxy_init();
228         __connman_detect_init();
229         __connman_session_init();
230         __connman_timeserver_init();
231         __connman_connection_init();
232
233         __connman_plugin_init(option_plugin, option_noplugin);
234
235         __connman_element_start();
236
237         g_free(option_device);
238         g_free(option_plugin);
239         g_free(option_nodevice);
240         g_free(option_noplugin);
241
242         memset(&sa, 0, sizeof(sa));
243         sa.sa_handler = sig_term;
244         sigaction(SIGINT, &sa, NULL);
245         sigaction(SIGTERM, &sa, NULL);
246
247         g_main_loop_run(main_loop);
248
249         __connman_element_stop();
250
251         __connman_plugin_cleanup();
252
253         __connman_connection_cleanup();
254         __connman_timeserver_cleanup();
255         __connman_session_cleanup();
256         __connman_detect_cleanup();
257         __connman_proxy_cleanup();
258         __connman_task_cleanup();
259         __connman_rtnl_cleanup();
260         __connman_ipconfig_cleanup();
261         __connman_resolver_cleanup();
262
263         __connman_stats_cleanup();
264         __connman_config_cleanup();
265         __connman_profile_cleanup();
266         __connman_manager_cleanup();
267         __connman_counter_cleanup();
268         __connman_agent_cleanup();
269         __connman_tethering_cleanup();
270         __connman_iptables_cleanup();
271
272         __connman_element_cleanup();
273         __connman_storage_cleanup();
274
275         __connman_dbus_cleanup();
276
277         __connman_log_cleanup();
278
279         dbus_connection_unref(conn);
280
281         g_main_loop_unref(main_loop);
282
283         return 0;
284 }