Commit | Line | Data |
---|---|---|
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 */ | |
29 | static unsigned int session_count; | |
30 | ||
31 | /* Static internal function */ | |
32 | static void add_session_list(struct ltt_session *ls); | |
33 | static void del_session_list(struct ltt_session *ls); | |
34 | ||
35 | /* Init session's list */ | |
36 | static 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 | */ | |
45 | unsigned 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 | */ | |
55 | static void add_session_list(struct ltt_session *ls) | |
56 | { | |
57 | cds_list_add(&ls->list, <t_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 | */ | |
66 | static 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 | */ | |
80 | struct 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, <t_session_list.head, list) { | |
91 | if (uuid_compare(iter->uuid, session_id) == 0) { | |
92 | found = 1; | |
93 | break; | |
94 | } | |
95 | } | |
96 | ||
97 | end: | |
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 | */ | |
110 | struct 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, <t_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 | */ | |
138 | int destroy_session(uuid_t *uuid) | |
139 | { | |
140 | int found = -1; | |
141 | struct ltt_session *iter; | |
142 | ||
143 | cds_list_for_each_entry(iter, <t_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 | */ | |
162 | int 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 | |
204 | /* Set trace list counter */ | |
205 | new_session->ust_trace_count = 0; | |
5b74c7b1 DG |
206 | |
207 | /* Add new session to the global session list */ | |
208 | add_session_list(new_session); | |
209 | ||
210 | return 0; | |
211 | ||
212 | error: | |
213 | return -1; | |
214 | ||
215 | error_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 | */ | |
5461b305 | 225 | void get_lttng_session(struct lttng_session *sessions) |
5b74c7b1 DG |
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, <t_session_list.head, list) { | |
237 | /* Copy name and uuid */ | |
96243366 | 238 | uuid_copy(lsess.uuid, iter->uuid); |
5b74c7b1 DG |
239 | strncpy(lsess.name, iter->name, sizeof(lsess.name)); |
240 | lsess.name[sizeof(lsess.name) - 1] = '\0'; | |
5461b305 | 241 | memcpy(&sessions[i], &lsess, sizeof(lsess)); |
5b74c7b1 DG |
242 | i++; |
243 | /* Reset struct for next pass */ | |
244 | memset(&lsess, 0, sizeof(lsess)); | |
245 | } | |
246 | } | |
247 |