Commit | Line | Data |
---|---|---|
71a559f8 JG |
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 | int parse_application_context(const char *str, char **out_provider_name, | |
29 | char **out_ctx_name) | |
30 | { | |
31 | const char app_ctx_prefix[] = "$app."; | |
32 | char *provider_name = NULL, *ctx_name = NULL; | |
33 | size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len; | |
34 | ||
35 | if (!str || !out_provider_name || !out_ctx_name) { | |
36 | goto not_found; | |
37 | } | |
38 | ||
39 | len = strlen(str); | |
40 | if (len <= sizeof(app_ctx_prefix) - 1) { | |
41 | goto not_found; | |
42 | } | |
43 | ||
44 | /* String starts with $app. */ | |
45 | if (strncmp(str, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) { | |
46 | goto not_found; | |
47 | } | |
48 | ||
49 | /* Validate that the ':' separator is present. */ | |
50 | for (i = sizeof(app_ctx_prefix); i < len; i++) { | |
51 | const char c = str[i]; | |
52 | ||
53 | if (c == ':') { | |
54 | colon_pos = i; | |
55 | break; | |
56 | } | |
57 | } | |
58 | ||
59 | /* | |
60 | * No colon found or no ctx name ("$app.provider:") or no provider name | |
61 | * given ("$app.:..."), which is invalid. | |
62 | */ | |
63 | if (!colon_pos || colon_pos == len || | |
64 | colon_pos == sizeof(app_ctx_prefix)) { | |
65 | goto not_found; | |
66 | } | |
67 | ||
68 | provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; | |
69 | provider_name = zmalloc(provider_name_len); | |
70 | if (!provider_name) { | |
71 | PERROR("malloc provider_name"); | |
72 | goto not_found; | |
73 | } | |
74 | strncpy(provider_name, str + sizeof(app_ctx_prefix) - 1, | |
75 | provider_name_len - 1); | |
76 | ||
77 | ctx_name_len = len - colon_pos; | |
78 | ctx_name = zmalloc(ctx_name_len); | |
79 | if (!ctx_name) { | |
80 | PERROR("malloc ctx_name"); | |
81 | goto not_found; | |
82 | } | |
83 | strncpy(ctx_name, str + colon_pos + 1, ctx_name_len - 1); | |
84 | ||
85 | *out_provider_name = provider_name; | |
86 | *out_ctx_name = ctx_name; | |
87 | return 0; | |
88 | not_found: | |
89 | free(provider_name); | |
90 | free(ctx_name); | |
91 | return -1; | |
92 | } | |
93 |