/
libconfigd.c
1122 lines (889 loc) · 29.2 KB
/
libconfigd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#include <semaphore.h>
#include "libconfigd.h"
#include "Logging.h"
#define GETCONFIGS_METHOD "palm://com.webos.service.config/getConfigs"
#define SETCONFIGS_METHOD "palm://com.webos.service.config/setConfigs"
#define RECONFIGS_METHOD "palm://com.webos.service.config/reconfigure"
static bool config_setLSHandle(LSHandle *lsHandle);
static LSHandle *config_getLSHandle();
static bool config_cbWatchConfigs(LSHandle *lsHandle, LSMessage *message, void *userData);
static void config_cbSetConfigs(LSHandle *lsHandle, LSMessage *message, void *userData);
static void config_cbReconfigs(LSHandle *lsHandle, LSMessage *message, void *userData);
static bool configd_getConfigsUnsafe(LSHandle *lsHandle, jvalue_ref queryObject, int32_t *errorCode);
static void config_cbReLoadConfigs(LSHandle *lsHandle, LSMessage *message, void *userData);
static void config_cbResultConfigs(LSHandle *lsHandle, LSMessage *message, void *userData);
static void config_evaluateMissingConfigsSafe();
/**
* usage example
* {
* CONFIG_COND_WAIT(&gcond, &gmutex)
* do something
* g_mutex_unlock(&gmutex);
* }
*/
#define CONFIG_COND_WAIT(cond, lock) \
g_mutex_lock(lock); \
while (configUpdateWaitingLsCount) \
{ \
g_cond_wait(cond, lock); \
}
/* __J_ARRAY is a valid jvalue_ref object of type array*/
#define C_ARRAY_TO_J_ARRAY(__J_ARRAY, __C_ARRAY) \
{ \
int32_t __i = 0; \
while(__C_ARRAY[__i] && strcmp(__C_ARRAY[__i], "")) \
{ \
jarray_append(__J_ARRAY, jstring_create(__C_ARRAY[__i])); \
++__i; \
} \
}
PmLogContext confdLibContext; // For Logging.h keep variable name as is
static jvalue_ref configQueryNames = NULL;
static jvalue_ref configCacheObj = NULL;
static jvalue_ref configMissingConfigs = NULL;
static GMutex configCacheLock;
static GCond configCacheCond;
static int32_t configUpdateWaitingLsCount = 0;
static bool configReLoadDone = false;
static GHashTable *configWatchers = NULL;
static LSHandle *configLSHandle = NULL;
static bool config_setLSHandle(LSHandle *lsHandle)
{
// Use internally store LSHandle
if (NULL != lsHandle)
configLSHandle = lsHandle;
return (NULL != configLSHandle);
}
static LSHandle *config_getLSHandle()
{
return configLSHandle;
}
static bool config_cbWatchConfigs(LSHandle *lsHandle, LSMessage *message, void *userData)
{
JSchemaInfo schemaInfo;
jvalue_ref replyObj = NULL;
jvalue_ref configsObj = NULL; // No ownership
jvalue_ref missingConfigsObj = NULL; // No ownership
jvalue_ref queryObject = (jvalue_ref) userData;
g_mutex_lock(&configCacheLock);
const char *method = LSMessageGetMethod(message);
if (!g_strcmp0(method, LUNABUS_ERROR_SERVICE_DOWN))
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "Service Disconnected.");
if (configd_getConfigsUnsafe(lsHandle, queryObject, NULL))
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "Service Re-connected.");
}
g_mutex_unlock(&configCacheLock);
return true;
}
jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
replyObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
if (jobject_get_exists(replyObj, J_CSTR_TO_BUF("configs"), &configsObj))
{
if (NULL == configCacheObj)
{
configCacheObj = jvalue_duplicate(configsObj);
}
else
{
//update the current cache with the new updated values
// adding one by one for subscription reply and multiple Config_loadConfigs support.
jobject_iter it;
jobject_key_value keyValPair;
jobject_iter_init(&it, configsObj);
while (jobject_iter_next(&it, &keyValPair))
{
jobject_put(configCacheObj, jvalue_copy(keyValPair.key), jvalue_copy(keyValPair.value));
}
}
}
// remove from cache if any config is been deleted.
if ((NULL != configCacheObj) && jobject_get_exists(replyObj, J_CSTR_TO_BUF("missingConfigs"), &missingConfigsObj))
{
raw_buffer missingConfigBuf;
int32_t index = 0;
for (index = 0; index < jarray_size(missingConfigsObj); index++)
{
missingConfigBuf = jstring_get(jarray_get(missingConfigsObj, index));
if (missingConfigBuf.m_str)
{
jobject_remove(configCacheObj, missingConfigBuf);
jstring_free_buffer(missingConfigBuf);
}
}
}
config_evaluateMissingConfigsSafe();
if (configUpdateWaitingLsCount)
{
--configUpdateWaitingLsCount;
}
// wake up all pending clients
g_cond_broadcast(&configCacheCond);
g_mutex_unlock(&configCacheLock);
// notify to all watchers
// To let callback functions to call config_getXXX(), it's important to unlock first.
if (configWatchers)
{
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, configWatchers);
while (g_hash_table_iter_next(&iter, &key, &value))
{
if (key)
{
configdNotify notify = (configdNotify)key;
notify(configsObj, value);
}
}
}
j_release(&replyObj);
return true;
}
static void config_cbSetConfigs(LSHandle *lsHandle, LSMessage *message, void *userData)
{
JSchemaInfo schemaInfo;
jvalue_ref replyObj = NULL;
jvalue_ref resultObj = NULL;
bool result = false;
jvalue_ref backupConfigObj = (jvalue_ref) userData;
g_mutex_lock(&configCacheLock);
jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
replyObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
if (jobject_get_exists(replyObj, J_CSTR_TO_BUF("returnValue"), &resultObj))
{
if (CONV_OK == jboolean_get(resultObj, &result))
{
result = true;
}
}
if (result)
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "setConfigs is requested successfully");
j_release(&backupConfigObj);
}
else
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "setConfigs request is failed");
if (!jis_null(backupConfigObj))
{
j_release(&configCacheObj);
configCacheObj = backupConfigObj;
}
}
j_release(&replyObj);
g_mutex_unlock(&configCacheLock);
}
static void config_cbReconfigs(LSHandle *lsHandle, LSMessage *message, void *userData)
{
JSchemaInfo schemaInfo;
jvalue_ref replyObj = NULL;
jvalue_ref resultObj = NULL;
bool result = false;
jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
replyObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
if (jobject_get_exists(replyObj, J_CSTR_TO_BUF("returnValue"), &resultObj))
{
if (CONV_OK == jboolean_get(resultObj, &result))
{
result = true;
}
}
if (result)
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "setConfigs is requested successfully");
}
else
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "setConfigs request is failed");
}
j_release(&replyObj);
}
//Prefetch the requested configs
bool config_loadConfigs(LSHandle *lsHandle, char *configNames[], int32_t *errorCode)
{
jvalue_ref jArrayConfigNames = NULL;
jvalue_ref queryObject = NULL;
bool retVal = false;
PmLogGetContext("libconfigd", &confdLibContext);
// sanity check for input arguments
if (!config_setLSHandle(lsHandle) || (NULL == configNames))
{
if (NULL != errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
g_mutex_lock(&configCacheLock);
assert(!configQueryNames);
queryObject = jobject_create();
if (jis_null(queryObject))
{
if (NULL != errorCode)
{
*errorCode = CONFIG_MEMORY_ALLOCATION_ERROR;
}
g_mutex_unlock(&configCacheLock);
return false;
}
jArrayConfigNames = jarray_create(NULL);
if (jis_null(jArrayConfigNames))
{
if (NULL != errorCode)
{
*errorCode = CONFIG_MEMORY_ALLOCATION_ERROR;
}
j_release(&jArrayConfigNames);
g_mutex_unlock(&configCacheLock);
return false;
}
C_ARRAY_TO_J_ARRAY(jArrayConfigNames, configNames);
jobject_put(queryObject, jstring_create("configNames"), jArrayConfigNames);
jobject_put(queryObject, jstring_create("subscribe"), jboolean_create(true));
if ((retVal = configd_getConfigsUnsafe(config_getLSHandle(), jvalue_duplicate(queryObject), errorCode)))
{
configQueryNames = jvalue_duplicate(jArrayConfigNames);
}
j_release(&queryObject);
g_mutex_unlock(&configCacheLock);
return retVal;
}
static bool configd_getConfigsUnsafe(LSHandle *lsHandle, jvalue_ref queryObject, int32_t *errorCode)
{
if (LSCall(lsHandle, GETCONFIGS_METHOD, jvalue_tostring_simple(queryObject), config_cbWatchConfigs, queryObject, NULL,
NULL))
{
configUpdateWaitingLsCount++;
if (!LSCall(config_getLSHandle(), "palm://com.palm.bus/signal/addmatch",
"{\"category\":\"/com/webos/config\", \"method\":\"reloadDone\"}",
config_cbReLoadConfigs, NULL, NULL, NULL))
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "Failed to Register signal callback");
}
if (NULL != errorCode)
{
*errorCode = CONFIG_NO_ERROR;
}
return true;
}
if (NULL != errorCode)
{
*errorCode = CONFIG_LSCALL_FAILURE;
}
j_release(&queryObject);
return false;
}
static void config_cbReLoadConfigs(LSHandle *lsHandle, LSMessage *message, void *userData)
{
JSchemaInfo schemaInfo;
jvalue_ref replyObj = NULL;
jvalue_ref resultObj = NULL;
bool result = true;
jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
replyObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
// Ignore basic reponse from bus system.
// luna-bus return with payload {"returnValue":true} for first registeration
if (jobject_get_exists(replyObj, J_CSTR_TO_BUF("returnValue"), &resultObj))
{
result = false;
}
j_release(&replyObj);
if (!result) return;
g_mutex_lock(&configCacheLock);
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "Re-Load Configs Done");
configReLoadDone = true;
// wake up all pending clients
g_cond_broadcast(&configCacheCond);
g_mutex_unlock(&configCacheLock);
}
static void config_cbResultConfigs(LSHandle *lsHandle, LSMessage *message, void *userData)
{
JSchemaInfo schemaInfo;
jvalue_ref replyObj = NULL;
jvalue_ref configsObj = NULL; // No ownership
jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
replyObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
if (jobject_get_exists(replyObj, J_CSTR_TO_BUF("configs"), &configsObj))
{
configdNotify cbFunc = (configdNotify)userData;
cbFunc(jvalue_duplicate(configsObj), NULL);
}
j_release(&replyObj);
}
void config_addWatch(configdNotify func, void *watch_context)
{
g_mutex_lock(&configCacheLock);
if (!configWatchers)
{
configWatchers = g_hash_table_new(g_direct_hash, g_direct_equal);
}
g_hash_table_insert(configWatchers, func, watch_context);
g_mutex_unlock(&configCacheLock);
}
bool config_removeWatch(configdNotify func)
{
bool retVal = false;
g_mutex_lock(&configCacheLock);
if (configWatchers)
{
retVal = g_hash_table_remove(configWatchers, func);
}
g_mutex_unlock(&configCacheLock);
return retVal;
}
jvalue_ref config_getReqConfigNamesArray()
{
jvalue_ref namesArray = jnull();
g_mutex_lock(&configCacheLock);
if (configQueryNames && jis_valid(configQueryNames) && jis_array(configQueryNames))
{
namesArray = jvalue_duplicate(configQueryNames);
}
g_mutex_unlock(&configCacheLock);
return namesArray;
}
jvalue_ref config_getAllConfigs()
{
jvalue_ref allConfigs = jnull();
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (configCacheObj && jis_valid(configCacheObj) && jis_object(configCacheObj))
{
allConfigs = jvalue_duplicate(configCacheObj);
}
g_mutex_unlock(&configCacheLock);
return allConfigs;
}
jvalue_ref config_getMissingConfigs()
{
jvalue_ref missingConfigs = jnull();
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (jis_valid(configMissingConfigs) && jis_array(configMissingConfigs))
{
missingConfigs = jvalue_duplicate(configMissingConfigs);
}
g_mutex_unlock(&configCacheLock);
return missingConfigs;
}
static void config_evaluateMissingConfigsSafe()
{
if (!jis_null(configQueryNames))
{
if (!jis_null(configMissingConfigs))
{
j_release(&configMissingConfigs);
configMissingConfigs = jnull();
}
if (!jis_null(configCacheObj))
{
jvalue_ref missingFromRequested = jarray_create(NULL);
for (int index = 0; index < jarray_size(configQueryNames); index++)
{
jvalue_ref configName = jarray_get(configQueryNames, index);
raw_buffer configNameBuf = jstring_get_fast(configName);
if (jis_string(configName)
&& configNameBuf.m_str
&& configNameBuf.m_str[configNameBuf.m_len - 1] != CONFIG_WILDCARD)
{
if (!jobject_containskey(configCacheObj, configNameBuf))
{
jarray_append(missingFromRequested, jvalue_copy(configName));
}
}
}
if (jarray_size(missingFromRequested) > 0)
{
configMissingConfigs = missingFromRequested;
}
else
{
j_release(&missingFromRequested);
}
}
else
{ // first getConfigs reply is not received
configMissingConfigs = jvalue_duplicate(configQueryNames);
}
}
}
bool config_waitConfigs(int64_t timeoutMilli)
{
assert(timeoutMilli>0);
assert(jarray_size(configQueryNames)>0);
gint64 tsBegin = g_get_monotonic_time();
gint64 tsEnd = tsBegin + timeoutMilli * G_TIME_SPAN_MILLISECOND;
bool retVal = true;
g_mutex_lock(&configCacheLock);
config_evaluateMissingConfigsSafe();
while (configUpdateWaitingLsCount
|| ( !jis_null(configMissingConfigs) && jarray_size(configMissingConfigs) > 0))
{
if (!g_cond_wait_until(&configCacheCond, &configCacheLock, tsEnd))
{ // timeout expired
retVal = false;
break;
}
}
g_mutex_unlock(&configCacheLock);
return retVal;
}
bool config_waitReLoadConfigs(int64_t timeoutMilli)
{
assert(timeoutMilli>0);
assert(jarray_size(configQueryNames)>0);
gint64 tsBegin = g_get_monotonic_time();
gint64 tsEnd = tsBegin + timeoutMilli * G_TIME_SPAN_MILLISECOND;
bool retVal = true;
g_mutex_lock(&configCacheLock);
while (!configReLoadDone)
{
if (!g_cond_wait_until(&configCacheCond, &configCacheLock, tsEnd))
{ // timeout expired
retVal = false;
break;
}
}
g_mutex_unlock(&configCacheLock);
return retVal;
}
bool config_getBoolean(raw_buffer configNameBuf, int32_t *pData, int32_t *errorCode)
{
bool bConfigVal = false;
jvalue_ref valObject = NULL;
if (!configNameBuf.m_str || !pData)
{
if (errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (!configCacheObj)
{
if (errorCode)
{
//Config_loadConfig not called
*errorCode = CONFIG_MODULE_NOT_INITIALIZED;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (!(jobject_get_exists(configCacheObj, configNameBuf, &valObject)))
{
if (errorCode)
{
*errorCode = CONFIG_VALUE_NOT_FOUND;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (CONV_NOT_A_BOOLEAN == jboolean_get(valObject, &bConfigVal))
{
if (errorCode)
{
*errorCode = CONFIG_DATA_TYPE_MISMATCH;
}
g_mutex_unlock(&configCacheLock);
return false;
}
g_mutex_unlock(&configCacheLock);
//Assuming jboolen_get will return actual config value
*pData = bConfigVal;
if (errorCode)
{
*errorCode = CONFIG_NO_ERROR;
}
return true;
}
bool config_getInteger(raw_buffer configNameBuf, int32_t *pData, int32_t *errorCode)
{
jvalue_ref valObject = NULL;
if (!configNameBuf.m_str || !pData)
{
if (errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (!configCacheObj)
{
if (errorCode)
{
*errorCode = CONFIG_MODULE_NOT_INITIALIZED;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (!(jobject_get_exists(configCacheObj, configNameBuf, &valObject)))
{
if (errorCode)
{
*errorCode = CONFIG_VALUE_NOT_FOUND;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (CONV_OK != jnumber_get_i32(valObject, pData))
{
if (errorCode)
{
*errorCode = CONFIG_DATA_TYPE_MISMATCH;
}
g_mutex_unlock(&configCacheLock);
return false;
}
g_mutex_unlock(&configCacheLock);
if (errorCode)
{
*errorCode = CONFIG_NO_ERROR;
}
return true;
}
bool config_getStringDup(raw_buffer configNameBuf, raw_buffer *pData, int32_t *errorCode)
{
jvalue_ref valObject = NULL;
if (!configNameBuf.m_str || !pData)
{
if (errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (!configCacheObj)
{
if (errorCode)
{
*errorCode = CONFIG_MODULE_NOT_INITIALIZED;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (!(jobject_get_exists(configCacheObj, configNameBuf, &valObject)))
{
if (errorCode)
{
*errorCode = CONFIG_VALUE_NOT_FOUND;
}
g_mutex_unlock(&configCacheLock);
return false;
}
*pData = jstring_get(valObject);
g_mutex_unlock(&configCacheLock);
if (NULL == pData->m_str)
{
if (errorCode)
{
*errorCode = CONFIG_VALUE_NOT_FOUND;
}
return false;
}
if (errorCode)
{
*errorCode = CONFIG_NO_ERROR;
}
return true;
}
bool config_getJsonObject(raw_buffer configNameBuf, jvalue_ref *pData, int32_t *errorCode)
{
jvalue_ref valObject = NULL;
if (!configNameBuf.m_str || !pData)
{
if (errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
if (!configCacheObj)
{
if (errorCode)
{
*errorCode = CONFIG_MODULE_NOT_INITIALIZED;
}
g_mutex_unlock(&configCacheLock);
return false;
}
if (!(jobject_get_exists(configCacheObj, configNameBuf, &valObject)))
{
if (errorCode)
{
*errorCode = CONFIG_VALUE_NOT_FOUND;
}
g_mutex_unlock(&configCacheLock);
return false;
}
*pData = jvalue_duplicate(valObject);
g_mutex_unlock(&configCacheLock);
if (errorCode)
{
*errorCode = CONFIG_NO_ERROR;
}
return true;
}
bool config_setConfigs(jvalue_ref configsObj, int32_t *errorCode)
{
LSHandle *lsHandle = config_getLSHandle();
if ((NULL == lsHandle) || !jis_valid(configsObj))
{
*errorCode = CONFIG_INVALID_LSHANDLER;
return false;
}
if (NULL != errorCode)
*errorCode = CONFIG_NO_ERROR;
// update configs cache to provide new value immediately
jobject_iter it;
jobject_key_value keyValPair;
jvalue_ref configsValObj = NULL;
if (jobject_get_exists(configsObj, J_CSTR_TO_BUF("configs"), &configsValObj))
{
if (jobject_size(configsValObj)) {
jobject_iter_init(&it, configsValObj);
} else {
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "%s: request with empty configs", __FUNCTION__);
return true;
}
} else {
if (NULL != errorCode)
*errorCode = CONFIG_INVALID_ARGUMENTS;
return false;
}
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
// Restore old configCacheObj in case error
// free it on callback or error case
jvalue_ref backupConfigObj = NULL;
if (!jis_null(configCacheObj))
{
LOG_LIB_INFO_PAIRS(MSGID_LIBCONFIGD, 0, "%s: Copy configCacheObj for backup", __FUNCTION__);
backupConfigObj = jvalue_duplicate(configCacheObj);
}
while (jobject_iter_next(&it, &keyValPair))
{
if (jis_null(keyValPair.value)) continue;
if (jobject_containskey2(configCacheObj, keyValPair.key))
{
raw_buffer jKeyBuf = jstring_get(keyValPair.key);
jobject_remove(configCacheObj, jKeyBuf);
}
jobject_put(configCacheObj, jvalue_copy(keyValPair.key), jvalue_copy(keyValPair.value));
}
if (!LSCallOneReply(lsHandle, SETCONFIGS_METHOD, jvalue_tostring_simple(configsObj),
config_cbSetConfigs, backupConfigObj, NULL, NULL))
{
if (NULL != errorCode)
*errorCode = CONFIG_LSCALL_FAILURE;
j_release(&configCacheObj);
configCacheObj = backupConfigObj;
g_mutex_unlock(&configCacheLock);
return false;
}
g_mutex_unlock(&configCacheLock);
return true;
}
bool config_getConfigs(char *configNames[], void *cbFunc, int32_t *errorCode)
{
jvalue_ref jArrayConfigNames = NULL;
jvalue_ref queryObject = NULL;
bool retVal = false;
LSHandle *lsHandle = config_getLSHandle();
if (NULL == lsHandle)
{
if (NULL != errorCode)
{
*errorCode = CONFIG_INVALID_LSHANDLER;
}
return false;
}
if (NULL == configNames)
{
if (NULL != errorCode)
{
*errorCode = CONFIG_INVALID_ARGUMENTS;
}
return false;
}
if (NULL != errorCode)
*errorCode = CONFIG_NO_ERROR;
CONFIG_COND_WAIT(&configCacheCond, &configCacheLock);
queryObject = jobject_create();
jArrayConfigNames = jarray_create(NULL);
if (jis_null(queryObject) || jis_null(jArrayConfigNames))
{
if (NULL != errorCode)
{
*errorCode = CONFIG_MEMORY_ALLOCATION_ERROR;
}
g_mutex_unlock(&configCacheLock);
j_release(&queryObject);
j_release(&jArrayConfigNames);
return false;
}
C_ARRAY_TO_J_ARRAY(jArrayConfigNames, configNames);
jobject_put(queryObject, jstring_create("configNames"), jArrayConfigNames);
retVal = LSCallOneReply(lsHandle, GETCONFIGS_METHOD, jvalue_tostring_simple(queryObject),
config_cbResultConfigs, cbFunc, NULL, NULL);
if (!retVal)
{
if (NULL != errorCode)
{
*errorCode = CONFIG_LSCALL_FAILURE;
}
}
j_release(&queryObject);
j_release(&jArrayConfigNames);
g_mutex_unlock(&configCacheLock);
return retVal;
}
bool config_reconfigs(uint32_t timeout, int32_t *errorCode)
{
LSHandle *lsHandle = config_getLSHandle();
if (NULL == lsHandle)
{
*errorCode = CONFIG_INVALID_LSHANDLER;
return false;
}
if (NULL != errorCode)
*errorCode = CONFIG_NO_ERROR;
jvalue_ref optObject = jobject_create();
jobject_put(optObject, jstring_create("timeout"), jnumber_create_i32(timeout));
if (!LSCallOneReply(lsHandle, RECONFIGS_METHOD, jvalue_tostring_simple(optObject),
config_cbReconfigs, NULL, NULL, NULL))
{
if (NULL != errorCode)
*errorCode = CONFIG_LSCALL_FAILURE;
j_release(&optObject);
return false;
}
j_release(&optObject);
return true;
}
bool config_loadConfigsFromFile(char *path, char *fileName, int32_t *errorCode)
{
JSchemaInfo schemaInfo;
jvalue_ref parseObj = NULL;
char *category = NULL;
int categoryLength = 0;
if (!path || !fileName)
{
if (NULL != errorCode)
*errorCode = CONFIG_INVALID_ARGUMENTS;
return false;
}
char *fullPath = g_strconcat(path, "/", fileName, NULL);
if (!fullPath) {
// CID 9026655, 9158810 - handle null pointer dereference