Fix session list deadlock
[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>
5b74c7b1
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
5b74c7b1 24
54d01ffb 25#include <lttng-sessiond-comm.h>
1e307fab
DG
26#include <lttngerr.h>
27
5b74c7b1
DG
28#include "session.h"
29
8c0faa1d 30/*
b5541356 31 * NOTES:
8c0faa1d 32 *
b5541356
DG
33 * No ltt_session.lock is taken here because those data structure are widely
34 * spread across the lttng-tools code base so before caling functions below
35 * that can read/write a session, the caller MUST acquire the session lock
54d01ffb 36 * using session_lock() and session_unlock().
8c0faa1d 37 */
8c0faa1d 38
5b74c7b1 39/*
b5541356 40 * Init tracing session list.
5b74c7b1 41 *
b5541356 42 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 43 */
b5541356
DG
44static struct ltt_session_list ltt_session_list = {
45 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
46 .lock = PTHREAD_MUTEX_INITIALIZER,
47 .count = 0,
48};
5b74c7b1
DG
49
50/*
050349bb 51 * Add a ltt_session structure to the global list.
5b74c7b1 52 *
050349bb 53 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
54 */
55static void add_session_list(struct ltt_session *ls)
56{
57 cds_list_add(&ls->list, &ltt_session_list.head);
b5541356 58 ltt_session_list.count++;
5b74c7b1
DG
59}
60
61/*
050349bb 62 * Delete a ltt_session structure to the global list.
b5541356 63 *
050349bb 64 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
65 */
66static void del_session_list(struct ltt_session *ls)
67{
68 cds_list_del(&ls->list);
69 /* Sanity check */
b5541356
DG
70 if (ltt_session_list.count > 0) {
71 ltt_session_list.count--;
5b74c7b1
DG
72 }
73}
74
b5541356 75/*
050349bb 76 * Return a pointer to the session list.
b5541356 77 */
54d01ffb 78struct ltt_session_list *session_get_list(void)
b5541356
DG
79{
80 return &ltt_session_list;
81}
82
83/*
6c9cc2ab 84 * Acquire session list lock
b5541356 85 */
54d01ffb 86void session_lock_list(void)
b5541356 87{
6c9cc2ab 88 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
89}
90
91/*
6c9cc2ab 92 * Release session list lock
b5541356 93 */
54d01ffb 94void session_unlock_list(void)
b5541356 95{
6c9cc2ab 96 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
97}
98
99/*
6c9cc2ab 100 * Acquire session lock
b5541356 101 */
54d01ffb 102void session_lock(struct ltt_session *session)
b5541356 103{
6c9cc2ab
DG
104 pthread_mutex_lock(&session->lock);
105}
b5541356 106
6c9cc2ab
DG
107/*
108 * Release session lock
109 */
54d01ffb 110void session_unlock(struct ltt_session *session)
6c9cc2ab
DG
111{
112 pthread_mutex_unlock(&session->lock);
b5541356
DG
113}
114
5b74c7b1 115/*
050349bb
DG
116 * Return a ltt_session structure ptr that matches name.
117 * If no session found, NULL is returned.
5b74c7b1 118 */
54d01ffb 119struct ltt_session *session_find_by_name(char *name)
5b74c7b1
DG
120{
121 int found = 0;
122 struct ltt_session *iter;
123
5f822d0a
DG
124 DBG2("Trying to find session by name %s", name);
125
54d01ffb 126 session_lock_list();
5b74c7b1 127 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 128 if (strncmp(iter->name, name, NAME_MAX) == 0) {
5b74c7b1
DG
129 found = 1;
130 break;
131 }
132 }
54d01ffb 133 session_unlock_list();
5b74c7b1
DG
134
135 if (!found) {
136 iter = NULL;
137 }
138
139 return iter;
140}
141
142/*
050349bb 143 * Delete session from the session list and free the memory.
5b74c7b1 144 *
050349bb 145 * Return -1 if no session is found. On success, return 1;
5b74c7b1 146 */
271933a4 147int session_destroy(struct ltt_session *session)
5b74c7b1 148{
271933a4
DG
149 /* Safety check */
150 if (session == NULL) {
151 ERR("Session pointer was null on session destroy");
152 return LTTCOMM_OK;
5b74c7b1 153 }
271933a4
DG
154
155 DBG("Destroying session %s", session->name);
156 del_session_list(session);
157 free(session->name);
158 free(session->path);
159 pthread_mutex_destroy(&session->lock);
160 free(session);
5b74c7b1 161
54d01ffb 162 return LTTCOMM_OK;
5b74c7b1
DG
163}
164
165/*
050349bb 166 * Create a brand new session and add it to the session list.
5b74c7b1 167 */
54d01ffb 168int session_create(char *name, char *path)
5b74c7b1 169{
f3ed775e 170 int ret;
5b74c7b1 171 struct ltt_session *new_session;
e07ae692 172
54d01ffb 173 new_session = session_find_by_name(name);
5b74c7b1 174 if (new_session != NULL) {
54d01ffb 175 ret = LTTCOMM_EXIST_SESS;
f3ed775e 176 goto error_exist;
5b74c7b1
DG
177 }
178
179 /* Allocate session data structure */
180 new_session = malloc(sizeof(struct ltt_session));
181 if (new_session == NULL) {
182 perror("malloc");
54d01ffb 183 ret = LTTCOMM_FATAL;
f3ed775e 184 goto error_malloc;
5b74c7b1
DG
185 }
186
f3ed775e 187 /* Define session name */
5b74c7b1
DG
188 if (name != NULL) {
189 if (asprintf(&new_session->name, "%s", name) < 0) {
54d01ffb 190 ret = LTTCOMM_FATAL;
f3ed775e 191 goto error_asprintf;
5b74c7b1
DG
192 }
193 } else {
f3ed775e 194 ERR("No session name given");
54d01ffb 195 ret = LTTCOMM_FATAL;
f3ed775e
DG
196 goto error;
197 }
198
199 /* Define session system path */
200 if (path != NULL) {
d6175221 201 if (asprintf(&new_session->path, "%s", path) < 0) {
54d01ffb 202 ret = LTTCOMM_FATAL;
f3ed775e 203 goto error_asprintf;
5b74c7b1 204 }
f3ed775e
DG
205 } else {
206 ERR("No session path given");
54d01ffb 207 ret = LTTCOMM_FATAL;
f3ed775e 208 goto error;
5b74c7b1
DG
209 }
210
1d4b027a
DG
211 /* Init kernel session */
212 new_session->kernel_session = NULL;
5b74c7b1 213
0177d773
DG
214 /* Init UST session list */
215 CDS_INIT_LIST_HEAD(&new_session->ust_session_list.head);
1657e9bb 216
0177d773
DG
217 /* Init lock */
218 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 219
b5541356 220 /* Add new session to the session list */
54d01ffb 221 session_lock_list();
5b74c7b1 222 add_session_list(new_session);
54d01ffb 223 session_unlock_list();
b5541356 224
0177d773 225 DBG("Tracing session %s created in %s", name, path);
b082db07 226
54d01ffb 227 return LTTCOMM_OK;
5b74c7b1
DG
228
229error:
f3ed775e
DG
230error_asprintf:
231 if (new_session != NULL) {
232 free(new_session);
233 }
5b74c7b1 234
f3ed775e
DG
235error_exist:
236error_malloc:
237 return ret;
5b74c7b1 238}
This page took 0.036481 seconds and 4 git commands to generate.