2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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
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.
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.
25 #include <urcu/list.h>
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().
41 * Init tracing session list.
43 * Please see session.h for more explanation and correct usage of the list.
45 static struct ltt_session_list ltt_session_list
= {
46 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
47 .lock
= PTHREAD_MUTEX_INITIALIZER
,
52 * Add a ltt_session structure to the global list.
54 * The caller MUST acquire the session list lock before.
56 static void add_session_list(struct ltt_session
*ls
)
58 cds_list_add(&ls
->list
, <t_session_list
.head
);
59 ltt_session_list
.count
++;
63 * Delete a ltt_session structure to the global list.
65 * The caller MUST acquire the session list lock before.
67 static void del_session_list(struct ltt_session
*ls
)
69 cds_list_del(&ls
->list
);
71 if (ltt_session_list
.count
> 0) {
72 ltt_session_list
.count
--;
77 * Return a pointer to the session list.
79 struct ltt_session_list
*get_session_list(void)
81 return <t_session_list
;
85 * Acquire session list lock
87 void lock_session_list(void)
89 pthread_mutex_lock(<t_session_list
.lock
);
93 * Release session list lock
95 void unlock_session_list(void)
97 pthread_mutex_unlock(<t_session_list
.lock
);
101 * Acquire session lock
103 void lock_session(struct ltt_session
*session
)
105 pthread_mutex_lock(&session
->lock
);
109 * Release session lock
111 void unlock_session(struct ltt_session
*session
)
113 pthread_mutex_unlock(&session
->lock
);
117 * Return a ltt_session structure ptr that matches name.
118 * If no session found, NULL is returned.
120 struct ltt_session
*find_session_by_name(char *name
)
123 struct ltt_session
*iter
;
126 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
127 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
132 unlock_session_list();
142 * Delete session from the session list and free the memory.
144 * Return -1 if no session is found. On success, return 1;
146 int destroy_session(char *name
)
149 struct ltt_session
*iter
, *tmp
;
152 cds_list_for_each_entry_safe(iter
, tmp
, <t_session_list
.head
, list
) {
153 if (strcmp(iter
->name
, name
) == 0) {
154 DBG("Destroying session %s", iter
->name
);
155 del_session_list(iter
);
158 pthread_mutex_destroy(&iter
->lock
);
164 unlock_session_list();
170 * Create a brand new session and add it to the session list.
172 int create_session(char *name
, char *path
)
175 struct ltt_session
*new_session
;
177 new_session
= find_session_by_name(name
);
178 if (new_session
!= NULL
) {
183 /* Allocate session data structure */
184 new_session
= malloc(sizeof(struct ltt_session
));
185 if (new_session
== NULL
) {
191 /* Define session name */
193 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
198 ERR("No session name given");
203 /* Define session system path */
205 if (asprintf(&new_session
->path
, "%s", path
) < 0) {
210 ERR("No session path given");
215 /* Init kernel session */
216 new_session
->kernel_session
= NULL
;
219 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
221 /* Set trace list counter */
222 new_session
->ust_trace_count
= 0;
224 /* Add new session to the session list */
226 add_session_list(new_session
);
227 unlock_session_list();
230 pthread_mutex_init(&new_session
->lock
, NULL
);
232 DBG("Tracing session %s created in %s", new_session
->name
, new_session
->path
);
238 if (new_session
!= NULL
) {