Fix bad cleanup of context structure
[lttng-tools.git] / ltt-sessiond / session.c
CommitLineData
5b74c7b1
DG
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.
91d76f53 8 *
5b74c7b1
DG
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
b5541356 20#include <pthread.h>
5b74c7b1
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
b082db07 24#include <time.h>
5b74c7b1 25#include <urcu/list.h>
5b74c7b1
DG
26
27#include "lttngerr.h"
28#include "session.h"
29
8c0faa1d 30/*
b5541356 31 * NOTES:
8c0faa1d 32 *
b5541356
DG
33 * No ltt_session.lock is taken here because those data structure are widely
34 * spread across the lttng-tools code base so before caling functions below
35 * that can read/write a session, the caller MUST acquire the session lock
36 * using lock_session() and unlock_session().
8c0faa1d 37 */
8c0faa1d 38
5b74c7b1 39/*
b5541356 40 * Init tracing session list.
5b74c7b1 41 *
b5541356 42 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 43 */
b5541356
DG
44static struct ltt_session_list ltt_session_list = {
45 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
46 .lock = PTHREAD_MUTEX_INITIALIZER,
47 .count = 0,
48};
5b74c7b1
DG
49
50/*
51 * add_session_list
52 *
53 * Add a ltt_session structure to the global list.
b5541356
DG
54 *
55 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
56 */
57static void add_session_list(struct ltt_session *ls)
58{
59 cds_list_add(&ls->list, &ltt_session_list.head);
b5541356 60 ltt_session_list.count++;
5b74c7b1
DG
61}
62
63/*
64 * del_session_list
65 *
66 * Delete a ltt_session structure to the global list.
b5541356
DG
67 *
68 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
69 */
70static void del_session_list(struct ltt_session *ls)
71{
72 cds_list_del(&ls->list);
73 /* Sanity check */
b5541356
DG
74 if (ltt_session_list.count > 0) {
75 ltt_session_list.count--;
5b74c7b1
DG
76 }
77}
78
b5541356
DG
79/*
80 * get_session_list
81 *
82 * Return a pointer to the session list.
83 */
84struct ltt_session_list *get_session_list(void)
85{
86 return &ltt_session_list;
87}
88
89/*
90 * Acquire session lock
91 */
92void lock_session(struct ltt_session *session)
93{
94 pthread_mutex_lock(&session->lock);
95}
96
97/*
98 * Release session lock
99 */
100void unlock_session(struct ltt_session *session)
101{
102 pthread_mutex_unlock(&session->lock);
103}
104
105/*
106 * get_session_count
107 *
108 * Return session_count
109 */
110unsigned int get_session_count(void)
111{
112 unsigned int count;
113
114 pthread_mutex_lock(&ltt_session_list.lock);
115 count = ltt_session_list.count;
116 pthread_mutex_unlock(&ltt_session_list.lock);
117
118 return count;
119}
120
5b74c7b1
DG
121/*
122 * find_session_by_name
123 *
124 * Return a ltt_session structure ptr that matches name.
125 * If no session found, NULL is returned.
126 */
127struct ltt_session *find_session_by_name(char *name)
128{
129 int found = 0;
130 struct ltt_session *iter;
131
b5541356 132 pthread_mutex_lock(&ltt_session_list.lock);
5b74c7b1 133 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
b42dc92c 134 if (strncmp(iter->name, name, strlen(name)) == 0) {
5b74c7b1
DG
135 found = 1;
136 break;
137 }
138 }
b5541356 139 pthread_mutex_unlock(&ltt_session_list.lock);
5b74c7b1
DG
140
141 if (!found) {
142 iter = NULL;
143 }
144
145 return iter;
146}
147
148/*
149 * destroy_session
150 *
b5541356 151 * Delete session from the session list and free the memory.
5b74c7b1 152 *
b5541356 153 * Return -1 if no session is found. On success, return 1;
5b74c7b1 154 */
f3ed775e 155int destroy_session(char *name)
5b74c7b1
DG
156{
157 int found = -1;
158 struct ltt_session *iter;
159
b5541356 160 pthread_mutex_lock(&ltt_session_list.lock);
5b74c7b1 161 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
f3ed775e 162 if (strcmp(iter->name, name) == 0) {
e07ae692 163 DBG("Destroying session %s", iter->name);
5b74c7b1 164 del_session_list(iter);
b5541356
DG
165 free(iter->name);
166 free(iter->path);
167 pthread_mutex_destroy(&iter->lock);
5b74c7b1
DG
168 free(iter);
169 found = 1;
170 break;
171 }
172 }
b5541356 173 pthread_mutex_unlock(&ltt_session_list.lock);
5b74c7b1
DG
174
175 return found;
176}
177
178/*
179 * create_session
180 *
b5541356 181 * Create a brand new session and add it to the session list.
5b74c7b1 182 */
f3ed775e 183int create_session(char *name, char *path)
5b74c7b1 184{
f3ed775e 185 int ret;
b082db07 186 char date_time[NAME_MAX];
5b74c7b1 187 struct ltt_session *new_session;
b082db07
DG
188 time_t rawtime;
189 struct tm *timeinfo;
e07ae692 190
5b74c7b1
DG
191 new_session = find_session_by_name(name);
192 if (new_session != NULL) {
f3ed775e
DG
193 ret = -EEXIST;
194 goto error_exist;
5b74c7b1
DG
195 }
196
197 /* Allocate session data structure */
198 new_session = malloc(sizeof(struct ltt_session));
199 if (new_session == NULL) {
200 perror("malloc");
f3ed775e
DG
201 ret = -ENOMEM;
202 goto error_malloc;
5b74c7b1
DG
203 }
204
f3ed775e 205 /* Define session name */
5b74c7b1
DG
206 if (name != NULL) {
207 if (asprintf(&new_session->name, "%s", name) < 0) {
f3ed775e
DG
208 ret = -ENOMEM;
209 goto error_asprintf;
5b74c7b1
DG
210 }
211 } else {
f3ed775e
DG
212 ERR("No session name given");
213 ret = -1;
214 goto error;
215 }
216
217 /* Define session system path */
218 if (path != NULL) {
b082db07
DG
219 if (strstr(name, "auto-") == NULL) {
220 time(&rawtime);
221 timeinfo = localtime(&rawtime);
222 strftime(date_time, sizeof(date_time), "-%Y%m%d-%H%M%S", timeinfo);
223 } else {
224 date_time[0] = '\0';
225 }
226
227 if (asprintf(&new_session->path, "%s/%s%s", path, name, date_time) < 0) {
f3ed775e
DG
228 ret = -ENOMEM;
229 goto error_asprintf;
5b74c7b1 230 }
f3ed775e
DG
231 } else {
232 ERR("No session path given");
233 ret = -1;
234 goto error;
5b74c7b1
DG
235 }
236
1d4b027a
DG
237 /* Init kernel session */
238 new_session->kernel_session = NULL;
5b74c7b1
DG
239
240 /* Init list */
241 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
242
243 /* Set trace list counter */
244 new_session->ust_trace_count = 0;
5b74c7b1 245
b5541356
DG
246 /* Add new session to the session list */
247 pthread_mutex_lock(&ltt_session_list.lock);
5b74c7b1 248 add_session_list(new_session);
b5541356
DG
249 pthread_mutex_unlock(&ltt_session_list.lock);
250
251 /* Init lock */
252 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 253
b5541356 254 DBG("Tracing session %s created in %s", new_session->name, new_session->path);
b082db07 255
5b74c7b1
DG
256 return 0;
257
258error:
f3ed775e
DG
259error_asprintf:
260 if (new_session != NULL) {
261 free(new_session);
262 }
5b74c7b1 263
f3ed775e
DG
264error_exist:
265error_malloc:
266 return ret;
5b74c7b1
DG
267}
268
269/*
270 * get_lttng_session
271 *
f3ed775e 272 * Iterate over the global session list and fill the lttng_session array.
5b74c7b1 273 */
5461b305 274void get_lttng_session(struct lttng_session *sessions)
5b74c7b1
DG
275{
276 int i = 0;
277 struct ltt_session *iter;
278 struct lttng_session lsess;
279
e07ae692
DG
280 DBG("Getting all available session");
281
b5541356
DG
282 /*
283 * Iterate over session list and append data after the control struct in
284 * the buffer.
5b74c7b1 285 */
b5541356 286 pthread_mutex_lock(&ltt_session_list.lock);
5b74c7b1 287 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
f3ed775e
DG
288 strncpy(lsess.path, iter->path, sizeof(lsess.path));
289 lsess.path[sizeof(lsess.path) - 1] = '\0';
5b74c7b1
DG
290 strncpy(lsess.name, iter->name, sizeof(lsess.name));
291 lsess.name[sizeof(lsess.name) - 1] = '\0';
5461b305 292 memcpy(&sessions[i], &lsess, sizeof(lsess));
5b74c7b1
DG
293 i++;
294 /* Reset struct for next pass */
295 memset(&lsess, 0, sizeof(lsess));
296 }
b5541356 297 pthread_mutex_unlock(&ltt_session_list.lock);
5b74c7b1
DG
298}
299
This page took 0.038416 seconds and 5 git commands to generate.