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 global session list */
36 struct ltt_session_list ltt_session_list
= {
37 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
43 * Return a pointer to the session list.
45 struct ltt_session_list
*get_session_list(void)
47 return <t_session_list
;
53 * Return session_count
55 unsigned int get_session_count(void)
63 * Add a ltt_session structure to the global list.
65 static void add_session_list(struct ltt_session
*ls
)
67 cds_list_add(&ls
->list
, <t_session_list
.head
);
74 * Delete a ltt_session structure to the global list.
76 static void del_session_list(struct ltt_session
*ls
)
78 cds_list_del(&ls
->list
);
80 if (session_count
!= 0) {
86 * find_session_by_uuid
88 * Return a ltt_session structure ptr that matches the uuid.
90 struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
93 struct ltt_session
*iter
;
95 /* Sanity check for NULL session_id */
96 if (uuid_is_null(session_id
)) {
100 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
101 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
115 * find_session_by_name
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
;
125 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
126 if (strncmp(iter
->name
, name
, strlen(name
)) == 0) {
142 * Delete session from the global session list
143 * and free the memory.
145 * Return -1 if no session is found.
146 * On success, return 1;
148 int destroy_session(uuid_t
*uuid
)
151 struct ltt_session
*iter
;
153 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
154 if (uuid_compare(iter
->uuid
, *uuid
) == 0) {
155 DBG("Destroying session %s", iter
->name
);
156 del_session_list(iter
);
169 * Create a brand new session and add it to the
170 * global session list.
172 int create_session(char *name
, uuid_t
*session_id
)
174 struct ltt_session
*new_session
;
176 DBG("Creating session %s", name
);
178 new_session
= find_session_by_name(name
);
179 if (new_session
!= NULL
) {
183 /* Allocate session data structure */
184 new_session
= malloc(sizeof(struct ltt_session
));
185 if (new_session
== NULL
) {
191 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
195 /* Generate session name based on the session count */
196 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
201 /* UUID generation */
202 uuid_generate(new_session
->uuid
);
203 uuid_copy(*session_id
, new_session
->uuid
);
206 * Set consumer (identifier) to 0. This means that there is
207 * NO consumer attach to that session yet.
209 new_session
->ust_consumer
= 0;
211 /* Init kernel session */
212 new_session
->kernel_session
= NULL
;
213 new_session
->kern_session_count
= 0;
216 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
218 /* Set trace list counter */
219 new_session
->ust_trace_count
= 0;
221 /* Add new session to the global session list */
222 add_session_list(new_session
);
236 * Iterate over the global session list and
237 * fill the lttng_session array.
239 void get_lttng_session(struct lttng_session
*sessions
)
242 struct ltt_session
*iter
;
243 struct lttng_session lsess
;
245 DBG("Getting all available session");
247 /* Iterate over session list and append data after
248 * the control struct in the buffer.
250 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
251 /* Copy name and uuid */
252 uuid_copy(lsess
.uuid
, iter
->uuid
);
253 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
254 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
255 memcpy(&sessions
[i
], &lsess
, sizeof(lsess
));
257 /* Reset struct for next pass */
258 memset(&lsess
, 0, sizeof(lsess
));