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> | |
5b74c7b1 | 24 | |
54d01ffb | 25 | #include <lttng-sessiond-comm.h> |
1e307fab DG |
26 | #include <lttngerr.h> |
27 | ||
5b74c7b1 DG |
28 | #include "session.h" |
29 | ||
8c0faa1d | 30 | /* |
b5541356 | 31 | * NOTES: |
8c0faa1d | 32 | * |
b5541356 DG |
33 | * No ltt_session.lock is taken here because those data structure are widely |
34 | * spread across the lttng-tools code base so before caling functions below | |
35 | * that can read/write a session, the caller MUST acquire the session lock | |
54d01ffb | 36 | * using session_lock() and session_unlock(). |
8c0faa1d | 37 | */ |
8c0faa1d | 38 | |
5b74c7b1 | 39 | /* |
b5541356 | 40 | * Init tracing session list. |
5b74c7b1 | 41 | * |
b5541356 | 42 | * Please see session.h for more explanation and correct usage of the list. |
5b74c7b1 | 43 | */ |
b5541356 DG |
44 | static struct ltt_session_list ltt_session_list = { |
45 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
46 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
47 | .count = 0, | |
48 | }; | |
5b74c7b1 DG |
49 | |
50 | /* | |
050349bb | 51 | * Add a ltt_session structure to the global list. |
5b74c7b1 | 52 | * |
050349bb | 53 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
54 | */ |
55 | static void add_session_list(struct ltt_session *ls) | |
56 | { | |
57 | cds_list_add(&ls->list, <t_session_list.head); | |
b5541356 | 58 | ltt_session_list.count++; |
5b74c7b1 DG |
59 | } |
60 | ||
61 | /* | |
050349bb | 62 | * Delete a ltt_session structure to the global list. |
b5541356 | 63 | * |
050349bb | 64 | * The caller MUST acquire the session list lock before. |
5b74c7b1 DG |
65 | */ |
66 | static void del_session_list(struct ltt_session *ls) | |
67 | { | |
68 | cds_list_del(&ls->list); | |
69 | /* Sanity check */ | |
b5541356 DG |
70 | if (ltt_session_list.count > 0) { |
71 | ltt_session_list.count--; | |
5b74c7b1 DG |
72 | } |
73 | } | |
74 | ||
b5541356 | 75 | /* |
050349bb | 76 | * Return a pointer to the session list. |
b5541356 | 77 | */ |
54d01ffb | 78 | struct ltt_session_list *session_get_list(void) |
b5541356 DG |
79 | { |
80 | return <t_session_list; | |
81 | } | |
82 | ||
83 | /* | |
6c9cc2ab | 84 | * Acquire session list lock |
b5541356 | 85 | */ |
54d01ffb | 86 | void session_lock_list(void) |
b5541356 | 87 | { |
6c9cc2ab | 88 | pthread_mutex_lock(<t_session_list.lock); |
b5541356 DG |
89 | } |
90 | ||
91 | /* | |
6c9cc2ab | 92 | * Release session list lock |
b5541356 | 93 | */ |
54d01ffb | 94 | void session_unlock_list(void) |
b5541356 | 95 | { |
6c9cc2ab | 96 | pthread_mutex_unlock(<t_session_list.lock); |
b5541356 DG |
97 | } |
98 | ||
99 | /* | |
6c9cc2ab | 100 | * Acquire session lock |
b5541356 | 101 | */ |
54d01ffb | 102 | void session_lock(struct ltt_session *session) |
b5541356 | 103 | { |
6c9cc2ab DG |
104 | pthread_mutex_lock(&session->lock); |
105 | } | |
b5541356 | 106 | |
6c9cc2ab DG |
107 | /* |
108 | * Release session lock | |
109 | */ | |
54d01ffb | 110 | void session_unlock(struct ltt_session *session) |
6c9cc2ab DG |
111 | { |
112 | pthread_mutex_unlock(&session->lock); | |
b5541356 DG |
113 | } |
114 | ||
5b74c7b1 | 115 | /* |
050349bb DG |
116 | * Return a ltt_session structure ptr that matches name. |
117 | * If no session found, NULL is returned. | |
5b74c7b1 | 118 | */ |
54d01ffb | 119 | struct ltt_session *session_find_by_name(char *name) |
5b74c7b1 DG |
120 | { |
121 | int found = 0; | |
122 | struct ltt_session *iter; | |
123 | ||
54d01ffb | 124 | session_lock_list(); |
5b74c7b1 | 125 | cds_list_for_each_entry(iter, <t_session_list.head, list) { |
1b110e1b | 126 | if (strncmp(iter->name, name, NAME_MAX) == 0) { |
5b74c7b1 DG |
127 | found = 1; |
128 | break; | |
129 | } | |
130 | } | |
54d01ffb | 131 | session_unlock_list(); |
5b74c7b1 DG |
132 | |
133 | if (!found) { | |
134 | iter = NULL; | |
135 | } | |
136 | ||
137 | return iter; | |
138 | } | |
139 | ||
140 | /* | |
050349bb | 141 | * Delete session from the session list and free the memory. |
5b74c7b1 | 142 | * |
050349bb | 143 | * Return -1 if no session is found. On success, return 1; |
5b74c7b1 | 144 | */ |
54d01ffb | 145 | int session_destroy(char *name) |
5b74c7b1 | 146 | { |
af9737e9 | 147 | struct ltt_session *iter, *tmp; |
5b74c7b1 | 148 | |
54d01ffb | 149 | session_lock_list(); |
af9737e9 | 150 | cds_list_for_each_entry_safe(iter, tmp, <t_session_list.head, list) { |
f3ed775e | 151 | if (strcmp(iter->name, name) == 0) { |
e07ae692 | 152 | DBG("Destroying session %s", iter->name); |
5b74c7b1 | 153 | del_session_list(iter); |
b5541356 DG |
154 | free(iter->name); |
155 | free(iter->path); | |
156 | pthread_mutex_destroy(&iter->lock); | |
5b74c7b1 | 157 | free(iter); |
5b74c7b1 DG |
158 | break; |
159 | } | |
160 | } | |
54d01ffb | 161 | session_unlock_list(); |
5b74c7b1 | 162 | |
54d01ffb | 163 | return LTTCOMM_OK; |
5b74c7b1 DG |
164 | } |
165 | ||
166 | /* | |
050349bb | 167 | * Create a brand new session and add it to the session list. |
5b74c7b1 | 168 | */ |
54d01ffb | 169 | int session_create(char *name, char *path) |
5b74c7b1 | 170 | { |
f3ed775e | 171 | int ret; |
5b74c7b1 | 172 | struct ltt_session *new_session; |
e07ae692 | 173 | |
54d01ffb | 174 | new_session = session_find_by_name(name); |
5b74c7b1 | 175 | if (new_session != NULL) { |
54d01ffb | 176 | ret = LTTCOMM_EXIST_SESS; |
f3ed775e | 177 | goto error_exist; |
5b74c7b1 DG |
178 | } |
179 | ||
180 | /* Allocate session data structure */ | |
181 | new_session = malloc(sizeof(struct ltt_session)); | |
182 | if (new_session == NULL) { | |
183 | perror("malloc"); | |
54d01ffb | 184 | ret = LTTCOMM_FATAL; |
f3ed775e | 185 | goto error_malloc; |
5b74c7b1 DG |
186 | } |
187 | ||
f3ed775e | 188 | /* Define session name */ |
5b74c7b1 DG |
189 | if (name != NULL) { |
190 | if (asprintf(&new_session->name, "%s", name) < 0) { | |
54d01ffb | 191 | ret = LTTCOMM_FATAL; |
f3ed775e | 192 | goto error_asprintf; |
5b74c7b1 DG |
193 | } |
194 | } else { | |
f3ed775e | 195 | ERR("No session name given"); |
54d01ffb | 196 | ret = LTTCOMM_FATAL; |
f3ed775e DG |
197 | goto error; |
198 | } | |
199 | ||
200 | /* Define session system path */ | |
201 | if (path != NULL) { | |
d6175221 | 202 | if (asprintf(&new_session->path, "%s", path) < 0) { |
54d01ffb | 203 | ret = LTTCOMM_FATAL; |
f3ed775e | 204 | goto error_asprintf; |
5b74c7b1 | 205 | } |
f3ed775e DG |
206 | } else { |
207 | ERR("No session path given"); | |
54d01ffb | 208 | ret = LTTCOMM_FATAL; |
f3ed775e | 209 | goto error; |
5b74c7b1 DG |
210 | } |
211 | ||
1d4b027a DG |
212 | /* Init kernel session */ |
213 | new_session->kernel_session = NULL; | |
5b74c7b1 | 214 | |
0177d773 DG |
215 | /* Init UST session list */ |
216 | CDS_INIT_LIST_HEAD(&new_session->ust_session_list.head); | |
1657e9bb | 217 | |
0177d773 DG |
218 | /* Init lock */ |
219 | pthread_mutex_init(&new_session->lock, NULL); | |
5b74c7b1 | 220 | |
b5541356 | 221 | /* Add new session to the session list */ |
54d01ffb | 222 | session_lock_list(); |
5b74c7b1 | 223 | add_session_list(new_session); |
54d01ffb | 224 | session_unlock_list(); |
b5541356 | 225 | |
0177d773 | 226 | DBG("Tracing session %s created in %s", name, path); |
b082db07 | 227 | |
54d01ffb | 228 | return LTTCOMM_OK; |
5b74c7b1 DG |
229 | |
230 | error: | |
f3ed775e DG |
231 | error_asprintf: |
232 | if (new_session != NULL) { | |
233 | free(new_session); | |
234 | } | |
5b74c7b1 | 235 | |
f3ed775e DG |
236 | error_exist: |
237 | error_malloc: | |
238 | return ret; | |
5b74c7b1 | 239 | } |