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