a777b041428443fe856b4e7613920a377e7b494f
[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 <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <urcu.h>
26
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29
30 #include "session.h"
31
32 /*
33 * NOTES:
34 *
35 * No ltt_session.lock is taken here because those data structure are widely
36 * spread across the lttng-tools code base so before caling functions below
37 * that can read/write a session, the caller MUST acquire the session lock
38 * using session_lock() and session_unlock().
39 */
40
41 /*
42 * Init tracing session list.
43 *
44 * Please see session.h for more explanation and correct usage of the list.
45 */
46 static struct ltt_session_list ltt_session_list = {
47 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
48 .lock = PTHREAD_MUTEX_INITIALIZER,
49 .next_uuid = 0,
50 };
51
52 /*
53 * Add a ltt_session structure to the global list.
54 *
55 * The caller MUST acquire the session list lock before.
56 * Returns the unique identifier for the session.
57 */
58 static uint64_t add_session_list(struct ltt_session *ls)
59 {
60 assert(ls);
61
62 cds_list_add(&ls->list, &ltt_session_list.head);
63 return ltt_session_list.next_uuid++;
64 }
65
66 /*
67 * Delete a ltt_session structure to the global list.
68 *
69 * The caller MUST acquire the session list lock before.
70 */
71 static void del_session_list(struct ltt_session *ls)
72 {
73 assert(ls);
74
75 cds_list_del(&ls->list);
76 }
77
78 /*
79 * Return a pointer to the session list.
80 */
81 struct ltt_session_list *session_get_list(void)
82 {
83 return &ltt_session_list;
84 }
85
86 /*
87 * Acquire session list lock
88 */
89 void session_lock_list(void)
90 {
91 pthread_mutex_lock(&ltt_session_list.lock);
92 }
93
94 /*
95 * Release session list lock
96 */
97 void session_unlock_list(void)
98 {
99 pthread_mutex_unlock(&ltt_session_list.lock);
100 }
101
102 /*
103 * Acquire session lock
104 */
105 void session_lock(struct ltt_session *session)
106 {
107 assert(session);
108
109 pthread_mutex_lock(&session->lock);
110 }
111
112 /*
113 * Release session lock
114 */
115 void session_unlock(struct ltt_session *session)
116 {
117 assert(session);
118
119 pthread_mutex_unlock(&session->lock);
120 }
121
122 /*
123 * Return a ltt_session structure ptr that matches name. If no session found,
124 * NULL is returned. This must be called with the session lock held using
125 * session_lock_list and session_unlock_list.
126 */
127 struct ltt_session *session_find_by_name(char *name)
128 {
129 struct ltt_session *iter;
130
131 assert(name);
132
133 DBG2("Trying to find session by name %s", name);
134
135 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
136 if (strncmp(iter->name, name, NAME_MAX) == 0) {
137 goto found;
138 }
139 }
140
141 iter = NULL;
142
143 found:
144 return iter;
145 }
146
147 /*
148 * Delete session from the session list and free the memory.
149 *
150 * Return -1 if no session is found. On success, return 1;
151 * Should *NOT* be called with RCU read-side lock held.
152 */
153 int session_destroy(struct ltt_session *session)
154 {
155 /* Safety check */
156 assert(session);
157
158 DBG("Destroying session %s", session->name);
159 del_session_list(session);
160 pthread_mutex_destroy(&session->lock);
161
162 consumer_destroy_output(session->consumer);
163 snapshot_destroy(&session->snapshot);
164 free(session);
165
166 return LTTNG_OK;
167 }
168
169 /*
170 * Create a brand new session and add it to the session list.
171 */
172 int session_create(char *name, uid_t uid, gid_t gid)
173 {
174 int ret;
175 struct ltt_session *new_session;
176
177 /* Allocate session data structure */
178 new_session = zmalloc(sizeof(struct ltt_session));
179 if (new_session == NULL) {
180 PERROR("zmalloc");
181 ret = LTTNG_ERR_FATAL;
182 goto error_malloc;
183 }
184
185 /* Define session name */
186 if (name != NULL) {
187 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
188 ret = LTTNG_ERR_FATAL;
189 goto error_asprintf;
190 }
191 } else {
192 ERR("No session name given");
193 ret = LTTNG_ERR_FATAL;
194 goto error;
195 }
196
197 /* Init kernel session */
198 new_session->kernel_session = NULL;
199 new_session->ust_session = NULL;
200
201 /* Init lock */
202 pthread_mutex_init(&new_session->lock, NULL);
203
204 new_session->uid = uid;
205 new_session->gid = gid;
206
207 ret = snapshot_init(&new_session->snapshot);
208 if (ret < 0) {
209 ret = LTTNG_ERR_NOMEM;
210 goto error;
211 }
212
213 /* Add new session to the session list */
214 session_lock_list();
215 new_session->id = add_session_list(new_session);
216 session_unlock_list();
217
218 /*
219 * Consumer is let to NULL since the create_session_uri command will set it
220 * up and, if valid, assign it to the session.
221 */
222
223 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
224 name, new_session->id, new_session->uid, new_session->gid);
225
226 return LTTNG_OK;
227
228 error:
229 error_asprintf:
230 free(new_session);
231
232 error_malloc:
233 return ret;
234 }
235
236 /*
237 * Check if the UID or GID match the session. Root user has access to all
238 * sessions.
239 */
240 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
241 {
242 assert(session);
243
244 if (uid != session->uid && gid != session->gid && uid != 0) {
245 return 0;
246 } else {
247 return 1;
248 }
249 }
This page took 0.032976 seconds and 3 git commands to generate.