Improve trace output path and config path
[lttng-tools.git] / ltt-sessiond / session.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <urcu/list.h>
25
26 #include "lttngerr.h"
27 #include "session.h"
28
29 /* Variables */
30 static unsigned int session_count;
31
32 /* Static internal function */
33 static void add_session_list(struct ltt_session *ls);
34 static void del_session_list(struct ltt_session *ls);
35
36 /* Init global session list */
37 struct ltt_session_list ltt_session_list = {
38 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
39 };
40
41 /*
42 * get_session_list
43 *
44 * Return a pointer to the session list.
45 */
46 struct ltt_session_list *get_session_list(void)
47 {
48 return &ltt_session_list;
49 }
50
51 /*
52 * get_session_count
53 *
54 * Return session_count
55 */
56 unsigned int get_session_count(void)
57 {
58 return session_count;
59 }
60
61 /*
62 * add_session_list
63 *
64 * Add a ltt_session structure to the global list.
65 */
66 static void add_session_list(struct ltt_session *ls)
67 {
68 cds_list_add(&ls->list, &ltt_session_list.head);
69 session_count++;
70 }
71
72 /*
73 * del_session_list
74 *
75 * Delete a ltt_session structure to the global list.
76 */
77 static void del_session_list(struct ltt_session *ls)
78 {
79 cds_list_del(&ls->list);
80 /* Sanity check */
81 if (session_count != 0) {
82 session_count--;
83 }
84 }
85
86 /*
87 * find_session_by_uuid
88 *
89 * Return a ltt_session structure ptr that matches the uuid.
90 */
91 struct ltt_session *find_session_by_uuid(uuid_t session_id)
92 {
93 int found = 0;
94 struct ltt_session *iter;
95
96 /* Sanity check for NULL session_id */
97 if (uuid_is_null(session_id)) {
98 goto end;
99 }
100
101 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
102 if (uuid_compare(iter->uuid, session_id) == 0) {
103 found = 1;
104 break;
105 }
106 }
107
108 end:
109 if (!found) {
110 iter = NULL;
111 }
112 return iter;
113 }
114
115 /*
116 * find_session_by_name
117 *
118 * Return a ltt_session structure ptr that matches name.
119 * If no session found, NULL is returned.
120 */
121 struct ltt_session *find_session_by_name(char *name)
122 {
123 int found = 0;
124 struct ltt_session *iter;
125
126 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
127 if (strncmp(iter->name, name, strlen(name)) == 0) {
128 found = 1;
129 break;
130 }
131 }
132
133 if (!found) {
134 iter = NULL;
135 }
136
137 return iter;
138 }
139
140 /*
141 * destroy_session
142 *
143 * Delete session from the global session list
144 * and free the memory.
145 *
146 * Return -1 if no session is found.
147 * On success, return 1;
148 */
149 int destroy_session(char *name)
150 {
151 int found = -1;
152 struct ltt_session *iter;
153
154 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
155 if (strcmp(iter->name, name) == 0) {
156 DBG("Destroying session %s", iter->name);
157 del_session_list(iter);
158 free(iter);
159 found = 1;
160 break;
161 }
162 }
163
164 return found;
165 }
166
167 /*
168 * create_session
169 *
170 * Create a brand new session and add it to the global session list.
171 */
172 int create_session(char *name, char *path)
173 {
174 int ret;
175 char date_time[NAME_MAX];
176 struct ltt_session *new_session;
177 time_t rawtime;
178 struct tm *timeinfo;
179
180 new_session = find_session_by_name(name);
181 if (new_session != NULL) {
182 ret = -EEXIST;
183 goto error_exist;
184 }
185
186 /* Allocate session data structure */
187 new_session = malloc(sizeof(struct ltt_session));
188 if (new_session == NULL) {
189 perror("malloc");
190 ret = -ENOMEM;
191 goto error_malloc;
192 }
193
194 /* Define session name */
195 if (name != NULL) {
196 if (asprintf(&new_session->name, "%s", name) < 0) {
197 ret = -ENOMEM;
198 goto error_asprintf;
199 }
200 } else {
201 ERR("No session name given");
202 ret = -1;
203 goto error;
204 }
205
206 /* Define session system path */
207 if (path != NULL) {
208 if (strstr(name, "auto-") == NULL) {
209 time(&rawtime);
210 timeinfo = localtime(&rawtime);
211 strftime(date_time, sizeof(date_time), "-%Y%m%d-%H%M%S", timeinfo);
212 } else {
213 date_time[0] = '\0';
214 }
215
216 if (asprintf(&new_session->path, "%s/%s%s", path, name, date_time) < 0) {
217 ret = -ENOMEM;
218 goto error_asprintf;
219 }
220 } else {
221 ERR("No session path given");
222 ret = -1;
223 goto error;
224 }
225
226 /* UUID generation */
227 uuid_generate(new_session->uuid);
228
229 /*
230 * Set consumer (identifier) to 0. This means that there is
231 * NO consumer attach to that session yet.
232 */
233 new_session->ust_consumer = 0;
234
235 /* Init kernel session */
236 new_session->kernel_session = NULL;
237 new_session->kern_session_count = 0;
238
239 /* Init list */
240 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
241
242 /* Set trace list counter */
243 new_session->ust_trace_count = 0;
244
245 /* Add new session to the global session list */
246 add_session_list(new_session);
247
248 DBG("Tracing session %s created in %s", name, new_session->path);
249
250 return 0;
251
252 error:
253 error_asprintf:
254 if (new_session != NULL) {
255 free(new_session);
256 }
257
258 error_exist:
259 error_malloc:
260 return ret;
261 }
262
263 /*
264 * get_lttng_session
265 *
266 * Iterate over the global session list and fill the lttng_session array.
267 */
268 void get_lttng_session(struct lttng_session *sessions)
269 {
270 int i = 0;
271 struct ltt_session *iter;
272 struct lttng_session lsess;
273
274 DBG("Getting all available session");
275
276 /* Iterate over session list and append data after
277 * the control struct in the buffer.
278 */
279 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
280 strncpy(lsess.path, iter->path, sizeof(lsess.path));
281 lsess.path[sizeof(lsess.path) - 1] = '\0';
282 strncpy(lsess.name, iter->name, sizeof(lsess.name));
283 lsess.name[sizeof(lsess.name) - 1] = '\0';
284 memcpy(&sessions[i], &lsess, sizeof(lsess));
285 i++;
286 /* Reset struct for next pass */
287 memset(&lsess, 0, sizeof(lsess));
288 }
289 }
290
This page took 0.052259 seconds and 4 git commands to generate.