Initial import of libustctl and libustcomm
[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 */
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
1d4b027a
DG
205 /*
206 * Set consumer (identifier) to 0. This means that there is
5b74c7b1
DG
207 * NO consumer attach to that session yet.
208 */
209 new_session->ust_consumer = 0;
1d4b027a
DG
210
211 /* Init kernel session */
212 new_session->kernel_session = NULL;
213 new_session->kern_session_count = 0;
5b74c7b1
DG
214
215 /* Init list */
216 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
217
218 /* Set trace list counter */
219 new_session->ust_trace_count = 0;
5b74c7b1
DG
220
221 /* Add new session to the global session list */
222 add_session_list(new_session);
223
224 return 0;
225
226error:
227 return -1;
228
229error_mem:
230 return -ENOMEM;
231}
232
233/*
234 * get_lttng_session
235 *
236 * Iterate over the global session list and
237 * fill the lttng_session array.
238 */
5461b305 239void get_lttng_session(struct lttng_session *sessions)
5b74c7b1
DG
240{
241 int i = 0;
242 struct ltt_session *iter;
243 struct lttng_session lsess;
244
e07ae692
DG
245 DBG("Getting all available session");
246
5b74c7b1
DG
247 /* Iterate over session list and append data after
248 * the control struct in the buffer.
249 */
250 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
251 /* Copy name and uuid */
96243366 252 uuid_copy(lsess.uuid, iter->uuid);
5b74c7b1
DG
253 strncpy(lsess.name, iter->name, sizeof(lsess.name));
254 lsess.name[sizeof(lsess.name) - 1] = '\0';
5461b305 255 memcpy(&sessions[i], &lsess, sizeof(lsess));
5b74c7b1
DG
256 i++;
257 /* Reset struct for next pass */
258 memset(&lsess, 0, sizeof(lsess));
259 }
260}
261
This page took 0.032659 seconds and 4 git commands to generate.