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.
20 #include <lttng/liblttngctl.h>
24 #include <urcu/list.h>
25 #include <uuid/uuid.h>
31 static unsigned int session_count
;
33 /* Static internal function */
34 static void add_session_list(struct ltt_session
*ls
);
35 static void del_session_list(struct ltt_session
*ls
);
37 /* Init session's list */
38 static struct ltt_session_list ltt_session_list
= {
39 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
45 * Return session_count
47 unsigned int get_session_count(void)
55 * Add a ltt_session structure to the global list.
57 static void add_session_list(struct ltt_session
*ls
)
59 cds_list_add(&ls
->list
, <t_session_list
.head
);
66 * Delete a ltt_session structure to the global list.
68 static void del_session_list(struct ltt_session
*ls
)
70 cds_list_del(&ls
->list
);
72 if (session_count
!= 0) {
78 * find_session_by_uuid
80 * Return a ltt_session structure ptr that matches the uuid.
82 struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
85 struct ltt_session
*iter
;
87 /* Sanity check for NULL session_id */
88 if (uuid_is_null(session_id
)) {
92 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
93 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
107 * find_session_by_name
109 * Return a ltt_session structure ptr that matches name.
110 * If no session found, NULL is returned.
112 struct ltt_session
*find_session_by_name(char *name
)
115 struct ltt_session
*iter
;
117 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
118 if (strncmp(iter
->name
, name
, strlen(iter
->name
)) == 0) {
134 * Delete session from the global session list
135 * and free the memory.
137 * Return -1 if no session is found.
138 * On success, return 1;
140 int destroy_session(uuid_t
*uuid
)
143 struct ltt_session
*iter
;
145 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
146 if (uuid_compare(iter
->uuid
, *uuid
) == 0) {
147 del_session_list(iter
);
160 * Create a brand new session and add it to the
161 * global session list.
163 int create_session(char *name
, uuid_t
*session_id
)
165 struct ltt_session
*new_session
;
167 new_session
= find_session_by_name(name
);
168 if (new_session
!= NULL
) {
172 /* Allocate session data structure */
173 new_session
= malloc(sizeof(struct ltt_session
));
174 if (new_session
== NULL
) {
180 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
184 /* Generate session name based on the session count */
185 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
190 /* UUID generation */
191 uuid_generate(new_session
->uuid
);
192 uuid_copy(*session_id
, new_session
->uuid
);
194 /* Set consumer (identifier) to 0. This means that there is
195 * NO consumer attach to that session yet.
197 new_session
->ust_consumer
= 0;
198 new_session
->lttng_consumer
= 0;
201 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
202 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
204 /* Add new session to the global session list */
205 add_session_list(new_session
);
219 * Iterate over the global session list and
220 * fill the lttng_session array.
222 void get_lttng_session(struct lttng_session
*lt
)
225 struct ltt_session
*iter
;
226 struct lttng_session lsess
;
228 /* Iterate over session list and append data after
229 * the control struct in the buffer.
231 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
232 /* Copy name and uuid */
233 uuid_unparse(iter
->uuid
, lsess
.uuid
);
234 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
235 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
236 memcpy(<
[i
], &lsess
, sizeof(lsess
));
238 /* Reset struct for next pass */
239 memset(&lsess
, 0, sizeof(lsess
));