| 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 _LGPL_SOURCE |
| 19 | #include <limits.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <urcu.h> |
| 26 | #include <dirent.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include <common/common.h> |
| 30 | #include <common/sessiond-comm/sessiond-comm.h> |
| 31 | |
| 32 | #include "session.h" |
| 33 | |
| 34 | /* |
| 35 | * NOTES: |
| 36 | * |
| 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 |
| 40 | * using session_lock() and session_unlock(). |
| 41 | */ |
| 42 | |
| 43 | /* |
| 44 | * Init tracing session list. |
| 45 | * |
| 46 | * Please see session.h for more explanation and correct usage of the list. |
| 47 | */ |
| 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 | .next_uuid = 0, |
| 52 | }; |
| 53 | |
| 54 | /* These characters are forbidden in a session name. Used by validate_name. */ |
| 55 | static const char *forbidden_name_chars = "/"; |
| 56 | |
| 57 | /* |
| 58 | * Validate the session name for forbidden characters. |
| 59 | * |
| 60 | * Return 0 on success else -1 meaning a forbidden char. has been found. |
| 61 | */ |
| 62 | static int validate_name(const char *name) |
| 63 | { |
| 64 | int ret; |
| 65 | char *tok, *tmp_name; |
| 66 | |
| 67 | assert(name); |
| 68 | |
| 69 | tmp_name = strdup(name); |
| 70 | if (!tmp_name) { |
| 71 | /* ENOMEM here. */ |
| 72 | ret = -1; |
| 73 | goto error; |
| 74 | } |
| 75 | |
| 76 | tok = strpbrk(tmp_name, forbidden_name_chars); |
| 77 | if (tok) { |
| 78 | DBG("Session name %s contains a forbidden character", name); |
| 79 | /* Forbidden character has been found. */ |
| 80 | ret = -1; |
| 81 | goto error; |
| 82 | } |
| 83 | ret = 0; |
| 84 | |
| 85 | error: |
| 86 | free(tmp_name); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Add a ltt_session structure to the global list. |
| 92 | * |
| 93 | * The caller MUST acquire the session list lock before. |
| 94 | * Returns the unique identifier for the session. |
| 95 | */ |
| 96 | static uint64_t add_session_list(struct ltt_session *ls) |
| 97 | { |
| 98 | assert(ls); |
| 99 | |
| 100 | cds_list_add(&ls->list, <t_session_list.head); |
| 101 | return ltt_session_list.next_uuid++; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Delete a ltt_session structure to the global list. |
| 106 | * |
| 107 | * The caller MUST acquire the session list lock before. |
| 108 | */ |
| 109 | static void del_session_list(struct ltt_session *ls) |
| 110 | { |
| 111 | assert(ls); |
| 112 | |
| 113 | cds_list_del(&ls->list); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Return a pointer to the session list. |
| 118 | */ |
| 119 | struct ltt_session_list *session_get_list(void) |
| 120 | { |
| 121 | return <t_session_list; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Acquire session list lock |
| 126 | */ |
| 127 | void session_lock_list(void) |
| 128 | { |
| 129 | pthread_mutex_lock(<t_session_list.lock); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Release session list lock |
| 134 | */ |
| 135 | void session_unlock_list(void) |
| 136 | { |
| 137 | pthread_mutex_unlock(<t_session_list.lock); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Acquire session lock |
| 142 | */ |
| 143 | void session_lock(struct ltt_session *session) |
| 144 | { |
| 145 | assert(session); |
| 146 | |
| 147 | pthread_mutex_lock(&session->lock); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Release session lock |
| 152 | */ |
| 153 | void session_unlock(struct ltt_session *session) |
| 154 | { |
| 155 | assert(session); |
| 156 | |
| 157 | pthread_mutex_unlock(&session->lock); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Return a ltt_session structure ptr that matches name. If no session found, |
| 162 | * NULL is returned. This must be called with the session lock held using |
| 163 | * session_lock_list and session_unlock_list. |
| 164 | */ |
| 165 | struct ltt_session *session_find_by_name(const char *name) |
| 166 | { |
| 167 | struct ltt_session *iter; |
| 168 | |
| 169 | assert(name); |
| 170 | |
| 171 | DBG2("Trying to find session by name %s", name); |
| 172 | |
| 173 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
| 174 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
| 175 | goto found; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | iter = NULL; |
| 180 | |
| 181 | found: |
| 182 | return iter; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Delete session from the session list and free the memory. |
| 187 | * |
| 188 | * Return -1 if no session is found. On success, return 1; |
| 189 | * Should *NOT* be called with RCU read-side lock held. |
| 190 | */ |
| 191 | int session_destroy(struct ltt_session *session) |
| 192 | { |
| 193 | /* Safety check */ |
| 194 | assert(session); |
| 195 | |
| 196 | DBG("Destroying session %s", session->name); |
| 197 | del_session_list(session); |
| 198 | pthread_mutex_destroy(&session->lock); |
| 199 | |
| 200 | consumer_output_put(session->consumer); |
| 201 | snapshot_destroy(&session->snapshot); |
| 202 | free(session); |
| 203 | |
| 204 | return LTTNG_OK; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Create a brand new session and add it to the session list. |
| 209 | */ |
| 210 | int session_create(char *name, uid_t uid, gid_t gid) |
| 211 | { |
| 212 | int ret; |
| 213 | struct ltt_session *new_session; |
| 214 | |
| 215 | /* Allocate session data structure */ |
| 216 | new_session = zmalloc(sizeof(struct ltt_session)); |
| 217 | if (new_session == NULL) { |
| 218 | PERROR("zmalloc"); |
| 219 | ret = LTTNG_ERR_FATAL; |
| 220 | goto error_malloc; |
| 221 | } |
| 222 | |
| 223 | /* Define session name */ |
| 224 | if (name != NULL) { |
| 225 | if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) { |
| 226 | ret = LTTNG_ERR_FATAL; |
| 227 | goto error_asprintf; |
| 228 | } |
| 229 | } else { |
| 230 | ERR("No session name given"); |
| 231 | ret = LTTNG_ERR_FATAL; |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | ret = validate_name(name); |
| 236 | if (ret < 0) { |
| 237 | ret = LTTNG_ERR_SESSION_INVALID_CHAR; |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | ret = gethostname(new_session->hostname, sizeof(new_session->hostname)); |
| 242 | if (ret < 0) { |
| 243 | if (errno == ENAMETOOLONG) { |
| 244 | new_session->hostname[sizeof(new_session->hostname) - 1] = '\0'; |
| 245 | } else { |
| 246 | ret = LTTNG_ERR_FATAL; |
| 247 | goto error; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Init kernel session */ |
| 252 | new_session->kernel_session = NULL; |
| 253 | new_session->ust_session = NULL; |
| 254 | |
| 255 | /* Init lock */ |
| 256 | pthread_mutex_init(&new_session->lock, NULL); |
| 257 | |
| 258 | new_session->uid = uid; |
| 259 | new_session->gid = gid; |
| 260 | |
| 261 | ret = snapshot_init(&new_session->snapshot); |
| 262 | if (ret < 0) { |
| 263 | ret = LTTNG_ERR_NOMEM; |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | /* Add new session to the session list */ |
| 268 | session_lock_list(); |
| 269 | new_session->id = add_session_list(new_session); |
| 270 | session_unlock_list(); |
| 271 | |
| 272 | /* |
| 273 | * Consumer is let to NULL since the create_session_uri command will set it |
| 274 | * up and, if valid, assign it to the session. |
| 275 | */ |
| 276 | |
| 277 | DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d", |
| 278 | name, new_session->id, new_session->uid, new_session->gid); |
| 279 | |
| 280 | return LTTNG_OK; |
| 281 | |
| 282 | error: |
| 283 | error_asprintf: |
| 284 | free(new_session); |
| 285 | |
| 286 | error_malloc: |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Check if the UID or GID match the session. Root user has access to all |
| 292 | * sessions. |
| 293 | */ |
| 294 | int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) |
| 295 | { |
| 296 | assert(session); |
| 297 | |
| 298 | if (uid != session->uid && gid != session->gid && uid != 0) { |
| 299 | return 0; |
| 300 | } else { |
| 301 | return 1; |
| 302 | } |
| 303 | } |