Cleanup comments and bad indent
[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
050349bb 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b74c7b1
DG
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/*
050349bb 52 * Add a ltt_session structure to the global list.
5b74c7b1 53 *
050349bb 54 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
55 */
56static void add_session_list(struct ltt_session *ls)
57{
58 cds_list_add(&ls->list, &ltt_session_list.head);
b5541356 59 ltt_session_list.count++;
5b74c7b1
DG
60}
61
62/*
050349bb 63 * Delete a ltt_session structure to the global list.
b5541356 64 *
050349bb 65 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
66 */
67static void del_session_list(struct ltt_session *ls)
68{
69 cds_list_del(&ls->list);
70 /* Sanity check */
b5541356
DG
71 if (ltt_session_list.count > 0) {
72 ltt_session_list.count--;
5b74c7b1
DG
73 }
74}
75
b5541356 76/*
050349bb 77 * Return a pointer to the session list.
b5541356
DG
78 */
79struct ltt_session_list *get_session_list(void)
80{
81 return &ltt_session_list;
82}
83
84/*
6c9cc2ab 85 * Acquire session list lock
b5541356 86 */
6c9cc2ab 87void lock_session_list(void)
b5541356 88{
6c9cc2ab 89 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
90}
91
92/*
6c9cc2ab 93 * Release session list lock
b5541356 94 */
6c9cc2ab 95void unlock_session_list(void)
b5541356 96{
6c9cc2ab 97 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
98}
99
100/*
6c9cc2ab 101 * Acquire session lock
b5541356 102 */
6c9cc2ab 103void lock_session(struct ltt_session *session)
b5541356 104{
6c9cc2ab
DG
105 pthread_mutex_lock(&session->lock);
106}
b5541356 107
6c9cc2ab
DG
108/*
109 * Release session lock
110 */
111void unlock_session(struct ltt_session *session)
112{
113 pthread_mutex_unlock(&session->lock);
b5541356
DG
114}
115
5b74c7b1 116/*
050349bb
DG
117 * Return a ltt_session structure ptr that matches name.
118 * If no session found, NULL is returned.
5b74c7b1
DG
119 */
120struct ltt_session *find_session_by_name(char *name)
121{
122 int found = 0;
123 struct ltt_session *iter;
124
6c9cc2ab 125 lock_session_list();
5b74c7b1 126 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 127 if (strncmp(iter->name, name, NAME_MAX) == 0) {
5b74c7b1
DG
128 found = 1;
129 break;
130 }
131 }
6c9cc2ab 132 unlock_session_list();
5b74c7b1
DG
133
134 if (!found) {
135 iter = NULL;
136 }
137
138 return iter;
139}
140
141/*
050349bb 142 * Delete session from the session list and free the memory.
5b74c7b1 143 *
050349bb 144 * Return -1 if no session is found. On success, return 1;
5b74c7b1 145 */
f3ed775e 146int destroy_session(char *name)
5b74c7b1
DG
147{
148 int found = -1;
af9737e9 149 struct ltt_session *iter, *tmp;
5b74c7b1 150
6c9cc2ab 151 lock_session_list();
af9737e9 152 cds_list_for_each_entry_safe(iter, tmp, &ltt_session_list.head, list) {
f3ed775e 153 if (strcmp(iter->name, name) == 0) {
e07ae692 154 DBG("Destroying session %s", iter->name);
5b74c7b1 155 del_session_list(iter);
b5541356
DG
156 free(iter->name);
157 free(iter->path);
158 pthread_mutex_destroy(&iter->lock);
5b74c7b1
DG
159 free(iter);
160 found = 1;
161 break;
162 }
163 }
6c9cc2ab 164 unlock_session_list();
5b74c7b1
DG
165
166 return found;
167}
168
169/*
050349bb 170 * Create a brand new session and add it to the session list.
5b74c7b1 171 */
f3ed775e 172int create_session(char *name, char *path)
5b74c7b1 173{
f3ed775e 174 int ret;
5b74c7b1 175 struct ltt_session *new_session;
e07ae692 176
5b74c7b1
DG
177 new_session = find_session_by_name(name);
178 if (new_session != NULL) {
f3ed775e
DG
179 ret = -EEXIST;
180 goto error_exist;
5b74c7b1
DG
181 }
182
183 /* Allocate session data structure */
184 new_session = malloc(sizeof(struct ltt_session));
185 if (new_session == NULL) {
186 perror("malloc");
f3ed775e
DG
187 ret = -ENOMEM;
188 goto error_malloc;
5b74c7b1
DG
189 }
190
f3ed775e 191 /* Define session name */
5b74c7b1
DG
192 if (name != NULL) {
193 if (asprintf(&new_session->name, "%s", name) < 0) {
f3ed775e
DG
194 ret = -ENOMEM;
195 goto error_asprintf;
5b74c7b1
DG
196 }
197 } else {
f3ed775e
DG
198 ERR("No session name given");
199 ret = -1;
200 goto error;
201 }
202
203 /* Define session system path */
204 if (path != NULL) {
d6175221 205 if (asprintf(&new_session->path, "%s", path) < 0) {
f3ed775e
DG
206 ret = -ENOMEM;
207 goto error_asprintf;
5b74c7b1 208 }
f3ed775e
DG
209 } else {
210 ERR("No session path given");
211 ret = -1;
212 goto error;
5b74c7b1
DG
213 }
214
1d4b027a
DG
215 /* Init kernel session */
216 new_session->kernel_session = NULL;
5b74c7b1
DG
217
218 /* Init list */
219 CDS_INIT_LIST_HEAD(&new_session->ust_traces);
1657e9bb
DG
220
221 /* Set trace list counter */
222 new_session->ust_trace_count = 0;
5b74c7b1 223
b5541356 224 /* Add new session to the session list */
6c9cc2ab 225 lock_session_list();
5b74c7b1 226 add_session_list(new_session);
6c9cc2ab 227 unlock_session_list();
b5541356
DG
228
229 /* Init lock */
230 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 231
b5541356 232 DBG("Tracing session %s created in %s", new_session->name, new_session->path);
b082db07 233
5b74c7b1
DG
234 return 0;
235
236error:
f3ed775e
DG
237error_asprintf:
238 if (new_session != NULL) {
239 free(new_session);
240 }
5b74c7b1 241
f3ed775e
DG
242error_exist:
243error_malloc:
244 return ret;
5b74c7b1 245}
This page took 0.035151 seconds and 4 git commands to generate.