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