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.
27 #include <sys/types.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
37 * No ltt_session.lock is taken here because those data structure are widely
38 * spread across the lttng-tools code base so before caling functions below
39 * that can read/write a session, the caller MUST acquire the session lock
40 * using session_lock() and session_unlock().
44 * Init tracing session list.
46 * Please see session.h for more explanation and correct usage of the list.
48 static struct ltt_session_list ltt_session_list
= {
49 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
50 .lock
= PTHREAD_MUTEX_INITIALIZER
,
54 /* These characters are forbidden in a session name. Used by validate_name. */
55 static const char *forbidden_name_chars
= "/";
58 * Validate the session name for forbidden characters.
60 * Return 0 on success else -1 meaning a forbidden char. has been found.
62 static int validate_name(const char *name
)
69 tmp_name
= strdup(name
);
76 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
78 DBG("Session name %s contains a forbidden character", name
);
79 /* Forbidden character has been found. */
91 * Add a ltt_session structure to the global list.
93 * The caller MUST acquire the session list lock before.
94 * Returns the unique identifier for the session.
96 static uint64_t add_session_list(struct ltt_session
*ls
)
100 cds_list_add(&ls
->list
, <t_session_list
.head
);
101 return ltt_session_list
.next_uuid
++;
105 * Delete a ltt_session structure to the global list.
107 * The caller MUST acquire the session list lock before.
109 static void del_session_list(struct ltt_session
*ls
)
113 cds_list_del(&ls
->list
);
117 * Return a pointer to the session list.
119 struct ltt_session_list
*session_get_list(void)
121 return <t_session_list
;
125 * Acquire session list lock
127 void session_lock_list(void)
129 pthread_mutex_lock(<t_session_list
.lock
);
133 * Release session list lock
135 void session_unlock_list(void)
137 pthread_mutex_unlock(<t_session_list
.lock
);
141 * Acquire session lock
143 void session_lock(struct ltt_session
*session
)
147 pthread_mutex_lock(&session
->lock
);
151 * Release session lock
153 void session_unlock(struct ltt_session
*session
)
157 pthread_mutex_unlock(&session
->lock
);
161 * Return a ltt_session structure ptr that matches name. If no session found,
162 * NULL is returned. This must be called with the session lock held using
163 * session_lock_list and session_unlock_list.
165 struct ltt_session
*session_find_by_name(const char *name
)
167 struct ltt_session
*iter
;
171 DBG2("Trying to find session by name %s", name
);
173 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
174 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
186 * Delete session from the session list and free the memory.
188 * Return -1 if no session is found. On success, return 1;
189 * Should *NOT* be called with RCU read-side lock held.
191 int session_destroy(struct ltt_session
*session
)
196 DBG("Destroying session %s", session
->name
);
197 del_session_list(session
);
198 pthread_mutex_destroy(&session
->lock
);
200 consumer_output_put(session
->consumer
);
201 snapshot_destroy(&session
->snapshot
);
208 * Create a brand new session and add it to the session list.
210 int session_create(char *name
, uid_t uid
, gid_t gid
)
213 struct ltt_session
*new_session
;
215 /* Allocate session data structure */
216 new_session
= zmalloc(sizeof(struct ltt_session
));
217 if (new_session
== NULL
) {
219 ret
= LTTNG_ERR_FATAL
;
223 /* Define session name */
225 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
226 ret
= LTTNG_ERR_FATAL
;
230 ERR("No session name given");
231 ret
= LTTNG_ERR_FATAL
;
235 ret
= validate_name(name
);
237 ret
= LTTNG_ERR_SESSION_INVALID_CHAR
;
241 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
243 if (errno
== ENAMETOOLONG
) {
244 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
246 ret
= LTTNG_ERR_FATAL
;
251 /* Init kernel session */
252 new_session
->kernel_session
= NULL
;
253 new_session
->ust_session
= NULL
;
256 pthread_mutex_init(&new_session
->lock
, NULL
);
258 new_session
->uid
= uid
;
259 new_session
->gid
= gid
;
261 ret
= snapshot_init(&new_session
->snapshot
);
263 ret
= LTTNG_ERR_NOMEM
;
267 /* Add new session to the session list */
269 new_session
->id
= add_session_list(new_session
);
270 session_unlock_list();
273 * Consumer is let to NULL since the create_session_uri command will set it
274 * up and, if valid, assign it to the session.
277 DBG("Tracing session %s created with ID %" PRIu64
" by UID %d GID %d",
278 name
, new_session
->id
, new_session
->uid
, new_session
->gid
);
291 * Check if the UID or GID match the session. Root user has access to all
294 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
298 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {