Register with system bus in container environment
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <sys/signalfd.h>
33 #include <getopt.h>
34 #include <sys/stat.h>
35 #include <net/if.h>
36 #include <netdb.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39
40 #include <gdbus.h>
41
42 #include "connman.h"
43
44 #define CONF_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]) - 1)
45
46 #define DEFAULT_INPUT_REQUEST_TIMEOUT (120 * 1000)
47 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT (300 * 1000)
48
49 #if defined TIZEN_EXT
50 #define DEFAULT_WIFI_INTERFACE "wlan0"
51 #define CONTAINER_FILE "/run/systemd/container"
52 #endif
53
54 #define MAINFILE "main.conf"
55 #define CONFIGMAINFILE CONFIGDIR "/" MAINFILE
56
57 static char *default_auto_connect[] = {
58         "wifi",
59         "ethernet",
60         "cellular",
61         NULL
62 };
63
64 static char *default_favorite_techs[] = {
65         "ethernet",
66         NULL
67 };
68
69 static char *default_blacklist[] = {
70         "vmnet",
71         "vboxnet",
72         "virbr",
73         "ifb",
74         "ve-",
75         "vb-",
76         NULL
77 };
78
79 static struct {
80         bool bg_scan;
81         char **pref_timeservers;
82         unsigned int *auto_connect;
83         unsigned int *favorite_techs;
84         unsigned int *preferred_techs;
85         unsigned int *always_connected_techs;
86         char **fallback_nameservers;
87         unsigned int timeout_inputreq;
88         unsigned int timeout_browserlaunch;
89         char **blacklisted_interfaces;
90         bool allow_hostname_updates;
91         bool allow_domainname_updates;
92         bool single_tech;
93         char **tethering_technologies;
94         bool persistent_tethering_mode;
95         bool enable_6to4;
96         char *vendor_class_id;
97         bool enable_online_check;
98         bool auto_connect_roaming_services;
99         bool acd;
100         bool use_gateways_as_timeservers;
101 #if defined TIZEN_EXT
102         char **cellular_interfaces;
103         bool tizen_tv_extension;
104         bool auto_ip;
105         char *global_nameserver;
106         bool supplicant_debug;
107         char *def_wifi_ifname;
108         bool file_log;
109         bool dlog_log;
110         bool simple_log;
111 #endif
112 } connman_settings  = {
113         .bg_scan = true,
114         .pref_timeservers = NULL,
115         .auto_connect = NULL,
116         .favorite_techs = NULL,
117         .preferred_techs = NULL,
118         .always_connected_techs = NULL,
119         .fallback_nameservers = NULL,
120         .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
121         .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
122         .blacklisted_interfaces = NULL,
123         .allow_hostname_updates = true,
124         .allow_domainname_updates = true,
125         .single_tech = false,
126         .tethering_technologies = NULL,
127         .persistent_tethering_mode = false,
128         .enable_6to4 = false,
129         .vendor_class_id = NULL,
130         .enable_online_check = true,
131         .auto_connect_roaming_services = false,
132         .acd = false,
133         .use_gateways_as_timeservers = false,
134 #if defined TIZEN_EXT
135         .cellular_interfaces = NULL,
136         .tizen_tv_extension = false,
137         .auto_ip = true,
138         .global_nameserver = NULL,
139         .supplicant_debug = false,
140         .def_wifi_ifname = DEFAULT_WIFI_INTERFACE,
141         .file_log = true,
142         .dlog_log = true,
143         .simple_log = true,
144 #endif
145 };
146
147 #if defined TIZEN_EXT
148 static struct {
149         /* BSSID */
150         char *ins_preferred_freq_bssid;
151         bool ins_last_connected_bssid;
152         bool ins_assoc_reject;
153         bool ins_signal_bssid;
154         unsigned int ins_preferred_freq_bssid_score;
155         unsigned int ins_last_connected_bssid_score;
156         unsigned int ins_assoc_reject_score;
157         /* SSID */
158         bool ins_last_user_selection;
159         unsigned int ins_last_user_selection_time;
160         bool ins_last_connected;
161         char *ins_preferred_freq;
162         char **ins_security_priority;
163         unsigned int ins_security_priority_count;
164         bool ins_signal;
165         bool ins_internet;
166         unsigned int ins_last_user_selection_score;
167         unsigned int ins_last_connected_score;
168         unsigned int ins_preferred_freq_score;
169         unsigned int ins_security_priority_score;
170         unsigned int ins_internet_score;
171         /* Common */
172         int ins_signal_level3_5ghz;
173         int ins_signal_level3_24ghz;
174 } connman_ins_settings = {
175         /* BSSID */
176         .ins_preferred_freq_bssid = NULL,
177         .ins_last_connected_bssid = true,
178         .ins_assoc_reject = true,
179         .ins_signal_bssid = true,
180         .ins_preferred_freq_bssid_score = 20,
181         .ins_last_connected_bssid_score = 20,
182         .ins_assoc_reject_score = 10,
183         /* SSID */
184         .ins_last_user_selection = true,
185         .ins_last_user_selection_time = 480,
186         .ins_last_connected = true,
187         .ins_preferred_freq = NULL,
188         .ins_security_priority = NULL,
189         .ins_security_priority_count = 0,
190         .ins_signal = true,
191         .ins_internet = true,
192         .ins_last_user_selection_score = 30,
193         .ins_last_connected_score = 30,
194         .ins_preferred_freq_score = 60,
195         .ins_security_priority_score = 5,
196         .ins_internet_score = 30,
197         /* Common */
198         .ins_signal_level3_5ghz = -76,
199         .ins_signal_level3_24ghz = -74,
200 };
201 #endif
202
203 #define CONF_BG_SCAN                    "BackgroundScanning"
204 #define CONF_PREF_TIMESERVERS           "FallbackTimeservers"
205 #define CONF_AUTO_CONNECT_TECHS         "DefaultAutoConnectTechnologies"
206 #define CONF_FAVORITE_TECHS             "DefaultFavoriteTechnologies"
207 #define CONF_ALWAYS_CONNECTED_TECHS     "AlwaysConnectedTechnologies"
208 #define CONF_PREFERRED_TECHS            "PreferredTechnologies"
209 #define CONF_FALLBACK_NAMESERVERS       "FallbackNameservers"
210 #define CONF_TIMEOUT_INPUTREQ           "InputRequestTimeout"
211 #define CONF_TIMEOUT_BROWSERLAUNCH      "BrowserLaunchTimeout"
212 #define CONF_BLACKLISTED_INTERFACES     "NetworkInterfaceBlacklist"
213 #define CONF_ALLOW_HOSTNAME_UPDATES     "AllowHostnameUpdates"
214 #define CONF_ALLOW_DOMAINNAME_UPDATES   "AllowDomainnameUpdates"
215 #define CONF_SINGLE_TECH                "SingleConnectedTechnology"
216 #define CONF_TETHERING_TECHNOLOGIES      "TetheringTechnologies"
217 #define CONF_PERSISTENT_TETHERING_MODE  "PersistentTetheringMode"
218 #define CONF_ENABLE_6TO4                "Enable6to4"
219 #define CONF_VENDOR_CLASS_ID            "VendorClassID"
220 #define CONF_ENABLE_ONLINE_CHECK        "EnableOnlineCheck"
221 #define CONF_AUTO_CONNECT_ROAMING_SERVICES "AutoConnectRoamingServices"
222 #define CONF_ACD                        "AddressConflictDetection"
223 #define CONF_USE_GATEWAYS_AS_TIMESERVERS "UseGatewaysAsTimeservers"
224 #if defined TIZEN_EXT
225 #define CONF_CELLULAR_INTERFACE         "NetworkCellularInterfaceList"
226 #define CONF_TIZEN_TV_EXT                       "TizenTVExtension"
227 #define CONF_ENABLE_AUTO_IP                     "EnableAutoIp"
228 #define CONF_GLOBAL_NAMESERVER          "GlobalNameserver"
229 #define CONF_CONNMAN_SUPPLICANT_DEBUG   "ConnmanSupplicantDebug"
230 #define CONF_CONNMAN_WIFI_DEF_IFNAME    "DefaultWifiInterface"
231 #define CONF_CONNMAN_FILE_LOG           "FileLogging"
232 #define CONF_CONNMAN_DLOG_LOG           "DlogLogging"
233 #define CONF_CONNMAN_SIMPLIFIED_LOG     "SimplifiedLog"
234 #endif
235
236 #if defined TIZEN_EXT
237 /* BSSID */
238 #define CONF_INS_PREFERRED_FREQ_BSSID        "INSPreferredFreqBSSID"
239 #define CONF_INS_PREFERRED_FREQ_BSSID_SCORE  "INSPreferredFreqBSSIDScore"
240 #define CONF_INS_LAST_CONNECTED_BSSID        "INSLastConnectedBSSID"
241 #define CONF_INS_LAST_CONNECTED_BSSID_SCORE  "INSLastConnectedBSSIDScore"
242 #define CONF_INS_ASSOC_REJECT                "INSAssocReject"
243 #define CONF_INS_ASSOC_REJECT_SCORE          "INSAssocRejectScore"
244 #define CONF_INS_SIGNAL_BSSID                "INSSignalBSSID"
245 /* SSID */
246 #define CONF_INS_LAST_USER_SELECTION         "INSLastUserSelection"
247 #define CONF_INS_LAST_USER_SELECTION_TIME    "INSLastUserSelectionTime"
248 #define CONF_INS_LAST_USER_SELECTION_SCORE   "INSLastUserSelectionScore"
249 #define CONF_INS_LAST_CONNECTED              "INSLastConnected"
250 #define CONF_INS_LAST_CONNECTED_SCORE        "INSLastConnectedScore"
251 #define CONF_INS_PREFERRED_FREQ              "INSPreferredFreq"
252 #define CONF_INS_PREFERRED_FREQ_SCORE        "INSPreferredFreqScore"
253 #define CONF_INS_SECURITY_PRIORITY           "INSSecurityPriority"
254 #define CONF_INS_SECURITY_PRIORITY_COUNT     "INSSecurityPriorityCount"
255 #define CONF_INS_SECURITY_PRIORITY_SCORE     "INSSecurityPriorityScore"
256 #define CONF_INS_SIGNAL                      "INSSignal"
257 #define CONF_INS_INTERNET                    "INSInternet"
258 #define CONF_INS_INTERNET_SCORE              "INSInternetScore"
259 /* Common */
260 #define CONF_INS_SIGNAL_LEVEL3_5GHZ          "INSSignalLevel3_5GHz"
261 #define CONF_INS_SIGNAL_LEVEL3_24GHZ         "INSSignalLevel3_24GHz"
262 #endif
263
264 static const char *supported_options[] = {
265         CONF_BG_SCAN,
266         CONF_PREF_TIMESERVERS,
267         CONF_AUTO_CONNECT_TECHS,
268         CONF_ALWAYS_CONNECTED_TECHS,
269         CONF_PREFERRED_TECHS,
270         CONF_FALLBACK_NAMESERVERS,
271         CONF_TIMEOUT_INPUTREQ,
272         CONF_TIMEOUT_BROWSERLAUNCH,
273         CONF_BLACKLISTED_INTERFACES,
274         CONF_ALLOW_HOSTNAME_UPDATES,
275         CONF_ALLOW_DOMAINNAME_UPDATES,
276         CONF_SINGLE_TECH,
277         CONF_TETHERING_TECHNOLOGIES,
278         CONF_PERSISTENT_TETHERING_MODE,
279         CONF_ENABLE_6TO4,
280         CONF_VENDOR_CLASS_ID,
281         CONF_ENABLE_ONLINE_CHECK,
282         CONF_AUTO_CONNECT_ROAMING_SERVICES,
283         CONF_ACD,
284         CONF_USE_GATEWAYS_AS_TIMESERVERS,
285 #if defined TIZEN_EXT
286         CONF_CELLULAR_INTERFACE,
287         CONF_TIZEN_TV_EXT,
288         CONF_ENABLE_AUTO_IP,
289         CONF_GLOBAL_NAMESERVER,
290         CONF_CONNMAN_SUPPLICANT_DEBUG,
291         CONF_CONNMAN_WIFI_DEF_IFNAME,
292         CONF_CONNMAN_FILE_LOG,
293         CONF_CONNMAN_DLOG_LOG,
294         CONF_CONNMAN_SIMPLIFIED_LOG,
295 #endif
296         NULL
297 };
298
299 #if defined TIZEN_EXT
300 static const char *supported_ins_options[] = {
301         /* BSSID */
302         CONF_INS_PREFERRED_FREQ_BSSID,
303         CONF_INS_PREFERRED_FREQ_BSSID_SCORE,
304         CONF_INS_LAST_CONNECTED_BSSID,
305         CONF_INS_LAST_CONNECTED_BSSID_SCORE,
306         CONF_INS_ASSOC_REJECT,
307         CONF_INS_ASSOC_REJECT_SCORE,
308         CONF_INS_SIGNAL_BSSID,
309         /* SSID */
310         CONF_INS_LAST_USER_SELECTION,
311         CONF_INS_LAST_USER_SELECTION_TIME,
312         CONF_INS_LAST_USER_SELECTION_SCORE,
313         CONF_INS_LAST_CONNECTED,
314         CONF_INS_LAST_CONNECTED_SCORE,
315         CONF_INS_PREFERRED_FREQ,
316         CONF_INS_PREFERRED_FREQ_SCORE,
317         CONF_INS_SECURITY_PRIORITY,
318         CONF_INS_SECURITY_PRIORITY_COUNT,
319         CONF_INS_SECURITY_PRIORITY_SCORE,
320         CONF_INS_SIGNAL,
321         CONF_INS_INTERNET,
322         CONF_INS_INTERNET_SCORE,
323         /* Common */
324         CONF_INS_SIGNAL_LEVEL3_5GHZ,
325         CONF_INS_SIGNAL_LEVEL3_24GHZ,
326         NULL
327 };
328 #endif
329
330 static GKeyFile *load_config(const char *file)
331 {
332         GError *err = NULL;
333         GKeyFile *keyfile;
334
335         keyfile = g_key_file_new();
336
337         g_key_file_set_list_separator(keyfile, ',');
338
339         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
340                 if (err->code != G_FILE_ERROR_NOENT) {
341                         connman_error("Parsing %s failed: %s", file,
342                                                                 err->message);
343                 }
344
345                 g_error_free(err);
346                 g_key_file_free(keyfile);
347                 return NULL;
348         }
349
350         return keyfile;
351 }
352
353 static uint *parse_service_types(char **str_list, gsize len)
354 {
355         unsigned int *type_list;
356         int i, j;
357         enum connman_service_type type;
358
359         type_list = g_try_new0(unsigned int, len + 1);
360         if (!type_list)
361                 return NULL;
362
363         i = 0;
364         j = 0;
365         while (str_list[i]) {
366                 type = __connman_service_string2type(str_list[i]);
367
368                 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
369                         type_list[j] = type;
370                         j += 1;
371                 }
372                 i += 1;
373         }
374
375         type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
376
377         return type_list;
378 }
379
380 static char **parse_fallback_nameservers(char **nameservers, gsize len)
381 {
382         char **servers;
383         int i, j;
384
385         servers = g_try_new0(char *, len + 1);
386         if (!servers)
387                 return NULL;
388
389         i = 0;
390         j = 0;
391         while (nameservers[i]) {
392                 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
393                         servers[j] = g_strdup(nameservers[i]);
394                         j += 1;
395                 }
396                 i += 1;
397         }
398
399         return servers;
400 }
401
402 static void check_config(GKeyFile *config)
403 {
404         char **keys;
405         int j;
406
407         if (!config)
408                 return;
409
410         keys = g_key_file_get_groups(config, NULL);
411
412         for (j = 0; keys && keys[j]; j++) {
413 #if defined TIZEN_EXT
414                 if (g_strcmp0(keys[j], "General") != 0 &&
415                         g_strcmp0(keys[j], "INS") != 0)
416 #else
417                 if (g_strcmp0(keys[j], "General") != 0)
418 #endif
419                         connman_warn("Unknown group %s in %s",
420                                                 keys[j], MAINFILE);
421         }
422
423         g_strfreev(keys);
424
425         keys = g_key_file_get_keys(config, "General", NULL, NULL);
426
427         for (j = 0; keys && keys[j]; j++) {
428                 bool found;
429                 int i;
430
431                 found = false;
432                 for (i = 0; supported_options[i]; i++) {
433                         if (g_strcmp0(keys[j], supported_options[i]) == 0) {
434                                 found = true;
435                                 break;
436                         }
437                 }
438                 if (!found && !supported_options[i])
439                         connman_warn("Unknown option %s in %s",
440                                                 keys[j], MAINFILE);
441         }
442
443         g_strfreev(keys);
444
445 #if defined TIZEN_EXT
446         keys = g_key_file_get_keys(config, "INS", NULL, NULL);
447
448         for (j = 0; keys && keys[j]; j++) {
449                 bool found;
450                 int i;
451
452                 found = false;
453                 for (i = 0; supported_ins_options[i]; i++) {
454                         if (g_strcmp0(keys[j], supported_ins_options[i]) == 0) {
455                                 found = true;
456                                 break;
457                         }
458                 }
459                 if (!found && !supported_ins_options[i])
460                         connman_warn("Unknown option %s in %s",
461                                                 keys[j], MAINFILE);
462         }
463
464         g_strfreev(keys);
465 #endif
466 }
467
468 #if defined TIZEN_EXT
469 static void check_Tizen_INS_configuration(GKeyFile *config)
470 {
471         GError *error = NULL;
472         char *ins_preferred_freq_bssid;
473         char *ins_preferred_freq;
474         char **ins_security_priority;
475         bool boolean;
476         int integer;
477         gsize len;
478
479         ins_preferred_freq_bssid = __connman_config_get_string(config, "INS",
480                                         CONF_INS_PREFERRED_FREQ_BSSID, &error);
481         if (!error)
482                 connman_ins_settings.ins_preferred_freq_bssid = ins_preferred_freq_bssid;
483
484         g_clear_error(&error);
485
486         integer = g_key_file_get_integer(config, "INS",
487                         CONF_INS_PREFERRED_FREQ_BSSID_SCORE, &error);
488         if (!error && integer >= 0)
489                 connman_ins_settings.ins_preferred_freq_bssid_score = integer;
490
491         g_clear_error(&error);
492
493         boolean = __connman_config_get_bool(config, "INS",
494                         CONF_INS_LAST_CONNECTED_BSSID, &error);
495         if (!error)
496                 connman_ins_settings.ins_last_connected_bssid = boolean;
497
498         g_clear_error(&error);
499
500         integer = g_key_file_get_integer(config, "INS",
501                         CONF_INS_LAST_CONNECTED_BSSID_SCORE, &error);
502         if (!error && integer >= 0)
503                 connman_ins_settings.ins_last_connected_bssid_score = integer;
504
505         g_clear_error(&error);
506
507         boolean = __connman_config_get_bool(config, "INS",
508                         CONF_INS_ASSOC_REJECT, &error);
509         if (!error)
510                 connman_ins_settings.ins_assoc_reject = boolean;
511
512         g_clear_error(&error);
513
514         integer = g_key_file_get_integer(config, "INS",
515                         CONF_INS_ASSOC_REJECT_SCORE, &error);
516         if (!error && integer >= 0)
517                 connman_ins_settings.ins_assoc_reject_score = integer;
518
519         g_clear_error(&error);
520
521         boolean = __connman_config_get_bool(config, "INS",
522                         CONF_INS_SIGNAL_BSSID, &error);
523         if (!error)
524                 connman_ins_settings.ins_signal_bssid = boolean;
525
526         g_clear_error(&error);
527
528         boolean = __connman_config_get_bool(config, "INS",
529                         CONF_INS_LAST_USER_SELECTION, &error);
530         if (!error)
531                 connman_ins_settings.ins_last_user_selection = boolean;
532
533         g_clear_error(&error);
534
535         integer = g_key_file_get_integer(config, "INS",
536                         CONF_INS_LAST_USER_SELECTION_TIME, &error);
537         if (!error && integer >= 0)
538                 connman_ins_settings.ins_last_user_selection_time = integer;
539
540         g_clear_error(&error);
541
542         integer = g_key_file_get_integer(config, "INS",
543                         CONF_INS_LAST_USER_SELECTION_SCORE, &error);
544         if (!error && integer >= 0)
545                 connman_ins_settings.ins_last_user_selection_score = integer;
546
547         g_clear_error(&error);
548
549         boolean = __connman_config_get_bool(config, "INS",
550                         CONF_INS_LAST_CONNECTED, &error);
551         if (!error)
552                 connman_ins_settings.ins_last_connected = boolean;
553
554         g_clear_error(&error);
555
556         integer = g_key_file_get_integer(config, "INS",
557                         CONF_INS_LAST_CONNECTED_SCORE, &error);
558         if (!error && integer >= 0)
559                 connman_ins_settings.ins_last_connected_score = integer;
560
561         g_clear_error(&error);
562
563         ins_preferred_freq = __connman_config_get_string(config, "INS",
564                                         CONF_INS_PREFERRED_FREQ, &error);
565         if (!error)
566                 connman_ins_settings.ins_preferred_freq = ins_preferred_freq;
567
568         g_clear_error(&error);
569
570         integer = g_key_file_get_integer(config, "INS",
571                         CONF_INS_PREFERRED_FREQ_SCORE, &error);
572         if (!error && integer >= 0)
573                 connman_ins_settings.ins_preferred_freq_score = integer;
574
575         g_clear_error(&error);
576
577         ins_security_priority = g_key_file_get_string_list(config, "INS",
578                         CONF_INS_SECURITY_PRIORITY, &len, &error);
579
580         if (error == NULL) {
581                 connman_ins_settings.ins_security_priority = ins_security_priority;
582                 connman_ins_settings.ins_security_priority_count = len;
583         }
584
585         g_clear_error(&error);
586
587         integer = g_key_file_get_integer(config, "INS",
588                         CONF_INS_SECURITY_PRIORITY_SCORE, &error);
589         if (!error && integer >= 0)
590                 connman_ins_settings.ins_security_priority_score = integer;
591
592         g_clear_error(&error);
593
594         boolean = __connman_config_get_bool(config, "INS",
595                         CONF_INS_SIGNAL, &error);
596         if (!error)
597                 connman_ins_settings.ins_signal = boolean;
598
599         g_clear_error(&error);
600
601         boolean = __connman_config_get_bool(config, "INS",
602                         CONF_INS_INTERNET, &error);
603         if (!error)
604                 connman_ins_settings.ins_internet = boolean;
605
606         g_clear_error(&error);
607
608         integer = g_key_file_get_integer(config, "INS",
609                         CONF_INS_INTERNET_SCORE, &error);
610         if (!error && integer >= 0)
611                 connman_ins_settings.ins_internet_score = integer;
612
613         g_clear_error(&error);
614
615         integer = g_key_file_get_integer(config, "INS",
616                         CONF_INS_SIGNAL_LEVEL3_5GHZ, &error);
617         if (!error)
618                 connman_ins_settings.ins_signal_level3_5ghz = integer;
619
620         g_clear_error(&error);
621
622         integer = g_key_file_get_integer(config, "INS",
623                         CONF_INS_SIGNAL_LEVEL3_24GHZ, &error);
624         if (!error)
625                 connman_ins_settings.ins_signal_level3_24ghz = integer;
626
627         g_clear_error(&error);
628 }
629
630 static void check_Tizen_configuration(GKeyFile *config)
631 {
632         GError *error = NULL;
633         char **cellular_interfaces;
634         char *global_nameserver;
635         char *default_wifi_ifname;
636         bool boolean;
637         gsize len;
638
639         cellular_interfaces = g_key_file_get_string_list(config, "General",
640                         CONF_CELLULAR_INTERFACE, &len, &error);
641
642         if (error == NULL)
643                 connman_settings.cellular_interfaces = cellular_interfaces;
644
645         g_clear_error(&error);
646
647         boolean = __connman_config_get_bool(config, "General",
648                         CONF_TIZEN_TV_EXT, &error);
649         if (!error)
650                 connman_settings.tizen_tv_extension = boolean;
651
652         g_clear_error(&error);
653
654         boolean = __connman_config_get_bool(config, "General",
655                         CONF_ENABLE_AUTO_IP, &error);
656         if (!error)
657                 connman_settings.auto_ip = boolean;
658
659         g_clear_error(&error);
660
661         global_nameserver = __connman_config_get_string(config, "General",
662                                         CONF_GLOBAL_NAMESERVER, &error);
663         if (!error)
664                 connman_settings.global_nameserver = global_nameserver;
665
666         g_clear_error(&error);
667
668         boolean = __connman_config_get_bool(config, "General",
669                         CONF_CONNMAN_SUPPLICANT_DEBUG, &error);
670         if (!error)
671                 connman_settings.supplicant_debug = boolean;
672
673         g_clear_error(&error);
674
675         default_wifi_ifname = __connman_config_get_string(config, "General",
676                         CONF_CONNMAN_WIFI_DEF_IFNAME, &error);
677         if (!error)
678                 connman_settings.def_wifi_ifname = default_wifi_ifname;
679
680         g_clear_error(&error);
681
682         boolean = __connman_config_get_bool(config, "General",
683                         CONF_CONNMAN_FILE_LOG, &error);
684         if (!error)
685                 connman_settings.file_log = boolean;
686
687         g_clear_error(&error);
688
689         boolean = __connman_config_get_bool(config, "General",
690                         CONF_CONNMAN_DLOG_LOG, &error);
691         if (!error)
692                 connman_settings.dlog_log = boolean;
693
694         g_clear_error(&error);
695
696         boolean = __connman_config_get_bool(config, "General",
697                         CONF_CONNMAN_SIMPLIFIED_LOG, &error);
698         if (!error)
699                 connman_settings.simple_log = boolean;
700
701         g_clear_error(&error);
702
703         check_Tizen_INS_configuration(config);
704 }
705
706 static void set_nofile_inc(void)
707 {
708         int err;
709         struct rlimit rlim;
710
711         rlim.rlim_cur = 8192;
712         rlim.rlim_max = 8192;
713
714         err = setrlimit(RLIMIT_NOFILE, &rlim);
715         if (err)
716                 DBG("fail to increase FILENO err(%d)", err);
717
718         return;
719 }
720 #endif
721
722 static void parse_config(GKeyFile *config)
723 {
724         GError *error = NULL;
725         bool boolean;
726         char **timeservers;
727         char **interfaces;
728         char **str_list;
729         char **tethering;
730         char *vendor_class_id;
731         gsize len;
732         int timeout;
733
734         if (!config) {
735                 connman_settings.auto_connect =
736                         parse_service_types(default_auto_connect, CONF_ARRAY_SIZE(default_auto_connect));
737                 connman_settings.favorite_techs =
738                         parse_service_types(default_favorite_techs, CONF_ARRAY_SIZE(default_favorite_techs));
739                 connman_settings.blacklisted_interfaces =
740                         g_strdupv(default_blacklist);
741                 return;
742         }
743
744         DBG("parsing %s", MAINFILE);
745
746         boolean = g_key_file_get_boolean(config, "General",
747                                                 CONF_BG_SCAN, &error);
748         if (!error)
749                 connman_settings.bg_scan = boolean;
750
751         g_clear_error(&error);
752
753         timeservers = __connman_config_get_string_list(config, "General",
754                                         CONF_PREF_TIMESERVERS, NULL, &error);
755         if (!error)
756                 connman_settings.pref_timeservers = timeservers;
757
758         g_clear_error(&error);
759
760         str_list = __connman_config_get_string_list(config, "General",
761                         CONF_AUTO_CONNECT_TECHS, &len, &error);
762
763         if (!error)
764                 connman_settings.auto_connect =
765                         parse_service_types(str_list, len);
766         else
767                 connman_settings.auto_connect =
768                         parse_service_types(default_auto_connect, CONF_ARRAY_SIZE(default_auto_connect));
769
770         g_strfreev(str_list);
771
772         g_clear_error(&error);
773
774         str_list = __connman_config_get_string_list(config, "General",
775                         CONF_FAVORITE_TECHS, &len, &error);
776
777         if (!error)
778                 connman_settings.favorite_techs =
779                         parse_service_types(str_list, len);
780         else
781                 connman_settings.favorite_techs =
782                         parse_service_types(default_favorite_techs, CONF_ARRAY_SIZE(default_favorite_techs));
783
784         g_strfreev(str_list);
785
786         g_clear_error(&error);
787
788         str_list = __connman_config_get_string_list(config, "General",
789                         CONF_PREFERRED_TECHS, &len, &error);
790
791         if (!error)
792                 connman_settings.preferred_techs =
793                         parse_service_types(str_list, len);
794
795         g_strfreev(str_list);
796
797         g_clear_error(&error);
798
799         str_list = __connman_config_get_string_list(config, "General",
800                         CONF_ALWAYS_CONNECTED_TECHS, &len, &error);
801
802         if (!error)
803                 connman_settings.always_connected_techs =
804                         parse_service_types(str_list, len);
805
806         g_strfreev(str_list);
807
808         g_clear_error(&error);
809
810         str_list = __connman_config_get_string_list(config, "General",
811                         CONF_FALLBACK_NAMESERVERS, &len, &error);
812
813         if (!error)
814                 connman_settings.fallback_nameservers =
815                         parse_fallback_nameservers(str_list, len);
816
817         g_strfreev(str_list);
818
819         g_clear_error(&error);
820
821         timeout = g_key_file_get_integer(config, "General",
822                         CONF_TIMEOUT_INPUTREQ, &error);
823         if (!error && timeout >= 0)
824                 connman_settings.timeout_inputreq = timeout * 1000;
825
826         g_clear_error(&error);
827
828         timeout = g_key_file_get_integer(config, "General",
829                         CONF_TIMEOUT_BROWSERLAUNCH, &error);
830         if (!error && timeout >= 0)
831                 connman_settings.timeout_browserlaunch = timeout * 1000;
832
833         g_clear_error(&error);
834
835         interfaces = __connman_config_get_string_list(config, "General",
836                         CONF_BLACKLISTED_INTERFACES, &len, &error);
837
838         if (!error)
839                 connman_settings.blacklisted_interfaces = interfaces;
840         else
841                 connman_settings.blacklisted_interfaces =
842                         g_strdupv(default_blacklist);
843
844         g_clear_error(&error);
845
846         boolean = __connman_config_get_bool(config, "General",
847                                         CONF_ALLOW_HOSTNAME_UPDATES,
848                                         &error);
849         if (!error)
850                 connman_settings.allow_hostname_updates = boolean;
851
852         g_clear_error(&error);
853
854         boolean = __connman_config_get_bool(config, "General",
855                                         CONF_ALLOW_DOMAINNAME_UPDATES,
856                                         &error);
857         if (!error)
858                 connman_settings.allow_domainname_updates = boolean;
859
860         g_clear_error(&error);
861
862         boolean = __connman_config_get_bool(config, "General",
863                         CONF_SINGLE_TECH, &error);
864         if (!error)
865                 connman_settings.single_tech = boolean;
866
867         g_clear_error(&error);
868
869         tethering = __connman_config_get_string_list(config, "General",
870                         CONF_TETHERING_TECHNOLOGIES, &len, &error);
871
872         if (!error)
873                 connman_settings.tethering_technologies = tethering;
874
875         g_clear_error(&error);
876
877         boolean = __connman_config_get_bool(config, "General",
878                                         CONF_PERSISTENT_TETHERING_MODE,
879                                         &error);
880         if (!error)
881                 connman_settings.persistent_tethering_mode = boolean;
882
883         g_clear_error(&error);
884
885         boolean = __connman_config_get_bool(config, "General",
886                                         CONF_ENABLE_6TO4, &error);
887         if (!error)
888                 connman_settings.enable_6to4 = boolean;
889
890         g_clear_error(&error);
891
892         vendor_class_id = __connman_config_get_string(config, "General",
893                                         CONF_VENDOR_CLASS_ID, &error);
894         if (!error)
895                 connman_settings.vendor_class_id = vendor_class_id;
896
897         g_clear_error(&error);
898
899         boolean = __connman_config_get_bool(config, "General",
900                                         CONF_ENABLE_ONLINE_CHECK, &error);
901         if (!error) {
902                 connman_settings.enable_online_check = boolean;
903                 if (!boolean)
904                         connman_info("Online check disabled by main config.");
905         }
906
907         g_clear_error(&error);
908
909         boolean = __connman_config_get_bool(config, "General",
910                                 CONF_AUTO_CONNECT_ROAMING_SERVICES, &error);
911         if (!error)
912                 connman_settings.auto_connect_roaming_services = boolean;
913
914         g_clear_error(&error);
915
916         boolean = __connman_config_get_bool(config, "General", CONF_ACD, &error);
917         if (!error)
918                 connman_settings.acd = boolean;
919
920         g_clear_error(&error);
921
922         boolean = __connman_config_get_bool(config, "General",
923                                 CONF_USE_GATEWAYS_AS_TIMESERVERS, &error);
924         if (!error)
925                 connman_settings.use_gateways_as_timeservers = boolean;
926
927         g_clear_error(&error);
928
929 #if defined TIZEN_EXT
930         check_Tizen_configuration(config);
931 #endif
932 }
933
934 static int config_init(const char *file)
935 {
936         GKeyFile *config;
937
938 #if defined TIZEN_EXT
939         set_nofile_inc();
940 #endif
941         config = load_config(file);
942         check_config(config);
943         parse_config(config);
944         if (config)
945                 g_key_file_free(config);
946
947 #if defined TIZEN_EXT
948         set_simple_log_option(connman_settings.simple_log);
949         set_dlog_logging_option(connman_settings.dlog_log);
950         set_file_logging_option(connman_settings.file_log);
951 #endif
952         return 0;
953 }
954
955 static GMainLoop *main_loop = NULL;
956
957 static unsigned int __terminated = 0;
958
959 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
960                                                         gpointer user_data)
961 {
962         struct signalfd_siginfo si;
963         ssize_t result;
964         int fd;
965
966         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
967                 return FALSE;
968
969         fd = g_io_channel_unix_get_fd(channel);
970
971         result = read(fd, &si, sizeof(si));
972         if (result != sizeof(si))
973                 return FALSE;
974
975         switch (si.ssi_signo) {
976         case SIGINT:
977         case SIGTERM:
978                 if (__terminated == 0) {
979                         connman_info("Terminating");
980                         g_main_loop_quit(main_loop);
981                 }
982
983                 __terminated = 1;
984                 break;
985         }
986
987         return TRUE;
988 }
989
990 static guint setup_signalfd(void)
991 {
992         GIOChannel *channel;
993         guint source;
994         sigset_t mask;
995         int fd;
996
997         sigemptyset(&mask);
998         sigaddset(&mask, SIGINT);
999         sigaddset(&mask, SIGTERM);
1000
1001         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
1002                 perror("Failed to set signal mask");
1003                 return 0;
1004         }
1005
1006         fd = signalfd(-1, &mask, 0);
1007         if (fd < 0) {
1008                 perror("Failed to create signal descriptor");
1009                 return 0;
1010         }
1011
1012         channel = g_io_channel_unix_new(fd);
1013
1014         g_io_channel_set_close_on_unref(channel, TRUE);
1015         g_io_channel_set_encoding(channel, NULL, NULL);
1016         g_io_channel_set_buffered(channel, FALSE);
1017
1018         source = g_io_add_watch(channel,
1019                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1020                                 signal_handler, NULL);
1021
1022         g_io_channel_unref(channel);
1023
1024         return source;
1025 }
1026
1027 static void disconnect_callback(DBusConnection *conn, void *user_data)
1028 {
1029         connman_error("D-Bus disconnect");
1030
1031         g_main_loop_quit(main_loop);
1032 }
1033
1034 static gchar *option_config = NULL;
1035 static gchar *option_debug = NULL;
1036 static gchar *option_device = NULL;
1037 static gchar *option_plugin = NULL;
1038 static gchar *option_nodevice = NULL;
1039 static gchar *option_noplugin = NULL;
1040 static gchar *option_wifi = NULL;
1041 static gboolean option_detach = TRUE;
1042 static gboolean option_dnsproxy = TRUE;
1043 static gboolean option_backtrace = TRUE;
1044 static gboolean option_version = FALSE;
1045
1046 static bool parse_debug(const char *key, const char *value,
1047                                         gpointer user_data, GError **error)
1048 {
1049         if (value) {
1050                 if (option_debug) {
1051                         char *prev = option_debug;
1052
1053                         option_debug = g_strconcat(prev, ",", value, NULL);
1054                         g_free(prev);
1055                 } else {
1056                         option_debug = g_strdup(value);
1057                 }
1058         } else {
1059                 g_free(option_debug);
1060                 option_debug = g_strdup("*");
1061         }
1062
1063         return true;
1064 }
1065
1066 static bool parse_noplugin(const char *key, const char *value,
1067                                         gpointer user_data, GError **error)
1068 {
1069         if (option_noplugin) {
1070                 char *prev = option_noplugin;
1071
1072                 option_noplugin = g_strconcat(prev, ",", value, NULL);
1073                 g_free(prev);
1074         } else {
1075                 option_noplugin = g_strdup(value);
1076         }
1077
1078         return true;
1079 }
1080
1081 static GOptionEntry options[] = {
1082         { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
1083                                 "Load the specified configuration file "
1084                                 "instead of " CONFIGMAINFILE, "FILE" },
1085         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
1086                                 G_OPTION_ARG_CALLBACK, parse_debug,
1087                                 "Specify debug options to enable", "DEBUG" },
1088         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
1089                         "Specify networking devices or interfaces", "DEV,..." },
1090         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
1091                         "Specify networking interfaces to ignore", "DEV,..." },
1092         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
1093                                 "Specify plugins to load", "NAME,..." },
1094         { "noplugin", 'P', 0, G_OPTION_ARG_CALLBACK, &parse_noplugin,
1095                                 "Specify plugins not to load", "NAME,..." },
1096         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
1097                                 "Specify driver for WiFi/Supplicant", "NAME" },
1098         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
1099                                 G_OPTION_ARG_NONE, &option_detach,
1100                                 "Don't fork daemon to background" },
1101         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
1102                                 G_OPTION_ARG_NONE, &option_dnsproxy,
1103                                 "Don't support DNS resolving" },
1104         { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
1105                                 G_OPTION_ARG_NONE, &option_backtrace,
1106                                 "Don't print out backtrace information" },
1107         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
1108                                 "Show version information and exit" },
1109         { NULL },
1110 };
1111
1112 const char *connman_option_get_string(const char *key)
1113 {
1114         if (g_str_equal(key, CONF_VENDOR_CLASS_ID))
1115                 return connman_settings.vendor_class_id;
1116
1117         if (g_strcmp0(key, "wifi") == 0) {
1118                 if (!option_wifi)
1119                         return "nl80211,wext";
1120                 else
1121                         return option_wifi;
1122         }
1123
1124 #if defined TIZEN_EXT
1125         if (g_str_equal(key, CONF_GLOBAL_NAMESERVER))
1126                 return connman_settings.global_nameserver;
1127
1128         if (g_str_equal(key, CONF_INS_PREFERRED_FREQ_BSSID))
1129                 return connman_ins_settings.ins_preferred_freq_bssid;
1130
1131         if (g_str_equal(key, CONF_INS_PREFERRED_FREQ))
1132                 return connman_ins_settings.ins_preferred_freq;
1133
1134         if (g_str_equal(key, CONF_CONNMAN_WIFI_DEF_IFNAME))
1135                 return connman_settings.def_wifi_ifname;
1136 #endif
1137         return NULL;
1138 }
1139
1140 bool connman_setting_get_bool(const char *key)
1141 {
1142         if (g_str_equal(key, CONF_BG_SCAN))
1143                 return connman_settings.bg_scan;
1144
1145         if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
1146                 return connman_settings.allow_hostname_updates;
1147
1148         if (g_str_equal(key, CONF_ALLOW_DOMAINNAME_UPDATES))
1149                 return connman_settings.allow_domainname_updates;
1150
1151         if (g_str_equal(key, CONF_SINGLE_TECH))
1152                 return connman_settings.single_tech;
1153
1154         if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
1155                 return connman_settings.persistent_tethering_mode;
1156
1157         if (g_str_equal(key, CONF_ENABLE_6TO4))
1158                 return connman_settings.enable_6to4;
1159
1160         if (g_str_equal(key, CONF_ENABLE_ONLINE_CHECK))
1161                 return connman_settings.enable_online_check;
1162
1163         if (g_str_equal(key, CONF_AUTO_CONNECT_ROAMING_SERVICES))
1164                 return connman_settings.auto_connect_roaming_services;
1165
1166         if (g_str_equal(key, CONF_ACD))
1167                 return connman_settings.acd;
1168
1169         if (g_str_equal(key, CONF_USE_GATEWAYS_AS_TIMESERVERS))
1170                 return connman_settings.use_gateways_as_timeservers;
1171
1172 #if defined TIZEN_EXT
1173         if (g_str_equal(key, CONF_ENABLE_AUTO_IP))
1174                 return connman_settings.auto_ip;
1175
1176         if (g_str_equal(key, CONF_CONNMAN_SUPPLICANT_DEBUG))
1177                 return connman_settings.supplicant_debug;
1178
1179         if (g_str_equal(key, CONF_CONNMAN_FILE_LOG))
1180                 return connman_settings.file_log;
1181
1182         if (g_str_equal(key, CONF_CONNMAN_DLOG_LOG))
1183                 return connman_settings.dlog_log;
1184
1185         if (g_str_equal(key, CONF_CONNMAN_SIMPLIFIED_LOG))
1186                 return connman_settings.simple_log;
1187 #endif
1188
1189 #if defined TIZEN_EXT
1190         if (g_str_equal(key, CONF_INS_LAST_CONNECTED_BSSID))
1191                 return connman_ins_settings.ins_last_connected_bssid;
1192
1193         if (g_str_equal(key, CONF_INS_ASSOC_REJECT))
1194                 return connman_ins_settings.ins_assoc_reject;
1195
1196         if (g_str_equal(key, CONF_INS_SIGNAL_BSSID))
1197                 return connman_ins_settings.ins_signal_bssid;
1198
1199         if (g_str_equal(key, CONF_INS_LAST_USER_SELECTION))
1200                 return connman_ins_settings.ins_last_user_selection;
1201
1202         if (g_str_equal(key, CONF_INS_LAST_CONNECTED))
1203                 return connman_ins_settings.ins_last_connected;
1204
1205         if (g_str_equal(key, CONF_INS_SIGNAL))
1206                 return connman_ins_settings.ins_signal;
1207
1208         if (g_str_equal(key, CONF_INS_INTERNET))
1209                 return connman_ins_settings.ins_internet;
1210 #endif
1211
1212         return false;
1213 }
1214
1215 #if defined TIZEN_EXT
1216 unsigned int connman_setting_get_uint(const char *key)
1217 {
1218         if (g_str_equal(key, CONF_INS_PREFERRED_FREQ_BSSID_SCORE))
1219                 return connman_ins_settings.ins_preferred_freq_bssid_score;
1220
1221         if (g_str_equal(key, CONF_INS_LAST_CONNECTED_BSSID_SCORE))
1222                 return connman_ins_settings.ins_last_connected_bssid_score;
1223
1224         if (g_str_equal(key, CONF_INS_ASSOC_REJECT_SCORE))
1225                 return connman_ins_settings.ins_assoc_reject_score;
1226
1227         if (g_str_equal(key, CONF_INS_LAST_USER_SELECTION_TIME))
1228                 return connman_ins_settings.ins_last_user_selection_time;
1229
1230         if (g_str_equal(key, CONF_INS_SECURITY_PRIORITY_COUNT))
1231                 return connman_ins_settings.ins_security_priority_count;
1232
1233         if (g_str_equal(key, CONF_INS_LAST_USER_SELECTION_SCORE))
1234                 return connman_ins_settings.ins_last_user_selection_score;
1235
1236         if (g_str_equal(key, CONF_INS_LAST_CONNECTED_SCORE))
1237                 return connman_ins_settings.ins_last_connected_score;
1238
1239         if (g_str_equal(key, CONF_INS_PREFERRED_FREQ_SCORE))
1240                 return connman_ins_settings.ins_preferred_freq_score;
1241
1242         if (g_str_equal(key, CONF_INS_SECURITY_PRIORITY_SCORE))
1243                 return connman_ins_settings.ins_security_priority_score;
1244
1245         if (g_str_equal(key, CONF_INS_INTERNET_SCORE))
1246                 return connman_ins_settings.ins_internet_score;
1247
1248         return 0;
1249 }
1250
1251 int connman_setting_get_int(const char *key)
1252 {
1253         if (g_str_equal(key, CONF_INS_SIGNAL_LEVEL3_5GHZ))
1254                 return connman_ins_settings.ins_signal_level3_5ghz;
1255
1256         if (g_str_equal(key, CONF_INS_SIGNAL_LEVEL3_24GHZ))
1257                 return connman_ins_settings.ins_signal_level3_24ghz;
1258
1259         return 0;
1260 }
1261 #endif
1262
1263 char **connman_setting_get_string_list(const char *key)
1264 {
1265         if (g_str_equal(key, CONF_PREF_TIMESERVERS))
1266                 return connman_settings.pref_timeservers;
1267
1268         if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
1269                 return connman_settings.fallback_nameservers;
1270
1271         if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
1272                 return connman_settings.blacklisted_interfaces;
1273
1274         if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
1275                 return connman_settings.tethering_technologies;
1276
1277 #if defined TIZEN_EXT
1278         if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
1279                 return connman_settings.cellular_interfaces;
1280 #endif
1281
1282 #if defined TIZEN_EXT
1283         if (g_str_equal(key, CONF_INS_SECURITY_PRIORITY))
1284                 return connman_ins_settings.ins_security_priority;
1285 #endif
1286
1287         return NULL;
1288 }
1289
1290 unsigned int *connman_setting_get_uint_list(const char *key)
1291 {
1292         if (g_str_equal(key, CONF_AUTO_CONNECT_TECHS))
1293                 return connman_settings.auto_connect;
1294
1295         if (g_str_equal(key, CONF_FAVORITE_TECHS))
1296                 return connman_settings.favorite_techs;
1297
1298         if (g_str_equal(key, CONF_PREFERRED_TECHS))
1299                 return connman_settings.preferred_techs;
1300
1301         if (g_str_equal(key, CONF_ALWAYS_CONNECTED_TECHS))
1302                 return connman_settings.always_connected_techs;
1303
1304         return NULL;
1305 }
1306
1307 unsigned int connman_timeout_input_request(void)
1308 {
1309         return connman_settings.timeout_inputreq;
1310 }
1311
1312 unsigned int connman_timeout_browser_launch(void)
1313 {
1314         return connman_settings.timeout_browserlaunch;
1315 }
1316
1317 int main(int argc, char *argv[])
1318 {
1319         GOptionContext *context;
1320         GError *error = NULL;
1321         DBusConnection *conn;
1322         DBusError err;
1323         guint signal;
1324
1325         context = g_option_context_new(NULL);
1326         g_option_context_add_main_entries(context, options, NULL);
1327
1328         if (!g_option_context_parse(context, &argc, &argv, &error)) {
1329                 if (error) {
1330                         g_printerr("%s\n", error->message);
1331                         g_error_free(error);
1332                 } else
1333                         g_printerr("An unknown error occurred\n");
1334                 exit(1);
1335         }
1336
1337         g_option_context_free(context);
1338
1339         if (option_version) {
1340                 printf("%s\n", VERSION);
1341                 exit(0);
1342         }
1343
1344         if (option_detach) {
1345                 if (daemon(0, 0)) {
1346                         perror("Can't start daemon");
1347                         exit(1);
1348                 }
1349         }
1350
1351         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
1352                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
1353                 if (errno != EEXIST)
1354                         perror("Failed to create storage directory");
1355         }
1356
1357         umask(0077);
1358
1359         main_loop = g_main_loop_new(NULL, FALSE);
1360
1361         signal = setup_signalfd();
1362
1363         dbus_error_init(&err);
1364
1365         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
1366         if (!conn) {
1367                 if (dbus_error_is_set(&err)) {
1368                         fprintf(stderr, "%s\n", err.message);
1369                         dbus_error_free(&err);
1370                 } else
1371                         fprintf(stderr, "Can't register with system bus\n");
1372                 exit(1);
1373         }
1374
1375         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
1376
1377         __connman_log_init(argv[0], option_debug, option_detach,
1378                         option_backtrace, "Connection Manager", VERSION);
1379
1380         __connman_dbus_init(conn);
1381
1382 #if defined TIZEN_EXT
1383         if (access(CONTAINER_FILE, F_OK) == 0) {
1384                 g_main_loop_run(main_loop);
1385
1386                 g_source_remove(signal);
1387                 dbus_connection_unref(conn);
1388                 g_main_loop_unref(main_loop);
1389
1390                 __connman_dbus_cleanup();
1391                 __connman_log_cleanup(option_backtrace);
1392
1393                 return 0;
1394         }
1395 #endif
1396
1397         if (!option_config)
1398                 config_init(CONFIGMAINFILE);
1399         else
1400                 config_init(option_config);
1401
1402         __connman_util_init();
1403         __connman_inotify_init();
1404         __connman_technology_init();
1405         __connman_notifier_init();
1406         __connman_agent_init();
1407         __connman_service_init();
1408         __connman_peer_service_init();
1409         __connman_peer_init();
1410 #if defined TIZEN_EXT_WIFI_MESH
1411         __connman_mesh_init();
1412 #endif /* TIZEN_EXT_WIFI_MESH */
1413         __connman_provider_init();
1414         __connman_network_init();
1415         __connman_config_init();
1416         __connman_device_init(option_device, option_nodevice);
1417
1418         __connman_ippool_init();
1419         __connman_firewall_init();
1420         __connman_nat_init();
1421         __connman_tethering_init();
1422         __connman_counter_init();
1423         __connman_manager_init();
1424         __connman_stats_init();
1425         __connman_clock_init();
1426
1427         __connman_ipconfig_init();
1428 #if defined TIZEN_EXT
1429         __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
1430 #else /* TIZEN_EXT */
1431         __connman_rtnl_init();
1432 #endif /* TIZEN_EXT */
1433         __connman_task_init();
1434         __connman_proxy_init();
1435         __connman_detect_init();
1436         __connman_session_init();
1437         __connman_timeserver_init();
1438         __connman_connection_init();
1439
1440         __connman_plugin_init(option_plugin, option_noplugin);
1441
1442         __connman_resolver_init(option_dnsproxy);
1443         __connman_rtnl_start();
1444         __connman_dhcp_init();
1445         __connman_dhcpv6_init();
1446         __connman_wpad_init();
1447         __connman_wispr_init();
1448 #if !defined TIZEN_EXT
1449         __connman_rfkill_init();
1450         __connman_machine_init();
1451 #endif
1452
1453         g_free(option_config);
1454         g_free(option_device);
1455         g_free(option_plugin);
1456         g_free(option_nodevice);
1457         g_free(option_noplugin);
1458
1459         g_main_loop_run(main_loop);
1460
1461         g_source_remove(signal);
1462
1463 #if !defined TIZEN_EXT
1464         __connman_machine_cleanup();
1465         __connman_rfkill_cleanup();
1466 #endif
1467         __connman_wispr_cleanup();
1468         __connman_wpad_cleanup();
1469         __connman_dhcpv6_cleanup();
1470         __connman_session_cleanup();
1471         __connman_plugin_cleanup();
1472         __connman_provider_cleanup();
1473         __connman_connection_cleanup();
1474         __connman_timeserver_cleanup();
1475         __connman_detect_cleanup();
1476         __connman_proxy_cleanup();
1477         __connman_task_cleanup();
1478         __connman_rtnl_cleanup();
1479         __connman_resolver_cleanup();
1480
1481         __connman_clock_cleanup();
1482         __connman_stats_cleanup();
1483         __connman_config_cleanup();
1484         __connman_manager_cleanup();
1485         __connman_counter_cleanup();
1486         __connman_tethering_cleanup();
1487         __connman_nat_cleanup();
1488         __connman_firewall_cleanup();
1489         __connman_peer_service_cleanup();
1490         __connman_peer_cleanup();
1491 #if defined TIZEN_EXT_WIFI_MESH
1492         __connman_mesh_cleanup();
1493 #endif /* TIZEN_EXT_WIFI_MESH */
1494         __connman_ippool_cleanup();
1495         __connman_device_cleanup();
1496         __connman_network_cleanup();
1497         __connman_dhcp_cleanup();
1498         __connman_service_cleanup();
1499         __connman_agent_cleanup();
1500         __connman_ipconfig_cleanup();
1501         __connman_notifier_cleanup();
1502         __connman_technology_cleanup();
1503         __connman_inotify_cleanup();
1504
1505         __connman_util_cleanup();
1506         __connman_dbus_cleanup();
1507
1508         __connman_log_cleanup(option_backtrace);
1509
1510         dbus_connection_unref(conn);
1511
1512         g_main_loop_unref(main_loop);
1513
1514         if (connman_settings.pref_timeservers)
1515                 g_strfreev(connman_settings.pref_timeservers);
1516
1517         g_free(connman_settings.auto_connect);
1518         g_free(connman_settings.favorite_techs);
1519         g_free(connman_settings.preferred_techs);
1520         g_strfreev(connman_settings.fallback_nameservers);
1521         g_strfreev(connman_settings.blacklisted_interfaces);
1522         g_strfreev(connman_settings.tethering_technologies);
1523
1524 #if defined TIZEN_EXT
1525         g_free(connman_ins_settings.ins_preferred_freq_bssid);
1526         g_free(connman_ins_settings.ins_preferred_freq);
1527         if (connman_ins_settings.ins_security_priority)
1528                 g_strfreev(connman_ins_settings.ins_security_priority);
1529 #endif
1530
1531         g_free(option_debug);
1532         g_free(option_wifi);
1533
1534         return 0;
1535 }