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 <lttng-sessiond-comm.h>
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().
40 * Init tracing session list.
42 * Please see session.h for more explanation and correct usage of the list.
44 static struct ltt_session_list ltt_session_list
= {
45 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
46 .lock
= PTHREAD_MUTEX_INITIALIZER
,
51 * Add a ltt_session structure to the global list.
53 * The caller MUST acquire the session list lock before.
55 static void add_session_list(struct ltt_session
*ls
)
57 cds_list_add(&ls
->list
, <t_session_list
.head
);
58 ltt_session_list
.count
++;
62 * Delete a ltt_session structure to the global list.
64 * The caller MUST acquire the session list lock before.
66 static void del_session_list(struct ltt_session
*ls
)
68 cds_list_del(&ls
->list
);
70 if (ltt_session_list
.count
> 0) {
71 ltt_session_list
.count
--;
76 * Return a pointer to the session list.
78 struct ltt_session_list
*session_get_list(void)
80 return <t_session_list
;
84 * Acquire session list lock
86 void session_lock_list(void)
88 pthread_mutex_lock(<t_session_list
.lock
);
92 * Release session list lock
94 void session_unlock_list(void)
96 pthread_mutex_unlock(<t_session_list
.lock
);
100 * Acquire session lock
102 void session_lock(struct ltt_session
*session
)
104 pthread_mutex_lock(&session
->lock
);
108 * Release session lock
110 void session_unlock(struct ltt_session
*session
)
112 pthread_mutex_unlock(&session
->lock
);
116 * Return a ltt_session structure ptr that matches name.
117 * If no session found, NULL is returned.
119 struct ltt_session
*session_find_by_name(char *name
)
122 struct ltt_session
*iter
;
125 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
126 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
131 session_unlock_list();
141 * Delete session from the session list and free the memory.
143 * Return -1 if no session is found. On success, return 1;
145 int session_destroy(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
);
161 session_unlock_list();
167 * Create a brand new session and add it to the session list.
169 int session_create(char *name
, char *path
)
172 struct ltt_session
*new_session
;
174 new_session
= session_find_by_name(name
);
175 if (new_session
!= NULL
) {
176 ret
= LTTCOMM_EXIST_SESS
;
180 /* Allocate session data structure */
181 new_session
= malloc(sizeof(struct ltt_session
));
182 if (new_session
== NULL
) {
188 /* Define session name */
190 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
195 ERR("No session name given");
200 /* Define session system path */
202 if (asprintf(&new_session
->path
, "%s", path
) < 0) {
207 ERR("No session path given");
212 /* Init kernel session */
213 new_session
->kernel_session
= NULL
;
215 /* Init UST session list */
216 CDS_INIT_LIST_HEAD(&new_session
->ust_session_list
.head
);
219 pthread_mutex_init(&new_session
->lock
, NULL
);
221 /* Add new session to the session list */
223 add_session_list(new_session
);
224 session_unlock_list();
226 DBG("Tracing session %s created in %s", name
, path
);
232 if (new_session
!= NULL
) {