Add a save API to lttng-ctl
[lttng-tools.git] / src / lib / lttng-ctl / save.c
... / ...
CommitLineData
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 _GNU_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
29struct lttng_save_session_attr *lttng_save_session_attr_create(void)
30{
31 return zmalloc(sizeof(struct lttng_save_session_attr));
32}
33
34void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output)
35{
36 if (output) {
37 free(output);
38 }
39}
40
41const 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
53const 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
65int lttng_save_session_attr_get_overwrite(
66 struct lttng_save_session_attr *attr)
67{
68 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
69}
70
71int lttng_save_session_attr_set_session_name(
72 struct lttng_save_session_attr *attr, const char *session_name)
73{
74 int ret = 0;
75
76 if (!attr) {
77 ret = -LTTNG_ERR_INVALID;
78 goto error;
79 }
80
81 if (session_name) {
82 size_t len;
83
84 len = strlen(session_name);
85 if (len >= NAME_MAX) {
86 ret = -LTTNG_ERR_INVALID;
87 goto error;
88 }
89
90 strncpy(attr->session_name, session_name, len);
91 } else {
92 attr->session_name[0] = '\0';
93 }
94error:
95 return ret;
96}
97
98int lttng_save_session_attr_set_output_url(
99 struct lttng_save_session_attr *attr, const char *url)
100{
101 int ret = 0;
102
103 if (!attr) {
104 ret = -LTTNG_ERR_INVALID;
105 goto error;
106 }
107
108 if (url) {
109 size_t len;
110
111 len = strlen(url);
112 if (len >= PATH_MAX) {
113 ret = -LTTNG_ERR_INVALID;
114 goto error;
115 }
116
117 strncpy(attr->configuration_url, url, len);
118 } else {
119 attr->configuration_url[0] = '\0';
120 }
121error:
122 return ret;
123}
124
125int lttng_save_session_attr_set_overwrite(
126 struct lttng_save_session_attr *attr, int overwrite)
127{
128 int ret = 0;
129
130 if (!attr) {
131 ret = -LTTNG_ERR_INVALID;
132 goto end;
133 }
134
135 attr->overwrite = !!overwrite;
136end:
137 return ret;
138}
139
140/*
141 * The lttng-ctl API does not expose all the information needed to save the
142 * session configurations. Thus, we must send a save command to the session
143 * daemon which will, in turn, save its current session configuration.
144 */
145int lttng_save_session(struct lttng_save_session_attr *attr)
146{
147 struct lttcomm_session_msg lsm;
148 int ret;
149
150 if (!attr) {
151 ret = -LTTNG_ERR_INVALID;
152 goto end;
153 }
154
155 memset(&lsm, 0, sizeof(lsm));
156 lsm.cmd_type = LTTNG_SAVE_SESSION;
157
158 memcpy(&lsm.u.save_session.attr, attr,
159 sizeof(struct lttng_save_session_attr));
160 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
161end:
162 return ret;
163}
This page took 0.024828 seconds and 5 git commands to generate.