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