Commit | Line | Data |
---|---|---|
5b74c7b1 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
91d76f53 | 8 | * |
5b74c7b1 DG |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
050349bb | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5b74c7b1 DG |
12 | * GNU General Public License for more details. |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
6c9cc2ab | 20 | #include <limits.h> |
5b74c7b1 DG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
a304c14c MD |
24 | #include <sys/stat.h> |
25 | #include <sys/types.h> | |
f6a9efaa | 26 | #include <urcu.h> |
5b74c7b1 | 27 | |
54d01ffb | 28 | #include <lttng-sessiond-comm.h> |
1e307fab | 29 | #include <lttngerr.h> |
a304c14c | 30 | #include <runas.h> |
1e307fab | 31 | |
5b74c7b1 DG |
32 | #include "session.h" |
33 | ||
8c0faa1d | 34 | /* |
b5541356 | 35 | * NOTES: |
8c0faa1d | 36 | * |
b5541356 DG |
37 | * No ltt_session.lock is taken here because those data structure are widely |
38 | * spread across the lttng-tools code base so before caling functions below | |
39 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 40 | * using session_lock() and session_unlock(). |
8c0faa1d | 41 | */ |
8c0faa1d | 42 | |
5b74c7b1 | 43 | /* |
b5541356 | 44 | * Init tracing session list. |
5b74c7b1 | 45 | * |
b5541356 | 46 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 47 | */ |
b5541356 DG |
48 | static struct ltt_session_list ltt_session_list = { |
49 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
50 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
51 | .count = 0, | |
52 | }; | |
5b74c7b1 DG |
53 | |
54 | /* | |
050349bb | 55 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 56 | * |
050349bb | 57 | * The caller MUST acquire the session list lock before. |
44e96653 | 58 | * Returns the unique identifier for the session. |
5b74c7b1 | 59 | */ |
44e96653 | 60 | static int add_session_list(struct ltt_session *ls) |
5b74c7b1 DG |
61 | { |
62 | cds_list_add(&ls->list, <t_session_list.head); | |
44e96653 | 63 | return ++ltt_session_list.count; |
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 | { | |
73 | cds_list_del(&ls->list); | |
74 | /* Sanity check */ | |
b5541356 DG |
75 | if (ltt_session_list.count > 0) { |
76 | ltt_session_list.count--; | |
5b74c7b1 DG |
77 | } |
78 | } | |
79 | ||
b5541356 | 80 | /* |
050349bb | 81 | * Return a pointer to the session list. |
b5541356 | 82 | */ |
54d01ffb | 83 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
84 | { |
85 | return <t_session_list; | |
86 | } | |
87 | ||
88 | /* | |
6c9cc2ab | 89 | * Acquire session list lock |
b5541356 | 90 | */ |
54d01ffb | 91 | void session_lock_list(void) |
b5541356 | 92 | { |
6c9cc2ab | 93 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
94 | } |
95 | ||
96 | /* | |
6c9cc2ab | 97 | * Release session list lock |
b5541356 | 98 | */ |
54d01ffb | 99 | void session_unlock_list(void) |
b5541356 | 100 | { |
6c9cc2ab | 101 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
102 | } |
103 | ||
104 | /* | |
6c9cc2ab | 105 | * Acquire session lock |
b5541356 | 106 | */ |
54d01ffb | 107 | void session_lock(struct ltt_session *session) |
b5541356 | 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 DG |
116 | { |
117 | pthread_mutex_unlock(&session->lock); | |
b5541356 DG |
118 | } |
119 | ||
5b74c7b1 | 120 | /* |
74babd95 DG |
121 | * Return a ltt_session structure ptr that matches name. If no session found, |
122 | * NULL is returned. This must be called with the session lock held using | |
123 | * session_lock_list and session_unlock_list. | |
5b74c7b1 | 124 | */ |
54d01ffb | 125 | struct ltt_session *session_find_by_name(char *name) |
5b74c7b1 | 126 | { |
5b74c7b1 DG |
127 | struct ltt_session *iter; |
128 | ||
5f822d0a DG |
129 | DBG2("Trying to find session by name %s", name); |
130 | ||
5b74c7b1 | 131 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 132 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
74babd95 | 133 | goto found; |
5b74c7b1 DG |
134 | } |
135 | } | |
136 | ||
74babd95 | 137 | iter = NULL; |
5b74c7b1 | 138 | |
74babd95 | 139 | found: |
5b74c7b1 DG |
140 | return iter; |
141 | } | |
142 | ||
143 | /* | |
050349bb | 144 | * Delete session from the session list and free the memory. |
5b74c7b1 | 145 | * |
050349bb | 146 | * Return -1 if no session is found. On success, return 1; |
5b74c7b1 | 147 | */ |
271933a4 | 148 | int session_destroy(struct ltt_session *session) |
5b74c7b1 | 149 | { |
271933a4 DG |
150 | /* Safety check */ |
151 | if (session == NULL) { | |
152 | ERR("Session pointer was null on session destroy"); | |
153 | return LTTCOMM_OK; | |
5b74c7b1 | 154 | } |
271933a4 DG |
155 | |
156 | DBG("Destroying session %s", session->name); | |
157 | del_session_list(session); | |
271933a4 DG |
158 | pthread_mutex_destroy(&session->lock); |
159 | free(session); | |
5b74c7b1 | 160 | |
54d01ffb | 161 | return LTTCOMM_OK; |
5b74c7b1 DG |
162 | } |
163 | ||
164 | /* | |
050349bb | 165 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 166 | */ |
6df2e2c9 | 167 | int session_create(char *name, char *path, uid_t uid, gid_t gid) |
5b74c7b1 | 168 | { |
f3ed775e | 169 | int ret; |
5b74c7b1 | 170 | struct ltt_session *new_session; |
e07ae692 | 171 | |
54d01ffb | 172 | new_session = session_find_by_name(name); |
5b74c7b1 | 173 | if (new_session != NULL) { |
54d01ffb | 174 | ret = LTTCOMM_EXIST_SESS; |
f3ed775e | 175 | goto error_exist; |
5b74c7b1 DG |
176 | } |
177 | ||
178 | /* Allocate session data structure */ | |
ba7f0ae5 | 179 | new_session = zmalloc(sizeof(struct ltt_session)); |
5b74c7b1 | 180 | if (new_session == NULL) { |
ba7f0ae5 | 181 | perror("zmalloc"); |
54d01ffb | 182 | ret = LTTCOMM_FATAL; |
f3ed775e | 183 | goto error_malloc; |
5b74c7b1 DG |
184 | } |
185 | ||
f3ed775e | 186 | /* Define session name */ |
5b74c7b1 | 187 | if (name != NULL) { |
74babd95 | 188 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
54d01ffb | 189 | ret = LTTCOMM_FATAL; |
f3ed775e | 190 | goto error_asprintf; |
5b74c7b1 DG |
191 | } |
192 | } else { | |
f3ed775e | 193 | ERR("No session name given"); |
54d01ffb | 194 | ret = LTTCOMM_FATAL; |
f3ed775e DG |
195 | goto error; |
196 | } | |
197 | ||
198 | /* Define session system path */ | |
199 | if (path != NULL) { | |
74babd95 | 200 | if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) { |
54d01ffb | 201 | ret = LTTCOMM_FATAL; |
f3ed775e | 202 | goto error_asprintf; |
5b74c7b1 | 203 | } |
f3ed775e DG |
204 | } else { |
205 | ERR("No session path given"); | |
54d01ffb | 206 | ret = LTTCOMM_FATAL; |
f3ed775e | 207 | goto error; |
5b74c7b1 DG |
208 | } |
209 | ||
1d4b027a DG |
210 | /* Init kernel session */ |
211 | new_session->kernel_session = NULL; | |
f6a9efaa | 212 | new_session->ust_session = NULL; |
1657e9bb | 213 | |
0177d773 DG |
214 | /* Init lock */ |
215 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 216 | |
6df2e2c9 MD |
217 | new_session->uid = uid; |
218 | new_session->gid = gid; | |
219 | ||
a304c14c MD |
220 | ret = mkdir_recursive_run_as(new_session->path, S_IRWXU | S_IRWXG, |
221 | new_session->uid, new_session->gid); | |
222 | if (ret < 0) { | |
223 | if (ret != -EEXIST) { | |
224 | ERR("Trace directory creation error"); | |
225 | ret = LTTCOMM_CREATE_FAIL; | |
226 | goto error; | |
227 | } | |
228 | } | |
229 | ||
b5541356 | 230 | /* Add new session to the session list */ |
54d01ffb | 231 | session_lock_list(); |
a991f516 | 232 | new_session->id = add_session_list(new_session); |
54d01ffb | 233 | session_unlock_list(); |
b5541356 | 234 | |
6df2e2c9 MD |
235 | DBG("Tracing session %s created in %s with ID %d by UID %d GID %d", |
236 | name, path, new_session->id, | |
237 | new_session->uid, new_session->gid); | |
b082db07 | 238 | |
54d01ffb | 239 | return LTTCOMM_OK; |
5b74c7b1 DG |
240 | |
241 | error: | |
f3ed775e DG |
242 | error_asprintf: |
243 | if (new_session != NULL) { | |
244 | free(new_session); | |
245 | } | |
5b74c7b1 | 246 | |
f3ed775e DG |
247 | error_exist: |
248 | error_malloc: | |
249 | return ret; | |
5b74c7b1 | 250 | } |