Fix RCU-related hangs: incorrect lttng_ht_destroy use
[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 .next_uuid = 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 assert(ls);
60
61 cds_list_add(&ls->list, &ltt_session_list.head);
62 return ltt_session_list.next_uuid++;
63 }
64
65 /*
66 * Delete a ltt_session structure to the global list.
67 *
68 * The caller MUST acquire the session list lock before.
69 */
70 static void del_session_list(struct ltt_session *ls)
71 {
72 assert(ls);
73
74 cds_list_del(&ls->list);
75 }
76
77 /*
78 * Return a pointer to the session list.
79 */
80 struct ltt_session_list *session_get_list(void)
81 {
82 return &ltt_session_list;
83 }
84
85 /*
86 * Acquire session list lock
87 */
88 void session_lock_list(void)
89 {
90 pthread_mutex_lock(&ltt_session_list.lock);
91 }
92
93 /*
94 * Release session list lock
95 */
96 void session_unlock_list(void)
97 {
98 pthread_mutex_unlock(&ltt_session_list.lock);
99 }
100
101 /*
102 * Acquire session lock
103 */
104 void session_lock(struct ltt_session *session)
105 {
106 assert(session);
107
108 pthread_mutex_lock(&session->lock);
109 }
110
111 /*
112 * Release session lock
113 */
114 void session_unlock(struct ltt_session *session)
115 {
116 assert(session);
117
118 pthread_mutex_unlock(&session->lock);
119 }
120
121 /*
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.
125 */
126 struct ltt_session *session_find_by_name(char *name)
127 {
128 struct ltt_session *iter;
129
130 assert(name);
131
132 DBG2("Trying to find session by name %s", name);
133
134 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
135 if (strncmp(iter->name, name, NAME_MAX) == 0) {
136 goto found;
137 }
138 }
139
140 iter = NULL;
141
142 found:
143 return iter;
144 }
145
146 /*
147 * Delete session from the session list and free the memory.
148 *
149 * Return -1 if no session is found. On success, return 1;
150 * Should *NOT* be called with RCU read-side lock held.
151 */
152 int session_destroy(struct ltt_session *session)
153 {
154 /* Safety check */
155 assert(session);
156
157 DBG("Destroying session %s", session->name);
158 del_session_list(session);
159 pthread_mutex_destroy(&session->lock);
160
161 consumer_destroy_output(session->consumer);
162 free(session);
163
164 return LTTNG_OK;
165 }
166
167 /*
168 * Create a brand new session and add it to the session list.
169 */
170 int session_create(char *name, uid_t uid, gid_t gid)
171 {
172 int ret;
173 struct ltt_session *new_session;
174
175 /* Allocate session data structure */
176 new_session = zmalloc(sizeof(struct ltt_session));
177 if (new_session == NULL) {
178 PERROR("zmalloc");
179 ret = LTTNG_ERR_FATAL;
180 goto error_malloc;
181 }
182
183 /* Define session name */
184 if (name != NULL) {
185 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
186 ret = LTTNG_ERR_FATAL;
187 goto error_asprintf;
188 }
189 } else {
190 ERR("No session name given");
191 ret = LTTNG_ERR_FATAL;
192 goto error;
193 }
194
195 /* Init kernel session */
196 new_session->kernel_session = NULL;
197 new_session->ust_session = NULL;
198
199 /* Init lock */
200 pthread_mutex_init(&new_session->lock, NULL);
201
202 new_session->uid = uid;
203 new_session->gid = gid;
204
205 /* Add new session to the session list */
206 session_lock_list();
207 new_session->id = add_session_list(new_session);
208 session_unlock_list();
209
210 /*
211 * Consumer is let to NULL since the create_session_uri command will set it
212 * up and, if valid, assign it to the session.
213 */
214
215 DBG("Tracing session %s created with ID %u by UID %d GID %d", name,
216 new_session->id, new_session->uid, new_session->gid);
217
218 return LTTNG_OK;
219
220 error:
221 error_asprintf:
222 free(new_session);
223
224 error_malloc:
225 return ret;
226 }
227
228 /*
229 * Check if the UID or GID match the session. Root user has access to all
230 * sessions.
231 */
232 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
233 {
234 assert(session);
235
236 if (uid != session->uid && gid != session->gid && uid != 0) {
237 return 0;
238 } else {
239 return 1;
240 }
241 }
This page took 0.033892 seconds and 4 git commands to generate.