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