Fix: The session list count should provide unique identifiers
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
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 _GNU_SOURCE
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <urcu.h>
25
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28
29 #include "session.h"
30
31 /*
32 * NOTES:
33 *
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
37 * using session_lock() and session_unlock().
38 */
39
40 /*
41 * Init tracing session list.
42 *
43 * Please see session.h for more explanation and correct usage of the list.
44 */
45 static struct ltt_session_list ltt_session_list = {
46 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
47 .lock = PTHREAD_MUTEX_INITIALIZER,
48 .count = 0,
49 };
50
51 /*
52 * Add a ltt_session structure to the global list.
53 *
54 * The caller MUST acquire the session list lock before.
55 * Returns the unique identifier for the session.
56 */
57 static unsigned int add_session_list(struct ltt_session *ls)
58 {
59 cds_list_add(&ls->list, &ltt_session_list.head);
60 return ++ltt_session_list.count;
61 }
62
63 /*
64 * Delete a ltt_session structure to the global list.
65 *
66 * The caller MUST acquire the session list lock before.
67 * The session list count CANNOT be decremented, as it is used as unique
68 * identifier for the session in UST app hash table lookups.
69 */
70 static void del_session_list(struct ltt_session *ls)
71 {
72 cds_list_del(&ls->list);
73 }
74
75 /*
76 * Return a pointer to the session list.
77 */
78 struct ltt_session_list *session_get_list(void)
79 {
80 return &ltt_session_list;
81 }
82
83 /*
84 * Acquire session list lock
85 */
86 void session_lock_list(void)
87 {
88 pthread_mutex_lock(&ltt_session_list.lock);
89 }
90
91 /*
92 * Release session list lock
93 */
94 void session_unlock_list(void)
95 {
96 pthread_mutex_unlock(&ltt_session_list.lock);
97 }
98
99 /*
100 * Acquire session lock
101 */
102 void session_lock(struct ltt_session *session)
103 {
104 pthread_mutex_lock(&session->lock);
105 }
106
107 /*
108 * Release session lock
109 */
110 void session_unlock(struct ltt_session *session)
111 {
112 pthread_mutex_unlock(&session->lock);
113 }
114
115 /*
116 * Return a ltt_session structure ptr that matches name. If no session found,
117 * NULL is returned. This must be called with the session lock held using
118 * session_lock_list and session_unlock_list.
119 */
120 struct ltt_session *session_find_by_name(char *name)
121 {
122 struct ltt_session *iter;
123
124 DBG2("Trying to find session by name %s", name);
125
126 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
127 if (strncmp(iter->name, name, NAME_MAX) == 0) {
128 goto found;
129 }
130 }
131
132 iter = NULL;
133
134 found:
135 return iter;
136 }
137
138 /*
139 * Delete session from the session list and free the memory.
140 *
141 * Return -1 if no session is found. On success, return 1;
142 */
143 int session_destroy(struct ltt_session *session)
144 {
145 /* Safety check */
146 if (session == NULL) {
147 ERR("Session pointer was null on session destroy");
148 return LTTCOMM_OK;
149 }
150
151 DBG("Destroying session %s", session->name);
152 del_session_list(session);
153 pthread_mutex_destroy(&session->lock);
154 free(session);
155
156 return LTTCOMM_OK;
157 }
158
159 /*
160 * Create a brand new session and add it to the session list.
161 */
162 int session_create(char *name, char *path, uid_t uid, gid_t gid)
163 {
164 int ret;
165 struct ltt_session *new_session;
166
167 /* Allocate session data structure */
168 new_session = zmalloc(sizeof(struct ltt_session));
169 if (new_session == NULL) {
170 PERROR("zmalloc");
171 ret = LTTCOMM_FATAL;
172 goto error_malloc;
173 }
174
175 /* Define session name */
176 if (name != NULL) {
177 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
178 ret = LTTCOMM_FATAL;
179 goto error_asprintf;
180 }
181 } else {
182 ERR("No session name given");
183 ret = LTTCOMM_FATAL;
184 goto error;
185 }
186
187 /* Define session system path */
188 if (path != NULL) {
189 if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) {
190 ret = LTTCOMM_FATAL;
191 goto error_asprintf;
192 }
193 } else {
194 ERR("No session path given");
195 ret = LTTCOMM_FATAL;
196 goto error;
197 }
198
199 /* Init kernel session */
200 new_session->kernel_session = NULL;
201 new_session->ust_session = NULL;
202
203 /* Init lock */
204 pthread_mutex_init(&new_session->lock, NULL);
205
206 new_session->uid = uid;
207 new_session->gid = gid;
208
209 /* Mkdir if we have a valid path length */
210 if (strlen(new_session->path) > 0) {
211 ret = run_as_mkdir_recursive(new_session->path, S_IRWXU | S_IRWXG,
212 new_session->uid, new_session->gid);
213 if (ret < 0) {
214 if (ret != -EEXIST) {
215 ERR("Trace directory creation error");
216 ret = LTTCOMM_CREATE_DIR_FAIL;
217 goto error;
218 }
219 }
220 }
221
222 /* Add new session to the session list */
223 session_lock_list();
224 new_session->id = add_session_list(new_session);
225 session_unlock_list();
226
227 DBG("Tracing session %s created in %s with ID %u by UID %d GID %d",
228 name, path, new_session->id,
229 new_session->uid, new_session->gid);
230
231 return LTTCOMM_OK;
232
233 error:
234 error_asprintf:
235 if (new_session != NULL) {
236 free(new_session);
237 }
238
239 error_malloc:
240 return ret;
241 }
This page took 0.035207 seconds and 5 git commands to generate.