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 | |
6c9cc2ab | 19 | #include <limits.h> |
d022620a | 20 | #include <inttypes.h> |
5b74c7b1 DG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
a304c14c | 24 | #include <sys/stat.h> |
f6a9efaa | 25 | #include <urcu.h> |
5b74c7b1 | 26 | |
990570ed | 27 | #include <common/common.h> |
db758600 | 28 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 29 | |
5b74c7b1 DG |
30 | #include "session.h" |
31 | ||
8c0faa1d | 32 | /* |
b5541356 | 33 | * NOTES: |
8c0faa1d | 34 | * |
b5541356 DG |
35 | * No ltt_session.lock is taken here because those data structure are widely |
36 | * spread across the lttng-tools code base so before caling functions below | |
37 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 38 | * using session_lock() and session_unlock(). |
8c0faa1d | 39 | */ |
8c0faa1d | 40 | |
5b74c7b1 | 41 | /* |
b5541356 | 42 | * Init tracing session list. |
5b74c7b1 | 43 | * |
b5541356 | 44 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 45 | */ |
b5541356 DG |
46 | static struct ltt_session_list ltt_session_list = { |
47 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
48 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a24f7994 | 49 | .next_uuid = 0, |
b5541356 | 50 | }; |
5b74c7b1 DG |
51 | |
52 | /* | |
050349bb | 53 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 54 | * |
050349bb | 55 | * The caller MUST acquire the session list lock before. |
44e96653 | 56 | * Returns the unique identifier for the session. |
5b74c7b1 | 57 | */ |
d022620a | 58 | static uint64_t add_session_list(struct ltt_session *ls) |
5b74c7b1 | 59 | { |
0525e9ae DG |
60 | assert(ls); |
61 | ||
5b74c7b1 | 62 | cds_list_add(&ls->list, <t_session_list.head); |
a24f7994 | 63 | return ltt_session_list.next_uuid++; |
5b74c7b1 DG |
64 | } |
65 | ||
66 | /* | |
050349bb | 67 | * Delete a ltt_session structure to the global list. |
b5541356 | 68 | * |
050349bb | 69 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
70 | */ |
71 | static void del_session_list(struct ltt_session *ls) | |
72 | { | |
0525e9ae DG |
73 | assert(ls); |
74 | ||
5b74c7b1 | 75 | cds_list_del(&ls->list); |
5b74c7b1 DG |
76 | } |
77 | ||
b5541356 | 78 | /* |
050349bb | 79 | * Return a pointer to the session list. |
b5541356 | 80 | */ |
54d01ffb | 81 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
82 | { |
83 | return <t_session_list; | |
84 | } | |
85 | ||
86 | /* | |
6c9cc2ab | 87 | * Acquire session list lock |
b5541356 | 88 | */ |
54d01ffb | 89 | void session_lock_list(void) |
b5541356 | 90 | { |
6c9cc2ab | 91 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
92 | } |
93 | ||
94 | /* | |
6c9cc2ab | 95 | * Release session list lock |
b5541356 | 96 | */ |
54d01ffb | 97 | void session_unlock_list(void) |
b5541356 | 98 | { |
6c9cc2ab | 99 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
100 | } |
101 | ||
102 | /* | |
6c9cc2ab | 103 | * Acquire session lock |
b5541356 | 104 | */ |
54d01ffb | 105 | void session_lock(struct ltt_session *session) |
b5541356 | 106 | { |
0525e9ae DG |
107 | assert(session); |
108 | ||
6c9cc2ab DG |
109 | pthread_mutex_lock(&session->lock); |
110 | } | |
b5541356 | 111 | |
6c9cc2ab DG |
112 | /* |
113 | * Release session lock | |
114 | */ | |
54d01ffb | 115 | void session_unlock(struct ltt_session *session) |
6c9cc2ab | 116 | { |
0525e9ae DG |
117 | assert(session); |
118 | ||
6c9cc2ab | 119 | pthread_mutex_unlock(&session->lock); |
b5541356 DG |
120 | } |
121 | ||
5b74c7b1 | 122 | /* |
74babd95 DG |
123 | * Return a ltt_session structure ptr that matches name. If no session found, |
124 | * NULL is returned. This must be called with the session lock held using | |
125 | * session_lock_list and session_unlock_list. | |
5b74c7b1 | 126 | */ |
54d01ffb | 127 | struct ltt_session *session_find_by_name(char *name) |
5b74c7b1 | 128 | { |
5b74c7b1 DG |
129 | struct ltt_session *iter; |
130 | ||
0525e9ae DG |
131 | assert(name); |
132 | ||
5f822d0a DG |
133 | DBG2("Trying to find session by name %s", name); |
134 | ||
5b74c7b1 | 135 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 136 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
74babd95 | 137 | goto found; |
5b74c7b1 DG |
138 | } |
139 | } | |
140 | ||
74babd95 | 141 | iter = NULL; |
5b74c7b1 | 142 | |
74babd95 | 143 | found: |
5b74c7b1 DG |
144 | return iter; |
145 | } | |
146 | ||
147 | /* | |
050349bb | 148 | * Delete session from the session list and free the memory. |
5b74c7b1 | 149 | * |
050349bb | 150 | * Return -1 if no session is found. On success, return 1; |
36b588ed | 151 | * Should *NOT* be called with RCU read-side lock held. |
5b74c7b1 | 152 | */ |
271933a4 | 153 | int session_destroy(struct ltt_session *session) |
5b74c7b1 | 154 | { |
271933a4 | 155 | /* Safety check */ |
0525e9ae | 156 | assert(session); |
271933a4 DG |
157 | |
158 | DBG("Destroying session %s", session->name); | |
159 | del_session_list(session); | |
271933a4 | 160 | pthread_mutex_destroy(&session->lock); |
07424f16 | 161 | |
07424f16 | 162 | consumer_destroy_output(session->consumer); |
6dc3064a | 163 | snapshot_destroy(&session->snapshot); |
271933a4 | 164 | free(session); |
5b74c7b1 | 165 | |
f73fabfd | 166 | return LTTNG_OK; |
5b74c7b1 DG |
167 | } |
168 | ||
169 | /* | |
050349bb | 170 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 171 | */ |
dec56f6c | 172 | int session_create(char *name, uid_t uid, gid_t gid) |
5b74c7b1 | 173 | { |
f3ed775e | 174 | int ret; |
5b74c7b1 | 175 | struct ltt_session *new_session; |
e07ae692 | 176 | |
5b74c7b1 | 177 | /* Allocate session data structure */ |
ba7f0ae5 | 178 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 179 | if (new_session == NULL) { |
df0f840b | 180 | PERROR("zmalloc"); |
f73fabfd | 181 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 182 | goto error_malloc; |
5b74c7b1 DG |
183 | } |
184 | ||
f3ed775e | 185 | /* Define session name */ |
5b74c7b1 | 186 | if (name != NULL) { |
74babd95 | 187 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
f73fabfd | 188 | ret = LTTNG_ERR_FATAL; |
f3ed775e | 189 | goto error_asprintf; |
5b74c7b1 DG |
190 | } |
191 | } else { | |
f3ed775e | 192 | ERR("No session name given"); |
f73fabfd | 193 | ret = LTTNG_ERR_FATAL; |
f3ed775e DG |
194 | goto error; |
195 | } | |
196 | ||
d3e2ba59 | 197 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
73184835 DG |
198 | if (ret < 0) { |
199 | if (errno == ENAMETOOLONG) { | |
200 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; | |
201 | } else { | |
202 | ret = LTTNG_ERR_FATAL; | |
203 | goto error; | |
204 | } | |
d3e2ba59 JD |
205 | } |
206 | ||
1d4b027a DG |
207 | /* Init kernel session */ |
208 | new_session->kernel_session = NULL; | |
f6a9efaa | 209 | new_session->ust_session = NULL; |
1657e9bb | 210 | |
0177d773 DG |
211 | /* Init lock */ |
212 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 213 | |
6df2e2c9 MD |
214 | new_session->uid = uid; |
215 | new_session->gid = gid; | |
216 | ||
6dc3064a DG |
217 | ret = snapshot_init(&new_session->snapshot); |
218 | if (ret < 0) { | |
219 | ret = LTTNG_ERR_NOMEM; | |
220 | goto error; | |
221 | } | |
222 | ||
b5541356 | 223 | /* Add new session to the session list */ |
54d01ffb | 224 | session_lock_list(); |
a991f516 | 225 | new_session->id = add_session_list(new_session); |
54d01ffb | 226 | session_unlock_list(); |
b5541356 | 227 | |
a4b92340 DG |
228 | /* |
229 | * Consumer is let to NULL since the create_session_uri command will set it | |
230 | * up and, if valid, assign it to the session. | |
231 | */ | |
232 | ||
d022620a DG |
233 | DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d", |
234 | name, new_session->id, new_session->uid, new_session->gid); | |
b082db07 | 235 | |
f73fabfd | 236 | return LTTNG_OK; |
5b74c7b1 DG |
237 | |
238 | error: | |
f3ed775e | 239 | error_asprintf: |
0e428499 | 240 | free(new_session); |
5b74c7b1 | 241 | |
f3ed775e DG |
242 | error_malloc: |
243 | return ret; | |
5b74c7b1 | 244 | } |
2f77fc4b DG |
245 | |
246 | /* | |
247 | * Check if the UID or GID match the session. Root user has access to all | |
248 | * sessions. | |
249 | */ | |
250 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) | |
251 | { | |
252 | assert(session); | |
253 | ||
254 | if (uid != session->uid && gid != session->gid && uid != 0) { | |
255 | return 0; | |
256 | } else { | |
257 | return 1; | |
258 | } | |
259 | } |