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