Move session data and functions to session.c/.h
[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.
8 *
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) {
147 del_session_list(iter);
148 free(iter);
149 found = 1;
150 break;
151 }
152 }
153
154 return found;
155}
156
157/*
158 * create_session
159 *
160 * Create a brand new session and add it to the
161 * global session list.
162 */
163int create_session(char *name, uuid_t *session_id)
164{
165 struct ltt_session *new_session;
166
167 new_session = find_session_by_name(name);
168 if (new_session != NULL) {
169 goto error;
170 }
171
172 /* Allocate session data structure */
173 new_session = malloc(sizeof(struct ltt_session));
174 if (new_session == NULL) {
175 perror("malloc");
176 goto error_mem;
177 }
178
179 if (name != NULL) {
180 if (asprintf(&new_session->name, "%s", name) < 0) {
181 goto error_mem;
182 }
183 } else {
184 /* Generate session name based on the session count */
185 if (asprintf(&new_session->name, "%s%d", "lttng-", session_count) < 0) {
186 goto error_mem;
187 }
188 }
189
190 /* UUID generation */
191 uuid_generate(new_session->uuid);
192 uuid_copy(*session_id, new_session->uuid);
193
194 /* Set consumer (identifier) to 0. This means that there is
195 * NO consumer attach to that session yet.
196 */
197 new_session->ust_consumer = 0;
198 new_session->lttng_consumer = 0;
199
200 /* Init list */
201 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
202 CDS_INIT_LIST_HEAD(&new_session->lttng_traces);
203
204 /* Add new session to the global session list */
205 add_session_list(new_session);
206
207 return 0;
208
209error:
210 return -1;
211
212error_mem:
213 return -ENOMEM;
214}
215
216/*
217 * get_lttng_session
218 *
219 * Iterate over the global session list and
220 * fill the lttng_session array.
221 */
222void get_lttng_session(struct lttng_session *lt)
223{
224 int i = 0;
225 struct ltt_session *iter;
226 struct lttng_session lsess;
227
228 /* Iterate over session list and append data after
229 * the control struct in the buffer.
230 */
231 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
232 /* Copy name and uuid */
233 uuid_unparse(iter->uuid, lsess.uuid);
234 strncpy(lsess.name, iter->name, sizeof(lsess.name));
235 lsess.name[sizeof(lsess.name) - 1] = '\0';
236 memcpy(&lt[i], &lsess, sizeof(lsess));
237 i++;
238 /* Reset struct for next pass */
239 memset(&lsess, 0, sizeof(lsess));
240 }
241}
242
This page took 0.045062 seconds and 4 git commands to generate.