blob: 386ee054db36c30584c88137cbdc0b9fd9a7e319 [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 Krejci7ada9a02018-09-07 15:34:05 +020015#define _DEFAULT_SOURCE
16#define _BSD_SOURCE
Radek Krejci0af5f5d2018-09-07 15:00:30 +020017#include <errno.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020018#include <limits.h>
19#include <stdlib.h>
20#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020021#include <sys/stat.h>
22#include <sys/types.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020023#include <unistd.h>
24
Radek Krejci0af5f5d2018-09-07 15:00:30 +020025#include "context.h"
Radek Krejciad573502018-09-07 15:26:55 +020026#include "common.h"
27
28#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020029
30#define LY_INTERNAL_MODS_COUNT 0 /* TODO 6 when parser available */
31
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
40#if LY_INTERNAL_MODS_COUNT
41#include IETF_YANG_METADATA_PATH
42#include YANG_PATH
43#include IETF_INET_TYPES_PATH
44#include IETF_YANG_TYPES_PATH
45#include IETF_DATASTORES
46#include IETF_YANG_LIB_PATH
Radek Krejci0af5f5d2018-09-07 15:00:30 +020047
48static struct internal_modules_s {
49 const char *name;
50 const char *revision;
51 const char *data;
52 uint8_t implemented;
53 LYS_INFORMAT format;
54} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020055 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yin, 0, LYS_IN_YIN},
56 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yin, 1, LYS_IN_YIN},
57 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yin, 0, LYS_IN_YIN},
58 {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yin, 0, LYS_IN_YIN},
59 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
60 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yin, 0, LYS_IN_YIN},
61 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yin, 1, LYS_IN_YIN}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020062};
Radek Krejciad573502018-09-07 15:26:55 +020063#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +020064
65API LY_ERR
66ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
67{
Radek Krejci0e212402018-09-20 11:29:38 +020068 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020070 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020071
72 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
73
74 if (search_dir) {
Radek Krejci0e212402018-09-20 11:29:38 +020075 LY_CHECK_ERR_RET(access(search_dir, R_OK | X_OK),
76 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)),
77 LY_EINVAL);
78 LY_CHECK_ERR_RET(stat(search_dir, &st),
79 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s)", search_dir, strerror(errno)),
80 LY_ESYS);
81 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
82 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", search_dir),
83 LY_EINVAL);
Radek Krejci0af5f5d2018-09-07 15:00:30 +020084 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020085 LY_CHECK_ERR_RET(!new_dir,
86 LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)),
87 LY_ESYS);
Radek Krejci0e212402018-09-20 11:29:38 +020088 /* avoid path duplication */
89 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020090 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020091 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020092 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020093 }
94 }
95 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020096 free(new_dir);
97 return LY_EMEM;
98 }
99
Radek Krejci0e212402018-09-20 11:29:38 +0200100 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200101 } else {
102 /* consider that no change is not actually an error */
103 return LY_SUCCESS;
104 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200105}
106
107API const char * const *
108ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
109{
Radek Krejci05026432018-09-20 12:13:43 +0200110 void **new;
111
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200112 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200113
114 if (ctx->search_paths.count == ctx->search_paths.size) {
115 /* not enough space for terminating NULL byte */
116 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
117 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
118 ((struct ly_ctx *)ctx)->search_paths.size += 8;
119 ((struct ly_ctx *)ctx)->search_paths.objs = new;
120 }
121 /* set terminating NULL byte to the strings list */
122 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
123
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200124 return (const char * const *)ctx->search_paths.objs;
125}
126
127API LY_ERR
128ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index)
129{
Radek Krejcicd649072018-09-20 12:14:45 +0200130 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
131 LY_CHECK_ERR_RET(index >= 0 && (unsigned int)index >= ctx->search_paths.count, LOGARG(ctx, index), LY_EINVAL);
132
Michal Vaskob34480a2018-09-17 10:34:45 +0200133 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200134 return LY_SUCCESS;
135 }
136
137 if (index >= 0) {
138 /* remove specific search directory */
Radek Krejci820d2262018-09-20 12:15:31 +0200139 return ly_set_rm_index(&ctx->search_paths, index, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200140 } else {
141 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200142 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200143 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
144 }
145
146 return LY_SUCCESS;
147}
148
149API LY_ERR
150ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
151{
152 struct ly_ctx *ctx = NULL;
153 struct lys_module *module;
154 char *search_dir_list;
155 char *sep, *dir;
156 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200157
158 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200159 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200160
161 /* dictionary */
162 lydict_init(&ctx->dict);
163
Radek Krejciad573502018-09-07 15:26:55 +0200164#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200165 /* plugins */
166 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200167#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200168
169 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200170 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200171
172 /* models list */
173 ctx->flags = options;
174 if (search_dir) {
175 search_dir_list = strdup(search_dir);
176 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
177
178 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
179 *sep = 0;
180 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200181 if (rc == LY_EEXIST) {
182 /* ignore duplication */
183 rc = LY_SUCCESS;
184 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200185 }
186 if (*dir && rc == LY_SUCCESS) {
187 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200188 if (rc == LY_EEXIST) {
189 /* ignore duplication */
190 rc = LY_SUCCESS;
191 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200192 }
193 free(search_dir_list);
194
195 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
196 if (rc != LY_SUCCESS) {
197 goto error;
198 }
199 }
200 ctx->module_set_id = 1;
201
Radek Krejciad573502018-09-07 15:26:55 +0200202#if 0 /* TODO when parser implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200203 /* load internal modules */
204 for (i = 0; i < (options & LY_CTX_NOYANGLIBRARY) ? LY_INTERNAL_MODS_COUNT - 2 : LY_INTERNAL_MODS_COUNT; i++) {
205 module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format);
206 LY_CHECK_GOTO(!module, error);
207 module->parsed->implemented = internal_modules[i].implemented;
208 }
Radek Krejciad573502018-09-07 15:26:55 +0200209#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200210
211 *new_ctx = ctx;
212 return rc;
213
214error:
215 ly_ctx_destroy(ctx, NULL);
216 return rc;
217}
218
219API void
220ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx)
221{
222 LY_CHECK_ARG_RET(ctx, ctx,);
223 ctx->flags |= LY_CTX_DISABLE_SEARCHDIRS;
224}
225
226API void
227ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx)
228{
229 LY_CHECK_ARG_RET(ctx, ctx,);
230 ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIRS;
231}
232
233API void
234ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx)
235{
236 LY_CHECK_ARG_RET(ctx, ctx,);
237 ctx->flags |= LY_CTX_DISABLE_SEARCHDIR_CWD;
238}
239
240API void
241ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx)
242{
243 LY_CHECK_ARG_RET(ctx, ctx,);
244 ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
245}
246
247API void
248ly_ctx_set_prefer_searchdirs(struct ly_ctx *ctx)
249{
250 LY_CHECK_ARG_RET(ctx, ctx,);
251 ctx->flags |= LY_CTX_PREFER_SEARCHDIRS;
252}
253
254API void
255ly_ctx_unset_prefer_searchdirs(struct ly_ctx *ctx)
256{
257 LY_CHECK_ARG_RET(ctx, ctx,);
258 ctx->flags &= ~LY_CTX_PREFER_SEARCHDIRS;
259}
260
261API void
262ly_ctx_set_allimplemented(struct ly_ctx *ctx)
263{
264 LY_CHECK_ARG_RET(ctx, ctx,);
265 ctx->flags |= LY_CTX_ALLIMPLEMENTED;
266}
267
268API void
269ly_ctx_unset_allimplemented(struct ly_ctx *ctx)
270{
271 LY_CHECK_ARG_RET(ctx, ctx,);
272 ctx->flags &= ~LY_CTX_ALLIMPLEMENTED;
273}
274
275API void
276ly_ctx_set_trusted(struct ly_ctx *ctx)
277{
278 LY_CHECK_ARG_RET(ctx, ctx,);
279 ctx->flags |= LY_CTX_TRUSTED;
280}
281
282API void
283ly_ctx_unset_trusted(struct ly_ctx *ctx)
284{
285 LY_CHECK_ARG_RET(ctx, ctx,);
286 ctx->flags &= ~LY_CTX_TRUSTED;
287}
288
289API int
290ly_ctx_get_options(struct ly_ctx *ctx)
291{
292 return ctx->flags;
293}
294
295API uint16_t
296ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
297{
298 return ctx->module_set_id;
299}
300
301API void
Radek Krejciad573502018-09-07 15:26:55 +0200302ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200303{
304 LY_CHECK_ARG_RET(ctx, ctx,);
305
306 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200307 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200308 /* remove the module */
309#if 0 /* TODO when parser implemented */
Michal Vaskob34480a2018-09-17 10:34:45 +0200310 lys_free(ctx->list[ctx->list.count - 1], private_destructor, 1, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200311#endif
312 }
313 free(ctx->list.objs);
314
315 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200316 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200317
318 /* clean the error list */
319 ly_err_clean(ctx, 0);
320 pthread_key_delete(ctx->errlist_key);
321
322 /* dictionary */
323 lydict_clean(&ctx->dict);
324
325#if 0 /* TODO when plugins implemented */
326 /* plugins - will be removed only if this is the last context */
327 ly_clean_plugins();
328#endif
329
330 free(ctx);
331}