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