/
usb_moded-config.c
892 lines (763 loc) · 21.4 KB
1
2
3
4
/**
@file usb_moded-config.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
Copyright (C) 2012-2015 Jolla. All rights reserved.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@author: Philippe De Swert <philippe.de-swert@nokia.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
28
#include <fcntl.h>
29
30
31
32
#include <sys/stat.h>
#include <sys/types.h>
33
#include <glib.h>
34
#include <glob.h>
35
/*
36
#include <glib/gkeyfile.h>
37
#include <glib/gstdio.h>
38
*/
39
#include "usb_moded.h"
40
#include "usb_moded-config.h"
41
#include "usb_moded-config-private.h"
42
#include "usb_moded-log.h"
43
#include "usb_moded-modes.h"
44
#include "usb_moded-modesetting.h"
45
46
47
48
49
#ifdef USE_MER_SSU
# include "usb_moded-ssu.h"
#endif
50
static int get_conf_int(const gchar *entry, const gchar *key);
51
52
static char * get_conf_string(const gchar *entry, const gchar *key);
static char * get_kcmdline_string(const char *entry);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static int validate_ip(const char *ipadd)
{
unsigned int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
return(-1);
if ((b1 | b2 | b3 | b4) > 255)
return(-1);
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
return(-1);
/* all ok */
return 0;
}
70
char *find_mounts(void)
71
{
72
73
char *ret = NULL;
74
75
76
ret = get_conf_string(FS_MOUNT_ENTRY, FS_MOUNT_KEY);
if(ret == NULL)
77
78
{
ret = g_strdup(FS_MOUNT_DEFAULT);
79
log_debug("Default mount = %s\n", ret);
80
81
82
83
84
85
86
}
return(ret);
}
int find_sync(void)
{
87
return(get_conf_int(FS_SYNC_ENTRY, FS_SYNC_KEY));
88
89
}
90
char * find_alt_mount(void)
91
{
92
return(get_conf_string(ALT_MOUNT_ENTRY, ALT_MOUNT_KEY));
93
94
}
95
char * find_udev_path(void)
96
{
97
return(get_conf_string(UDEV_PATH_ENTRY, UDEV_PATH_KEY));
98
}
99
100
char * find_udev_subsystem(void)
101
102
103
{
return(get_conf_string(UDEV_PATH_ENTRY, UDEV_SUBSYSTEM_KEY));
}
104
105
char * check_trigger(void)
106
107
108
109
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PATH_KEY));
}
110
char * get_trigger_subsystem(void)
111
112
113
114
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_UDEV_SUBSYSTEM));
}
115
char * get_trigger_mode(void)
116
117
118
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_MODE_KEY));
}
119
120
char * get_trigger_property(void)
121
122
123
124
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_KEY));
}
125
char * get_trigger_value(void)
126
127
128
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_VALUE_KEY));
}
129
130
static char * get_network_ip(void)
131
{
132
char * ip = get_kcmdline_string(NETWORK_IP_KEY);
133
if (ip != NULL)
134
135
if(!validate_ip(ip))
return(ip);
136
137
138
139
return(get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY));
}
140
static char * get_network_interface(void)
141
142
143
144
{
return(get_conf_string(NETWORK_ENTRY, NETWORK_INTERFACE_KEY));
}
145
static char * get_network_gateway(void)
146
{
147
char * gw = get_kcmdline_string(NETWORK_GATEWAY_KEY);
148
149
150
if (gw != NULL)
return(gw);
151
152
153
return(get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY));
}
154
static char * get_network_netmask(void)
155
156
157
158
159
160
161
162
{
char * netmask = get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
return(netmask);
return(get_conf_string(NETWORK_ENTRY, NETWORK_NETMASK_KEY));
}
163
static char * get_network_nat_interface(void)
164
165
166
167
{
return(get_conf_string(NETWORK_ENTRY, NETWORK_NAT_INTERFACE_KEY));
}
168
169
170
171
/* create basic conffile with sensible defaults */
static void create_conf_file(void)
{
GKeyFile *settingsfile;
172
gchar *keyfile;
173
int dir = 1;
174
struct stat dir_stat;
175
176
177
178
/* since this function can also be called when the dir exists we only create
it if it is missing */
if(stat(CONFIG_FILE_DIR, &dir_stat))
179
{
180
181
182
183
184
185
186
dir = mkdir(CONFIG_FILE_DIR, 0755);
if(dir < 0)
{
log_warning("Could not create confdir, continuing without configuration!\n");
/* no point in trying to generate the config file if the dir cannot be created */
return;
}
187
}
188
189
190
191
192
193
settingsfile = g_key_file_new();
g_key_file_set_string(settingsfile, MODE_SETTING_ENTRY, MODE_SETTING_KEY, MODE_DEVELOPER );
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
if(g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL) == 0)
194
log_debug("Conffile creation failed. Continuing without configuration!\n");
195
free(keyfile);
196
g_key_file_free(settingsfile);
197
198
}
199
static int get_conf_int(const gchar *entry, const gchar *key)
200
201
202
{
GKeyFile *settingsfile;
gboolean test = FALSE;
203
gchar **keys, **origkeys;
204
int ret = 0;
205
206
207
208
209
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
210
211
log_debug("no conffile, Creating\n");
create_conf_file();
212
}
213
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
214
if(keys == NULL)
215
return ret;
216
origkeys = keys;
217
218
while (*keys != NULL)
{
219
220
221
222
223
224
if(!strcmp(*keys, key))
{
ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL);
log_debug("%s key value = %d\n", key, ret);
}
keys++;
225
}
226
g_strfreev(origkeys);
227
228
g_key_file_free(settingsfile);
return(ret);
229
230
}
231
232
static char * get_conf_string(const gchar *entry, const gchar *key)
233
234
235
{
GKeyFile *settingsfile;
gboolean test = FALSE;
236
gchar **keys, **origkeys, *tmp_char = NULL;
237
238
239
240
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
241
242
log_debug("No conffile. Creating\n");
create_conf_file();
243
244
/* should succeed now */
g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
245
}
246
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
247
if(keys == NULL)
248
249
goto end;
origkeys = keys;
250
251
while (*keys != NULL)
{
252
253
254
255
256
257
258
259
260
if(!strcmp(*keys, key))
{
tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL);
if(tmp_char)
{
log_debug("key %s value = %s\n", key, tmp_char);
}
}
keys++;
261
}
262
263
g_strfreev(origkeys);
end:
264
g_key_file_free(settingsfile);
265
return(tmp_char);
266
267
}
268
269
static char * get_kcmdline_string(const char *entry)
270
271
272
{
int fd;
char cmdLine[1024];
273
char *ret = NULL;
274
275
276
int len;
gint argc = 0;
gchar **argv = NULL;
277
gchar **arg_tokens = NULL, **network_tokens = NULL;
278
279
280
GError *optErr = NULL;
int i;
281
282
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
283
284
285
286
287
288
289
log_debug("could not read /proc/cmdline");
return(ret);
}
len = read(fd, cmdLine, sizeof(cmdLine) - 1);
close(fd);
290
291
if (len <= 0)
{
292
293
294
295
296
297
log_debug("kernel command line was empty");
return(ret);
}
cmdLine[len] = '\0';
298
/* we're looking for a piece of the kernel command line matching this:
299
ip=192.168.3.100::192.168.3.1:255.255.255.0::usb0:on */
300
301
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
302
303
304
305
g_error_free(optErr);
return(ret);
}
306
/* find the ip token */
307
308
for (i=0; i < argc; i++)
{
309
arg_tokens = g_strsplit(argv[i], "=", 2);
310
if (!g_ascii_strcasecmp(arg_tokens[0], "usb_moded_ip"))
311
{
312
network_tokens = g_strsplit(arg_tokens[1], ":", 7);
313
314
/* check if it is for the usb or rndis interface */
if(g_strrstr(network_tokens[5], "usb")|| (g_strrstr(network_tokens[5], "rndis")))
315
316
317
{
if(!strcmp(entry, NETWORK_IP_KEY))
{
318
g_free(ret), ret = g_strdup(network_tokens[0]);
319
320
321
322
323
324
325
log_debug("Command line ip = %s\n", ret);
}
if(!strcmp(entry, NETWORK_GATEWAY_KEY))
{
/* gateway might be empty, so we do not want to return an empty string */
if(strlen(network_tokens[2]) > 2)
{
326
g_free(ret), ret = g_strdup(network_tokens[2]);
327
328
329
log_debug("Command line gateway = %s\n", ret);
}
}
330
331
if(!strcmp(entry, NETWORK_NETMASK_KEY))
{
332
g_free(ret), ret = g_strdup(network_tokens[3]);
333
334
log_debug("Command line netmask = %s\n", ret);
}
335
}
336
}
337
g_strfreev(arg_tokens);
338
}
339
g_strfreev(argv);
340
g_strfreev(network_tokens);
341
342
343
344
return(ret);
}
345
char * get_mode_setting(void)
346
{
347
char * mode = get_kcmdline_string(MODE_SETTING_KEY);
348
349
350
if (mode != NULL)
return(mode);
351
352
return(get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
}
353
354
355
356
357
358
/*
* @param settingsfile: already opened settingsfile we want to read an entry from
* @param entry: entry we want to read
* @param key: key value of the entry we want to read
* @new_value: potentially new value we want to compare against
*
359
* @return: 0 when the old value is the same as the new one, 1 otherwise
360
361
362
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
363
364
365
366
char *old_value = g_key_file_get_string(settingsfile, entry, key, NULL);
int changed = (g_strcmp0(old_value, new_value) != 0);
g_free(old_value);
return changed;
367
}
368
369
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value)
370
371
372
{
GKeyFile *settingsfile;
gboolean test = FALSE;
373
set_config_result_t ret = SET_CONFIG_ERROR;
374
375
376
377
gchar *keyfile;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
378
379
if(test)
{
380
if(!config_value_changed(settingsfile, entry, key, value))
381
382
383
384
385
386
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
else
387
{
388
389
log_debug("No conffile. Creating.\n");
create_conf_file();
390
391
}
392
g_key_file_set_string(settingsfile, entry, key, value);
393
394
395
396
397
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
398
399
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
400
g_free(keyfile);
401
402
return (ret);
403
}
404
405
set_config_result_t set_mode_setting(const char *mode)
406
{
407
408
if (strcmp(mode, MODE_ASK) && valid_mode(mode))
return SET_CONFIG_ERROR;
409
410
411
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}
412
413
/* Builds the string used for hidden modes, when hide set to one builds the
new string of hidden modes when adding one, otherwise it will remove one */
414
static char * make_modes_string(const char *key, const char *mode_name, int include)
415
{
416
417
418
419
char *modes_new = 0;
char *modes_old = 0;
gchar **modes_arr = 0;
GString *modes_tmp = 0;
420
421
int i;
422
/* Get current comma separated list of hidden modes */
423
424
modes_old = get_conf_string(MODE_SETTING_ENTRY, key);
if(!modes_old)
425
{
426
modes_old = g_strdup("");
427
428
}
429
modes_arr = g_strsplit(modes_old, ",", 0);
430
431
modes_tmp = g_string_new(NULL);
432
433
for(i = 0; modes_arr[i] != NULL; i++)
434
{
435
if(strlen(modes_arr[i]) == 0)
436
437
438
439
440
{
/* Skip any empty strings */
continue;
}
441
if(!strcmp(modes_arr[i], mode_name))
442
443
{
/* When unhiding, just skip all matching entries */
444
if(!include)
445
446
447
continue;
/* When hiding, keep the 1st match and ignore the rest */
448
include = 0;
449
450
}
451
452
453
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, modes_arr[i]);
454
}
455
456
if(include)
457
{
458
/* Adding a hidden mode and no matching entry was found */
459
460
461
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, mode_name);
462
}
463
464
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
465
466
g_strfreev(modes_arr), modes_arr = 0;
467
468
g_free(modes_old), modes_old = 0;
469
470
return modes_new;
471
472
}
473
474
set_config_result_t set_hide_mode_setting(const char *mode)
{
475
476
set_config_result_t ret = SET_CONFIG_UNCHANGED;
477
char *hidden_modes = make_modes_string(MODE_HIDE_KEY, mode, 1);
478
479
480
481
if( hidden_modes ) {
ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
482
483
484
485
if(ret == SET_CONFIG_UPDATED) {
send_hidden_modes_signal();
send_supported_modes_signal();
486
send_available_modes_signal();
487
}
488
489
490
g_free(hidden_modes);
491
return(ret);
492
493
494
495
}
set_config_result_t set_unhide_mode_setting(const char *mode)
{
496
497
set_config_result_t ret = SET_CONFIG_UNCHANGED;
498
char *hidden_modes = make_modes_string(MODE_HIDE_KEY, mode, 0);
499
500
501
502
if( hidden_modes ) {
ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
503
504
505
506
if(ret == SET_CONFIG_UPDATED) {
send_hidden_modes_signal();
send_supported_modes_signal();
507
send_available_modes_signal();
508
}
509
510
511
g_free(hidden_modes);
512
return(ret);
513
514
}
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
set_config_result_t set_mode_whitelist(const char *whitelist)
{
set_config_result_t ret = set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
mode_setting = get_mode_setting();
if (strcmp(mode_setting, MODE_ASK) && valid_mode(mode_setting))
set_mode_setting(MODE_ASK);
g_free(mode_setting);
current_mode = get_usb_mode();
if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && valid_mode(current_mode)) {
usb_moded_mode_cleanup(get_usb_module());
set_usb_mode(MODE_CHARGING_FALLBACK);
}
usb_moded_send_whitelisted_modes_signal(whitelist);
send_available_modes_signal();
}
return ret;
}
set_config_result_t set_mode_in_whitelist(const char *mode, int allowed)
{
set_config_result_t ret = SET_CONFIG_UNCHANGED;
char *whitelist = make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
if (whitelist) {
ret = set_mode_whitelist(whitelist);
}
g_free(whitelist);
return(ret);
}
556
557
558
559
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
560
set_config_result_t set_network_setting(const char *config, const char *setting)
561
562
563
564
565
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar *keyfile;
566
567
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
if(validate_ip(setting) != 0)
568
return SET_CONFIG_ERROR;
569
570
571
572
573
574
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
{
575
576
577
set_config_result_t ret = SET_CONFIG_ERROR;
if (test)
{
578
if(!config_value_changed(settingsfile, NETWORK_ENTRY, config, setting))
579
{
580
581
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
582
583
584
585
586
587
588
589
}
}
else
{
log_debug("No conffile. Creating.\n");
create_conf_file();
}
590
591
592
593
594
595
g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting);
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
596
597
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
598
free(keyfile);
599
return ret;
600
601
602
603
}
else
{
g_key_file_free(settingsfile);
604
return SET_CONFIG_ERROR;
605
606
}
}
607
608
char * get_network_setting(const char *config)
609
{
610
char * ret = 0;
611
struct mode_list_elem *data;
612
613
614
615
616
617
618
619
620
if(!strcmp(config, NETWORK_IP_KEY))
{
ret = get_network_ip();
if(!ret)
ret = strdup("192.168.2.15");
}
else if(!strcmp(config, NETWORK_INTERFACE_KEY))
{
621
622
623
624
625
626
627
628
629
/* check main configuration before using
the information from the specific mode */
ret = get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
from the mode config */
630
631
632
633
634
635
636
637
638
data = get_usb_mode_data();
if(data)
{
if(data->network_interface)
{
ret = strdup(data->network_interface);
goto end;
}
}
639
ret = strdup("usb0");
640
641
642
}
else if(!strcmp(config, NETWORK_GATEWAY_KEY))
return(get_network_gateway());
643
else if(!strcmp(config, NETWORK_NETMASK_KEY))
644
645
646
647
648
{
ret = get_network_netmask();
if(!ret)
ret = strdup("255.255.255.0");
}
649
650
else if(!strcmp(config, NETWORK_NAT_INTERFACE_KEY))
return(get_network_nat_interface());
651
652
653
else
/* no matching keys, return error */
return(NULL);
654
end:
655
656
657
return(ret);
}
658
659
660
661
662
663
664
665
666
667
668
669
/**
* Merge value from one keyfile to another
*
* Existing values will be overridden
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
* @param key value key to merge
*/
static void merge_key(GKeyFile *dest, GKeyFile *srce,
const char *grp, const char *key)
670
{
671
672
673
674
675
676
677
gchar *val = g_key_file_get_value(srce, grp, key, 0);
if( val ) {
log_debug("[%s] %s = %s", grp, key, val);
g_key_file_set_value(dest, grp, key, val);
g_free(val);
}
}
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* Merge group of values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
*/
static void merge_group(GKeyFile *dest, GKeyFile *srce,
const char *grp)
{
gchar **key = g_key_file_get_keys(srce, grp, 0, 0);
if( key ) {
for( size_t i = 0; key[i]; ++i )
merge_key(dest, srce, grp, key[i]);
g_strfreev(key);
}
}
696
697
698
699
700
701
702
703
704
705
/**
* Merge all groups and values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
*/
static void merge_file(GKeyFile *dest, GKeyFile *srce)
{
gchar **grp = g_key_file_get_groups(srce, 0);
706
707
708
709
710
if( grp ) {
for( size_t i = 0; grp[i]; ++i )
merge_group(dest, srce, grp[i]);
g_strfreev(grp);
711
}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
}
/**
* Callback function for logging errors within glob()
*
* @param path path to file/dir where error occurred
* @param err errno that occurred
*
* @return 0 (= do not stop glob)
*/
static int glob_error_cb(const char *path, int err)
{
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
}
/**
* Read *.ini files on CONFIG_FILE_DIR in the order of [0-9][A-Z][a-z]
*
* @return the in memory value-pair file.
*/
static GKeyFile *read_ini_files(void)
{
static const char pattern[] = CONFIG_FILE_DIR"/*.ini";
GKeyFile *ini = g_key_file_new();
glob_t gb;
memset(&gb, 0, sizeof gb);
if( glob(pattern, 0, glob_error_cb, &gb) != 0 ) {
log_debug("no configuration ini-files found");
g_key_file_free(ini);
ini = NULL;
goto exit;
747
}
748
749
750
751
752
753
754
755
756
757
758
759
760
761
for( size_t i = 0; i < gb.gl_pathc; ++i ) {
const char *path = gb.gl_pathv[i];
GError *err = 0;
GKeyFile *tmp = g_key_file_new();
if( !g_key_file_load_from_file(tmp, path, 0, &err) ) {
log_debug("%s: can't load: %s", path, err->message);
} else {
log_debug("processing %s ...", path);
merge_file(ini, tmp);
}
g_clear_error(&err);
g_key_file_free(tmp);
762
}
763
764
765
766
exit:
globfree(&gb);
return ini;
}
767
768
769
770
771
772
773
774
775
776
777
778
/**
* Read the *.ini files and create/overwrite FS_MOUNT_CONFIG_FILE with
* the merged data.
*
* @return 0 on failure
*/
int conf_file_merge(void)
{
GString *keyfile_string = NULL;
GKeyFile *settingsfile,*tempfile;
int ret = 0;
779
780
781
settingsfile = read_ini_files();
if (!settingsfile)
782
{
783
784
785
786
log_debug("No configuration. Creating defaults.");
create_conf_file();
/* There was no configuration so no info to be merged */
return ret;
787
}
788
789
790
791
792
793
794
tempfile = g_key_file_new();
if (g_key_file_load_from_file(tempfile, FS_MOUNT_CONFIG_FILE,
G_KEY_FILE_NONE,NULL)) {
if (!g_strcmp0(g_key_file_to_data(settingsfile, NULL, NULL),
g_key_file_to_data(tempfile, NULL, NULL)))
goto out;
795
}
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
log_debug("Merging configuration");
keyfile_string = g_string_new(NULL);
keyfile_string = g_string_append(keyfile_string,
g_key_file_to_data(settingsfile,
NULL, NULL));
if (keyfile_string) {
ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE,
keyfile_string->str,-1, NULL);
g_string_free(keyfile_string, TRUE);
}
out:
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
811
}
812
813
char * get_android_manufacturer(void)
814
{
815
816
817
818
819
820
821
822
823
824
825
#ifdef USE_MER_SSU
/* If SSU can provide manufacturer name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_manufacturer_name();
if( ssu_name )
{
return ssu_name;
}
#endif
return get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
826
}
827
828
char * get_android_vendor_id(void)
829
{
830
return(get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY));
831
832
}
833
char * get_android_product(void)
834
{
835
836
837
838
839
840
841
842
843
844
845
#ifdef USE_MER_SSU
/* If SSU can provide device model name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_product_name();
if( ssu_name )
{
return ssu_name;
}
#endif
return get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
846
847
}
848
char * get_android_product_id(void)
849
850
851
852
{
return(get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY));
}
853
854
855
856
char * get_hidden_modes(void)
{
return(get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY));
}
857
858
859
860
char * get_mode_whitelist(void)
{
return(get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY));
}
861
862
863
864
865
866
int check_android_section(void)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar **keys;
867
int ret = 1;
868
869
870
871
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
872
873
874
875
{
ret = 0;
goto cleanup;
}
876
877
878
keys = g_key_file_get_keys (settingsfile, ANDROID_ENTRY, NULL, NULL);
if(keys == NULL)
{
879
880
ret = 0;
goto cleanup;
881
882
}
883
884
g_strfreev(keys);
cleanup:
885
g_key_file_free(settingsfile);
886
return(ret);
887
888
}
889
890
891
892
int is_roaming_not_allowed(void)
{
return(get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY));
}