2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
28 #include <sys/types.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
38 * No ltt_session.lock is taken here because those data structure are widely
39 * spread across the lttng-tools code base so before caling functions below
40 * that can read/write a session, the caller MUST acquire the session lock
41 * using session_lock() and session_unlock().
45 * Init tracing session list.
47 * Please see session.h for more explanation and correct usage of the list.
49 static struct ltt_session_list ltt_session_list
= {
50 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
51 .lock
= PTHREAD_MUTEX_INITIALIZER
,
55 /* These characters are forbidden in a session name. Used by validate_name. */
56 static const char *forbidden_name_chars
= "/";
59 * Validate the session name for forbidden characters.
61 * Return 0 on success else -1 meaning a forbidden char. has been found.
63 static int validate_name(const char *name
)
70 tmp_name
= strdup(name
);
77 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
79 DBG("Session name %s contains a forbidden character", name
);
80 /* Forbidden character has been found. */
92 * Add a ltt_session structure to the global list.
94 * The caller MUST acquire the session list lock before.
95 * Returns the unique identifier for the session.
97 static uint64_t add_session_list(struct ltt_session
*ls
)
101 cds_list_add(&ls
->list
, <t_session_list
.head
);
102 return ltt_session_list
.next_uuid
++;
106 * Delete a ltt_session structure to the global list.
108 * The caller MUST acquire the session list lock before.
110 static void del_session_list(struct ltt_session
*ls
)
114 cds_list_del(&ls
->list
);
118 * Return a pointer to the session list.
120 struct ltt_session_list
*session_get_list(void)
122 return <t_session_list
;
126 * Acquire session list lock
128 void session_lock_list(void)
130 pthread_mutex_lock(<t_session_list
.lock
);
134 * Release session list lock
136 void session_unlock_list(void)
138 pthread_mutex_unlock(<t_session_list
.lock
);
142 * Acquire session lock
144 void session_lock(struct ltt_session
*session
)
148 pthread_mutex_lock(&session
->lock
);
152 * Release session lock
154 void session_unlock(struct ltt_session
*session
)
158 pthread_mutex_unlock(&session
->lock
);
162 * Return a ltt_session structure ptr that matches name. If no session found,
163 * NULL is returned. This must be called with the session lock held using
164 * session_lock_list and session_unlock_list.
166 struct ltt_session
*session_find_by_name(const char *name
)
168 struct ltt_session
*iter
;
172 DBG2("Trying to find session by name %s", name
);
174 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
175 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
187 * Delete session from the session list and free the memory.
189 * Return -1 if no session is found. On success, return 1;
190 * Should *NOT* be called with RCU read-side lock held.
192 int session_destroy(struct ltt_session
*session
)
197 DBG("Destroying session %s", session
->name
);
198 del_session_list(session
);
199 pthread_mutex_destroy(&session
->lock
);
201 consumer_destroy_output(session
->consumer
);
202 snapshot_destroy(&session
->snapshot
);
209 * Create a brand new session and add it to the session list.
211 int session_create(char *name
, uid_t uid
, gid_t gid
)
214 struct ltt_session
*new_session
;
216 /* Allocate session data structure */
217 new_session
= zmalloc(sizeof(struct ltt_session
));
218 if (new_session
== NULL
) {
220 ret
= LTTNG_ERR_FATAL
;
224 /* Define session name */
226 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
227 ret
= LTTNG_ERR_FATAL
;
231 ERR("No session name given");
232 ret
= LTTNG_ERR_FATAL
;
236 ret
= validate_name(name
);
238 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
242 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
244 if (errno
== ENAMETOOLONG
) {
245 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
247 ret
= LTTNG_ERR_FATAL
;
252 /* Init kernel session */
253 new_session
->kernel_session
= NULL
;
254 new_session
->ust_session
= NULL
;
257 pthread_mutex_init(&new_session
->lock
, NULL
);
259 new_session
->uid
= uid
;
260 new_session
->gid
= gid
;
262 ret
= snapshot_init(&new_session
->snapshot
);
264 ret
= LTTNG_ERR_NOMEM
;
268 /* Add new session to the session list */
270 new_session
->id
= add_session_list(new_session
);
271 session_unlock_list();
274 * Consumer is let to NULL since the create_session_uri command will set it
275 * up and, if valid, assign it to the session.
278 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
279 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
292 * Check if the UID or GID match the session. Root user has access to all
295 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
299 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {