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