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> | |
b082db07 | 23 | #include <time.h> |
5b74c7b1 | 24 | #include <urcu/list.h> |
5b74c7b1 DG |
25 | |
26 | #include "lttngerr.h" | |
27 | #include "session.h" | |
28 | ||
29 | /* Variables */ | |
30 | static unsigned int session_count; | |
31 | ||
32 | /* Static internal function */ | |
33 | static void add_session_list(struct ltt_session *ls); | |
34 | static void del_session_list(struct ltt_session *ls); | |
35 | ||
1d4b027a | 36 | /* Init global session list */ |
8c0faa1d | 37 | struct ltt_session_list ltt_session_list = { |
5b74c7b1 DG |
38 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), |
39 | }; | |
40 | ||
8c0faa1d DG |
41 | /* |
42 | * get_session_list | |
43 | * | |
44 | * Return a pointer to the session list. | |
45 | */ | |
46 | struct ltt_session_list *get_session_list(void) | |
47 | { | |
48 | return <t_session_list; | |
49 | } | |
50 | ||
5b74c7b1 DG |
51 | /* |
52 | * get_session_count | |
53 | * | |
54 | * Return session_count | |
55 | */ | |
56 | unsigned int get_session_count(void) | |
57 | { | |
58 | return session_count; | |
59 | } | |
60 | ||
61 | /* | |
62 | * add_session_list | |
63 | * | |
64 | * Add a ltt_session structure to the global list. | |
65 | */ | |
66 | static void add_session_list(struct ltt_session *ls) | |
67 | { | |
68 | cds_list_add(&ls->list, <t_session_list.head); | |
69 | session_count++; | |
70 | } | |
71 | ||
72 | /* | |
73 | * del_session_list | |
74 | * | |
75 | * Delete a ltt_session structure to the global list. | |
76 | */ | |
77 | static void del_session_list(struct ltt_session *ls) | |
78 | { | |
79 | cds_list_del(&ls->list); | |
80 | /* Sanity check */ | |
81 | if (session_count != 0) { | |
82 | session_count--; | |
83 | } | |
84 | } | |
85 | ||
86 | /* | |
87 | * find_session_by_uuid | |
88 | * | |
89 | * Return a ltt_session structure ptr that matches the uuid. | |
90 | */ | |
91 | struct ltt_session *find_session_by_uuid(uuid_t session_id) | |
92 | { | |
93 | int found = 0; | |
94 | struct ltt_session *iter; | |
95 | ||
96 | /* Sanity check for NULL session_id */ | |
97 | if (uuid_is_null(session_id)) { | |
98 | goto end; | |
99 | } | |
100 | ||
101 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
102 | if (uuid_compare(iter->uuid, session_id) == 0) { | |
103 | found = 1; | |
104 | break; | |
105 | } | |
106 | } | |
107 | ||
108 | end: | |
109 | if (!found) { | |
110 | iter = NULL; | |
111 | } | |
112 | return iter; | |
113 | } | |
114 | ||
115 | /* | |
116 | * find_session_by_name | |
117 | * | |
118 | * Return a ltt_session structure ptr that matches name. | |
119 | * If no session found, NULL is returned. | |
120 | */ | |
121 | struct ltt_session *find_session_by_name(char *name) | |
122 | { | |
123 | int found = 0; | |
124 | struct ltt_session *iter; | |
125 | ||
126 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
b42dc92c | 127 | if (strncmp(iter->name, name, strlen(name)) == 0) { |
5b74c7b1 DG |
128 | found = 1; |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | if (!found) { | |
134 | iter = NULL; | |
135 | } | |
136 | ||
137 | return iter; | |
138 | } | |
139 | ||
140 | /* | |
141 | * destroy_session | |
142 | * | |
143 | * Delete session from the global session list | |
144 | * and free the memory. | |
145 | * | |
146 | * Return -1 if no session is found. | |
147 | * On success, return 1; | |
148 | */ | |
f3ed775e | 149 | int destroy_session(char *name) |
5b74c7b1 DG |
150 | { |
151 | int found = -1; | |
152 | struct ltt_session *iter; | |
153 | ||
154 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
f3ed775e | 155 | if (strcmp(iter->name, name) == 0) { |
e07ae692 | 156 | DBG("Destroying session %s", iter->name); |
5b74c7b1 DG |
157 | del_session_list(iter); |
158 | free(iter); | |
159 | found = 1; | |
160 | break; | |
161 | } | |
162 | } | |
163 | ||
164 | return found; | |
165 | } | |
166 | ||
167 | /* | |
168 | * create_session | |
169 | * | |
f3ed775e | 170 | * Create a brand new session and add it to the global session list. |
5b74c7b1 | 171 | */ |
f3ed775e | 172 | int create_session(char *name, char *path) |
5b74c7b1 | 173 | { |
f3ed775e | 174 | int ret; |
b082db07 | 175 | char date_time[NAME_MAX]; |
5b74c7b1 | 176 | struct ltt_session *new_session; |
b082db07 DG |
177 | time_t rawtime; |
178 | struct tm *timeinfo; | |
e07ae692 | 179 | |
5b74c7b1 DG |
180 | new_session = find_session_by_name(name); |
181 | if (new_session != NULL) { | |
f3ed775e DG |
182 | ret = -EEXIST; |
183 | goto error_exist; | |
5b74c7b1 DG |
184 | } |
185 | ||
186 | /* Allocate session data structure */ | |
187 | new_session = malloc(sizeof(struct ltt_session)); | |
188 | if (new_session == NULL) { | |
189 | perror("malloc"); | |
f3ed775e DG |
190 | ret = -ENOMEM; |
191 | goto error_malloc; | |
5b74c7b1 DG |
192 | } |
193 | ||
f3ed775e | 194 | /* Define session name */ |
5b74c7b1 DG |
195 | if (name != NULL) { |
196 | if (asprintf(&new_session->name, "%s", name) < 0) { | |
f3ed775e DG |
197 | ret = -ENOMEM; |
198 | goto error_asprintf; | |
5b74c7b1 DG |
199 | } |
200 | } else { | |
f3ed775e DG |
201 | ERR("No session name given"); |
202 | ret = -1; | |
203 | goto error; | |
204 | } | |
205 | ||
206 | /* Define session system path */ | |
207 | if (path != NULL) { | |
b082db07 DG |
208 | if (strstr(name, "auto-") == NULL) { |
209 | time(&rawtime); | |
210 | timeinfo = localtime(&rawtime); | |
211 | strftime(date_time, sizeof(date_time), "-%Y%m%d-%H%M%S", timeinfo); | |
212 | } else { | |
213 | date_time[0] = '\0'; | |
214 | } | |
215 | ||
216 | if (asprintf(&new_session->path, "%s/%s%s", path, name, date_time) < 0) { | |
f3ed775e DG |
217 | ret = -ENOMEM; |
218 | goto error_asprintf; | |
5b74c7b1 | 219 | } |
f3ed775e DG |
220 | } else { |
221 | ERR("No session path given"); | |
222 | ret = -1; | |
223 | goto error; | |
5b74c7b1 DG |
224 | } |
225 | ||
226 | /* UUID generation */ | |
227 | uuid_generate(new_session->uuid); | |
5b74c7b1 | 228 | |
1d4b027a DG |
229 | /* |
230 | * Set consumer (identifier) to 0. This means that there is | |
5b74c7b1 DG |
231 | * NO consumer attach to that session yet. |
232 | */ | |
233 | new_session->ust_consumer = 0; | |
1d4b027a DG |
234 | |
235 | /* Init kernel session */ | |
236 | new_session->kernel_session = NULL; | |
237 | new_session->kern_session_count = 0; | |
5b74c7b1 DG |
238 | |
239 | /* Init list */ | |
240 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); | |
1657e9bb DG |
241 | |
242 | /* Set trace list counter */ | |
243 | new_session->ust_trace_count = 0; | |
5b74c7b1 DG |
244 | |
245 | /* Add new session to the global session list */ | |
246 | add_session_list(new_session); | |
247 | ||
b082db07 DG |
248 | DBG("Tracing session %s created in %s", name, new_session->path); |
249 | ||
5b74c7b1 DG |
250 | return 0; |
251 | ||
252 | error: | |
f3ed775e DG |
253 | error_asprintf: |
254 | if (new_session != NULL) { | |
255 | free(new_session); | |
256 | } | |
5b74c7b1 | 257 | |
f3ed775e DG |
258 | error_exist: |
259 | error_malloc: | |
260 | return ret; | |
5b74c7b1 DG |
261 | } |
262 | ||
263 | /* | |
264 | * get_lttng_session | |
265 | * | |
f3ed775e | 266 | * Iterate over the global session list and fill the lttng_session array. |
5b74c7b1 | 267 | */ |
5461b305 | 268 | void get_lttng_session(struct lttng_session *sessions) |
5b74c7b1 DG |
269 | { |
270 | int i = 0; | |
271 | struct ltt_session *iter; | |
272 | struct lttng_session lsess; | |
273 | ||
e07ae692 DG |
274 | DBG("Getting all available session"); |
275 | ||
5b74c7b1 DG |
276 | /* Iterate over session list and append data after |
277 | * the control struct in the buffer. | |
278 | */ | |
279 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
f3ed775e DG |
280 | strncpy(lsess.path, iter->path, sizeof(lsess.path)); |
281 | lsess.path[sizeof(lsess.path) - 1] = '\0'; | |
5b74c7b1 DG |
282 | strncpy(lsess.name, iter->name, sizeof(lsess.name)); |
283 | lsess.name[sizeof(lsess.name) - 1] = '\0'; | |
5461b305 | 284 | memcpy(&sessions[i], &lsess, sizeof(lsess)); |
5b74c7b1 DG |
285 | i++; |
286 | /* Reset struct for next pass */ | |
287 | memset(&lsess, 0, sizeof(lsess)); | |
288 | } | |
289 | } | |
290 |