| 1 | /* |
| 2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@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 _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <lttng/lttng-error.h> |
| 23 | #include <lttng/save.h> |
| 24 | #include <lttng/save-internal.h> |
| 25 | #include <common/sessiond-comm/sessiond-comm.h> |
| 26 | |
| 27 | #include "lttng-ctl-helper.h" |
| 28 | |
| 29 | struct lttng_save_session_attr *lttng_save_session_attr_create(void) |
| 30 | { |
| 31 | return zmalloc(sizeof(struct lttng_save_session_attr)); |
| 32 | } |
| 33 | |
| 34 | void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output) |
| 35 | { |
| 36 | if (output) { |
| 37 | free(output); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const char *lttng_save_session_attr_get_session_name( |
| 42 | struct lttng_save_session_attr *attr) |
| 43 | { |
| 44 | const char *ret = NULL; |
| 45 | |
| 46 | if (attr && attr->session_name[0]) { |
| 47 | ret = attr->session_name; |
| 48 | } |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | const char *lttng_save_session_attr_get_output_url( |
| 54 | struct lttng_save_session_attr *attr) |
| 55 | { |
| 56 | const char *ret = NULL; |
| 57 | |
| 58 | if (attr && attr->configuration_url[0]) { |
| 59 | ret = attr->configuration_url; |
| 60 | } |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | int lttng_save_session_attr_get_overwrite( |
| 66 | struct lttng_save_session_attr *attr) |
| 67 | { |
| 68 | return attr ? attr->overwrite : -LTTNG_ERR_INVALID; |
| 69 | } |
| 70 | |
| 71 | int lttng_save_session_attr_get_omit_name( |
| 72 | struct lttng_save_session_attr *attr) |
| 73 | { |
| 74 | return attr ? attr->omit_name : -LTTNG_ERR_INVALID; |
| 75 | } |
| 76 | |
| 77 | int lttng_save_session_attr_get_omit_output( |
| 78 | struct lttng_save_session_attr *attr) |
| 79 | { |
| 80 | return attr ? attr->omit_output : -LTTNG_ERR_INVALID; |
| 81 | } |
| 82 | |
| 83 | int lttng_save_session_attr_set_session_name( |
| 84 | struct lttng_save_session_attr *attr, const char *session_name) |
| 85 | { |
| 86 | int ret = 0; |
| 87 | |
| 88 | if (!attr) { |
| 89 | ret = -LTTNG_ERR_INVALID; |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | if (session_name) { |
| 94 | size_t len; |
| 95 | |
| 96 | len = strlen(session_name); |
| 97 | if (len >= LTTNG_NAME_MAX) { |
| 98 | ret = -LTTNG_ERR_INVALID; |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | ret = lttng_strncpy(attr->session_name, session_name, |
| 103 | sizeof(attr->session_name)); |
| 104 | if (ret) { |
| 105 | ret = -LTTNG_ERR_INVALID; |
| 106 | goto error; |
| 107 | } |
| 108 | } else { |
| 109 | attr->session_name[0] = '\0'; |
| 110 | } |
| 111 | error: |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | int lttng_save_session_attr_set_output_url( |
| 116 | struct lttng_save_session_attr *attr, const char *url) |
| 117 | { |
| 118 | int ret = 0; |
| 119 | size_t len; |
| 120 | ssize_t size; |
| 121 | struct lttng_uri *uris = NULL; |
| 122 | |
| 123 | if (!attr) { |
| 124 | ret = -LTTNG_ERR_INVALID; |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | if (!url) { |
| 129 | attr->configuration_url[0] = '\0'; |
| 130 | ret = 0; |
| 131 | goto end; |
| 132 | } |
| 133 | |
| 134 | len = strlen(url); |
| 135 | if (len >= PATH_MAX) { |
| 136 | ret = -LTTNG_ERR_INVALID; |
| 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | size = uri_parse_str_urls(url, NULL, &uris); |
| 141 | if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) { |
| 142 | ret = -LTTNG_ERR_INVALID; |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | /* Copy string plus the NULL terminated byte. */ |
| 147 | lttng_ctl_copy_string(attr->configuration_url, uris[0].dst.path, |
| 148 | sizeof(attr->configuration_url)); |
| 149 | |
| 150 | end: |
| 151 | error: |
| 152 | free(uris); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | int lttng_save_session_attr_set_overwrite( |
| 157 | struct lttng_save_session_attr *attr, int overwrite) |
| 158 | { |
| 159 | int ret = 0; |
| 160 | |
| 161 | if (!attr) { |
| 162 | ret = -LTTNG_ERR_INVALID; |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | attr->overwrite = !!overwrite; |
| 167 | end: |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | int lttng_save_session_attr_set_omit_name( |
| 172 | struct lttng_save_session_attr *attr, int omit_name) |
| 173 | { |
| 174 | int ret = 0; |
| 175 | |
| 176 | if (!attr) { |
| 177 | ret = -LTTNG_ERR_INVALID; |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | attr->omit_name = !!omit_name; |
| 182 | end: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | int lttng_save_session_attr_set_omit_output( |
| 187 | struct lttng_save_session_attr *attr, int omit_output) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | |
| 191 | if (!attr) { |
| 192 | ret = -LTTNG_ERR_INVALID; |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | attr->omit_output = !!omit_output; |
| 197 | end: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * The lttng-ctl API does not expose all the information needed to save the |
| 203 | * session configurations. Thus, we must send a save command to the session |
| 204 | * daemon which will, in turn, save its current session configuration. |
| 205 | */ |
| 206 | int lttng_save_session(struct lttng_save_session_attr *attr) |
| 207 | { |
| 208 | struct lttcomm_session_msg lsm; |
| 209 | int ret; |
| 210 | |
| 211 | if (!attr) { |
| 212 | ret = -LTTNG_ERR_INVALID; |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | memset(&lsm, 0, sizeof(lsm)); |
| 217 | lsm.cmd_type = LTTNG_SAVE_SESSION; |
| 218 | |
| 219 | memcpy(&lsm.u.save_session.attr, attr, |
| 220 | sizeof(struct lttng_save_session_attr)); |
| 221 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 222 | end: |
| 223 | return ret; |
| 224 | } |