Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 15 | #define _DEFAULT_SOURCE |
| 16 | #define _BSD_SOURCE |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Radek Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 18 | #include <limits.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 23 | #include "context.h" |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 24 | #include "common.h" |
| 25 | |
| 26 | #include "libyang.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 27 | |
| 28 | #define LY_INTERNAL_MODS_COUNT 0 /* TODO 6 when parser available */ |
| 29 | |
| 30 | #define IETF_YANG_METADATA_PATH "../models/ietf-yang-metadata@2016-08-05.h" |
| 31 | #define YANG_PATH "../models/yang@2017-02-20.h" |
| 32 | #define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h" |
| 33 | #define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h" |
| 34 | #define IETF_DATASTORES "../models/ietf-datastores@2017-08-17.h" |
| 35 | #define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2018-01-17.h" |
| 36 | #define IETF_YANG_LIB_REV "2018-01-17" |
| 37 | |
| 38 | #if LY_INTERNAL_MODS_COUNT |
| 39 | #include IETF_YANG_METADATA_PATH |
| 40 | #include YANG_PATH |
| 41 | #include IETF_INET_TYPES_PATH |
| 42 | #include IETF_YANG_TYPES_PATH |
| 43 | #include IETF_DATASTORES |
| 44 | #include IETF_YANG_LIB_PATH |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 45 | |
| 46 | static struct internal_modules_s { |
| 47 | const char *name; |
| 48 | const char *revision; |
| 49 | const char *data; |
| 50 | uint8_t implemented; |
| 51 | LYS_INFORMAT format; |
| 52 | } internal_modules[LY_INTERNAL_MODS_COUNT] = { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 53 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yin, 0, LYS_IN_YIN}, |
| 54 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yin, 1, LYS_IN_YIN}, |
| 55 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
| 56 | {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
| 57 | /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */ |
| 58 | {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yin, 0, LYS_IN_YIN}, |
| 59 | {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yin, 1, LYS_IN_YIN} |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 60 | }; |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 61 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 62 | |
| 63 | API LY_ERR |
| 64 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 65 | { |
| 66 | char *new_dir = NULL; |
| 67 | LY_ERR rc = LY_ESYS; |
| 68 | |
| 69 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 70 | |
| 71 | if (search_dir) { |
| 72 | LY_CHECK_ERR_GOTO(access(search_dir, R_OK | X_OK), |
| 73 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)), |
| 74 | cleanup); |
| 75 | new_dir = realpath(search_dir, NULL); |
| 76 | LY_CHECK_ERR_GOTO(!new_dir, |
| 77 | LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)), |
| 78 | cleanup); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 79 | if (ly_set_add(&ctx->search_paths, new_dir, 0) == -1) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 80 | free(new_dir); |
| 81 | return LY_EMEM; |
| 82 | } |
| 83 | |
| 84 | rc = LY_SUCCESS; |
| 85 | } else { |
| 86 | /* consider that no change is not actually an error */ |
| 87 | return LY_SUCCESS; |
| 88 | } |
| 89 | |
| 90 | cleanup: |
| 91 | free(new_dir); |
| 92 | return rc; |
| 93 | } |
| 94 | |
| 95 | API const char * const * |
| 96 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 97 | { |
| 98 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 99 | return (const char * const *)ctx->search_paths.objs; |
| 100 | } |
| 101 | |
| 102 | API LY_ERR |
| 103 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index) |
| 104 | { |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame^] | 105 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 106 | return LY_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | if (index >= 0) { |
| 110 | /* remove specific search directory */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 111 | return ly_set_rm_index(&ctx->search_paths, index); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 112 | } else { |
| 113 | /* remove them all */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame^] | 114 | for (; ctx->search_paths.count; ctx->search_paths.count--) { |
| 115 | free(ctx->search_paths.objs[ctx->search_paths.count - 1]); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 116 | } |
| 117 | free(ctx->search_paths.objs); |
| 118 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 119 | } |
| 120 | |
| 121 | return LY_SUCCESS; |
| 122 | } |
| 123 | |
| 124 | API LY_ERR |
| 125 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 126 | { |
| 127 | struct ly_ctx *ctx = NULL; |
| 128 | struct lys_module *module; |
| 129 | char *search_dir_list; |
| 130 | char *sep, *dir; |
| 131 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 132 | |
| 133 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 134 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 135 | |
| 136 | /* dictionary */ |
| 137 | lydict_init(&ctx->dict); |
| 138 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 139 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 140 | /* plugins */ |
| 141 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 142 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 143 | |
| 144 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 145 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 146 | |
| 147 | /* models list */ |
| 148 | ctx->flags = options; |
| 149 | if (search_dir) { |
| 150 | search_dir_list = strdup(search_dir); |
| 151 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 152 | |
| 153 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 154 | *sep = 0; |
| 155 | rc = ly_ctx_set_searchdir(ctx, dir); |
| 156 | } |
| 157 | if (*dir && rc == LY_SUCCESS) { |
| 158 | rc = ly_ctx_set_searchdir(ctx, dir); |
| 159 | } |
| 160 | free(search_dir_list); |
| 161 | |
| 162 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 163 | if (rc != LY_SUCCESS) { |
| 164 | goto error; |
| 165 | } |
| 166 | } |
| 167 | ctx->module_set_id = 1; |
| 168 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 169 | #if 0 /* TODO when parser implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 170 | /* load internal modules */ |
| 171 | for (i = 0; i < (options & LY_CTX_NOYANGLIBRARY) ? LY_INTERNAL_MODS_COUNT - 2 : LY_INTERNAL_MODS_COUNT; i++) { |
| 172 | module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format); |
| 173 | LY_CHECK_GOTO(!module, error); |
| 174 | module->parsed->implemented = internal_modules[i].implemented; |
| 175 | } |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 176 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 177 | |
| 178 | *new_ctx = ctx; |
| 179 | return rc; |
| 180 | |
| 181 | error: |
| 182 | ly_ctx_destroy(ctx, NULL); |
| 183 | return rc; |
| 184 | } |
| 185 | |
| 186 | API void |
| 187 | ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx) |
| 188 | { |
| 189 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 190 | ctx->flags |= LY_CTX_DISABLE_SEARCHDIRS; |
| 191 | } |
| 192 | |
| 193 | API void |
| 194 | ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx) |
| 195 | { |
| 196 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 197 | ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIRS; |
| 198 | } |
| 199 | |
| 200 | API void |
| 201 | ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx) |
| 202 | { |
| 203 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 204 | ctx->flags |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 205 | } |
| 206 | |
| 207 | API void |
| 208 | ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx) |
| 209 | { |
| 210 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 211 | ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 212 | } |
| 213 | |
| 214 | API void |
| 215 | ly_ctx_set_prefer_searchdirs(struct ly_ctx *ctx) |
| 216 | { |
| 217 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 218 | ctx->flags |= LY_CTX_PREFER_SEARCHDIRS; |
| 219 | } |
| 220 | |
| 221 | API void |
| 222 | ly_ctx_unset_prefer_searchdirs(struct ly_ctx *ctx) |
| 223 | { |
| 224 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 225 | ctx->flags &= ~LY_CTX_PREFER_SEARCHDIRS; |
| 226 | } |
| 227 | |
| 228 | API void |
| 229 | ly_ctx_set_allimplemented(struct ly_ctx *ctx) |
| 230 | { |
| 231 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 232 | ctx->flags |= LY_CTX_ALLIMPLEMENTED; |
| 233 | } |
| 234 | |
| 235 | API void |
| 236 | ly_ctx_unset_allimplemented(struct ly_ctx *ctx) |
| 237 | { |
| 238 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 239 | ctx->flags &= ~LY_CTX_ALLIMPLEMENTED; |
| 240 | } |
| 241 | |
| 242 | API void |
| 243 | ly_ctx_set_trusted(struct ly_ctx *ctx) |
| 244 | { |
| 245 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 246 | ctx->flags |= LY_CTX_TRUSTED; |
| 247 | } |
| 248 | |
| 249 | API void |
| 250 | ly_ctx_unset_trusted(struct ly_ctx *ctx) |
| 251 | { |
| 252 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 253 | ctx->flags &= ~LY_CTX_TRUSTED; |
| 254 | } |
| 255 | |
| 256 | API int |
| 257 | ly_ctx_get_options(struct ly_ctx *ctx) |
| 258 | { |
| 259 | return ctx->flags; |
| 260 | } |
| 261 | |
| 262 | API uint16_t |
| 263 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 264 | { |
| 265 | return ctx->module_set_id; |
| 266 | } |
| 267 | |
| 268 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 269 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 270 | { |
| 271 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 272 | |
| 273 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame^] | 274 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 275 | /* remove the module */ |
| 276 | #if 0 /* TODO when parser implemented */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame^] | 277 | lys_free(ctx->list[ctx->list.count - 1], private_destructor, 1, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 278 | #endif |
| 279 | } |
| 280 | free(ctx->list.objs); |
| 281 | |
| 282 | /* search paths list */ |
| 283 | ly_ctx_unset_searchdirs(ctx, -1); |
| 284 | |
| 285 | /* clean the error list */ |
| 286 | ly_err_clean(ctx, 0); |
| 287 | pthread_key_delete(ctx->errlist_key); |
| 288 | |
| 289 | /* dictionary */ |
| 290 | lydict_clean(&ctx->dict); |
| 291 | |
| 292 | #if 0 /* TODO when plugins implemented */ |
| 293 | /* plugins - will be removed only if this is the last context */ |
| 294 | ly_clean_plugins(); |
| 295 | #endif |
| 296 | |
| 297 | free(ctx); |
| 298 | } |