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 | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 26 | #include "common.h" |
| 27 | |
| 28 | #include "libyang.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 29 | |
| 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 Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 47 | |
| 48 | static 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 Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 55 | {"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 Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 62 | }; |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 63 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 64 | |
| 65 | API LY_ERR |
| 66 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 67 | { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 68 | struct stat st; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 69 | char *new_dir = NULL; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 70 | unsigned int u; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 71 | |
| 72 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 73 | |
| 74 | if (search_dir) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 75 | 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 Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 84 | new_dir = realpath(search_dir, NULL); |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 85 | 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 Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 88 | /* avoid path duplication */ |
| 89 | for (u = 0; u < ctx->search_paths.count; ++u) { |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 90 | if (!strcmp(new_dir, ctx->search_paths.objs[u])) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 91 | free(new_dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 92 | return LY_EEXIST; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 96 | free(new_dir); |
| 97 | return LY_EMEM; |
| 98 | } |
| 99 | |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 100 | return LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 101 | } else { |
| 102 | /* consider that no change is not actually an error */ |
| 103 | return LY_SUCCESS; |
| 104 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | API const char * const * |
| 108 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 109 | { |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 110 | void **new; |
| 111 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 112 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 113 | |
| 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 Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 124 | return (const char * const *)ctx->search_paths.objs; |
| 125 | } |
| 126 | |
| 127 | API LY_ERR |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 128 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 129 | { |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 130 | unsigned int index; |
| 131 | |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 132 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 133 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 134 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 135 | return LY_SUCCESS; |
| 136 | } |
| 137 | |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 138 | if (value) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 139 | /* remove specific search directory */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 140 | for (index = 0; index < ctx->search_paths.count; ++index) { |
| 141 | if (!strcmp(value, ctx->search_paths.objs[index])) { |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (index == ctx->search_paths.count) { |
| 146 | LOGARG(ctx, value); |
| 147 | return LY_EINVAL; |
| 148 | } else { |
| 149 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 150 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 151 | } else { |
| 152 | /* remove them all */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 153 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 154 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 155 | } |
| 156 | |
| 157 | return LY_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | API LY_ERR |
| 161 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 162 | { |
| 163 | struct ly_ctx *ctx = NULL; |
| 164 | struct lys_module *module; |
| 165 | char *search_dir_list; |
| 166 | char *sep, *dir; |
| 167 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 168 | |
| 169 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 170 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 171 | |
| 172 | /* dictionary */ |
| 173 | lydict_init(&ctx->dict); |
| 174 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 175 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 176 | /* plugins */ |
| 177 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 178 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 179 | |
| 180 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 181 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 182 | |
| 183 | /* models list */ |
| 184 | ctx->flags = options; |
| 185 | if (search_dir) { |
| 186 | search_dir_list = strdup(search_dir); |
| 187 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 188 | |
| 189 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 190 | *sep = 0; |
| 191 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 192 | if (rc == LY_EEXIST) { |
| 193 | /* ignore duplication */ |
| 194 | rc = LY_SUCCESS; |
| 195 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 196 | } |
| 197 | if (*dir && rc == LY_SUCCESS) { |
| 198 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 199 | if (rc == LY_EEXIST) { |
| 200 | /* ignore duplication */ |
| 201 | rc = LY_SUCCESS; |
| 202 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 203 | } |
| 204 | free(search_dir_list); |
| 205 | |
| 206 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 207 | if (rc != LY_SUCCESS) { |
| 208 | goto error; |
| 209 | } |
| 210 | } |
| 211 | ctx->module_set_id = 1; |
| 212 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 213 | #if 0 /* TODO when parser implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 214 | /* load internal modules */ |
| 215 | for (i = 0; i < (options & LY_CTX_NOYANGLIBRARY) ? LY_INTERNAL_MODS_COUNT - 2 : LY_INTERNAL_MODS_COUNT; i++) { |
| 216 | module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format); |
| 217 | LY_CHECK_GOTO(!module, error); |
| 218 | module->parsed->implemented = internal_modules[i].implemented; |
| 219 | } |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 220 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 221 | |
| 222 | *new_ctx = ctx; |
| 223 | return rc; |
| 224 | |
| 225 | error: |
| 226 | ly_ctx_destroy(ctx, NULL); |
| 227 | return rc; |
| 228 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 229 | API int |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 230 | ly_ctx_get_options(const struct ly_ctx *ctx) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 231 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 232 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 233 | return ctx->flags; |
| 234 | } |
| 235 | |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 236 | API LY_ERR |
| 237 | ly_ctx_set_option(struct ly_ctx *ctx, int option) |
| 238 | { |
| 239 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 240 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 241 | |
| 242 | /* set the option(s) */ |
| 243 | ctx->flags |= option; |
| 244 | |
| 245 | return LY_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | API LY_ERR |
| 249 | ly_ctx_unset_option(struct ly_ctx *ctx, int option) |
| 250 | { |
| 251 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 252 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 253 | |
| 254 | /* unset the option(s) */ |
| 255 | ctx->flags &= ~option; |
| 256 | |
| 257 | return LY_SUCCESS; |
| 258 | } |
| 259 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 260 | API uint16_t |
| 261 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 262 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 263 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 264 | return ctx->module_set_id; |
| 265 | } |
| 266 | |
| 267 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 268 | 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] | 269 | { |
| 270 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 271 | |
| 272 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 273 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 274 | /* remove the module */ |
| 275 | #if 0 /* TODO when parser implemented */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 276 | lys_free(ctx->list[ctx->list.count - 1], private_destructor, 1, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 277 | #endif |
| 278 | } |
| 279 | free(ctx->list.objs); |
| 280 | |
| 281 | /* search paths list */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 282 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 283 | |
| 284 | /* clean the error list */ |
| 285 | ly_err_clean(ctx, 0); |
| 286 | pthread_key_delete(ctx->errlist_key); |
| 287 | |
| 288 | /* dictionary */ |
| 289 | lydict_clean(&ctx->dict); |
| 290 | |
| 291 | #if 0 /* TODO when plugins implemented */ |
| 292 | /* plugins - will be removed only if this is the last context */ |
| 293 | ly_clean_plugins(); |
| 294 | #endif |
| 295 | |
| 296 | free(ctx); |
| 297 | } |