Update comments regarding the power of 2 constraint on sub-buffer sizes
[lttng-tools.git] / src / bin / lttng-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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <limits.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <urcu.h>
27 #include <dirent.h>
28 #include <sys/types.h>
29
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32
33 #include "session.h"
34
35 /*
36 * NOTES:
37 *
38 * No ltt_session.lock is taken here because those data structure are widely
39 * spread across the lttng-tools code base so before caling functions below
40 * that can read/write a session, the caller MUST acquire the session lock
41 * using session_lock() and session_unlock().
42 */
43
44 /*
45 * Init tracing session list.
46 *
47 * Please see session.h for more explanation and correct usage of the list.
48 */
49 static struct ltt_session_list ltt_session_list = {
50 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
51 .lock = PTHREAD_MUTEX_INITIALIZER,
52 .next_uuid = 0,
53 };
54
55 /* These characters are forbidden in a session name. Used by validate_name. */
56 static const char *forbidden_name_chars = "/";
57
58 /*
59 * Validate the session name for forbidden characters.
60 *
61 * Return 0 on success else -1 meaning a forbidden char. has been found.
62 */
63 static int validate_name(const char *name)
64 {
65 int ret;
66 char *tok, *tmp_name;
67
68 assert(name);
69
70 tmp_name = strdup(name);
71 if (!tmp_name) {
72 /* ENOMEM here. */
73 ret = -1;
74 goto error;
75 }
76
77 tok = strpbrk(tmp_name, forbidden_name_chars);
78 if (tok) {
79 DBG("Session name %s contains a forbidden character", name);
80 /* Forbidden character has been found. */
81 ret = -1;
82 goto error;
83 }
84 ret = 0;
85
86 error:
87 free(tmp_name);
88 return ret;
89 }
90
91 /*
92 * Add a ltt_session structure to the global list.
93 *
94 * The caller MUST acquire the session list lock before.
95 * Returns the unique identifier for the session.
96 */
97 static uint64_t add_session_list(struct ltt_session *ls)
98 {
99 assert(ls);
100
101 cds_list_add(&ls->list, &ltt_session_list.head);
102 return ltt_session_list.next_uuid++;
103 }
104
105 /*
106 * Delete a ltt_session structure to the global list.
107 *
108 * The caller MUST acquire the session list lock before.
109 */
110 static void del_session_list(struct ltt_session *ls)
111 {
112 assert(ls);
113
114 cds_list_del(&ls->list);
115 }
116
117 /*
118 * Return a pointer to the session list.
119 */
120 struct ltt_session_list *session_get_list(void)
121 {
122 return &ltt_session_list;
123 }
124
125 /*
126 * Acquire session list lock
127 */
128 void session_lock_list(void)
129 {
130 pthread_mutex_lock(&ltt_session_list.lock);
131 }
132
133 /*
134 * Release session list lock
135 */
136 void session_unlock_list(void)
137 {
138 pthread_mutex_unlock(&ltt_session_list.lock);
139 }
140
141 /*
142 * Acquire session lock
143 */
144 void session_lock(struct ltt_session *session)
145 {
146 assert(session);
147
148 pthread_mutex_lock(&session->lock);
149 }
150
151 /*
152 * Release session lock
153 */
154 void session_unlock(struct ltt_session *session)
155 {
156 assert(session);
157
158 pthread_mutex_unlock(&session->lock);
159 }
160
161 /*
162 * Return a ltt_session structure ptr that matches name. If no session found,
163 * NULL is returned. This must be called with the session lock held using
164 * session_lock_list and session_unlock_list.
165 */
166 struct ltt_session *session_find_by_name(const char *name)
167 {
168 struct ltt_session *iter;
169
170 assert(name);
171
172 DBG2("Trying to find session by name %s", name);
173
174 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
175 if (strncmp(iter->name, name, NAME_MAX) == 0) {
176 goto found;
177 }
178 }
179
180 iter = NULL;
181
182 found:
183 return iter;
184 }
185
186 /*
187 * Delete session from the session list and free the memory.
188 *
189 * Return -1 if no session is found. On success, return 1;
190 * Should *NOT* be called with RCU read-side lock held.
191 */
192 int session_destroy(struct ltt_session *session)
193 {
194 /* Safety check */
195 assert(session);
196
197 DBG("Destroying session %s", session->name);
198 del_session_list(session);
199 pthread_mutex_destroy(&session->lock);
200
201 consumer_destroy_output(session->consumer);
202 snapshot_destroy(&session->snapshot);
203 free(session);
204
205 return LTTNG_OK;
206 }
207
208 /*
209 * Create a brand new session and add it to the session list.
210 */
211 int session_create(char *name, uid_t uid, gid_t gid)
212 {
213 int ret;
214 struct ltt_session *new_session;
215
216 /* Allocate session data structure */
217 new_session = zmalloc(sizeof(struct ltt_session));
218 if (new_session == NULL) {
219 PERROR("zmalloc");
220 ret = LTTNG_ERR_FATAL;
221 goto error_malloc;
222 }
223
224 /* Define session name */
225 if (name != NULL) {
226 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
227 ret = LTTNG_ERR_FATAL;
228 goto error_asprintf;
229 }
230 } else {
231 ERR("No session name given");
232 ret = LTTNG_ERR_FATAL;
233 goto error;
234 }
235
236 ret = validate_name(name);
237 if (ret < 0) {
238 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
239 goto error;
240 }
241
242 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
243 if (ret < 0) {
244 if (errno == ENAMETOOLONG) {
245 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
246 } else {
247 ret = LTTNG_ERR_FATAL;
248 goto error;
249 }
250 }
251
252 /* Init kernel session */
253 new_session->kernel_session = NULL;
254 new_session->ust_session = NULL;
255
256 /* Init lock */
257 pthread_mutex_init(&new_session->lock, NULL);
258
259 new_session->uid = uid;
260 new_session->gid = gid;
261
262 ret = snapshot_init(&new_session->snapshot);
263 if (ret < 0) {
264 ret = LTTNG_ERR_NOMEM;
265 goto error;
266 }
267
268 /* Add new session to the session list */
269 session_lock_list();
270 new_session->id = add_session_list(new_session);
271 session_unlock_list();
272
273 /*
274 * Consumer is let to NULL since the create_session_uri command will set it
275 * up and, if valid, assign it to the session.
276 */
277
278 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
279 name, new_session->id, new_session->uid, new_session->gid);
280
281 return LTTNG_OK;
282
283 error:
284 error_asprintf:
285 free(new_session);
286
287 error_malloc:
288 return ret;
289 }
290
291 /*
292 * Check if the UID or GID match the session. Root user has access to all
293 * sessions.
294 */
295 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
296 {
297 assert(session);
298
299 if (uid != session->uid && gid != session->gid && uid != 0) {
300 return 0;
301 } else {
302 return 1;
303 }
304 }
This page took 0.038735 seconds and 4 git commands to generate.