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(char *name
)
151 struct ltt_session
*iter
;
153 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
154 if (strcmp(iter
->name
, name
) == 0) {
155 DBG("Destroying session %s", iter
->name
);
156 del_session_list(iter
);
169 * Create a brand new session and add it to the global session list.
171 int create_session(char *name
, char *path
)
174 struct ltt_session
*new_session
;
176 DBG("Creating session %s at %s", name
, path
);
178 new_session
= find_session_by_name(name
);
179 if (new_session
!= NULL
) {
184 /* Allocate session data structure */
185 new_session
= malloc(sizeof(struct ltt_session
));
186 if (new_session
== NULL
) {
192 /* Define session name */
194 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
199 ERR("No session name given");
204 /* Define session system path */
206 if (asprintf(&new_session
->path
, "%s", path
) < 0) {
211 ERR("No session path given");
216 /* UUID generation */
217 uuid_generate(new_session
->uuid
);
220 * Set consumer (identifier) to 0. This means that there is
221 * NO consumer attach to that session yet.
223 new_session
->ust_consumer
= 0;
225 /* Init kernel session */
226 new_session
->kernel_session
= NULL
;
227 new_session
->kern_session_count
= 0;
230 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
232 /* Set trace list counter */
233 new_session
->ust_trace_count
= 0;
235 /* Add new session to the global session list */
236 add_session_list(new_session
);
242 if (new_session
!= NULL
) {
254 * Iterate over the global session list and fill the lttng_session array.
256 void get_lttng_session(struct lttng_session
*sessions
)
259 struct ltt_session
*iter
;
260 struct lttng_session lsess
;
262 DBG("Getting all available session");
264 /* Iterate over session list and append data after
265 * the control struct in the buffer.
267 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
268 strncpy(lsess
.path
, iter
->path
, sizeof(lsess
.path
));
269 lsess
.path
[sizeof(lsess
.path
) - 1] = '\0';
270 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
271 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
272 memcpy(&sessions
[i
], &lsess
, sizeof(lsess
));
274 /* Reset struct for next pass */
275 memset(&lsess
, 0, sizeof(lsess
));