forked from alliedtelesis/apteryx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apteryx.c
2307 lines (2064 loc) · 58.8 KB
/
apteryx.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
/**
* @file apteryx.c
* API for configuration and state shared between Apteryx processes.
* Features:
* - A simple path:value database.
* - Tree like structure with each node being a value.
* - Path specified in directory format (e.g. /root/node1/node2).
* - Searching for nodes children requires substring search of the path.
*
* Copyright 2014, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <poll.h>
#include <semaphore.h>
#include <errno.h>
#include "internal.h"
#include "apteryx.h"
#include <glib.h>
/* Flag the destructor so things get tidied up when the shared library is unloaded */
bool apteryx_halt (void) __attribute__((destructor));
/* Configuration */
bool apteryx_debug = false; /* Debug enabled */
static const char *default_url = APTERYX_SERVER; /* Default path to Apteryx database */
static int ref_count = 0; /* Library reference count */
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; /* Protect globals */
static rpc_instance rpc = NULL; /* RPC Service */
static bool bound = false; /* Do we have a listen socket open */
static bool have_callbacks = false; /* Have we ever registered any callbacks */
/* Callback */
typedef struct _cb_t
{
uint64_t ref;
const char *path;
bool value;
void *fn;
void *data;
uint32_t flags;
guint timeout_ms;
} cb_t;
static uint64_t next_ref = 0;
static GList *cb_list = NULL;
static bool
find_callback (uint64_t ref, cb_t *r_cb)
{
bool rc = false;
GList *iter;
cb_t *cb;
pthread_mutex_lock (&lock);
for (iter = g_list_first (cb_list); iter; iter = g_list_next (iter))
{
cb = (cb_t *) iter->data;
if (cb->ref == ref)
{
*r_cb = *cb;
rc = true;
}
}
pthread_mutex_unlock (&lock);
return rc;
}
static void *
call_callback (uint64_t ref, const char *path, const char *value)
{
cb_t cb;
if (!find_callback (ref, &cb) || cb.fn == NULL)
{
DEBUG ("CB[%"PRIu64"]: not found\n", ref);
return NULL;
}
if (cb.value && cb.data)
return ((void*(*)(const char*, const char*, void*)) cb.fn) (path, value, cb.data);
else if (cb.value)
return ((void*(*)(const char*, const char*)) cb.fn) (path, value);
else if (cb.data)
return ((void*(*)(const char*, void*)) cb.fn) (path, cb.data);
else
return ((void*(*)(const char*)) cb.fn) (path);
}
/**
* 32 Bit systems won't return uint64_t correctly via a callback cast to void *
*/
static uint64_t
call_uint64_callback (uint64_t ref, const char *path, const char *value)
{
cb_t cb;
if (!find_callback (ref, &cb) || cb.fn == NULL)
{
DEBUG ("CB[%"PRIu64"]: not found\n", ref);
return 0;
}
if (cb.value && cb.data)
return ((uint64_t(*)(const char*, const char*, void*)) cb.fn) (path, value, cb.data);
else if (cb.value)
return ((uint64_t(*)(const char*, const char*)) cb.fn) (path, value);
else if (cb.data)
return ((uint64_t(*)(const char*, void*)) cb.fn) (path, cb.data);
else
return ((uint64_t(*)(const char*)) cb.fn) (path);
}
static const char *
validate_path (const char *path, char **url)
{
/* Database path or none at all */
if (path && path[0] == '/')
{
/* Use the default URL */
if (url)
*url = strdup(default_url);
return path;
}
/* Check for a full URL */
else if (path &&
(strncmp (path, "unix://", 7) == 0 ||
strncmp (path, "tcp://", 6) == 0))
{
if (url)
*url = strdup (path);
char *tmp = strstr (path + 6, ":/");
if (!tmp)
{
ERROR ("Invalid path (%s)!\n", path);
return NULL;
}
path = tmp + 1;
if (url)
{
tmp = strstr (*url + 6, ":/");
if (tmp != NULL)
{
tmp[0] = '\0';
}
}
return path;
}
else if (path)
{
ERROR ("Invalid path (%s)!\n", path);
}
return NULL;
}
static bool
handle_index (rpc_message msg)
{
GList *results = NULL;
uint64_t ref;
const char *path;
GList *iter = NULL;
int i;
/* Parse the parameters */
ref = rpc_msg_decode_uint64 (msg);
path = rpc_msg_decode_string (msg);
assert (path);
DEBUG ("INDEX CB: \"%s\" (0x%"PRIx64")\n", path, ref);
/* Call the callback */
results = (GList *) call_callback (ref, path, NULL);
/* Return result */
rpc_msg_reset (msg);
for (i = 0, iter = results; iter; iter = g_list_next (iter), i++)
{
DEBUG (" = %s\n", (char *) iter->data);
rpc_msg_encode_string (msg, (char *)iter->data);
}
g_list_free_full (results, g_free);
return true;
}
struct delayed_watch {
gpointer handle;
uint64_t ref;
void *fn;
GNode *root;
void *data;
guint timeout_ms;
};
static GList *dw_list = NULL;
static gboolean
dw_process (gpointer arg1)
{
struct delayed_watch *dw = (struct delayed_watch *) arg1;
dw_list = g_list_remove (dw_list, dw);
DEBUG ("WATCH-TREE(delayed) CB (0x%"PRIx64")\n", dw->ref);
DEBUG_TREE (dw->root);
if (dw->data)
((void*(*)(const GNode*, void*)) dw->fn) (dw->root, dw->data);
else
((void*(*)(const GNode*)) dw->fn) (dw->root);
g_free (dw);
return G_SOURCE_REMOVE;
}
static void
dw_add (uint64_t ref, void *fn, GNode *root, void *data, guint timeout_ms)
{
struct delayed_watch *dw = NULL;
/* Find existing delayed watch */
for (GList *iter = dw_list; iter; iter = g_list_next (iter))
{
dw = (struct delayed_watch *) iter->data;
if (dw->ref == ref && dw->fn == fn && dw->data == data && dw->timeout_ms == timeout_ms)
break;
dw = NULL;
}
/* If we found a match lets merge in the new data and restart the timer */
if (dw)
{
dw->root = apteryx_merge_tree (dw->root, root);
rpc_restart_callback (rpc, dw->handle, timeout_ms);
}
else
{
dw = (struct delayed_watch *) g_malloc0 (sizeof (struct delayed_watch));
dw->ref = ref;
dw->fn = fn;
dw->root = root;
dw->data = data;
dw->timeout_ms = timeout_ms;
dw_list = g_list_append (dw_list, dw);
dw->handle = rpc_add_callback (rpc, dw_process, (gpointer) dw, timeout_ms);
}
}
static bool
handle_watch (rpc_message msg)
{
cb_t cb;
uint64_t ref;
const char *path;
const char *value;
ref = rpc_msg_decode_uint64 (msg);
if (!find_callback (ref, &cb) || cb.fn == NULL)
{
DEBUG ("WATCH[%"PRIu64"]: cb not found\n", ref);
/* Not much we can do but pretend we completed the callback */
rpc_msg_reset (msg);
return true;
}
if (cb.flags == 0)
{
path = rpc_msg_decode_string (msg);
value = rpc_msg_decode_string (msg);
while (path && value)
{
DEBUG ("WATCH CB \"%s\" = \"%s\" (0x%"PRIx64")\n", path, value, ref);
if (value[0] == '\0')
value = NULL;
if (cb.data)
((void*(*)(const char*, const char*, void*)) cb.fn) (path, value, cb.data);
else
((void*(*)(const char*, const char*)) cb.fn) (path, value);
path = rpc_msg_decode_string (msg);
value = rpc_msg_decode_string (msg);
}
}
else
{
GNode *root = g_node_new (strdup ("/"));
path = rpc_msg_decode_string (msg);
while (path)
{
value = rpc_msg_decode_string (msg);
apteryx_path_to_node (root, path, value);
path = rpc_msg_decode_string (msg);
}
/* Delay the callback if requested to */
if (cb.timeout_ms)
{
/* Delay the watch but pretend we have already done it */
dw_add (ref, cb.fn, root, cb.data, cb.timeout_ms);
}
else
{
DEBUG ("WATCH-TREE CB (0x%"PRIx64")\n", ref);
DEBUG_TREE (root);
if (cb.data)
((void*(*)(const GNode*, void*)) cb.fn) (root, cb.data);
else
((void*(*)(const GNode*)) cb.fn) (root);
}
}
rpc_msg_reset (msg);
return true;
}
static bool
handle_validate (rpc_message msg)
{
uint32_t result = 0;
uint64_t ref;
const char *path;
const char *value;
/* Parse the parameters */
ref = rpc_msg_decode_uint64 (msg);
path = rpc_msg_decode_string (msg);
value = rpc_msg_decode_string (msg);
assert (path && value);
if (value && (value[0] == '\0'))
value = NULL;
DEBUG ("VALIDATE CB \"%s\" = \"%s\" (0x%"PRIx64")\n", path, value, ref);
/* Process callback */
result = (uint32_t) (size_t) call_callback (ref, path, value);
DEBUG (" = %d\n", result);
rpc_msg_reset (msg);
rpc_msg_encode_uint64 (msg, result);
return true;
}
static bool
handle_refresh (rpc_message msg)
{
uint64_t ref;
const char *path;
uint64_t timeout;
/* Parse the parameters */
ref = rpc_msg_decode_uint64 (msg);
path = rpc_msg_decode_string (msg);
assert (path);
DEBUG ("REFRESH CB: \"%s\" (0x%"PRIx64")\n", path, ref);
/* Process callback */
timeout = call_uint64_callback (ref, path, NULL);
rpc_msg_reset (msg);
rpc_msg_encode_uint64 (msg, timeout);
return true;
}
static bool
handle_provide (rpc_message msg)
{
char *value;
uint64_t ref;
const char *path;
/* Parse the parameters */
ref = rpc_msg_decode_uint64 (msg);
path = rpc_msg_decode_string (msg);
assert (path);
DEBUG ("PROVIDE CB: \"%s\" (0x%"PRIx64")\n", path, ref);
/* Process callback */
value = (char *) call_callback (ref, path, NULL);
rpc_msg_reset (msg);
if (value)
{
rpc_msg_encode_string (msg, value);
free (value);
}
return true;
}
static bool
msg_handler (rpc_message msg)
{
APTERYX_MODE mode = rpc_msg_decode_uint8 (msg);
/* If the client has indicated they are done then
don't process any more messages */
if (ref_count <= 0)
{
DEBUG ("MSG: Message after shutdown (%d)\n", mode);
return false;
}
switch (mode)
{
case MODE_INDEX:
return handle_index (msg);
case MODE_WATCH:
case MODE_WATCH_WITH_ACK:
return handle_watch (msg);
case MODE_VALIDATE:
return handle_validate (msg);
case MODE_REFRESH:
return handle_refresh (msg);
case MODE_PROVIDE:
return handle_provide (msg);
default:
DEBUG ("MSG: Unexpected mode %d\n", mode);
break;
}
return false;
}
bool
apteryx_init (bool debug_enabled)
{
/* Increment refcount */
pthread_mutex_lock (&lock);
ref_count++;
apteryx_debug |= debug_enabled;
if (ref_count == 1)
{
char * uri = NULL;
/* Create RPC instance */
rpc = rpc_init (RPC_CLIENT_TIMEOUT_US, false, msg_handler);
if (rpc == NULL)
{
ERROR ("Init: Failed to initialise RPC service\n");
ref_count--;
pthread_mutex_unlock (&lock);
return false;
}
/* Only need to bind if we have previously added callbacks */
if (have_callbacks)
{
/* Bind to the default uri for this client */
if (asprintf ((char **) &uri, APTERYX_CLIENT, getns (), (uint64_t) getpid ()) <= 0
|| !rpc_server_bind (rpc, uri, uri))
{
ERROR ("Failed to bind uri %s\n", uri);
ref_count--;
pthread_mutex_unlock (&lock);
free ((void*) uri);
return false;
}
DEBUG ("Bound to uri %s\n", uri);
bound = true;
free ((void*) uri);
}
}
pthread_mutex_unlock (&lock);
/* Ready to go */
if (ref_count == 1)
DEBUG ("Init: Initialised\n");
return true;
}
bool
apteryx_shutdown (void)
{
if (ref_count <= 0)
{
DEBUG ("SHUTDOWN: Not initialised\n");
return false;
}
/* Decrement ref count */
pthread_mutex_lock (&lock);
ref_count--;
pthread_mutex_unlock (&lock);
/* Check if there are still other users */
if (ref_count > 0)
{
DEBUG ("SHUTDOWN: More users (refcount=%d)\n", ref_count);
return true;
}
/* Shutdown */
DEBUG ("SHUTDOWN: Shutting down\n");
rpc_shutdown (rpc);
rpc = NULL;
bound = false;
DEBUG ("SHUTDOWN: Shutdown\n");
return true;
}
bool
apteryx_shutdown_force (void)
{
DEBUG ("SHUTDOWN: (Forced)\n");
while (ref_count > 0)
apteryx_shutdown ();
return true;
}
bool
apteryx_halt (void)
{
DEBUG ("SHUTDOWN: stopping threads prior to process exit\n");
rpc_halt (rpc);
return true;
}
int
apteryx_process (bool poll)
{
ASSERT ((ref_count > 0), return false, "PROCESS: Not initialised\n");
return rpc_server_process (rpc, poll);
}
bool
apteryx_bind (const char *url)
{
char path[PATH_MAX];
bool result;
ASSERT ((ref_count > 0), return false, "BIND: Not initialised\n");
ASSERT (url, return false, "BIND: Invalid parameters\n");
DEBUG ("BIND: %s\n", url);
if (sprintf (path, APTERYX_SOCKETS_PATH"/%zX",
(size_t)g_str_hash (url)) <= 0)
return false;
result = apteryx_set (path, url);
usleep (1000); /* Sockets need time to bind/unbind */
return result;
}
bool
apteryx_unbind (const char *url)
{
char path[PATH_MAX];
ASSERT ((ref_count > 0), return false, "UNBIND: Not initialised\n");
ASSERT (url, return false, "UNBIND: Invalid parameters\n");
DEBUG ("UNBIND: %s\n", url);
if (sprintf (path, APTERYX_SOCKETS_PATH"/%zX",
(size_t)g_str_hash (url)) <= 0)
return false;
return apteryx_set (path, NULL);
}
bool
apteryx_prune (const char *path)
{
char *url = NULL;
rpc_client rpc_client;
rpc_message_t msg = {};
int32_t result;
ASSERT ((ref_count > 0), return false, "PRUNE: Not initialised\n");
ASSERT (path, return false, "PRUNE: Invalid parameters\n");
DEBUG ("PRUNE: %s\n", path);
/* Check path */
path = validate_path (path, &url);
if (!path)
{
ERROR ("PRUNE: invalid path!\n");
assert (!apteryx_debug || path);
return false;
}
/* IPC */
rpc_client = rpc_client_connect (rpc, url);
if (!rpc_client)
{
ERROR ("PRUNE: Path(%s) Failed to connect to server: %s\n", path, strerror (errno));
free (url);
return false;
}
rpc_msg_encode_uint8 (&msg, MODE_PRUNE);
rpc_msg_encode_string (&msg, path);
if (!rpc_msg_send (rpc_client, &msg))
{
ERROR ("PRUNE: No response Path(%s)\n", path);
rpc_msg_reset (&msg);
rpc_client_release (rpc, rpc_client, false);
free (url);
return false;
}
result = rpc_msg_decode_uint64 (&msg);
rpc_msg_reset (&msg);
if (result < 0)
{
DEBUG ("PRUNE: Error response: %s\n", strerror (-result));
errno = result;
}
rpc_client_release (rpc, rpc_client, true);
free (url);
/* Success */
return result == 0;
}
bool
apteryx_dump (const char *path, FILE *fp)
{
char *value = NULL;
ASSERT ((ref_count > 0), return false, "DUMP: Not initialised\n");
ASSERT (path, return false, "DUMP: Invalid parameters\n");
ASSERT (fp, return false, "DUMP: Invalid parameters\n");
DEBUG ("DUMP: %s\n", path);
/* Check initialised */
if (ref_count <= 0)
{
ERROR ("DUMP: not initialised!\n");
assert(ref_count > 0);
return false;
}
if (strlen (path) > 0 && (value = apteryx_get (path)))
{
fprintf (fp, "%-64s\t%s\n", path, value);
free (value);
}
char *_path = NULL;
int len = asprintf (&_path, "%s/", path);
if (len >= 0)
{
GList *children, *iter;
children = apteryx_search (_path);
for (iter = children; iter; iter = g_list_next (iter))
{
apteryx_dump ((const char *) iter->data, fp);
}
g_list_free_full (children, free);
free (_path);
}
return true;
}
bool
apteryx_set_full (const char *path, const char *value, uint64_t ts, bool ack)
{
char *url = NULL;
rpc_client rpc_client;
rpc_message_t msg = {};
int result = -ETIMEDOUT;
ASSERT ((ref_count > 0), return false, "SET: Not initialised\n");
ASSERT (path, return false, "SET: Invalid parameters\n");
DEBUG ("SET: %s = %s\n", path, value);
/* Check path */
path = validate_path (path, &url);
if (!path || path[strlen(path) - 1] == '/')
{
ERROR ("SET: invalid path (%s)!\n", path ?: "NULL");
free (url);
assert (!apteryx_debug || path);
return false;
}
/* IPC */
rpc_client = rpc_client_connect (rpc, url);
if (!rpc_client)
{
ERROR ("SET: Path(%s) Failed to connect to server: %s\n", path, strerror (errno));
free (url);
return false;
}
rpc_msg_encode_uint8 (&msg, ack ? MODE_SET_WITH_ACK : MODE_SET);
rpc_msg_encode_uint64 (&msg, ts);
rpc_msg_encode_uint8 (&msg, rpc_value);
rpc_msg_encode_string (&msg, path);
if (value)
rpc_msg_encode_string (&msg, value);
else
rpc_msg_encode_string (&msg, "");
if (!rpc_msg_send (rpc_client, &msg))
{
ERROR ("SET: No response Path(%s)\n", path);
rpc_msg_reset (&msg);
rpc_client_release (rpc, rpc_client, false);
free (url);
return false;
}
result = rpc_msg_decode_uint64 (&msg);
rpc_msg_reset (&msg);
if (result < 0)
{
DEBUG ("SET: Error response: %s\n", strerror (-result));
errno = result;
}
rpc_client_release (rpc, rpc_client, true);
free (url);
/* Success */
return result == 0;
}
bool
apteryx_cas_string (const char *path, const char *key, const char *value, uint64_t ts)
{
char *full_path;
size_t len;
bool res = false;
/* Create full path */
if (key)
len = asprintf (&full_path, "%s/%s", path, key);
else
len = asprintf (&full_path, "%s", path);
if (len)
{
res = apteryx_cas (full_path, value, ts);
free (full_path);
}
return res;
}
bool
apteryx_set_string (const char *path, const char *key, const char *value)
{
return apteryx_cas_string (path, key, value, UINT64_MAX);
}
bool
apteryx_cas_int (const char *path, const char *key, int32_t value, uint64_t ts)
{
char *full_path;
size_t len;
char *v;
bool res = false;
/* Create full path */
if (key)
len = asprintf (&full_path, "%s/%s", path, key);
else
len = asprintf (&full_path, "%s", path);
if (len)
{
/* Store as a string at the moment */
len = asprintf ((char **) &v, "%d", value);
if (len)
{
res = apteryx_cas (full_path, v, ts);
free ((void *) v);
}
free (full_path);
}
return res;
}
bool
apteryx_set_int (const char *path, const char *key, int32_t value)
{
return apteryx_cas_int (path, key, value, UINT64_MAX);
}
char *
apteryx_get (const char *path)
{
char *url = NULL;
char *value = NULL;
rpc_client rpc_client;
rpc_message_t msg = {};
ASSERT ((ref_count > 0), return NULL, "GET: Not initialised\n");
ASSERT (path, return NULL, "GET: Invalid parameters\n");
DEBUG ("GET: %s\n", path);
/* Check path */
path = validate_path (path, &url);
if (!path || path[strlen(path)-1] == '/')
{
ERROR ("GET: invalid path (%s)!\n", path ?: "NULL");
free (url);
assert (!apteryx_debug || path);
return NULL;
}
/* IPC */
rpc_client = rpc_client_connect (rpc, url);
if (!rpc_client)
{
ERROR ("GET: Path(%s) Failed to connect to server: %s\n", path, strerror (errno));
free (url);
return NULL;
}
rpc_msg_encode_uint8 (&msg, MODE_GET);
rpc_msg_encode_string (&msg, path);
if (!rpc_msg_send (rpc_client, &msg))
{
ERROR ("GET: No response Path(%s)\n", path);
rpc_msg_reset (&msg);
rpc_client_release (rpc, rpc_client, false);
free (url);
return NULL;
}
value = rpc_msg_decode_string (&msg);
if (value)
value = strdup (value);
rpc_msg_reset (&msg);
rpc_client_release (rpc, rpc_client, true);
free (url);
DEBUG (" = %s\n", value);
return value;
}
char *
apteryx_get_string (const char *path, const char *key)
{
char *full_path;
size_t len;
char *value = NULL;
char *str = NULL;
/* Create full path */
if (key)
len = asprintf (&full_path, "%s/%s", path, key);
else
len = asprintf (&full_path, "%s", path);
if (len)
{
if ((value = apteryx_get ((const char *) full_path)))
{
str = (char *) value;
}
free (full_path);
}
return str;
}
char *
apteryx_get_string_default (const char *path, const char *key, const char *deflt)
{
char *value = NULL;
value = apteryx_get_string (path, key);
if (!value)
{
value = strdup (deflt);
}
return value;
}
int32_t
apteryx_get_int (const char *path, const char *key)
{
char *full_path;
size_t len;
char *v = NULL;
char *rem = NULL;
int32_t value = -1;
/* Create full path */
if (key)
len = asprintf (&full_path, "%s/%s", path, key);
else
len = asprintf (&full_path, "%s", path);
if (len)
{
if (apteryx_debug)
{
errno = 0;
}
if ((v = apteryx_get (full_path)))
{
value = strtol ((char *) v, &rem, 0);
if (*rem != '\0')
{
errno = -ERANGE;
value = -1;
}
free (v);
}
else
{
errno = -ERANGE;
}
if (apteryx_debug && errno == -ERANGE)
{
DEBUG ("Cannot represent value as int: %s\n", v);
}
free (full_path);
}
return value;
}
int32_t
apteryx_get_int_default (const char *path, const char *key, int32_t deflt)
{
int32_t value;
int errno_old;
errno_old = errno;
errno = 0;
value = apteryx_get_int (path, key);
if (value == -1 && errno == -ERANGE)
{
value = deflt;
}
errno = errno_old;
return value;
}
bool
apteryx_has_value (const char *path)
{
char *value = NULL;
value = apteryx_get (path);
if (value)
{
free (value);
return true;
}
return false;
}
GNode *
apteryx_find_child (GNode *parent, const char *name)
{
GNode *node;
for (node = g_node_first_child (parent); node; node = node->next)
{
if (g_strcmp0 (APTERYX_NAME (node), name) == 0)
{
return node;
}
}
return NULL;
}
static inline gboolean
_node_free (GNode *node, gpointer data)
{
free ((void *)node->data);
return FALSE;
}
void
apteryx_free_tree (GNode* root)
{
if (root)
{
g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, _node_free, NULL);
g_node_destroy (root);
}
}
static GNode *
merge (GNode *left, GNode *right, int (*cmp) (const char *a, const char *b))
{
if (!left)
return right;
if (!right)
return left;
if (cmp (left->data, right->data) < 0)
{
left->next = merge (left->next, right, cmp);
left->next->prev = left;
left->prev = NULL;
return left;
}
else
{
right->next = merge (left, right->next, cmp );
right->next->prev = right;
right->prev = NULL;
return right;
}