| 1 | /* |
| 2 | * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <lttng/lttng-error.h> |
| 23 | #include <lttng/load.h> |
| 24 | #include <lttng/load-internal.h> |
| 25 | #include <common/sessiond-comm/sessiond-comm.h> |
| 26 | #include <common/config/config.h> |
| 27 | |
| 28 | #include "lttng-ctl-helper.h" |
| 29 | |
| 30 | struct lttng_load_session_attr *lttng_load_session_attr_create(void) |
| 31 | { |
| 32 | return zmalloc(sizeof(struct lttng_load_session_attr)); |
| 33 | } |
| 34 | |
| 35 | void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr) |
| 36 | { |
| 37 | if (attr) { |
| 38 | free(attr); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const char *lttng_load_session_attr_get_session_name( |
| 43 | struct lttng_load_session_attr *attr) |
| 44 | { |
| 45 | const char *ret = NULL; |
| 46 | |
| 47 | if (attr && attr->session_name[0]) { |
| 48 | ret = attr->session_name; |
| 49 | } |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | const char *lttng_load_session_attr_get_input_url( |
| 55 | struct lttng_load_session_attr *attr) |
| 56 | { |
| 57 | const char *ret = NULL; |
| 58 | |
| 59 | if (attr && attr->input_url[0]) { |
| 60 | ret = attr->input_url; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | int lttng_load_session_attr_get_overwrite( |
| 67 | struct lttng_load_session_attr *attr) |
| 68 | { |
| 69 | return attr ? attr->overwrite : -LTTNG_ERR_INVALID; |
| 70 | } |
| 71 | |
| 72 | int lttng_load_session_attr_set_session_name( |
| 73 | struct lttng_load_session_attr *attr, const char *session_name) |
| 74 | { |
| 75 | int ret = 0; |
| 76 | |
| 77 | if (!attr) { |
| 78 | ret = -LTTNG_ERR_INVALID; |
| 79 | goto error; |
| 80 | } |
| 81 | |
| 82 | if (session_name) { |
| 83 | size_t len; |
| 84 | |
| 85 | len = strlen(session_name); |
| 86 | if (len >= NAME_MAX) { |
| 87 | ret = -LTTNG_ERR_INVALID; |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | strncpy(attr->session_name, session_name, len); |
| 92 | } else { |
| 93 | attr->session_name[0] = '\0'; |
| 94 | } |
| 95 | error: |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int lttng_load_session_attr_set_input_url( |
| 100 | struct lttng_load_session_attr *attr, const char *url) |
| 101 | { |
| 102 | int ret = 0; |
| 103 | size_t len, size; |
| 104 | struct lttng_uri *uris = NULL; |
| 105 | |
| 106 | if (!attr) { |
| 107 | ret = -LTTNG_ERR_INVALID; |
| 108 | goto error; |
| 109 | } |
| 110 | |
| 111 | if (!url) { |
| 112 | attr->input_url[0] = '\0'; |
| 113 | ret = 0; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | len = strlen(url); |
| 118 | if (len >= PATH_MAX) { |
| 119 | ret = -LTTNG_ERR_INVALID; |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | size = uri_parse_str_urls(url, NULL, &uris); |
| 124 | if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) { |
| 125 | ret = -LTTNG_ERR_INVALID; |
| 126 | goto error; |
| 127 | } |
| 128 | |
| 129 | /* Copy string plus the NULL terminated byte. */ |
| 130 | lttng_ctl_copy_string(attr->input_url, uris[0].dst.path, |
| 131 | sizeof(attr->input_url)); |
| 132 | |
| 133 | end: |
| 134 | error: |
| 135 | free(uris); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | int lttng_load_session_attr_set_overwrite( |
| 140 | struct lttng_load_session_attr *attr, int overwrite) |
| 141 | { |
| 142 | int ret = 0; |
| 143 | |
| 144 | if (!attr) { |
| 145 | ret = -LTTNG_ERR_INVALID; |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | attr->overwrite = !!overwrite; |
| 150 | end: |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * The lttng-ctl API does not expose all the information needed to load the |
| 156 | * session configurations. Thus, we must send a load command to the session |
| 157 | * daemon which will, in turn, load its current session configuration. |
| 158 | */ |
| 159 | int lttng_load_session(struct lttng_load_session_attr *attr) |
| 160 | { |
| 161 | int ret; |
| 162 | |
| 163 | if (!attr) { |
| 164 | ret = -LTTNG_ERR_INVALID; |
| 165 | goto end; |
| 166 | } |
| 167 | |
| 168 | ret = config_load_session(attr->input_url, attr->session_name, |
| 169 | attr->overwrite, 0); |
| 170 | |
| 171 | end: |
| 172 | return ret; |
| 173 | } |