2b7d65eaa1b61fbae82a230ee2a6b7247505d0ac
[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 _LGPL_SOURCE
19 #include <limits.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <urcu.h>
26 #include <dirent.h>
27 #include <sys/types.h>
28
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31
32 #include "session.h"
33 #include "utils.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 /* Global hash table to keep the sessions, indexed by id. */
59 static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
60
61 /*
62 * Validate the session name for forbidden characters.
63 *
64 * Return 0 on success else -1 meaning a forbidden char. has been found.
65 */
66 static int validate_name(const char *name)
67 {
68 int ret;
69 char *tok, *tmp_name;
70
71 assert(name);
72
73 tmp_name = strdup(name);
74 if (!tmp_name) {
75 /* ENOMEM here. */
76 ret = -1;
77 goto error;
78 }
79
80 tok = strpbrk(tmp_name, forbidden_name_chars);
81 if (tok) {
82 DBG("Session name %s contains a forbidden character", name);
83 /* Forbidden character has been found. */
84 ret = -1;
85 goto error;
86 }
87 ret = 0;
88
89 error:
90 free(tmp_name);
91 return ret;
92 }
93
94 /*
95 * Add a ltt_session structure to the global list.
96 *
97 * The caller MUST acquire the session list lock before.
98 * Returns the unique identifier for the session.
99 */
100 static uint64_t add_session_list(struct ltt_session *ls)
101 {
102 assert(ls);
103
104 cds_list_add(&ls->list, &ltt_session_list.head);
105 return ltt_session_list.next_uuid++;
106 }
107
108 /*
109 * Delete a ltt_session structure to the global list.
110 *
111 * The caller MUST acquire the session list lock before.
112 */
113 static void del_session_list(struct ltt_session *ls)
114 {
115 assert(ls);
116
117 cds_list_del(&ls->list);
118 }
119
120 /*
121 * Return a pointer to the session list.
122 */
123 struct ltt_session_list *session_get_list(void)
124 {
125 return &ltt_session_list;
126 }
127
128 /*
129 * Acquire session list lock
130 */
131 void session_lock_list(void)
132 {
133 pthread_mutex_lock(&ltt_session_list.lock);
134 }
135
136 /*
137 * Release session list lock
138 */
139 void session_unlock_list(void)
140 {
141 pthread_mutex_unlock(&ltt_session_list.lock);
142 }
143
144 /*
145 * Allocate the ltt_sessions_ht_by_id HT.
146 *
147 * The session list lock must be held.
148 */
149 int ltt_sessions_ht_alloc(void)
150 {
151 int ret = 0;
152
153 DBG("Allocating ltt_sessions_ht_by_id");
154 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
155 if (!ltt_sessions_ht_by_id) {
156 ret = -1;
157 ERR("Failed to allocate ltt_sessions_ht_by_id");
158 goto end;
159 }
160 end:
161 return ret;
162 }
163
164 /*
165 * Destroy the ltt_sessions_ht_by_id HT.
166 *
167 * The session list lock must be held.
168 */
169 static void ltt_sessions_ht_destroy(void)
170 {
171 if (!ltt_sessions_ht_by_id) {
172 return;
173 }
174 ht_cleanup_push(ltt_sessions_ht_by_id);
175 ltt_sessions_ht_by_id = NULL;
176 }
177
178 /*
179 * Add a ltt_session to the ltt_sessions_ht_by_id.
180 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
181 * The session list lock must be held.
182 */
183 static void add_session_ht(struct ltt_session *ls)
184 {
185 int ret;
186
187 assert(ls);
188
189 if (!ltt_sessions_ht_by_id) {
190 ret = ltt_sessions_ht_alloc();
191 if (ret) {
192 ERR("Error allocating the sessions HT");
193 goto end;
194 }
195 }
196 lttng_ht_node_init_u64(&ls->node, ls->id);
197 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
198
199 end:
200 return;
201 }
202
203 /*
204 * Test if ltt_sessions_ht_by_id is empty.
205 * Return 1 if empty, 0 if not empty.
206 * The session list lock must be held.
207 */
208 static int ltt_sessions_ht_empty(void)
209 {
210 int ret;
211
212 if (!ltt_sessions_ht_by_id) {
213 ret = 1;
214 goto end;
215 }
216
217 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
218 end:
219 return ret;
220 }
221
222 /*
223 * Remove a ltt_session from the ltt_sessions_ht_by_id.
224 * If empty, the ltt_sessions_ht_by_id HT is freed.
225 * The session list lock must be held.
226 */
227 static void del_session_ht(struct ltt_session *ls)
228 {
229 struct lttng_ht_iter iter;
230 int ret;
231
232 assert(ls);
233 assert(ltt_sessions_ht_by_id);
234
235 iter.iter.node = &ls->node.node;
236 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
237 assert(!ret);
238
239 if (ltt_sessions_ht_empty()) {
240 DBG("Empty ltt_sessions_ht_by_id, destroying it");
241 ltt_sessions_ht_destroy();
242 }
243 }
244
245 /*
246 * Acquire session lock
247 */
248 void session_lock(struct ltt_session *session)
249 {
250 assert(session);
251
252 pthread_mutex_lock(&session->lock);
253 }
254
255 /*
256 * Release session lock
257 */
258 void session_unlock(struct ltt_session *session)
259 {
260 assert(session);
261
262 pthread_mutex_unlock(&session->lock);
263 }
264
265 /*
266 * Return a ltt_session structure ptr that matches name. If no session found,
267 * NULL is returned. This must be called with the session list lock held using
268 * session_lock_list and session_unlock_list.
269 */
270 struct ltt_session *session_find_by_name(const char *name)
271 {
272 struct ltt_session *iter;
273
274 assert(name);
275
276 DBG2("Trying to find session by name %s", name);
277
278 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
279 if (strncmp(iter->name, name, NAME_MAX) == 0) {
280 goto found;
281 }
282 }
283
284 iter = NULL;
285
286 found:
287 return iter;
288 }
289
290 /*
291 * Return an ltt_session that matches the id. If no session is found,
292 * NULL is returned. This must be called with rcu_read_lock and
293 * session list lock held (to guarantee the lifetime of the session).
294 */
295 struct ltt_session *session_find_by_id(uint64_t id)
296 {
297 struct lttng_ht_node_u64 *node;
298 struct lttng_ht_iter iter;
299 struct ltt_session *ls;
300
301 if (!ltt_sessions_ht_by_id) {
302 goto end;
303 }
304
305 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
306 node = lttng_ht_iter_get_node_u64(&iter);
307 if (node == NULL) {
308 goto end;
309 }
310 ls = caa_container_of(node, struct ltt_session, node);
311
312 DBG3("Session %" PRIu64 " found by id.", id);
313 return ls;
314
315 end:
316 DBG3("Session %" PRIu64 " NOT found by id", id);
317 return NULL;
318 }
319
320 /*
321 * Delete session from the session list and free the memory.
322 *
323 * Return -1 if no session is found. On success, return 1;
324 * Should *NOT* be called with RCU read-side lock held.
325 */
326 int session_destroy(struct ltt_session *session)
327 {
328 /* Safety check */
329 assert(session);
330
331 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
332 del_session_list(session);
333 pthread_mutex_destroy(&session->lock);
334 del_session_ht(session);
335
336 consumer_output_put(session->consumer);
337 snapshot_destroy(&session->snapshot);
338 free(session);
339
340 return LTTNG_OK;
341 }
342
343 /*
344 * Create a brand new session and add it to the session list.
345 */
346 int session_create(char *name, uid_t uid, gid_t gid)
347 {
348 int ret;
349 struct ltt_session *new_session;
350
351 /* Allocate session data structure */
352 new_session = zmalloc(sizeof(struct ltt_session));
353 if (new_session == NULL) {
354 PERROR("zmalloc");
355 ret = LTTNG_ERR_FATAL;
356 goto error_malloc;
357 }
358
359 /* Define session name */
360 if (name != NULL) {
361 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
362 ret = LTTNG_ERR_FATAL;
363 goto error_asprintf;
364 }
365 } else {
366 ERR("No session name given");
367 ret = LTTNG_ERR_FATAL;
368 goto error;
369 }
370
371 ret = validate_name(name);
372 if (ret < 0) {
373 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
374 goto error;
375 }
376
377 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
378 if (ret < 0) {
379 if (errno == ENAMETOOLONG) {
380 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
381 } else {
382 ret = LTTNG_ERR_FATAL;
383 goto error;
384 }
385 }
386
387 /* Init kernel session */
388 new_session->kernel_session = NULL;
389 new_session->ust_session = NULL;
390
391 /* Init lock */
392 pthread_mutex_init(&new_session->lock, NULL);
393
394 new_session->uid = uid;
395 new_session->gid = gid;
396
397 ret = snapshot_init(&new_session->snapshot);
398 if (ret < 0) {
399 ret = LTTNG_ERR_NOMEM;
400 goto error;
401 }
402
403 new_session->rotate_pending = false;
404 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
405 new_session->rotate_pending_relay = false;
406 new_session->rotate_relay_pending_timer_enabled = false;
407 new_session->rotate_timer = false;
408
409 /* Add new session to the session list */
410 session_lock_list();
411 new_session->id = add_session_list(new_session);
412 /*
413 * Add the new session to the ltt_sessions_ht_by_id.
414 * No ownership is taken by the hash table; it is merely
415 * a wrapper around the session list used for faster access
416 * by session id.
417 */
418 add_session_ht(new_session);
419 session_unlock_list();
420
421 /*
422 * Consumer is let to NULL since the create_session_uri command will set it
423 * up and, if valid, assign it to the session.
424 */
425 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
426 name, new_session->id, new_session->uid, new_session->gid);
427
428 return LTTNG_OK;
429
430 error:
431 error_asprintf:
432 free(new_session);
433
434 error_malloc:
435 return ret;
436 }
437
438 /*
439 * Check if the UID or GID match the session. Root user has access to all
440 * sessions.
441 */
442 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
443 {
444 assert(session);
445
446 if (uid != session->uid && gid != session->gid && uid != 0) {
447 return 0;
448 } else {
449 return 1;
450 }
451 }
This page took 0.059777 seconds and 4 git commands to generate.