Fix comments spacing
[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
1d4b027a 35/* Init global session 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 */
f3ed775e 148int destroy_session(char *name)
5b74c7b1
DG
149{
150 int found = -1;
151 struct ltt_session *iter;
152
153 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
f3ed775e 154 if (strcmp(iter->name, name) == 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 *
f3ed775e 169 * Create a brand new session and add it to the global session list.
5b74c7b1 170 */
f3ed775e 171int create_session(char *name, char *path)
5b74c7b1 172{
f3ed775e 173 int ret;
5b74c7b1
DG
174 struct ltt_session *new_session;
175
f3ed775e 176 DBG("Creating session %s at %s", name, path);
e07ae692 177
5b74c7b1
DG
178 new_session = find_session_by_name(name);
179 if (new_session != NULL) {
f3ed775e
DG
180 ret = -EEXIST;
181 goto error_exist;
5b74c7b1
DG
182 }
183
184 /* Allocate session data structure */
185 new_session = malloc(sizeof(struct ltt_session));
186 if (new_session == NULL) {
187 perror("malloc");
f3ed775e
DG
188 ret = -ENOMEM;
189 goto error_malloc;
5b74c7b1
DG
190 }
191
f3ed775e 192 /* Define session name */
5b74c7b1
DG
193 if (name != NULL) {
194 if (asprintf(&new_session->name, "%s", name) < 0) {
f3ed775e
DG
195 ret = -ENOMEM;
196 goto error_asprintf;
5b74c7b1
DG
197 }
198 } else {
f3ed775e
DG
199 ERR("No session name given");
200 ret = -1;
201 goto error;
202 }
203
204 /* Define session system path */
205 if (path != NULL) {
206 if (asprintf(&new_session->path, "%s", path) < 0) {
207 ret = -ENOMEM;
208 goto error_asprintf;
5b74c7b1 209 }
f3ed775e
DG
210 } else {
211 ERR("No session path given");
212 ret = -1;
213 goto error;
5b74c7b1
DG
214 }
215
216 /* UUID generation */
217 uuid_generate(new_session->uuid);
5b74c7b1 218
1d4b027a
DG
219 /*
220 * Set consumer (identifier) to 0. This means that there is
5b74c7b1
DG
221 * NO consumer attach to that session yet.
222 */
223 new_session->ust_consumer = 0;
1d4b027a
DG
224
225 /* Init kernel session */
226 new_session->kernel_session = NULL;
227 new_session->kern_session_count = 0;
5b74c7b1
DG
228
229 /* Init list */
230 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
231
232 /* Set trace list counter */
233 new_session->ust_trace_count = 0;
5b74c7b1
DG
234
235 /* Add new session to the global session list */
236 add_session_list(new_session);
237
238 return 0;
239
240error:
f3ed775e
DG
241error_asprintf:
242 if (new_session != NULL) {
243 free(new_session);
244 }
5b74c7b1 245
f3ed775e
DG
246error_exist:
247error_malloc:
248 return ret;
5b74c7b1
DG
249}
250
251/*
252 * get_lttng_session
253 *
f3ed775e 254 * Iterate over the global session list and fill the lttng_session array.
5b74c7b1 255 */
5461b305 256void get_lttng_session(struct lttng_session *sessions)
5b74c7b1
DG
257{
258 int i = 0;
259 struct ltt_session *iter;
260 struct lttng_session lsess;
261
e07ae692
DG
262 DBG("Getting all available session");
263
5b74c7b1
DG
264 /* Iterate over session list and append data after
265 * the control struct in the buffer.
266 */
267 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
f3ed775e
DG
268 strncpy(lsess.path, iter->path, sizeof(lsess.path));
269 lsess.path[sizeof(lsess.path) - 1] = '\0';
5b74c7b1
DG
270 strncpy(lsess.name, iter->name, sizeof(lsess.name));
271 lsess.name[sizeof(lsess.name) - 1] = '\0';
5461b305 272 memcpy(&sessions[i], &lsess, sizeof(lsess));
5b74c7b1
DG
273 i++;
274 /* Reset struct for next pass */
275 memset(&lsess, 0, sizeof(lsess));
276 }
277}
278
This page took 0.055201 seconds and 4 git commands to generate.