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