Update documentation following babeltrace UI change
[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/*
74babd95
DG
116 * Return a ltt_session structure ptr that matches name. If no session found,
117 * NULL is returned. This must be called with the session lock held using
118 * session_lock_list and session_unlock_list.
5b74c7b1 119 */
54d01ffb 120struct ltt_session *session_find_by_name(char *name)
5b74c7b1 121{
5b74c7b1
DG
122 struct ltt_session *iter;
123
5f822d0a
DG
124 DBG2("Trying to find session by name %s", name);
125
5b74c7b1 126 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 127 if (strncmp(iter->name, name, NAME_MAX) == 0) {
74babd95 128 goto found;
5b74c7b1
DG
129 }
130 }
131
74babd95 132 iter = NULL;
5b74c7b1 133
74babd95 134found:
5b74c7b1
DG
135 return iter;
136}
137
138/*
050349bb 139 * Delete session from the session list and free the memory.
5b74c7b1 140 *
050349bb 141 * Return -1 if no session is found. On success, return 1;
5b74c7b1 142 */
271933a4 143int session_destroy(struct ltt_session *session)
5b74c7b1 144{
271933a4
DG
145 /* Safety check */
146 if (session == NULL) {
147 ERR("Session pointer was null on session destroy");
148 return LTTCOMM_OK;
5b74c7b1 149 }
271933a4
DG
150
151 DBG("Destroying session %s", session->name);
152 del_session_list(session);
271933a4
DG
153 pthread_mutex_destroy(&session->lock);
154 free(session);
5b74c7b1 155
54d01ffb 156 return LTTCOMM_OK;
5b74c7b1
DG
157}
158
159/*
050349bb 160 * Create a brand new session and add it to the session list.
5b74c7b1 161 */
54d01ffb 162int session_create(char *name, char *path)
5b74c7b1 163{
f3ed775e 164 int ret;
5b74c7b1 165 struct ltt_session *new_session;
e07ae692 166
54d01ffb 167 new_session = session_find_by_name(name);
5b74c7b1 168 if (new_session != NULL) {
54d01ffb 169 ret = LTTCOMM_EXIST_SESS;
f3ed775e 170 goto error_exist;
5b74c7b1
DG
171 }
172
173 /* Allocate session data structure */
174 new_session = malloc(sizeof(struct ltt_session));
175 if (new_session == NULL) {
176 perror("malloc");
54d01ffb 177 ret = LTTCOMM_FATAL;
f3ed775e 178 goto error_malloc;
5b74c7b1
DG
179 }
180
f3ed775e 181 /* Define session name */
5b74c7b1 182 if (name != NULL) {
74babd95 183 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
54d01ffb 184 ret = LTTCOMM_FATAL;
f3ed775e 185 goto error_asprintf;
5b74c7b1
DG
186 }
187 } else {
f3ed775e 188 ERR("No session name given");
54d01ffb 189 ret = LTTCOMM_FATAL;
f3ed775e
DG
190 goto error;
191 }
192
193 /* Define session system path */
194 if (path != NULL) {
74babd95 195 if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) {
54d01ffb 196 ret = LTTCOMM_FATAL;
f3ed775e 197 goto error_asprintf;
5b74c7b1 198 }
f3ed775e
DG
199 } else {
200 ERR("No session path given");
54d01ffb 201 ret = LTTCOMM_FATAL;
f3ed775e 202 goto error;
5b74c7b1
DG
203 }
204
1d4b027a
DG
205 /* Init kernel session */
206 new_session->kernel_session = NULL;
5b74c7b1 207
0177d773
DG
208 /* Init UST session list */
209 CDS_INIT_LIST_HEAD(&new_session->ust_session_list.head);
1657e9bb 210
0177d773
DG
211 /* Init lock */
212 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 213
b5541356 214 /* Add new session to the session list */
54d01ffb 215 session_lock_list();
5b74c7b1 216 add_session_list(new_session);
54d01ffb 217 session_unlock_list();
b5541356 218
0177d773 219 DBG("Tracing session %s created in %s", name, path);
b082db07 220
54d01ffb 221 return LTTCOMM_OK;
5b74c7b1
DG
222
223error:
f3ed775e
DG
224error_asprintf:
225 if (new_session != NULL) {
226 free(new_session);
227 }
5b74c7b1 228
f3ed775e
DG
229error_exist:
230error_malloc:
231 return ret;
5b74c7b1 232}
This page took 0.035655 seconds and 4 git commands to generate.