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 | |
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 */ | |
31 | static unsigned int session_count; | |
32 | ||
33 | /* Static internal function */ | |
34 | static void add_session_list(struct ltt_session *ls); | |
35 | static void del_session_list(struct ltt_session *ls); | |
36 | ||
37 | /* Init session's list */ | |
38 | static 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 | */ | |
47 | unsigned 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 | */ | |
57 | static void add_session_list(struct ltt_session *ls) | |
58 | { | |
59 | cds_list_add(&ls->list, <t_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 | */ | |
68 | static 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 | */ | |
82 | struct 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, <t_session_list.head, list) { | |
93 | if (uuid_compare(iter->uuid, session_id) == 0) { | |
94 | found = 1; | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | end: | |
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 | */ | |
112 | struct 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, <t_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 | */ | |
140 | int destroy_session(uuid_t *uuid) | |
141 | { | |
142 | int found = -1; | |
143 | struct ltt_session *iter; | |
144 | ||
145 | cds_list_for_each_entry(iter, <t_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 | */ | |
164 | int 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; | |
1657e9bb | 201 | new_session->kernel_consumer = 0; |
5b74c7b1 DG |
202 | |
203 | /* Init list */ | |
204 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); | |
1657e9bb DG |
205 | CDS_INIT_LIST_HEAD(&new_session->kernel_traces); |
206 | ||
207 | /* Set trace list counter */ | |
208 | new_session->ust_trace_count = 0; | |
209 | new_session->kern_trace_count = 0; | |
5b74c7b1 DG |
210 | |
211 | /* Add new session to the global session list */ | |
212 | add_session_list(new_session); | |
213 | ||
214 | return 0; | |
215 | ||
216 | error: | |
217 | return -1; | |
218 | ||
219 | error_mem: | |
220 | return -ENOMEM; | |
221 | } | |
222 | ||
223 | /* | |
224 | * get_lttng_session | |
225 | * | |
226 | * Iterate over the global session list and | |
227 | * fill the lttng_session array. | |
228 | */ | |
229 | void get_lttng_session(struct lttng_session *lt) | |
230 | { | |
231 | int i = 0; | |
232 | struct ltt_session *iter; | |
233 | struct lttng_session lsess; | |
234 | ||
e07ae692 DG |
235 | DBG("Getting all available session"); |
236 | ||
5b74c7b1 DG |
237 | /* Iterate over session list and append data after |
238 | * the control struct in the buffer. | |
239 | */ | |
240 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
241 | /* Copy name and uuid */ | |
242 | uuid_unparse(iter->uuid, lsess.uuid); | |
243 | strncpy(lsess.name, iter->name, sizeof(lsess.name)); | |
244 | lsess.name[sizeof(lsess.name) - 1] = '\0'; | |
245 | memcpy(<[i], &lsess, sizeof(lsess)); | |
246 | i++; | |
247 | /* Reset struct for next pass */ | |
248 | memset(&lsess, 0, sizeof(lsess)); | |
249 | } | |
250 | } | |
251 |