3131006f19254072119d41ecccfc3d37f9c1c95c
[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, NAME_MAX) == 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 struct ltt_session *new_session;
188
189 new_session = find_session_by_name(name);
190 if (new_session != NULL) {
191 ret = -EEXIST;
192 goto error_exist;
193 }
194
195 /* Allocate session data structure */
196 new_session = malloc(sizeof(struct ltt_session));
197 if (new_session == NULL) {
198 perror("malloc");
199 ret = -ENOMEM;
200 goto error_malloc;
201 }
202
203 /* Define session name */
204 if (name != NULL) {
205 if (asprintf(&new_session->name, "%s", name) < 0) {
206 ret = -ENOMEM;
207 goto error_asprintf;
208 }
209 } else {
210 ERR("No session name given");
211 ret = -1;
212 goto error;
213 }
214
215 /* Define session system path */
216 if (path != NULL) {
217 if (asprintf(&new_session->path, "%s", path) < 0) {
218 ret = -ENOMEM;
219 goto error_asprintf;
220 }
221 } else {
222 ERR("No session path given");
223 ret = -1;
224 goto error;
225 }
226
227 /* Init kernel session */
228 new_session->kernel_session = NULL;
229
230 /* Init list */
231 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
232
233 /* Set trace list counter */
234 new_session->ust_trace_count = 0;
235
236 /* Add new session to the session list */
237 lock_session_list();
238 add_session_list(new_session);
239 unlock_session_list();
240
241 /* Init lock */
242 pthread_mutex_init(&new_session->lock, NULL);
243
244 DBG("Tracing session %s created in %s", new_session->name, new_session->path);
245
246 return 0;
247
248 error:
249 error_asprintf:
250 if (new_session != NULL) {
251 free(new_session);
252 }
253
254 error_exist:
255 error_malloc:
256 return ret;
257 }
This page took 0.05096 seconds and 4 git commands to generate.