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