blob: ff6dc849f1e3237a6d7d8818d45c85fb3f4db0a3 [file] [log] [blame]
Radek Krejci0af5f5d2018-09-07 15:00:30 +02001/**
2 * @file context.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Context implementations
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcib7db73a2018-10-24 14:18:40 +020015#include "common.h"
16
Radek Krejci7ada9a02018-09-07 15:34:05 +020017#define _BSD_SOURCE
Radek Krejci0af5f5d2018-09-07 15:00:30 +020018#include <errno.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020019#include <limits.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020022#include <sys/stat.h>
23#include <sys/types.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020024#include <unistd.h>
25
Radek Krejci0af5f5d2018-09-07 15:00:30 +020026#include "context.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include "tree_schema_internal.h"
Radek Krejciad573502018-09-07 15:26:55 +020028#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020029
Radek Krejci86d106e2018-10-18 09:53:19 +020030#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020031
32#define IETF_YANG_METADATA_PATH "../models/ietf-yang-metadata@2016-08-05.h"
33#define YANG_PATH "../models/yang@2017-02-20.h"
34#define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h"
35#define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h"
36#define IETF_DATASTORES "../models/ietf-datastores@2017-08-17.h"
37#define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2018-01-17.h"
38#define IETF_YANG_LIB_REV "2018-01-17"
39
Radek Krejci0af5f5d2018-09-07 15:00:30 +020040#include IETF_YANG_METADATA_PATH
41#include YANG_PATH
42#include IETF_INET_TYPES_PATH
43#include IETF_YANG_TYPES_PATH
44#include IETF_DATASTORES
45#include IETF_YANG_LIB_PATH
Radek Krejci0af5f5d2018-09-07 15:00:30 +020046
47static struct internal_modules_s {
48 const char *name;
49 const char *revision;
50 const char *data;
51 uint8_t implemented;
52 LYS_INFORMAT format;
53} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci86d106e2018-10-18 09:53:19 +020054 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 0, LYS_IN_YANG},
55 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
56 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
57 {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yang, 0, LYS_IN_YANG},
Radek Krejci0af5f5d2018-09-07 15:00:30 +020058 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci86d106e2018-10-18 09:53:19 +020059 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 0, LYS_IN_YANG},
60 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yang, 1, LYS_IN_YANG}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020061};
62
63API LY_ERR
64ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
65{
Radek Krejci0e212402018-09-20 11:29:38 +020066 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020067 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020068 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069
70 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
71
72 if (search_dir) {
Radek Krejci0e212402018-09-20 11:29:38 +020073 LY_CHECK_ERR_RET(access(search_dir, R_OK | X_OK),
74 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)),
75 LY_EINVAL);
76 LY_CHECK_ERR_RET(stat(search_dir, &st),
77 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s)", search_dir, strerror(errno)),
78 LY_ESYS);
79 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
80 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", search_dir),
81 LY_EINVAL);
Radek Krejci0af5f5d2018-09-07 15:00:30 +020082 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020083 LY_CHECK_ERR_RET(!new_dir,
84 LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)),
85 LY_ESYS);
Radek Krejci0e212402018-09-20 11:29:38 +020086 /* avoid path duplication */
87 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020088 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020089 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020090 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020091 }
92 }
93 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020094 free(new_dir);
95 return LY_EMEM;
96 }
97
Radek Krejci0e212402018-09-20 11:29:38 +020098 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020099 } else {
100 /* consider that no change is not actually an error */
101 return LY_SUCCESS;
102 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200103}
104
105API const char * const *
106ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
107{
Radek Krejci05026432018-09-20 12:13:43 +0200108 void **new;
109
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200110 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200111
112 if (ctx->search_paths.count == ctx->search_paths.size) {
113 /* not enough space for terminating NULL byte */
114 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
115 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
116 ((struct ly_ctx *)ctx)->search_paths.size += 8;
117 ((struct ly_ctx *)ctx)->search_paths.objs = new;
118 }
119 /* set terminating NULL byte to the strings list */
120 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
121
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200122 return (const char * const *)ctx->search_paths.objs;
123}
124
125API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200126ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200127{
Radek Krejci0759b792018-09-20 13:53:15 +0200128 unsigned int index;
129
Radek Krejcicd649072018-09-20 12:14:45 +0200130 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200131
Michal Vaskob34480a2018-09-17 10:34:45 +0200132 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200133 return LY_SUCCESS;
134 }
135
Radek Krejci0759b792018-09-20 13:53:15 +0200136 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200137 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200138 for (index = 0; index < ctx->search_paths.count; ++index) {
139 if (!strcmp(value, ctx->search_paths.objs[index])) {
140 break;
141 }
142 }
143 if (index == ctx->search_paths.count) {
144 LOGARG(ctx, value);
145 return LY_EINVAL;
146 } else {
147 return ly_set_rm_index(&ctx->search_paths, index, free);
148 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200149 } else {
150 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200151 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200152 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
153 }
154
155 return LY_SUCCESS;
156}
157
158API LY_ERR
159ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
160{
161 struct ly_ctx *ctx = NULL;
162 struct lys_module *module;
163 char *search_dir_list;
164 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200165 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200166 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200167
168 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200169 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200170
171 /* dictionary */
172 lydict_init(&ctx->dict);
173
Radek Krejciad573502018-09-07 15:26:55 +0200174#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200175 /* plugins */
176 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200177#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200178
179 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200180 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200181
182 /* models list */
183 ctx->flags = options;
184 if (search_dir) {
185 search_dir_list = strdup(search_dir);
186 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
187
188 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
189 *sep = 0;
190 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200191 if (rc == LY_EEXIST) {
192 /* ignore duplication */
193 rc = LY_SUCCESS;
194 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200195 }
196 if (*dir && rc == LY_SUCCESS) {
197 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200198 if (rc == LY_EEXIST) {
199 /* ignore duplication */
200 rc = LY_SUCCESS;
201 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200202 }
203 free(search_dir_list);
204
205 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
206 if (rc != LY_SUCCESS) {
207 goto error;
208 }
209 }
210 ctx->module_set_id = 1;
211
212 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200213 for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200214 module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format);
215 LY_CHECK_GOTO(!module, error);
216 module->parsed->implemented = internal_modules[i].implemented;
217 }
218
219 *new_ctx = ctx;
220 return rc;
221
222error:
223 ly_ctx_destroy(ctx, NULL);
224 return rc;
225}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200226API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200227ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200228{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200229 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200230 return ctx->flags;
231}
232
Radek Krejcica828d92018-09-20 14:19:43 +0200233API LY_ERR
234ly_ctx_set_option(struct ly_ctx *ctx, int option)
235{
236 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
237 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
238
239 /* set the option(s) */
240 ctx->flags |= option;
241
242 return LY_SUCCESS;
243}
244
245API LY_ERR
246ly_ctx_unset_option(struct ly_ctx *ctx, int option)
247{
248 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
249 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
250
251 /* unset the option(s) */
252 ctx->flags &= ~option;
253
254 return LY_SUCCESS;
255}
256
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257API uint16_t
258ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
259{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200260 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200261 return ctx->module_set_id;
262}
263
Radek Krejcib7db73a2018-10-24 14:18:40 +0200264/**
265 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
266 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
267 *
268 * @param[in] ctx Context where to iterate.
269 * @param[in] key Key value to search for.
270 * @param[in] key_offset Key's offset in struct lysp_module and struct lysc_module to get value from the context's
271 * modules to match with the key.
272 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
273 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
274 * module in the context.
275 * @return Module matching the given key, NULL if no such module found.
276 */
277static const struct lys_module *
278ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
279{
280 const struct lys_module *mod;
281 const char *value;
282
283 for (; *index < ctx->list.count; ++(*index)) {
284 mod = ctx->list.objs[*index];
285 if (mod->compiled) {
286 value = *(const char**)(((int8_t*)(mod->compiled)) + key_offset);
287 } else {
288 value = *(const char**)(((int8_t*)(mod->parsed)) + key_offset);
289 }
290 if (!strcmp(key, value)) {
291 /* increment index for the next run */
292 ++(*index);
293 return mod;
294 }
295 }
296 /* done */
297 return NULL;
298}
299
300/**
301 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
302 * @param[in] ctx Context where to search.
303 * @param[in] key Name or Namespace as a search key.
304 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
305 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
306 * revision module, use ly_ctx_get_module_latest_by().
307 * @return Matching module if any.
308 */
309static const struct lys_module *
310ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
311{
312 const struct lys_module *mod;
313 unsigned int index = 0;
314
315 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
316 if (!revision) {
317 if ((mod->compiled && !mod->compiled->revs) || (!mod->compiled && !mod->parsed->revs)) {
318 /* found requested module without revision */
319 return mod;
320 }
321 } else {
322 if ((mod->compiled && mod->compiled->revs && !strcmp(mod->compiled->revs[0].date, revision)) ||
323 (!mod->compiled && mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, revision))) {
324 /* found requested module of the specific revision */
325 return mod;
326 }
327 }
328 }
329
330 return NULL;
331}
332
333API const struct lys_module *
334ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
335{
336 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
337 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lysp_module, ns), revision);
338}
339
340API const struct lys_module *
341ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
342{
343 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
344 return ly_ctx_get_module_by(ctx, name, offsetof(struct lysp_module, name), revision);
345}
346
347/**
348 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
349 * @param[in] ctx Context where to search.
350 * @param[in] key Name or Namespace as a search key.
351 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
352 * @return Matching module if any.
353 */
354static const struct lys_module *
355ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
356{
357 const struct lys_module *mod, *newest = NULL;
358 unsigned int index = 0;
359 const char *date, *date_newest = NULL;
360
361 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
362 if (!newest) {
363 newest = mod;
364 } else {
365 if ((newest->compiled && !newest->compiled->revs) || (!newest->compiled && !newest->parsed->revs)) {
366 /* prefer modules with revisions, module with no revision
367 * is supposed to be the oldest one */
368 newest = mod;
369 date_newest = NULL;
370 } else {
371 if (!date_newest) {
372 if (newest->compiled) {
373 date_newest = newest->compiled->revs[0].date;
374 } else {
375 date_newest = newest->parsed->revs[0].date;
376 }
377 }
378 if (mod->compiled) {
379 date = mod->compiled->revs[0].date;
380 } else {
381 date = mod->parsed->revs[0].date;
382 }
383 if (strcmp(date, date_newest) > 0) {
384 /* the current module is newer than so far newest, so remember it */
385 newest = mod;
386 date_newest = NULL;
387 }
388 }
389 }
390 }
391
392 return newest;
393}
394
395API const struct lys_module *
396ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
397{
398 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
399 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lysp_module, name));
400}
401
402const struct lys_module *
403ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
404{
405 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
406 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lysp_module, ns));
407}
408
409/**
410 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
411 * @param[in] ctx Context where to search.
412 * @param[in] key Name or Namespace as a search key.
413 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
414 * @return Matching module if any.
415 */
416static const struct lys_module *
417ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
418{
419 const struct lys_module *mod;
420 unsigned int index = 0;
421
422 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
423 if ((mod->compiled && mod->compiled->implemented) || (!mod->compiled && mod->parsed->implemented)) {
424 return mod;
425 }
426 }
427
428 return NULL;
429}
430
431API const struct lys_module *
432ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
433{
434 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
435 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lysp_module, name));
436}
437
438API const struct lys_module *
439ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
440{
441 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
442 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lysp_module, ns));
443}
444
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200445API void
Radek Krejciad573502018-09-07 15:26:55 +0200446ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200447{
Radek Krejci418823a2018-09-20 16:42:13 +0200448 if (!ctx) {
449 return;
450 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200451
452 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200453 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200454 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200455 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200456 }
457 free(ctx->list.objs);
458
459 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200460 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200461
462 /* clean the error list */
463 ly_err_clean(ctx, 0);
464 pthread_key_delete(ctx->errlist_key);
465
466 /* dictionary */
467 lydict_clean(&ctx->dict);
468
469#if 0 /* TODO when plugins implemented */
470 /* plugins - will be removed only if this is the last context */
471 ly_clean_plugins();
472#endif
473
474 free(ctx);
475}