Fix: define _LGPL_SOURCE in C files
[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
28 #include <common/common.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30
31 #include "session.h"
32
33 /*
34 * NOTES:
35 *
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
39 * using session_lock() and session_unlock().
40 */
41
42 /*
43 * Init tracing session list.
44 *
45 * Please see session.h for more explanation and correct usage of the list.
46 */
47 static struct ltt_session_list ltt_session_list = {
48 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
49 .lock = PTHREAD_MUTEX_INITIALIZER,
50 .next_uuid = 0,
51 };
52
53 /* These characters are forbidden in a session name. Used by validate_name. */
54 static 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 */
61 static 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
84 error:
85 free(tmp_name);
86 return ret;
87 }
88
89 /*
90 * Add a ltt_session structure to the global list.
91 *
92 * The caller MUST acquire the session list lock before.
93 * Returns the unique identifier for the session.
94 */
95 static uint64_t add_session_list(struct ltt_session *ls)
96 {
97 assert(ls);
98
99 cds_list_add(&ls->list, &ltt_session_list.head);
100 return ltt_session_list.next_uuid++;
101 }
102
103 /*
104 * Delete a ltt_session structure to the global list.
105 *
106 * The caller MUST acquire the session list lock before.
107 */
108 static void del_session_list(struct ltt_session *ls)
109 {
110 assert(ls);
111
112 cds_list_del(&ls->list);
113 }
114
115 /*
116 * Return a pointer to the session list.
117 */
118 struct ltt_session_list *session_get_list(void)
119 {
120 return &ltt_session_list;
121 }
122
123 /*
124 * Acquire session list lock
125 */
126 void session_lock_list(void)
127 {
128 pthread_mutex_lock(&ltt_session_list.lock);
129 }
130
131 /*
132 * Release session list lock
133 */
134 void session_unlock_list(void)
135 {
136 pthread_mutex_unlock(&ltt_session_list.lock);
137 }
138
139 /*
140 * Acquire session lock
141 */
142 void session_lock(struct ltt_session *session)
143 {
144 assert(session);
145
146 pthread_mutex_lock(&session->lock);
147 }
148
149 /*
150 * Release session lock
151 */
152 void session_unlock(struct ltt_session *session)
153 {
154 assert(session);
155
156 pthread_mutex_unlock(&session->lock);
157 }
158
159 /*
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.
163 */
164 struct ltt_session *session_find_by_name(const char *name)
165 {
166 struct ltt_session *iter;
167
168 assert(name);
169
170 DBG2("Trying to find session by name %s", name);
171
172 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
173 if (strncmp(iter->name, name, NAME_MAX) == 0) {
174 goto found;
175 }
176 }
177
178 iter = NULL;
179
180 found:
181 return iter;
182 }
183
184 /*
185 * Delete session from the session list and free the memory.
186 *
187 * Return -1 if no session is found. On success, return 1;
188 * Should *NOT* be called with RCU read-side lock held.
189 */
190 int session_destroy(struct ltt_session *session)
191 {
192 /* Safety check */
193 assert(session);
194
195 DBG("Destroying session %s", session->name);
196 del_session_list(session);
197 pthread_mutex_destroy(&session->lock);
198
199 consumer_destroy_output(session->consumer);
200 snapshot_destroy(&session->snapshot);
201 free(session);
202
203 return LTTNG_OK;
204 }
205
206 /*
207 * Create a brand new session and add it to the session list.
208 */
209 int session_create(char *name, uid_t uid, gid_t gid)
210 {
211 int ret;
212 struct ltt_session *new_session;
213
214 /* Allocate session data structure */
215 new_session = zmalloc(sizeof(struct ltt_session));
216 if (new_session == NULL) {
217 PERROR("zmalloc");
218 ret = LTTNG_ERR_FATAL;
219 goto error_malloc;
220 }
221
222 /* Define session name */
223 if (name != NULL) {
224 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
225 ret = LTTNG_ERR_FATAL;
226 goto error_asprintf;
227 }
228 } else {
229 ERR("No session name given");
230 ret = LTTNG_ERR_FATAL;
231 goto error;
232 }
233
234 ret = validate_name(name);
235 if (ret < 0) {
236 ret = LTTNG_ERR_SESSION_INVALID_CHAR;
237 goto error;
238 }
239
240 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
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 }
248 }
249
250 /* Init kernel session */
251 new_session->kernel_session = NULL;
252 new_session->ust_session = NULL;
253
254 /* Init lock */
255 pthread_mutex_init(&new_session->lock, NULL);
256
257 new_session->uid = uid;
258 new_session->gid = gid;
259
260 ret = snapshot_init(&new_session->snapshot);
261 if (ret < 0) {
262 ret = LTTNG_ERR_NOMEM;
263 goto error;
264 }
265
266 /* Add new session to the session list */
267 session_lock_list();
268 new_session->id = add_session_list(new_session);
269 session_unlock_list();
270
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
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);
278
279 return LTTNG_OK;
280
281 error:
282 error_asprintf:
283 free(new_session);
284
285 error_malloc:
286 return ret;
287 }
288
289 /*
290 * Check if the UID or GID match the session. Root user has access to all
291 * sessions.
292 */
293 int 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.035628 seconds and 5 git commands to generate.