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 | ||
5b74c7b1 DG |
86 | /* |
87 | * find_session_by_name | |
88 | * | |
89 | * Return a ltt_session structure ptr that matches name. | |
90 | * If no session found, NULL is returned. | |
91 | */ | |
92 | struct ltt_session *find_session_by_name(char *name) | |
93 | { | |
94 | int found = 0; | |
95 | struct ltt_session *iter; | |
96 | ||
97 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
b42dc92c | 98 | if (strncmp(iter->name, name, strlen(name)) == 0) { |
5b74c7b1 DG |
99 | found = 1; |
100 | break; | |
101 | } | |
102 | } | |
103 | ||
104 | if (!found) { | |
105 | iter = NULL; | |
106 | } | |
107 | ||
108 | return iter; | |
109 | } | |
110 | ||
111 | /* | |
112 | * destroy_session | |
113 | * | |
114 | * Delete session from the global session list | |
115 | * and free the memory. | |
116 | * | |
117 | * Return -1 if no session is found. | |
118 | * On success, return 1; | |
119 | */ | |
f3ed775e | 120 | int destroy_session(char *name) |
5b74c7b1 DG |
121 | { |
122 | int found = -1; | |
123 | struct ltt_session *iter; | |
124 | ||
125 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
f3ed775e | 126 | if (strcmp(iter->name, name) == 0) { |
e07ae692 | 127 | DBG("Destroying session %s", iter->name); |
5b74c7b1 DG |
128 | del_session_list(iter); |
129 | free(iter); | |
130 | found = 1; | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | return found; | |
136 | } | |
137 | ||
138 | /* | |
139 | * create_session | |
140 | * | |
f3ed775e | 141 | * Create a brand new session and add it to the global session list. |
5b74c7b1 | 142 | */ |
f3ed775e | 143 | int create_session(char *name, char *path) |
5b74c7b1 | 144 | { |
f3ed775e | 145 | int ret; |
b082db07 | 146 | char date_time[NAME_MAX]; |
5b74c7b1 | 147 | struct ltt_session *new_session; |
b082db07 DG |
148 | time_t rawtime; |
149 | struct tm *timeinfo; | |
e07ae692 | 150 | |
5b74c7b1 DG |
151 | new_session = find_session_by_name(name); |
152 | if (new_session != NULL) { | |
f3ed775e DG |
153 | ret = -EEXIST; |
154 | goto error_exist; | |
5b74c7b1 DG |
155 | } |
156 | ||
157 | /* Allocate session data structure */ | |
158 | new_session = malloc(sizeof(struct ltt_session)); | |
159 | if (new_session == NULL) { | |
160 | perror("malloc"); | |
f3ed775e DG |
161 | ret = -ENOMEM; |
162 | goto error_malloc; | |
5b74c7b1 DG |
163 | } |
164 | ||
f3ed775e | 165 | /* Define session name */ |
5b74c7b1 DG |
166 | if (name != NULL) { |
167 | if (asprintf(&new_session->name, "%s", name) < 0) { | |
f3ed775e DG |
168 | ret = -ENOMEM; |
169 | goto error_asprintf; | |
5b74c7b1 DG |
170 | } |
171 | } else { | |
f3ed775e DG |
172 | ERR("No session name given"); |
173 | ret = -1; | |
174 | goto error; | |
175 | } | |
176 | ||
177 | /* Define session system path */ | |
178 | if (path != NULL) { | |
b082db07 DG |
179 | if (strstr(name, "auto-") == NULL) { |
180 | time(&rawtime); | |
181 | timeinfo = localtime(&rawtime); | |
182 | strftime(date_time, sizeof(date_time), "-%Y%m%d-%H%M%S", timeinfo); | |
183 | } else { | |
184 | date_time[0] = '\0'; | |
185 | } | |
186 | ||
187 | if (asprintf(&new_session->path, "%s/%s%s", path, name, date_time) < 0) { | |
f3ed775e DG |
188 | ret = -ENOMEM; |
189 | goto error_asprintf; | |
5b74c7b1 | 190 | } |
f3ed775e DG |
191 | } else { |
192 | ERR("No session path given"); | |
193 | ret = -1; | |
194 | goto error; | |
5b74c7b1 DG |
195 | } |
196 | ||
1d4b027a DG |
197 | /* |
198 | * Set consumer (identifier) to 0. This means that there is | |
5b74c7b1 DG |
199 | * NO consumer attach to that session yet. |
200 | */ | |
201 | new_session->ust_consumer = 0; | |
1d4b027a DG |
202 | |
203 | /* Init kernel session */ | |
204 | new_session->kernel_session = NULL; | |
5b74c7b1 DG |
205 | |
206 | /* Init list */ | |
207 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); | |
1657e9bb DG |
208 | |
209 | /* Set trace list counter */ | |
210 | new_session->ust_trace_count = 0; | |
5b74c7b1 DG |
211 | |
212 | /* Add new session to the global session list */ | |
213 | add_session_list(new_session); | |
214 | ||
b082db07 DG |
215 | DBG("Tracing session %s created in %s", name, new_session->path); |
216 | ||
5b74c7b1 DG |
217 | return 0; |
218 | ||
219 | error: | |
f3ed775e DG |
220 | error_asprintf: |
221 | if (new_session != NULL) { | |
222 | free(new_session); | |
223 | } | |
5b74c7b1 | 224 | |
f3ed775e DG |
225 | error_exist: |
226 | error_malloc: | |
227 | return ret; | |
5b74c7b1 DG |
228 | } |
229 | ||
230 | /* | |
231 | * get_lttng_session | |
232 | * | |
f3ed775e | 233 | * Iterate over the global session list and fill the lttng_session array. |
5b74c7b1 | 234 | */ |
5461b305 | 235 | void get_lttng_session(struct lttng_session *sessions) |
5b74c7b1 DG |
236 | { |
237 | int i = 0; | |
238 | struct ltt_session *iter; | |
239 | struct lttng_session lsess; | |
240 | ||
e07ae692 DG |
241 | DBG("Getting all available session"); |
242 | ||
5b74c7b1 DG |
243 | /* Iterate over session list and append data after |
244 | * the control struct in the buffer. | |
245 | */ | |
246 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
f3ed775e DG |
247 | strncpy(lsess.path, iter->path, sizeof(lsess.path)); |
248 | lsess.path[sizeof(lsess.path) - 1] = '\0'; | |
5b74c7b1 DG |
249 | strncpy(lsess.name, iter->name, sizeof(lsess.name)); |
250 | lsess.name[sizeof(lsess.name) - 1] = '\0'; | |
5461b305 | 251 | memcpy(&sessions[i], &lsess, sizeof(lsess)); |
5b74c7b1 DG |
252 | i++; |
253 | /* Reset struct for next pass */ | |
254 | memset(&lsess, 0, sizeof(lsess)); | |
255 | } | |
256 | } | |
257 |