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.
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().
39 * Init tracing session list.
41 * Please see session.h for more explanation and correct usage of the list.
43 static struct ltt_session_list ltt_session_list
= {
44 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
45 .lock
= PTHREAD_MUTEX_INITIALIZER
,
50 * Add a ltt_session structure to the global list.
52 * The caller MUST acquire the session list lock before.
54 static void add_session_list(struct ltt_session
*ls
)
56 cds_list_add(&ls
->list
, <t_session_list
.head
);
57 ltt_session_list
.count
++;
61 * Delete a ltt_session structure to the global list.
63 * The caller MUST acquire the session list lock before.
65 static void del_session_list(struct ltt_session
*ls
)
67 cds_list_del(&ls
->list
);
69 if (ltt_session_list
.count
> 0) {
70 ltt_session_list
.count
--;
75 * Return a pointer to the session list.
77 struct ltt_session_list
*get_session_list(void)
79 return <t_session_list
;
83 * Acquire session list lock
85 void lock_session_list(void)
87 pthread_mutex_lock(<t_session_list
.lock
);
91 * Release session list lock
93 void unlock_session_list(void)
95 pthread_mutex_unlock(<t_session_list
.lock
);
99 * Acquire session lock
101 void lock_session(struct ltt_session
*session
)
103 pthread_mutex_lock(&session
->lock
);
107 * Release session lock
109 void unlock_session(struct ltt_session
*session
)
111 pthread_mutex_unlock(&session
->lock
);
115 * Return a ltt_session structure ptr that matches name.
116 * If no session found, NULL is returned.
118 struct ltt_session
*find_session_by_name(char *name
)
121 struct ltt_session
*iter
;
124 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
125 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
130 unlock_session_list();
140 * Delete session from the session list and free the memory.
142 * Return -1 if no session is found. On success, return 1;
144 int destroy_session(char *name
)
147 struct ltt_session
*iter
, *tmp
;
150 cds_list_for_each_entry_safe(iter
, tmp
, <t_session_list
.head
, list
) {
151 if (strcmp(iter
->name
, name
) == 0) {
152 DBG("Destroying session %s", iter
->name
);
153 del_session_list(iter
);
156 pthread_mutex_destroy(&iter
->lock
);
162 unlock_session_list();
168 * Create a brand new session and add it to the session list.
170 int create_session(char *name
, char *path
)
173 struct ltt_session
*new_session
;
175 new_session
= find_session_by_name(name
);
176 if (new_session
!= NULL
) {
181 /* Allocate session data structure */
182 new_session
= malloc(sizeof(struct ltt_session
));
183 if (new_session
== NULL
) {
189 /* Define session name */
191 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
196 ERR("No session name given");
201 /* Define session system path */
203 if (asprintf(&new_session
->path
, "%s", path
) < 0) {
208 ERR("No session path given");
213 /* Init kernel session */
214 new_session
->kernel_session
= NULL
;
216 /* Init UST session list */
217 CDS_INIT_LIST_HEAD(&new_session
->ust_session_list
.head
);
220 pthread_mutex_init(&new_session
->lock
, NULL
);
222 /* Add new session to the session list */
224 add_session_list(new_session
);
225 unlock_session_list();
227 DBG("Tracing session %s created in %s", name
, path
);
233 if (new_session
!= NULL
) {