Remove debug mode at kconsumerd exec
[lttng-tools.git] / ltt-sessiond / session.c
CommitLineData
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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
6c9cc2ab 20#include <limits.h>
b5541356 21#include <pthread.h>
5b74c7b1
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
b082db07 25#include <time.h>
5b74c7b1 26#include <urcu/list.h>
5b74c7b1
DG
27
28#include "lttngerr.h"
29#include "session.h"
30
8c0faa1d 31/*
b5541356 32 * NOTES:
8c0faa1d 33 *
b5541356
DG
34 * No ltt_session.lock is taken here because those data structure are widely
35 * spread across the lttng-tools code base so before caling functions below
36 * that can read/write a session, the caller MUST acquire the session lock
37 * using lock_session() and unlock_session().
8c0faa1d 38 */
8c0faa1d 39
5b74c7b1 40/*
b5541356 41 * Init tracing session list.
5b74c7b1 42 *
b5541356 43 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 44 */
b5541356
DG
45static struct ltt_session_list ltt_session_list = {
46 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
47 .lock = PTHREAD_MUTEX_INITIALIZER,
48 .count = 0,
49};
5b74c7b1
DG
50
51/*
52 * add_session_list
53 *
54 * Add a ltt_session structure to the global list.
b5541356
DG
55 *
56 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
57 */
58static void add_session_list(struct ltt_session *ls)
59{
60 cds_list_add(&ls->list, &ltt_session_list.head);
b5541356 61 ltt_session_list.count++;
5b74c7b1
DG
62}
63
64/*
65 * del_session_list
66 *
67 * Delete a ltt_session structure to the global list.
b5541356
DG
68 *
69 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
70 */
71static void del_session_list(struct ltt_session *ls)
72{
73 cds_list_del(&ls->list);
74 /* Sanity check */
b5541356
DG
75 if (ltt_session_list.count > 0) {
76 ltt_session_list.count--;
5b74c7b1
DG
77 }
78}
79
b5541356
DG
80/*
81 * get_session_list
82 *
83 * Return a pointer to the session list.
84 */
85struct ltt_session_list *get_session_list(void)
86{
87 return &ltt_session_list;
88}
89
90/*
6c9cc2ab 91 * Acquire session list lock
b5541356 92 */
6c9cc2ab 93void lock_session_list(void)
b5541356 94{
6c9cc2ab 95 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
96}
97
98/*
6c9cc2ab 99 * Release session list lock
b5541356 100 */
6c9cc2ab 101void unlock_session_list(void)
b5541356 102{
6c9cc2ab 103 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
104}
105
106/*
6c9cc2ab 107 * Acquire session lock
b5541356 108 */
6c9cc2ab 109void lock_session(struct ltt_session *session)
b5541356 110{
6c9cc2ab
DG
111 pthread_mutex_lock(&session->lock);
112}
b5541356 113
6c9cc2ab
DG
114/*
115 * Release session lock
116 */
117void unlock_session(struct ltt_session *session)
118{
119 pthread_mutex_unlock(&session->lock);
b5541356
DG
120}
121
5b74c7b1
DG
122/*
123 * find_session_by_name
124 *
125 * Return a ltt_session structure ptr that matches name.
126 * If no session found, NULL is returned.
127 */
128struct ltt_session *find_session_by_name(char *name)
129{
130 int found = 0;
131 struct ltt_session *iter;
132
6c9cc2ab 133 lock_session_list();
5b74c7b1 134 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
b42dc92c 135 if (strncmp(iter->name, name, strlen(name)) == 0) {
5b74c7b1
DG
136 found = 1;
137 break;
138 }
139 }
6c9cc2ab 140 unlock_session_list();
5b74c7b1
DG
141
142 if (!found) {
143 iter = NULL;
144 }
145
146 return iter;
147}
148
149/*
150 * destroy_session
151 *
b5541356 152 * Delete session from the session list and free the memory.
5b74c7b1 153 *
b5541356 154 * Return -1 if no session is found. On success, return 1;
5b74c7b1 155 */
f3ed775e 156int destroy_session(char *name)
5b74c7b1
DG
157{
158 int found = -1;
159 struct ltt_session *iter;
160
6c9cc2ab 161 lock_session_list();
5b74c7b1 162 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
f3ed775e 163 if (strcmp(iter->name, name) == 0) {
e07ae692 164 DBG("Destroying session %s", iter->name);
5b74c7b1 165 del_session_list(iter);
b5541356
DG
166 free(iter->name);
167 free(iter->path);
168 pthread_mutex_destroy(&iter->lock);
5b74c7b1
DG
169 free(iter);
170 found = 1;
171 break;
172 }
173 }
6c9cc2ab 174 unlock_session_list();
5b74c7b1
DG
175
176 return found;
177}
178
179/*
180 * create_session
181 *
b5541356 182 * Create a brand new session and add it to the session list.
5b74c7b1 183 */
f3ed775e 184int create_session(char *name, char *path)
5b74c7b1 185{
f3ed775e 186 int ret;
b082db07 187 char date_time[NAME_MAX];
5b74c7b1 188 struct ltt_session *new_session;
b082db07
DG
189 time_t rawtime;
190 struct tm *timeinfo;
e07ae692 191
5b74c7b1
DG
192 new_session = find_session_by_name(name);
193 if (new_session != NULL) {
f3ed775e
DG
194 ret = -EEXIST;
195 goto error_exist;
5b74c7b1
DG
196 }
197
198 /* Allocate session data structure */
199 new_session = malloc(sizeof(struct ltt_session));
200 if (new_session == NULL) {
201 perror("malloc");
f3ed775e
DG
202 ret = -ENOMEM;
203 goto error_malloc;
5b74c7b1
DG
204 }
205
f3ed775e 206 /* Define session name */
5b74c7b1
DG
207 if (name != NULL) {
208 if (asprintf(&new_session->name, "%s", name) < 0) {
f3ed775e
DG
209 ret = -ENOMEM;
210 goto error_asprintf;
5b74c7b1
DG
211 }
212 } else {
f3ed775e
DG
213 ERR("No session name given");
214 ret = -1;
215 goto error;
216 }
217
218 /* Define session system path */
219 if (path != NULL) {
b082db07
DG
220 if (strstr(name, "auto-") == NULL) {
221 time(&rawtime);
222 timeinfo = localtime(&rawtime);
223 strftime(date_time, sizeof(date_time), "-%Y%m%d-%H%M%S", timeinfo);
224 } else {
225 date_time[0] = '\0';
226 }
227
228 if (asprintf(&new_session->path, "%s/%s%s", path, name, date_time) < 0) {
f3ed775e
DG
229 ret = -ENOMEM;
230 goto error_asprintf;
5b74c7b1 231 }
f3ed775e
DG
232 } else {
233 ERR("No session path given");
234 ret = -1;
235 goto error;
5b74c7b1
DG
236 }
237
1d4b027a
DG
238 /* Init kernel session */
239 new_session->kernel_session = NULL;
5b74c7b1
DG
240
241 /* Init list */
242 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
243
244 /* Set trace list counter */
245 new_session->ust_trace_count = 0;
5b74c7b1 246
b5541356 247 /* Add new session to the session list */
6c9cc2ab 248 lock_session_list();
5b74c7b1 249 add_session_list(new_session);
6c9cc2ab 250 unlock_session_list();
b5541356
DG
251
252 /* Init lock */
253 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 254
b5541356 255 DBG("Tracing session %s created in %s", new_session->name, new_session->path);
b082db07 256
5b74c7b1
DG
257 return 0;
258
259error:
f3ed775e
DG
260error_asprintf:
261 if (new_session != NULL) {
262 free(new_session);
263 }
5b74c7b1 264
f3ed775e
DG
265error_exist:
266error_malloc:
267 return ret;
5b74c7b1 268}
This page took 0.035109 seconds and 4 git commands to generate.