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 DBG("Destroying session %s", iter
->name
);
148 del_session_list(iter
);
161 * Create a brand new session and add it to the
162 * global session list.
164 int create_session(char *name
, uuid_t
*session_id
)
166 struct ltt_session
*new_session
;
168 DBG("Creating session %s", name
);
170 new_session
= find_session_by_name(name
);
171 if (new_session
!= NULL
) {
175 /* Allocate session data structure */
176 new_session
= malloc(sizeof(struct ltt_session
));
177 if (new_session
== NULL
) {
183 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
187 /* Generate session name based on the session count */
188 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
193 /* UUID generation */
194 uuid_generate(new_session
->uuid
);
195 uuid_copy(*session_id
, new_session
->uuid
);
197 /* Set consumer (identifier) to 0. This means that there is
198 * NO consumer attach to that session yet.
200 new_session
->ust_consumer
= 0;
201 new_session
->kernel_consumer
= 0;
204 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
205 CDS_INIT_LIST_HEAD(&new_session
->kernel_traces
);
207 /* Set trace list counter */
208 new_session
->ust_trace_count
= 0;
209 new_session
->kern_trace_count
= 0;
211 /* Add new session to the global session list */
212 add_session_list(new_session
);
226 * Iterate over the global session list and
227 * fill the lttng_session array.
229 void get_lttng_session(struct lttng_session
*lt
)
232 struct ltt_session
*iter
;
233 struct lttng_session lsess
;
235 DBG("Getting all available session");
237 /* Iterate over session list and append data after
238 * the control struct in the buffer.
240 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
241 /* Copy name and uuid */
242 uuid_unparse(iter
->uuid
, lsess
.uuid
);
243 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
244 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
245 memcpy(<
[i
], &lsess
, sizeof(lsess
));
247 /* Reset struct for next pass */
248 memset(&lsess
, 0, sizeof(lsess
));