Commit | Line | Data |
---|---|---|
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> |
3d071855 MD |
27 | #include <dirent.h> |
28 | #include <sys/types.h> | |
5b74c7b1 | 29 | |
990570ed | 30 | #include <common/common.h> |
db758600 | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 32 | |
5b74c7b1 DG |
33 | #include "session.h" |
34 | ||
8c0faa1d | 35 | /* |
b5541356 | 36 | * NOTES: |
8c0faa1d | 37 | * |
b5541356 DG |
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 | |
54d01ffb | 41 | * using session_lock() and session_unlock(). |
8c0faa1d | 42 | */ |
8c0faa1d | 43 | |
5b74c7b1 | 44 | /* |
b5541356 | 45 | * Init tracing session list. |
5b74c7b1 | 46 | * |
b5541356 | 47 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 48 | */ |
b5541356 DG |
49 | static struct ltt_session_list ltt_session_list = { |
50 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
51 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a24f7994 | 52 | .next_uuid = 0, |
b5541356 | 53 | }; |
5b74c7b1 | 54 | |
1c1c3634 DG |
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 | ||
5b74c7b1 | 91 | /* |
050349bb | 92 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 93 | * |
050349bb | 94 | * The caller MUST acquire the session list lock before. |
44e96653 | 95 | * Returns the unique identifier for the session. |
5b74c7b1 | 96 | */ |
d022620a | 97 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 98 | { |
0525e9ae DG |
99 | assert(ls); |
100 | ||
5b74c7b1 | 101 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 102 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
103 | } |
104 | ||
105 | /* | |
050349bb | 106 | * Delete a ltt_session structure to the global list. |
b5541356 | 107 | * |
050349bb | 108 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
109 | */ |
110 | static void del_session_list(struct ltt_session *ls) | |
111 | { | |
0525e9ae DG |
112 | assert(ls); |
113 | ||
5b74c7b1 | 114 | cds_list_del(&ls->list); |
5b74c7b1 DG |
115 | } |
116 | ||
b5541356 | 117 | /* |
050349bb | 118 | * Return a pointer to the session list. |
b5541356 | 119 | */ |
54d01ffb | 120 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
121 | { |
122 | return <t_session_list; | |
123 | } | |
124 | ||
125 | /* | |
6c9cc2ab | 126 | * Acquire session list lock |
b5541356 | 127 | */ |
54d01ffb | 128 | void session_lock_list(void) |
b5541356 | 129 | { |
6c9cc2ab | 130 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
131 | } |
132 | ||
133 | /* | |
6c9cc2ab | 134 | * Release session list lock |
b5541356 | 135 | */ |
54d01ffb | 136 | void session_unlock_list(void) |
b5541356 | 137 | { |
6c9cc2ab | 138 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
139 | } |
140 | ||
141 | /* | |
6c9cc2ab | 142 | * Acquire session lock |
b5541356 | 143 | */ |
54d01ffb | 144 | void session_lock(struct ltt_session *session) |
b5541356 | 145 | { |
0525e9ae DG |
146 | assert(session); |
147 | ||
6c9cc2ab DG |
148 | pthread_mutex_lock(&session->lock); |
149 | } | |
b5541356 | 150 | |
6c9cc2ab DG |
151 | /* |
152 | * Release session lock | |
153 | */ | |
54d01ffb | 154 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 155 | { |
0525e9ae DG |
156 | assert(session); |
157 | ||
6c9cc2ab | 158 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
159 | } |
160 | ||
5b74c7b1 | 161 | /* |
74babd95 DG |
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. | |
5b74c7b1 | 165 | */ |
58a1a227 | 166 | struct ltt_session *session_find_by_name(const char *name) |
5b74c7b1 | 167 | { |
5b74c7b1 DG |
168 | struct ltt_session *iter; |
169 | ||
0525e9ae DG |
170 | assert(name); |
171 | ||
5f822d0a DG |
172 | DBG2("Trying to find session by name %s", name); |
173 | ||
5b74c7b1 | 174 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 175 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
74babd95 | 176 | goto found; |
5b74c7b1 DG |
177 | } |
178 | } | |
179 | ||
74babd95 | 180 | iter = NULL; |
5b74c7b1 | 181 | |
74babd95 | 182 | found: |
5b74c7b1 DG |
183 | return iter; |
184 | } | |
185 | ||
186 | /* | |
050349bb | 187 | * Delete session from the session list and free the memory. |
5b74c7b1 | 188 | * |
050349bb | 189 | * Return -1 if no session is found. On success, return 1; |
36b588ed | 190 | * Should *NOT* be called with RCU read-side lock held. |
5b74c7b1 | 191 | */ |
271933a4 | 192 | int session_destroy(struct ltt_session *session) |
5b74c7b1 | 193 | { |
271933a4 | 194 | /* Safety check */ |
0525e9ae | 195 | assert(session); |
271933a4 DG |
196 | |
197 | DBG("Destroying session %s", session->name); | |
198 | del_session_list(session); | |
271933a4 | 199 | pthread_mutex_destroy(&session->lock); |
07424f16 | 200 | |
95a90d01 | 201 | consumer_output_put(session->consumer); |
6dc3064a | 202 | snapshot_destroy(&session->snapshot); |
271933a4 | 203 | free(session); |
5b74c7b1 | 204 | |
f73fabfd | 205 | return LTTNG_OK; |
5b74c7b1 DG |
206 | } |
207 | ||
208 | /* | |
050349bb | 209 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 210 | */ |
dec56f6c | 211 | int session_create(char *name, uid_t uid, gid_t gid) |
5b74c7b1 | 212 | { |
f3ed775e | 213 | int ret; |
5b74c7b1 | 214 | struct ltt_session *new_session; |
e07ae692 | 215 | |
5b74c7b1 | 216 | /* Allocate session data structure */ |
ba7f0ae5 | 217 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 218 | if (new_session == NULL) { |
df0f840b | 219 | PERROR("zmalloc"); |
f73fabfd | 220 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 221 | goto error_malloc; |
5b74c7b1 DG |
222 | } |
223 | ||
f3ed775e | 224 | /* Define session name */ |
5b74c7b1 | 225 | if (name != NULL) { |
74babd95 | 226 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
f73fabfd | 227 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 228 | goto error_asprintf; |
5b74c7b1 DG |
229 | } |
230 | } else { | |
f3ed775e | 231 | ERR("No session name given"); |
f73fabfd | 232 | ret = LTTNG_ERR_FATAL; |
f3ed775e DG |
233 | goto error; |
234 | } | |
235 | ||
1c1c3634 DG |
236 | ret = validate_name(name); |
237 | if (ret < 0) { | |
238 | ret = LTTNG_ERR_SESSION_INVALID_CHAR; | |
239 | goto error; | |
240 | } | |
241 | ||
d3e2ba59 | 242 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
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 | } | |
d3e2ba59 JD |
250 | } |
251 | ||
1d4b027a DG |
252 | /* Init kernel session */ |
253 | new_session->kernel_session = NULL; | |
f6a9efaa | 254 | new_session->ust_session = NULL; |
1657e9bb | 255 | |
0177d773 DG |
256 | /* Init lock */ |
257 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 258 | |
6df2e2c9 MD |
259 | new_session->uid = uid; |
260 | new_session->gid = gid; | |
261 | ||
6dc3064a DG |
262 | ret = snapshot_init(&new_session->snapshot); |
263 | if (ret < 0) { | |
264 | ret = LTTNG_ERR_NOMEM; | |
265 | goto error; | |
266 | } | |
267 | ||
b5541356 | 268 | /* Add new session to the session list */ |
54d01ffb | 269 | session_lock_list(); |
a991f516 | 270 | new_session->id = add_session_list(new_session); |
54d01ffb | 271 | session_unlock_list(); |
b5541356 | 272 | |
a4b92340 DG |
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 | ||
d022620a DG |
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); | |
b082db07 | 280 | |
f73fabfd | 281 | return LTTNG_OK; |
5b74c7b1 DG |
282 | |
283 | error: | |
f3ed775e | 284 | error_asprintf: |
0e428499 | 285 | free(new_session); |
5b74c7b1 | 286 | |
f3ed775e DG |
287 | error_malloc: |
288 | return ret; | |
5b74c7b1 | 289 | } |
2f77fc4b DG |
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 | } |