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.
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
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().
41 * Init tracing session list.
43 * Please see session.h for more explanation and correct usage of the list.
45 static struct ltt_session_list ltt_session_list
= {
46 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
47 .lock
= PTHREAD_MUTEX_INITIALIZER
,
52 * Add a ltt_session structure to the global list.
54 * The caller MUST acquire the session list lock before.
55 * Returns the unique identifier for the session.
57 static unsigned int add_session_list(struct ltt_session
*ls
)
59 cds_list_add(&ls
->list
, <t_session_list
.head
);
60 return ltt_session_list
.next_uuid
++;
64 * Delete a ltt_session structure to the global list.
66 * The caller MUST acquire the session list lock before.
68 static void del_session_list(struct ltt_session
*ls
)
70 cds_list_del(&ls
->list
);
74 * Return a pointer to the session list.
76 struct ltt_session_list
*session_get_list(void)
78 return <t_session_list
;
82 * Acquire session list lock
84 void session_lock_list(void)
86 pthread_mutex_lock(<t_session_list
.lock
);
90 * Release session list lock
92 void session_unlock_list(void)
94 pthread_mutex_unlock(<t_session_list
.lock
);
98 * Acquire session lock
100 void session_lock(struct ltt_session
*session
)
102 pthread_mutex_lock(&session
->lock
);
106 * Release session lock
108 void session_unlock(struct ltt_session
*session
)
110 pthread_mutex_unlock(&session
->lock
);
114 * Return a ltt_session structure ptr that matches name. If no session found,
115 * NULL is returned. This must be called with the session lock held using
116 * session_lock_list and session_unlock_list.
118 struct ltt_session
*session_find_by_name(char *name
)
120 struct ltt_session
*iter
;
122 DBG2("Trying to find session by name %s", name
);
124 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
125 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
137 * Delete session from the session list and free the memory.
139 * Return -1 if no session is found. On success, return 1;
141 int session_destroy(struct ltt_session
*session
)
144 if (session
== NULL
) {
145 ERR("Session pointer was null on session destroy");
149 DBG("Destroying session %s", session
->name
);
150 del_session_list(session
);
151 pthread_mutex_destroy(&session
->lock
);
154 consumer_destroy_output(session
->consumer
);
162 * Create a brand new session and add it to the session list.
164 int session_create(char *name
, char *path
, uid_t uid
, gid_t gid
)
167 struct ltt_session
*new_session
;
169 /* Allocate session data structure */
170 new_session
= zmalloc(sizeof(struct ltt_session
));
171 if (new_session
== NULL
) {
173 ret
= LTTNG_ERR_FATAL
;
177 /* Define session name */
179 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
180 ret
= LTTNG_ERR_FATAL
;
184 ERR("No session name given");
185 ret
= LTTNG_ERR_FATAL
;
189 /* Define session system path */
191 if (snprintf(new_session
->path
, PATH_MAX
, "%s", path
) < 0) {
192 ret
= LTTNG_ERR_FATAL
;
195 new_session
->start_consumer
= 1;
197 /* No path indicates that there is no use for a consumer. */
198 new_session
->start_consumer
= 0;
199 new_session
->path
[0] = '\0';
202 /* Init kernel session */
203 new_session
->kernel_session
= NULL
;
204 new_session
->ust_session
= NULL
;
207 pthread_mutex_init(&new_session
->lock
, NULL
);
209 new_session
->uid
= uid
;
210 new_session
->gid
= gid
;
212 /* Mkdir if we have a valid path and length */
213 if (strlen(new_session
->path
) > 0) {
214 ret
= run_as_mkdir_recursive(new_session
->path
, S_IRWXU
| S_IRWXG
,
215 new_session
->uid
, new_session
->gid
);
217 if (ret
!= -EEXIST
) {
218 ERR("Trace directory creation error");
219 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
225 /* Add new session to the session list */
227 new_session
->id
= add_session_list(new_session
);
228 session_unlock_list();
231 * Consumer is let to NULL since the create_session_uri command will set it
232 * up and, if valid, assign it to the session.
235 DBG("Tracing session %s created in %s with ID %u by UID %d GID %d", name
,
236 path
, new_session
->id
, new_session
->uid
, new_session
->gid
);
242 if (new_session
!= NULL
) {
251 * Check if the UID or GID match the session. Root user has access to all
254 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
258 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {