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; either version 2
7 * of the License, or (at your option) any later version.
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.
23 #include <urcu/list.h>
29 static unsigned int session_count
;
31 /* Static internal function */
32 static void add_session_list(struct ltt_session
*ls
);
33 static void del_session_list(struct ltt_session
*ls
);
35 /* Init session's list */
36 static struct ltt_session_list ltt_session_list
= {
37 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
43 * Return session_count
45 unsigned int get_session_count(void)
53 * Add a ltt_session structure to the global list.
55 static void add_session_list(struct ltt_session
*ls
)
57 cds_list_add(&ls
->list
, <t_session_list
.head
);
64 * Delete a ltt_session structure to the global list.
66 static void del_session_list(struct ltt_session
*ls
)
68 cds_list_del(&ls
->list
);
70 if (session_count
!= 0) {
76 * find_session_by_uuid
78 * Return a ltt_session structure ptr that matches the uuid.
80 struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
83 struct ltt_session
*iter
;
85 /* Sanity check for NULL session_id */
86 if (uuid_is_null(session_id
)) {
90 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
91 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
105 * find_session_by_name
107 * Return a ltt_session structure ptr that matches name.
108 * If no session found, NULL is returned.
110 struct ltt_session
*find_session_by_name(char *name
)
113 struct ltt_session
*iter
;
115 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
116 if (strncmp(iter
->name
, name
, strlen(name
)) == 0) {
132 * Delete session from the global session list
133 * and free the memory.
135 * Return -1 if no session is found.
136 * On success, return 1;
138 int destroy_session(uuid_t
*uuid
)
141 struct ltt_session
*iter
;
143 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
144 if (uuid_compare(iter
->uuid
, *uuid
) == 0) {
145 DBG("Destroying session %s", iter
->name
);
146 del_session_list(iter
);
159 * Create a brand new session and add it to the
160 * global session list.
162 int create_session(char *name
, uuid_t
*session_id
)
164 struct ltt_session
*new_session
;
166 DBG("Creating session %s", name
);
168 new_session
= find_session_by_name(name
);
169 if (new_session
!= NULL
) {
173 /* Allocate session data structure */
174 new_session
= malloc(sizeof(struct ltt_session
));
175 if (new_session
== NULL
) {
181 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
185 /* Generate session name based on the session count */
186 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
191 /* UUID generation */
192 uuid_generate(new_session
->uuid
);
193 uuid_copy(*session_id
, new_session
->uuid
);
195 /* Set consumer (identifier) to 0. This means that there is
196 * NO consumer attach to that session yet.
198 new_session
->ust_consumer
= 0;
199 new_session
->kernel_consumer
= 0;
202 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
203 CDS_INIT_LIST_HEAD(&new_session
->kernel_traces
);
205 /* Set trace list counter */
206 new_session
->ust_trace_count
= 0;
207 new_session
->kern_trace_count
= 0;
209 /* Add new session to the global session list */
210 add_session_list(new_session
);
224 * Iterate over the global session list and
225 * fill the lttng_session array.
227 void get_lttng_session(struct lttng_session
*sessions
)
230 struct ltt_session
*iter
;
231 struct lttng_session lsess
;
233 DBG("Getting all available session");
235 /* Iterate over session list and append data after
236 * the control struct in the buffer.
238 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
239 /* Copy name and uuid */
240 uuid_copy(lsess
.uuid
, iter
->uuid
);
241 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
242 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
243 memcpy(&sessions
[i
], &lsess
, sizeof(lsess
));
245 /* Reset struct for next pass */
246 memset(&lsess
, 0, sizeof(lsess
));