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