Add support for UST application registration
[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 <urcu/list.h>
26
27 #include <lttngerr.h>
28
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 a ltt_session structure to the global list.
53 *
54 * The caller MUST acquire the session list lock before.
55 */
56 static void add_session_list(struct ltt_session *ls)
57 {
58 cds_list_add(&ls->list, &ltt_session_list.head);
59 ltt_session_list.count++;
60 }
61
62 /*
63 * Delete a ltt_session structure to the global list.
64 *
65 * The caller MUST acquire the session list lock before.
66 */
67 static void del_session_list(struct ltt_session *ls)
68 {
69 cds_list_del(&ls->list);
70 /* Sanity check */
71 if (ltt_session_list.count > 0) {
72 ltt_session_list.count--;
73 }
74 }
75
76 /*
77 * Return a pointer to the session list.
78 */
79 struct ltt_session_list *get_session_list(void)
80 {
81 return &ltt_session_list;
82 }
83
84 /*
85 * Acquire session list lock
86 */
87 void lock_session_list(void)
88 {
89 pthread_mutex_lock(&ltt_session_list.lock);
90 }
91
92 /*
93 * Release session list lock
94 */
95 void unlock_session_list(void)
96 {
97 pthread_mutex_unlock(&ltt_session_list.lock);
98 }
99
100 /*
101 * Acquire session lock
102 */
103 void lock_session(struct ltt_session *session)
104 {
105 pthread_mutex_lock(&session->lock);
106 }
107
108 /*
109 * Release session lock
110 */
111 void unlock_session(struct ltt_session *session)
112 {
113 pthread_mutex_unlock(&session->lock);
114 }
115
116 /*
117 * Return a ltt_session structure ptr that matches name.
118 * If no session found, NULL is returned.
119 */
120 struct ltt_session *find_session_by_name(char *name)
121 {
122 int found = 0;
123 struct ltt_session *iter;
124
125 lock_session_list();
126 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
127 if (strncmp(iter->name, name, NAME_MAX) == 0) {
128 found = 1;
129 break;
130 }
131 }
132 unlock_session_list();
133
134 if (!found) {
135 iter = NULL;
136 }
137
138 return iter;
139 }
140
141 /*
142 * Delete session from the session list and free the memory.
143 *
144 * Return -1 if no session is found. On success, return 1;
145 */
146 int destroy_session(char *name)
147 {
148 int found = -1;
149 struct ltt_session *iter, *tmp;
150
151 lock_session_list();
152 cds_list_for_each_entry_safe(iter, tmp, &ltt_session_list.head, list) {
153 if (strcmp(iter->name, name) == 0) {
154 DBG("Destroying session %s", iter->name);
155 del_session_list(iter);
156 free(iter->name);
157 free(iter->path);
158 pthread_mutex_destroy(&iter->lock);
159 free(iter);
160 found = 1;
161 break;
162 }
163 }
164 unlock_session_list();
165
166 return found;
167 }
168
169 /*
170 * Create a brand new session and add it to the session list.
171 */
172 int create_session(char *name, char *path)
173 {
174 int ret;
175 struct ltt_session *new_session;
176
177 new_session = find_session_by_name(name);
178 if (new_session != NULL) {
179 ret = -EEXIST;
180 goto error_exist;
181 }
182
183 /* Allocate session data structure */
184 new_session = malloc(sizeof(struct ltt_session));
185 if (new_session == NULL) {
186 perror("malloc");
187 ret = -ENOMEM;
188 goto error_malloc;
189 }
190
191 /* Define session name */
192 if (name != NULL) {
193 if (asprintf(&new_session->name, "%s", name) < 0) {
194 ret = -ENOMEM;
195 goto error_asprintf;
196 }
197 } else {
198 ERR("No session name given");
199 ret = -1;
200 goto error;
201 }
202
203 /* Define session system path */
204 if (path != NULL) {
205 if (asprintf(&new_session->path, "%s", path) < 0) {
206 ret = -ENOMEM;
207 goto error_asprintf;
208 }
209 } else {
210 ERR("No session path given");
211 ret = -1;
212 goto error;
213 }
214
215 /* Init kernel session */
216 new_session->kernel_session = NULL;
217
218 /* Init list */
219 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
220
221 /* Set trace list counter */
222 new_session->ust_trace_count = 0;
223
224 /* Add new session to the session list */
225 lock_session_list();
226 add_session_list(new_session);
227 unlock_session_list();
228
229 /* Init lock */
230 pthread_mutex_init(&new_session->lock, NULL);
231
232 DBG("Tracing session %s created in %s", new_session->name, new_session->path);
233
234 return 0;
235
236 error:
237 error_asprintf:
238 if (new_session != NULL) {
239 free(new_session);
240 }
241
242 error_exist:
243 error_malloc:
244 return ret;
245 }
This page took 0.033564 seconds and 4 git commands to generate.