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