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