92111ffed981cc5f2593bc0770efc8c5f8e893aa
[lttng-tools.git] / ltt-sessiond / session.c
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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <urcu/list.h>
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
36 /* Init global session list */
37 struct ltt_session_list ltt_session_list = {
38 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
39 };
40
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 &ltt_session_list;
49 }
50
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, &ltt_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_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, &ltt_session_list.head, list) {
98 if (strncmp(iter->name, name, strlen(name)) == 0) {
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 */
120 int destroy_session(char *name)
121 {
122 int found = -1;
123 struct ltt_session *iter;
124
125 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
126 if (strcmp(iter->name, name) == 0) {
127 DBG("Destroying session %s", iter->name);
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 *
141 * Create a brand new session and add it to the global session list.
142 */
143 int create_session(char *name, char *path)
144 {
145 int ret;
146 char date_time[NAME_MAX];
147 struct ltt_session *new_session;
148 time_t rawtime;
149 struct tm *timeinfo;
150
151 new_session = find_session_by_name(name);
152 if (new_session != NULL) {
153 ret = -EEXIST;
154 goto error_exist;
155 }
156
157 /* Allocate session data structure */
158 new_session = malloc(sizeof(struct ltt_session));
159 if (new_session == NULL) {
160 perror("malloc");
161 ret = -ENOMEM;
162 goto error_malloc;
163 }
164
165 /* Define session name */
166 if (name != NULL) {
167 if (asprintf(&new_session->name, "%s", name) < 0) {
168 ret = -ENOMEM;
169 goto error_asprintf;
170 }
171 } else {
172 ERR("No session name given");
173 ret = -1;
174 goto error;
175 }
176
177 /* Define session system path */
178 if (path != NULL) {
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) {
188 ret = -ENOMEM;
189 goto error_asprintf;
190 }
191 } else {
192 ERR("No session path given");
193 ret = -1;
194 goto error;
195 }
196
197 /*
198 * Set consumer (identifier) to 0. This means that there is
199 * NO consumer attach to that session yet.
200 */
201 new_session->ust_consumer = 0;
202
203 /* Init kernel session */
204 new_session->kernel_session = NULL;
205
206 /* Init list */
207 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
208
209 /* Set trace list counter */
210 new_session->ust_trace_count = 0;
211
212 /* Add new session to the global session list */
213 add_session_list(new_session);
214
215 DBG("Tracing session %s created in %s", name, new_session->path);
216
217 return 0;
218
219 error:
220 error_asprintf:
221 if (new_session != NULL) {
222 free(new_session);
223 }
224
225 error_exist:
226 error_malloc:
227 return ret;
228 }
229
230 /*
231 * get_lttng_session
232 *
233 * Iterate over the global session list and fill the lttng_session array.
234 */
235 void get_lttng_session(struct lttng_session *sessions)
236 {
237 int i = 0;
238 struct ltt_session *iter;
239 struct lttng_session lsess;
240
241 DBG("Getting all available session");
242
243 /* Iterate over session list and append data after
244 * the control struct in the buffer.
245 */
246 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
247 strncpy(lsess.path, iter->path, sizeof(lsess.path));
248 lsess.path[sizeof(lsess.path) - 1] = '\0';
249 strncpy(lsess.name, iter->name, sizeof(lsess.name));
250 lsess.name[sizeof(lsess.name) - 1] = '\0';
251 memcpy(&sessions[i], &lsess, sizeof(lsess));
252 i++;
253 /* Reset struct for next pass */
254 memset(&lsess, 0, sizeof(lsess));
255 }
256 }
257
This page took 0.033716 seconds and 4 git commands to generate.