Use trace.h for kernel and ust trace data struct
[lttng-tools.git] / ltt-sessiond / session.c
CommitLineData
5b74c7b1
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
91d76f53 8 *
5b74c7b1
DG
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.
13 *
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.
17 */
18
19#define _GNU_SOURCE
5b74c7b1
DG
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <urcu/list.h>
5b74c7b1
DG
24
25#include "lttngerr.h"
26#include "session.h"
27
28/* Variables */
29static unsigned int session_count;
30
31/* Static internal function */
32static void add_session_list(struct ltt_session *ls);
33static void del_session_list(struct ltt_session *ls);
34
35/* Init session's list */
8c0faa1d 36struct ltt_session_list ltt_session_list = {
5b74c7b1
DG
37 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
38};
39
8c0faa1d
DG
40/*
41 * get_session_list
42 *
43 * Return a pointer to the session list.
44 */
45struct ltt_session_list *get_session_list(void)
46{
47 return &ltt_session_list;
48}
49
5b74c7b1
DG
50/*
51 * get_session_count
52 *
53 * Return session_count
54 */
55unsigned int get_session_count(void)
56{
57 return session_count;
58}
59
60/*
61 * add_session_list
62 *
63 * Add a ltt_session structure to the global list.
64 */
65static void add_session_list(struct ltt_session *ls)
66{
67 cds_list_add(&ls->list, &ltt_session_list.head);
68 session_count++;
69}
70
71/*
72 * del_session_list
73 *
74 * Delete a ltt_session structure to the global list.
75 */
76static void del_session_list(struct ltt_session *ls)
77{
78 cds_list_del(&ls->list);
79 /* Sanity check */
80 if (session_count != 0) {
81 session_count--;
82 }
83}
84
85/*
86 * find_session_by_uuid
87 *
88 * Return a ltt_session structure ptr that matches the uuid.
89 */
90struct ltt_session *find_session_by_uuid(uuid_t session_id)
91{
92 int found = 0;
93 struct ltt_session *iter;
94
95 /* Sanity check for NULL session_id */
96 if (uuid_is_null(session_id)) {
97 goto end;
98 }
99
100 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
101 if (uuid_compare(iter->uuid, session_id) == 0) {
102 found = 1;
103 break;
104 }
105 }
106
107end:
108 if (!found) {
109 iter = NULL;
110 }
111 return iter;
112}
113
114/*
115 * find_session_by_name
116 *
117 * Return a ltt_session structure ptr that matches name.
118 * If no session found, NULL is returned.
119 */
120struct ltt_session *find_session_by_name(char *name)
121{
122 int found = 0;
123 struct ltt_session *iter;
124
125 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
b42dc92c 126 if (strncmp(iter->name, name, strlen(name)) == 0) {
5b74c7b1
DG
127 found = 1;
128 break;
129 }
130 }
131
132 if (!found) {
133 iter = NULL;
134 }
135
136 return iter;
137}
138
139/*
140 * destroy_session
141 *
142 * Delete session from the global session list
143 * and free the memory.
144 *
145 * Return -1 if no session is found.
146 * On success, return 1;
147 */
148int destroy_session(uuid_t *uuid)
149{
150 int found = -1;
151 struct ltt_session *iter;
152
153 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
154 if (uuid_compare(iter->uuid, *uuid) == 0) {
e07ae692 155 DBG("Destroying session %s", iter->name);
5b74c7b1
DG
156 del_session_list(iter);
157 free(iter);
158 found = 1;
159 break;
160 }
161 }
162
163 return found;
164}
165
166/*
167 * create_session
168 *
169 * Create a brand new session and add it to the
170 * global session list.
171 */
172int create_session(char *name, uuid_t *session_id)
173{
174 struct ltt_session *new_session;
175
e07ae692
DG
176 DBG("Creating session %s", name);
177
5b74c7b1
DG
178 new_session = find_session_by_name(name);
179 if (new_session != NULL) {
180 goto error;
181 }
182
183 /* Allocate session data structure */
184 new_session = malloc(sizeof(struct ltt_session));
185 if (new_session == NULL) {
186 perror("malloc");
187 goto error_mem;
188 }
189
190 if (name != NULL) {
191 if (asprintf(&new_session->name, "%s", name) < 0) {
192 goto error_mem;
193 }
194 } else {
195 /* Generate session name based on the session count */
196 if (asprintf(&new_session->name, "%s%d", "lttng-", session_count) < 0) {
197 goto error_mem;
198 }
199 }
200
201 /* UUID generation */
202 uuid_generate(new_session->uuid);
203 uuid_copy(*session_id, new_session->uuid);
204
205 /* Set consumer (identifier) to 0. This means that there is
206 * NO consumer attach to that session yet.
207 */
208 new_session->ust_consumer = 0;
1657e9bb 209 new_session->kernel_consumer = 0;
5b74c7b1
DG
210
211 /* Init list */
212 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
213
214 /* Set trace list counter */
215 new_session->ust_trace_count = 0;
5b74c7b1
DG
216
217 /* Add new session to the global session list */
218 add_session_list(new_session);
219
220 return 0;
221
222error:
223 return -1;
224
225error_mem:
226 return -ENOMEM;
227}
228
229/*
230 * get_lttng_session
231 *
232 * Iterate over the global session list and
233 * fill the lttng_session array.
234 */
5461b305 235void get_lttng_session(struct lttng_session *sessions)
5b74c7b1
DG
236{
237 int i = 0;
238 struct ltt_session *iter;
239 struct lttng_session lsess;
240
e07ae692
DG
241 DBG("Getting all available session");
242
5b74c7b1
DG
243 /* Iterate over session list and append data after
244 * the control struct in the buffer.
245 */
246 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
247 /* Copy name and uuid */
96243366 248 uuid_copy(lsess.uuid, iter->uuid);
5b74c7b1
DG
249 strncpy(lsess.name, iter->name, sizeof(lsess.name));
250 lsess.name[sizeof(lsess.name) - 1] = '\0';
5461b305 251 memcpy(&sessions[i], &lsess, sizeof(lsess));
5b74c7b1
DG
252 i++;
253 /* Reset struct for next pass */
254 memset(&lsess, 0, sizeof(lsess));
255 }
256}
257
This page took 0.034261 seconds and 5 git commands to generate.