This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathdatastore.c
More file actions
7007 lines (6232 loc) · 204 KB
/
datastore.c
File metadata and controls
7007 lines (6232 loc) · 204 KB
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 datastore.c
* \author Radek Krejci <[email protected]>
* \brief Implementation of the NETCONF datastore handling functions.
*
* Copyright (c) 2012-2014 CESNET, z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is, and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <signal.h>
#include <fcntl.h>
#include <assert.h>
#include <dlfcn.h>
#include <dirent.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#ifndef DISABLE_VALIDATION
# include <libxml/relaxng.h>
# include <libxslt/transform.h>
# include <libxslt/xsltInternals.h>
#else
# ifndef DISABLE_YANGSCHEMA
# include <libxslt/transform.h>
# include <libxslt/xsltInternals.h>
# endif
#endif
#include "netconf_internal.h"
#include "messages.h"
#include "messages_xml.h"
#include "messages_internal.h"
#include "error.h"
#include "with_defaults.h"
#include "session.h"
#include "datastore_xml.h"
#include "nacm.h"
#include "datastore/edit_config.h"
#include "datastore/datastore_internal.h"
#include "datastore/file/datastore_file.h"
#include "datastore/empty/datastore_empty.h"
#include "datastore/custom/datastore_custom_private.h"
#include "transapi/transapi_internal.h"
#include "config.h"
#ifndef DISABLE_URL
# include "url_internal.h"
#endif
#ifndef DISABLE_NOTIFICATIONS
# include "notifications.h"
#endif
#include "../models/ietf-netconf-monitoring.xxd"
#include "../models/ietf-netconf-notifications.xxd"
#include "../models/ietf-netconf-with-defaults.xxd"
#include "../models/nc-notifications.xxd"
#include "../models/ietf-netconf-acm.xxd"
#include "../models/ietf-netconf.xxd"
#include "../models/notifications.xxd"
#include "../models/libnetconf-notifications.xxd"
#include "../models/ietf-inet-types.xxd"
#include "../models/ietf-yang-types.xxd"
static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "RCSID" $";
/*
* From internal.c to be used by nc_session_get_cpblts_deault() to detect
* what part of libnetconf is initiated and can be executed
*/
extern int nc_init_flags;
extern struct nc_shared_info *nc_info;
/* reserve memory for error pointers such as ERROR_POINTER or NCDS_RPC_NOT_APPLICABLE */
API char error_area;
#define ERROR_POINTER ((void*)(&error_area))
char* server_capabilities;
pthread_spinlock_t server_cpblt_lock;
struct ncds_ds_list {
struct ncds_ds *datastore;
struct ncds_ds_list* next;
};
struct ds_desc {
NCDS_TYPE type;
char* filename;
};
struct ncds {
struct ncds_ds_list *datastores;
ncds_id* datastores_ids;
int count;
int array_size;
};
struct ncds_ds *nacm_ds = NULL; /* for NACM subsystem */
static struct ncds ncds = {NULL, NULL, 0, 0};
static struct model_list *models_list = NULL;
static struct transapi_list* augment_tapi_list = NULL;
static char** models_dirs = NULL;
static nc_reply* ncds_apply_rpc(ncds_id id, const struct nc_session* session, const nc_rpc* rpc, struct nc_filter* shared_filter);
static char* get_state_nacm(const char* UNUSED(model), const char* UNUSED(running), struct nc_err ** UNUSED(e));
static char* get_state_monitoring(const char* UNUSED(model), const char* UNUSED(running), struct nc_err ** UNUSED(e));
static int get_model_info(xmlXPathContextPtr model_ctxt, char **name, char **version, char **ns, char **prefix, char ***rpcs, char ***notifs);
static struct data_model* get_model(const char* module, const char* version);
static int ncds_features_parse(struct data_model* model);
static int ncds_update_uses_groupings(struct data_model* model);
static int ncds_update_uses_augments(struct data_model* model);
static void ncds_ds_model_free(struct data_model* model);
static xmlDocPtr ncxml_merge(const xmlDocPtr first, const xmlDocPtr second, const xmlDocPtr data_model);
extern int first_after_close;
static int ncds_update_features();
static int feature_check(xmlNodePtr node, struct data_model* model);
static struct model_feature** get_features_from_prefix(struct data_model* model, char* prefix);
#ifndef DISABLE_NOTIFICATIONS
static char* get_state_notifications(const char* UNUSED(model), const char* UNUSED(running), struct nc_err ** UNUSED(e));
#endif
static struct ncds_ds *datastores_get_ds(ncds_id id);
#ifndef DISABLE_YANGFORMAT
/* XSL stylesheet for transformation from YIN to YANG format */
#define YIN2YANG NC_WORKINGDIR_PATH"/yin2yang.xsl"
static xsltStylesheetPtr yin2yang_xsl = NULL;
#endif
/* Allocate and fill the ncds func structure based on the type. */
static struct ncds_ds* ncds_fill_func(NCDS_TYPE type)
{
struct ncds_ds* ds;
switch (type) {
case NCDS_TYPE_CUSTOM:
if ((ds = (struct ncds_ds*) calloc(1, sizeof(struct ncds_ds_custom))) == NULL) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
return (NULL);
}
ds->func.init = ncds_custom_init;
ds->func.free = ncds_custom_free;
ds->func.was_changed = ncds_custom_was_changed;
ds->func.rollback = ncds_custom_rollback;
ds->func.get_lockinfo = ncds_custom_get_lockinfo;
ds->func.lock = ncds_custom_lock;
ds->func.unlock = ncds_custom_unlock;
ds->func.getconfig = ncds_custom_getconfig;
ds->func.copyconfig = ncds_custom_copyconfig;
ds->func.deleteconfig = ncds_custom_deleteconfig;
ds->func.editconfig = ncds_custom_editconfig;
break;
case NCDS_TYPE_FILE:
if ((ds = (struct ncds_ds*) calloc(1, sizeof(struct ncds_ds_file))) == NULL ) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
return (NULL );
}
ds->func.init = ncds_file_init;
ds->func.free = ncds_file_free;
ds->func.was_changed = ncds_file_changed;
ds->func.rollback = ncds_file_rollback;
ds->func.get_lockinfo = ncds_file_lockinfo;
ds->func.lock = ncds_file_lock;
ds->func.unlock = ncds_file_unlock;
ds->func.getconfig = ncds_file_getconfig;
ds->func.copyconfig = ncds_file_copyconfig;
ds->func.deleteconfig = ncds_file_deleteconfig;
ds->func.editconfig = ncds_file_editconfig;
break;
case NCDS_TYPE_EMPTY:
if ((ds = (struct ncds_ds*) calloc(1, sizeof(struct ncds_ds_empty))) == NULL ) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
return (NULL );
}
ds->func.init = ncds_empty_init;
ds->func.free = ncds_empty_free;
ds->func.was_changed = ncds_empty_changed;
ds->func.rollback = ncds_empty_rollback;
ds->func.get_lockinfo = ncds_empty_lockinfo;
ds->func.lock = ncds_empty_lock;
ds->func.unlock = ncds_empty_unlock;
ds->func.getconfig = ncds_empty_getconfig;
ds->func.copyconfig = ncds_empty_copyconfig;
ds->func.deleteconfig = ncds_empty_deleteconfig;
ds->func.editconfig = ncds_empty_editconfig;
break;
default:
ERROR("Unsupported datastore implementation required.");
return (NULL );
}
return (ds);
}
#ifndef DISABLE_NOTIFICATIONS
#define INTERNAL_DS_COUNT 10
#define MONITOR_DS_INDEX 3
#define NOTIF_DS_INDEX_L 4
#define NOTIF_DS_INDEX_H 7
#define WD_DS_INDEX 8
#define NACM_DS_INDEX 9
#else
#define INTERNAL_DS_COUNT 6
#define MONITOR_DS_INDEX 3
#define WD_DS_INDEX 4
#define NACM_DS_INDEX 5
#endif
int internal_ds_count = 0;
int ncds_sysinit(int flags)
{
int i;
struct ncds_ds *ds;
struct ncds_ds_list *dsitem;
struct model_list *list_item;
unsigned char* model[INTERNAL_DS_COUNT] = {
ietf_inet_types_yin,
ietf_yang_types_yin,
ietf_netconf_yin,
ietf_netconf_monitoring_yin,
#ifndef DISABLE_NOTIFICATIONS
ietf_netconf_notifications_yin,
nc_notifications_yin,
notifications_yin,
libnetconf_notifications_yin,
#endif
ietf_netconf_with_defaults_yin,
ietf_netconf_acm_yin
};
unsigned int model_len[INTERNAL_DS_COUNT] = {
ietf_inet_types_yin_len,
ietf_yang_types_yin_len,
ietf_netconf_yin_len,
ietf_netconf_monitoring_yin_len,
#ifndef DISABLE_NOTIFICATIONS
ietf_netconf_notifications_yin_len,
nc_notifications_yin_len,
notifications_yin_len,
libnetconf_notifications_yin_len,
#endif
ietf_netconf_with_defaults_yin_len,
ietf_netconf_acm_yin_len
};
char* (*get_state_funcs[INTERNAL_DS_COUNT])(const char* model, const char* running, struct nc_err ** e) = {
NULL, /* ietf-inet-types */
NULL, /* ietf-yang-types */
NULL, /* ietf-netconf */
get_state_monitoring, /* ietf-netconf-monitoring */
#ifndef DISABLE_NOTIFICATIONS
NULL, /* ietf-netconf-notifications */
get_state_notifications, /* nc-notifications */
NULL, /* notifications */
NULL, /* libnetconf-notifications */
#endif
NULL, /* ietf-netconf-with-defaults */
get_state_nacm /* NACM status data */
};
struct ds_desc internal_ds_desc[INTERNAL_DS_COUNT] = {
{NCDS_TYPE_EMPTY, NULL},
{NCDS_TYPE_EMPTY, NULL},
{NCDS_TYPE_EMPTY, NULL},
{NCDS_TYPE_EMPTY, NULL},
#ifndef DISABLE_NOTIFICATIONS
{NCDS_TYPE_EMPTY, NULL}, /* ietf-netconf-notifications */
{NCDS_TYPE_EMPTY, NULL}, /* nc-notifications */
{NCDS_TYPE_EMPTY, NULL}, /* notifications */
{NCDS_TYPE_EMPTY, NULL}, /* libnetconf-notifications */
#endif
{NCDS_TYPE_EMPTY, NULL},
{NCDS_TYPE_FILE, NC_WORKINGDIR_PATH"/datastore-acm.xml"}
};
#ifndef DISABLE_VALIDATION
char* relaxng_validators[INTERNAL_DS_COUNT] = {
NULL, /* ietf-inet-types */
NULL, /* ietf-yang-types */
NULL, /* ietf-netconf */
NULL, /* ietf-netconf-monitoring */
#ifndef DISABLE_NOTIFICATIONS
NULL, /* ietf-netconf-notifications */
NULL, /* nc-notifications */
NULL, /* notifications */
NULL, /* libnetconf-notifications */
#endif
NULL, /* ietf-netconf-with-defaults */
NC_WORKINGDIR_PATH"/ietf-netconf-acm-config.rng" /* NACM RelaxNG schema */
};
char* schematron_validators[INTERNAL_DS_COUNT] = {
NULL, /* ietf-inet-types */
NULL, /* ietf-yang-types */
NULL, /* ietf-netconf */
NULL, /* ietf-netconf-monitoring */
#ifndef DISABLE_NOTIFICATIONS
NULL, /* ietf-netconf-notifications */
NULL, /* nc-notifications */
NULL, /* notifications */
NULL, /* libnetconf-notifications */
#endif
NULL, /* ietf-netconf-with-defaults */
NC_WORKINGDIR_PATH"/ietf-netconf-acm-schematron.xsl" /* NACM Schematron XSL stylesheet */
};
#endif
pthread_spin_init(&server_cpblt_lock, PTHREAD_PROCESS_SHARED);
internal_ds_count = 0;
for (i = 0; i < INTERNAL_DS_COUNT; i++) {
if ((i == NACM_DS_INDEX) && !(flags & NC_INIT_NACM)) {
/* NACM is not enabled */
continue;
}
if ((i == MONITOR_DS_INDEX) && !(flags & NC_INIT_MONITORING)) {
/* NETCONF monitoring is not enabled */
continue;
}
if ((i == WD_DS_INDEX) && !(flags & NC_INIT_WD)) {
/* NETCONF with-defaults capability is not enabled */
continue;
}
#ifndef DISABLE_NOTIFICATIONS
if ((i >= NOTIF_DS_INDEX_L && i <= NOTIF_DS_INDEX_H) && !(flags & NC_INIT_NOTIF)) {
/* Notifications are not enabled */
continue;
}
#endif
ds = ncds_fill_func(internal_ds_desc[i].type);
if (ds == NULL) {
/* The error was reported already. */
return (EXIT_FAILURE);
}
ds->id = internal_ds_count++;
ds->type = internal_ds_desc[i].type;
if (ds->type == NCDS_TYPE_FILE && ncds_file_set_path(ds, internal_ds_desc[i].filename) != 0) {
ERROR("Linking internal datastore to a file (%s) failed.", internal_ds_desc[i].filename);
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
ds->data_model = calloc(1, sizeof(struct data_model));
if (ds->data_model == NULL) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
ds->data_model->xml = xmlReadMemory ((char*)model[i], model_len[i], NULL, NULL, NC_XMLREAD_OPTIONS);
if (ds->data_model->xml == NULL ) {
ERROR("Unable to read the internal monitoring data model.");
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
/* prepare xpath evaluation context of the model for XPath */
if ((ds->data_model->ctxt = xmlXPathNewContext(ds->data_model->xml)) == NULL) {
ERROR("%s: Creating XPath context failed.", __func__);
ncds_free(ds);
internal_ds_count--;
/* with-defaults cannot be found */
return (EXIT_FAILURE);
}
if (xmlXPathRegisterNs(ds->data_model->ctxt, BAD_CAST NC_NS_YIN_ID, BAD_CAST NC_NS_YIN) != 0) {
xmlXPathFreeContext(ds->data_model->ctxt);
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
if (get_model_info(ds->data_model->ctxt,
&(ds->data_model->name),
&(ds->data_model->version),
&(ds->data_model->ns),
&(ds->data_model->prefix),
&(ds->data_model->rpcs),
&(ds->data_model->notifs)) != 0) {
ERROR("Unable to process internal configuration data model.");
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
asprintf(&ds->data_model->path, "internal_%d", i);
ncds_features_parse(ds->data_model);
ds->ext_model = ds->data_model->xml;
ds->ext_model_tree = NULL;
/* resolve uses statements in groupings and augments definitions */
ncds_update_uses_groupings(ds->data_model);
ncds_update_uses_augments(ds->data_model);
ds->last_access = 0;
ds->get_state = get_state_funcs[i];
/* update internal model lists */
list_item = malloc(sizeof(struct model_list));
if (list_item == NULL) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
list_item->model = ds->data_model;
list_item->next = models_list;
models_list = list_item;
#ifndef DISABLE_VALIDATION
/* set validation */
if (relaxng_validators[i] != NULL || schematron_validators[i] != NULL) {
ncds_set_validation(ds, 1, relaxng_validators[i], schematron_validators[i]);
VERB("Datastore %s initiated with ID %d.", ds->data_model->name, ds->id);
}
#endif
/* init */
ds->func.init(ds);
/* add to a datastore list */
if ((dsitem = malloc (sizeof(struct ncds_ds_list))) == NULL ) {
ERROR("Memory allocation failed (%s:%d).", __FILE__, __LINE__);
ncds_free(ds);
internal_ds_count--;
return (EXIT_FAILURE);
}
if (i == NACM_DS_INDEX) {
/* provide NACM datastore to the NACM subsystem for faster access */
nacm_ds = ds;
}
dsitem->datastore = ds;
dsitem->next = ncds.datastores;
ncds.datastores = dsitem;
ncds.count++;
if (ncds.count >= ncds.array_size) {
void *tmp = realloc(ncds.datastores_ids, (ncds.array_size + 10) * sizeof(ncds_id));
if (tmp == NULL) {
ERROR("Memory reallocation failed (%s:%d).", __FILE__, __LINE__);
ncds_free(ds);
internal_ds_count--;
ncds.datastores = NULL;
ncds.count--;
return (EXIT_FAILURE);
}
ncds.array_size += 10;
ncds.datastores_ids = tmp;
}
ds = NULL;
}
#ifndef DISABLE_YANGFORMAT
/* try to get yin2yang XSLT stylesheet */
errno = 0;
if (eaccess(YIN2YANG, R_OK) == -1 || (yin2yang_xsl = xsltParseStylesheetFile(BAD_CAST YIN2YANG)) == NULL) {
WARN("Unable to use %s (%s).", YIN2YANG, errno == 0 ? "XSLT parser failed" : strerror(errno));
WARN("YANG format data models will not be available via get-schema.");
}
#endif
return (EXIT_SUCCESS);
}
void ncds_startup_internal(void)
{
struct ncds_ds_list *ds_iter;
struct nc_err *e = NULL;
for (ds_iter = ncds.datastores; ds_iter != NULL ; ds_iter = ds_iter->next) {
/* apply startup to running */
ds_iter->datastore->func.copyconfig(ds_iter->datastore, NULL, NULL, NC_DATASTORE_RUNNING, NC_DATASTORE_STARTUP, NULL, &e);
nc_err_free(e);
e = NULL;
}
}
/**
* @brief Get ncds_ds structure from the datastore list containing storage
* information with the specified ID.
*
* @param[in] id ID of the storage.
* @return Pointer to the required ncds_ds structure inside internal
* datastores variable.
*/
static struct ncds_ds *datastores_get_ds(ncds_id id)
{
struct ncds_ds_list *ds_iter;
for (ds_iter = ncds.datastores; ds_iter != NULL; ds_iter = ds_iter->next) {
if (ds_iter->datastore != NULL && ds_iter->datastore->id == id) {
break;
}
}
if (ds_iter == NULL) {
return NULL;
}
return (ds_iter->datastore);
}
/**
* @brief Remove datastore with the specified ID from the internal datastore list.
*
* @param[in] id ID of the storage.
* @return Pointer to the required ncds_ds structure detached from the internal
* datastores variable.
*/
static struct ncds_ds *datastores_detach_ds(ncds_id id)
{
struct ncds_ds_list *ds_iter;
struct ncds_ds_list *ds_prev = NULL;
struct ncds_ds * retval = NULL;
if (id < internal_ds_count && !(nc_init_flags & NC_INIT_CLOSING)) {
/* ignore a try to detach some uninitialized or internal datastore */
return (NULL);
}
for (ds_iter = ncds.datastores; ds_iter != NULL; ds_prev = ds_iter, ds_iter = ds_iter->next) {
if (ds_iter->datastore != NULL && ds_iter->datastore->id == id) {
break;
}
}
if (ds_iter != NULL) {
/* required datastore was found */
if (ds_prev == NULL) {
/* we're removing the first item of the datastores list */
ncds.datastores = ds_iter->next;
} else {
ds_prev->next = ds_iter->next;
}
retval = ds_iter->datastore;
free(ds_iter);
ncds.count--;
}
return retval;
}
/*
* type 0 - backup
* type 1 - restore
*/
static int fmon_cp_file(const char* source, const char* target, uint8_t type)
{
char buf[4096];
int source_fd, target_fd;
mode_t mode;
uid_t uid;
gid_t gid;
struct stat finfo;
ssize_t r;
assert(source);
assert(target);
if ((source_fd = open(source, O_RDONLY|O_CLOEXEC)) == -1) {
if (type == 1) {
ERROR("Unable to open backup file \"%s\" (%s)", source, strerror(errno));
} else {
ERROR("Unable to open file \"%s\" to backup (%s)", source, strerror(errno));
}
return 1;
}
/* get source access rights */
if (fstat(source_fd, &finfo) == -1) {
if (type == 1) {
WARN("Unable to get information about backup file \"%s\" (%s).", source, strerror(errno));
VERB("Using default protection 0600 for restored file.");
} else {
WARN("Unable to get information about \"%s\" file to backup (%s).", source, strerror(errno));
VERB("Using default protection 0600 for backup file.");
}
mode = 00600;
uid = geteuid();
gid = getegid();
} else {
mode = finfo.st_mode;
uid = finfo.st_uid;
gid = finfo.st_gid;
}
if ((target_fd = open(target, O_WRONLY|O_CLOEXEC|O_CREAT|O_TRUNC, mode)) == -1) {
if (type == 1) {
ERROR("Unable to restore file \"%s\" (%s)", target, strerror(errno));
} else {
ERROR("Unable to create backup file \"%s\" (%s)", target, strerror(errno));
}
close(source_fd);
return 1;
}
if (fchown(target_fd, uid, gid) != 0) {
WARN("Failed to change owner of \"%s\" (%s).", target, strerror(errno));
}
fchmod(target_fd, mode); /* if not created, but rewriting some existing file */
for (;;) {
r = read(source_fd, buf, sizeof(buf));
if (r == 0) {
/* EOF */
break;
} else if (r < 0) {
/* ERROR */
if (type == 1) {
ERROR("Restoring file \"%s\" failed (%s).", target, strerror(errno));
} else {
ERROR("Creating backup file \"%s\" failed (%s).", target, strerror(errno));
}
break;
}
if (write(target_fd, buf, r) < r) {
ERROR("Writing into file \"%s\" failed (%s).", target, strerror(errno));
break;
}
}
close(source_fd);
close(target_fd);
return 0;
}
static int fmon_restore_file(const char* target)
{
char *source = NULL;
int ret;
assert(target);
if (asprintf(&source, "%s.netconf", target) != 0) {
return 1;
}
ret = fmon_cp_file(source, target, 1);
free(source);
return ret;
}
static int fmon_backup_file(const char* source)
{
char *target = NULL;
int ret;
assert(source);
if (asprintf(&target, "%s.netconf", source) == -1) {
return 1;
}
ret = fmon_cp_file(source, target, 0);
free(target);
return ret;
}
#define INOT_BUFLEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
struct fmon {
int wd;
char flags;
};
#define FMON_FLAG_MODIFIED 0x01
#define FMON_FLAG_IGNORED 0x02
#define FMON_FLAG_UPDATE 0x04
struct fmon_arg {
volatile int flag;
struct transapi_file_callbacks *fclbks;
struct ncds_ds *ds;
};
static void* transapi_fmon(void *arg)
{
struct fmon_arg *fmon_arg = (struct fmon_arg*)arg;
struct transapi_file_callbacks *fclbks = fmon_arg->fclbks;
struct ncds_ds *ds = fmon_arg->ds;
int inotify, i, r, ret;
struct fmon *wds;
char buf[INOT_BUFLEN], *p;
struct inotify_event *e;
char* config;
xmlDocPtr config_doc;
xmlNodePtr node;
xmlBufferPtr running_buf = NULL;
struct nc_err *err;
int execflag;
struct nc_session* dummy_session;
nc_rpc* rpc;
nc_reply *reply;
struct nc_cpblts* cpblts;
const struct ncds_lockinfo *lockinfo;
/* note thread creator that we stored passed arguments and the original
* fmon_arg structure can be rewritten.
*/
fmon_arg->flag = 0;
if ((inotify = inotify_init1(IN_CLOEXEC)) == -1) {
ERROR("FMON thread failed on initiating inotify (%s).", strerror(errno));
return NULL;
}
wds = malloc(sizeof(struct fmon) * fclbks->callbacks_count);
pthread_cleanup_push(free, wds);
running_buf = xmlBufferCreate();
pthread_cleanup_push((void (*)(void*))&xmlBufferFree, running_buf);
dummy_session = nc_session_dummy("fmon", "server", NULL, cpblts = nc_session_get_cpblts_default());
nc_cpblts_free(cpblts);
pthread_cleanup_push((void (*)(void*))&nc_session_free, dummy_session);
for (i = 0; i < fclbks->callbacks_count; i++) {
if ((wds[i].wd = inotify_add_watch(inotify, fclbks->callbacks[i].path, IN_MODIFY|IN_IGNORED|IN_CLOSE_WRITE)) == -1) {
ERROR("Unable to monitor \"%s\" (%s)", fclbks->callbacks[i].path, strerror(errno));
} else {
/* create backup file with current content */
fmon_backup_file(fclbks->callbacks[i].path);
}
wds[i].flags = 0;
}
for (;;) {
r = read(inotify, buf, INOT_BUFLEN);
if (r == 0) {
ERROR("Inotify failed (EOF).");
break;
} else if (r == -1) {
if(errno == EINTR) {
continue;
}
ERROR("Inotify failed (%s).", strerror(errno));
break;
}
for (p = buf; p < buf + r;) {
e = (struct inotify_event*)p;
/* get index of the modified file */
for (i = 0; i < fclbks->callbacks_count; i++) {
if (wds[i].wd == e->wd) {
break;
}
}
if (e->mask & IN_IGNORED) {
/* the file was removed or replaced */
if ((wds[i].wd = inotify_add_watch(inotify, fclbks->callbacks[i].path, IN_MODIFY|IN_IGNORED|IN_CLOSE_WRITE)) == -1) {
if (errno == ENOENT) {
/* the file was removed */
VERB("File \"%s\" was removed is no more monitored.", fclbks->callbacks[i].path);
} else {
/* the file was replaced, but we cannot access the new file */
ERROR("Unable to continue in monitoring \"%s\" file (%s)", fclbks->callbacks[i].path, strerror(errno));
}
} else {
/* file was replaced and we now monitor the newly created file */
/* set its modified flag to 2 to execute callback */
wds[i].flags |= FMON_FLAG_UPDATE;
}
} else {
if (e->mask & IN_MODIFY) {
wds[i].flags |= FMON_FLAG_MODIFIED;
}
if ((e->mask & IN_CLOSE_WRITE) && (wds[i].flags & FMON_FLAG_MODIFIED)) {
wds[i].flags |= FMON_FLAG_UPDATE;
}
}
if (wds[i].flags & FMON_FLAG_UPDATE) {
if (wds[i].flags & FMON_FLAG_IGNORED) {
/* ignore our own backup restore */
wds[i].flags = 0;
goto next_event;
}
/* null the variables */
wds[i].flags = 0;
config_doc = NULL;
execflag = 0;
/* check that datastore is not locked */
lockinfo = ds->func.get_lockinfo(ds, NC_DATASTORE_RUNNING);
if (lockinfo && lockinfo->sid) {
VERB("FMON: Running datastore is locked by \"%s\"", lockinfo->sid);
WARN("FMON: Replacing changed \"%s\" with the backup file.", fclbks->callbacks[i].path);
/* note that next update notification of this file should be ignored */
wds[i].flags = FMON_FLAG_IGNORED;
/* restore original content */
fmon_restore_file(fclbks->callbacks[i].path);
goto next_event;
}
fclbks->callbacks[i].func(fclbks->callbacks[i].path, &config_doc, &execflag);
if (config_doc != NULL) {
/*
* store running to the datastore
*/
/* check returned data format */
if (config_doc->children == NULL) {
ERROR("Invalid configuration data returned from transAPI FMON callback.");
goto next_event;
}
/* perform changes in datastore (and on device if set so) */
if (execflag) {
/* update running datastore including execution of the transAPI callbacks */
rpc = ncxml_rpc_editconfig(NC_DATASTORE_RUNNING,
NC_DATASTORE_CONFIG, NC_EDIT_DEFOP_NOTSET,
NC_EDIT_ERROPT_ROLLBACK, NC_EDIT_TESTOPT_NOTSET,
config_doc->children->children);
xmlFreeDoc(config_doc);
if (rpc == NULL) {
ERROR("FMON: Preparing edit-config RPC failed.");
goto next_event;
}
reply = ncds_apply_rpc2all(dummy_session, rpc, NULL);
nc_rpc_free(rpc);
if (reply == NULL || nc_reply_get_type(reply) != NC_REPLY_OK) {
ERROR("FMON: Performing edit-config RPC failed.");
}
nc_reply_free(reply);
} else {
/* do not execute transAPI callbacks, only update running datastore */
for (node = config_doc->children; node != NULL; node = node->next) {
xmlNodeDump(running_buf, config_doc, node, 0, 0);
}
xmlFreeDoc(config_doc);
config = strdup((char*)xmlBufferContent(running_buf));
xmlBufferEmpty(running_buf);
ret = ds->func.editconfig(ds, NULL, NULL,
NC_DATASTORE_RUNNING, config,
NC_EDIT_DEFOP_NOTSET, NC_EDIT_ERROPT_ROLLBACK, &err);
free(config);
if (ret != 0 && ret != EXIT_RPC_NOT_APPLICABLE) {
ERROR("Failed to update running configuration (%s).", err ? err->message : "unknown error");
nc_err_free(err);
}
}
/* update backup file */
fmon_backup_file(fclbks->callbacks[i].path);
}
}
next_event:
p += sizeof(struct inotify_event) + e->len;
}
}
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
return NULL;
}
API int ncds_device_init(ncds_id *id, struct nc_cpblts *cpblts, int force)
{
nc_rpc * rpc_msg = NULL;
nc_reply * reply_msg = NULL;
struct ncds_ds_list * ds_iter, *start = NULL;
struct ncds_ds * ds;
struct nc_session * dummy_session = NULL;
struct nc_err * err = NULL;
int nocpblts = 0, ret, retval = EXIT_SUCCESS;
xmlDocPtr running_doc = NULL, aux_doc1 = NULL, aux_doc2;
xmlNodePtr data_node;
char* new_running_config = NULL;
xmlBufferPtr running_buf = NULL;
struct transapi_list *tapi_iter;
static struct fmon_arg arg = {0, NULL, NULL};
if (id != NULL) {
/* initialize only the device connected with the given datastore ID */
if ((ds = datastores_get_ds(*id)) == NULL) {
ERROR("Unable to find module with id %d", *id);
return (EXIT_FAILURE);
}
start = calloc(1, sizeof(struct ncds_ds_list));
if (start == NULL) {
ERROR("Memory reallocation failed (%s:%d).", __FILE__, __LINE__);
return (EXIT_FAILURE);
}
start->datastore = ds;
} else {
/* OR if datastore not specified, initialize all transAPI capable modules */
start = ncds.datastores;
}
if (cpblts == NULL) {
cpblts = nc_session_get_cpblts_default();
nocpblts = 1;
}
/* create dummy session for applying copy-config (startup->running) */
if ((dummy_session = nc_session_dummy("dummy-internal", "server", NULL, cpblts)) == NULL) {
ERROR("%s: Creating dummy-internal session failed.", __func__);
retval = EXIT_FAILURE;
goto cleanup;
}
if (nocpblts) {
nc_cpblts_free(cpblts);
cpblts = NULL;
}
rpc_msg = nc_rpc_copyconfig(NC_DATASTORE_STARTUP, NC_DATASTORE_RUNNING);
running_buf = xmlBufferCreate();
for (ds_iter = start; ds_iter != NULL ; ds_iter = ds_iter->next) {
for (tapi_iter = ds_iter->datastore->transapis; tapi_iter != NULL; tapi_iter = tapi_iter->next) {
if (tapi_iter->tapi->init != NULL) {
/* module can return current configuration of device */
if (tapi_iter->tapi->init(&aux_doc1)) {
ERROR("init function from module %s failed.",
ds_iter->datastore->data_model->name);
retval = EXIT_FAILURE;
goto cleanup;
}
if (running_doc == NULL) {
running_doc = aux_doc1;
} else {
aux_doc2 = running_doc;
running_doc = ncxml_merge(aux_doc2, aux_doc1, ds_iter->datastore->ext_model);
xmlFreeDoc(aux_doc1);
xmlFreeDoc(aux_doc2);
}
}
}
if (first_after_close || force) {
/* if this process is first after a nc_close(system=1) or reinitialization is forced */
/* dump running configuration data returned by transapi_init() */
if (running_doc == NULL) {
new_running_config = strdup("");
} else {
for (data_node = running_doc->children; data_node != NULL; data_node = data_node->next) {
xmlNodeDump(running_buf, running_doc, data_node, 0, 0);
}
new_running_config = strdup((char*)xmlBufferContent(running_buf));
xmlBufferEmpty(running_buf);
}
/* Clean RUNNING datastore. This is important when transAPI is deployed and does not harm when not. */