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