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