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