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