| 1 | /* |
| 2 | * context.c |
| 3 | * |
| 4 | * LTTng context utilities. |
| 5 | * |
| 6 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "context.h" |
| 23 | #include <stddef.h> |
| 24 | #include <string.h> |
| 25 | #include <common/error.h> |
| 26 | #include <common/macros.h> |
| 27 | |
| 28 | LTTNG_HIDDEN |
| 29 | int parse_application_context(const char *str, char **out_provider_name, |
| 30 | char **out_ctx_name) |
| 31 | { |
| 32 | const char app_ctx_prefix[] = "$app."; |
| 33 | char *provider_name = NULL, *ctx_name = NULL; |
| 34 | size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len; |
| 35 | |
| 36 | if (!str || !out_provider_name || !out_ctx_name) { |
| 37 | goto not_found; |
| 38 | } |
| 39 | |
| 40 | len = strlen(str); |
| 41 | if (len <= sizeof(app_ctx_prefix) - 1) { |
| 42 | goto not_found; |
| 43 | } |
| 44 | |
| 45 | /* String starts with $app. */ |
| 46 | if (strncmp(str, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) { |
| 47 | goto not_found; |
| 48 | } |
| 49 | |
| 50 | /* Validate that the ':' separator is present. */ |
| 51 | for (i = sizeof(app_ctx_prefix); i < len; i++) { |
| 52 | const char c = str[i]; |
| 53 | |
| 54 | if (c == ':') { |
| 55 | colon_pos = i; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * No colon found or no ctx name ("$app.provider:") or no provider name |
| 62 | * given ("$app.:..."), which is invalid. |
| 63 | */ |
| 64 | if (!colon_pos || colon_pos == len || |
| 65 | colon_pos == sizeof(app_ctx_prefix)) { |
| 66 | goto not_found; |
| 67 | } |
| 68 | |
| 69 | provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; |
| 70 | provider_name = zmalloc(provider_name_len); |
| 71 | if (!provider_name) { |
| 72 | PERROR("malloc provider_name"); |
| 73 | goto not_found; |
| 74 | } |
| 75 | strncpy(provider_name, str + sizeof(app_ctx_prefix) - 1, |
| 76 | provider_name_len - 1); |
| 77 | |
| 78 | ctx_name_len = len - colon_pos; |
| 79 | ctx_name = zmalloc(ctx_name_len); |
| 80 | if (!ctx_name) { |
| 81 | PERROR("malloc ctx_name"); |
| 82 | goto not_found; |
| 83 | } |
| 84 | strncpy(ctx_name, str + colon_pos + 1, ctx_name_len - 1); |
| 85 | |
| 86 | *out_provider_name = provider_name; |
| 87 | *out_ctx_name = ctx_name; |
| 88 | return 0; |
| 89 | not_found: |
| 90 | free(provider_name); |
| 91 | free(ctx_name); |
| 92 | return -1; |
| 93 | } |
| 94 | |