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.
24 #include <sys/types.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
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().
42 * Init tracing session list.
44 * Please see session.h for more explanation and correct usage of the list.
46 static struct ltt_session_list ltt_session_list
= {
47 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
48 .lock
= PTHREAD_MUTEX_INITIALIZER
,
53 * Add a ltt_session structure to the global list.
55 * The caller MUST acquire the session list lock before.
56 * Returns the unique identifier for the session.
58 static int add_session_list(struct ltt_session
*ls
)
60 cds_list_add(&ls
->list
, <t_session_list
.head
);
61 return ++ltt_session_list
.count
;
65 * Delete a ltt_session structure to the global list.
67 * The caller MUST acquire the session list lock before.
69 static void del_session_list(struct ltt_session
*ls
)
71 cds_list_del(&ls
->list
);
73 if (ltt_session_list
.count
> 0) {
74 ltt_session_list
.count
--;
79 * Return a pointer to the session list.
81 struct ltt_session_list
*session_get_list(void)
83 return <t_session_list
;
87 * Acquire session list lock
89 void session_lock_list(void)
91 pthread_mutex_lock(<t_session_list
.lock
);
95 * Release session list lock
97 void session_unlock_list(void)
99 pthread_mutex_unlock(<t_session_list
.lock
);
103 * Acquire session lock
105 void session_lock(struct ltt_session
*session
)
107 pthread_mutex_lock(&session
->lock
);
111 * Release session lock
113 void session_unlock(struct ltt_session
*session
)
115 pthread_mutex_unlock(&session
->lock
);
119 * Return a ltt_session structure ptr that matches name. If no session found,
120 * NULL is returned. This must be called with the session lock held using
121 * session_lock_list and session_unlock_list.
123 struct ltt_session
*session_find_by_name(char *name
)
125 struct ltt_session
*iter
;
127 DBG2("Trying to find session by name %s", name
);
129 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
130 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
142 * Delete session from the session list and free the memory.
144 * Return -1 if no session is found. On success, return 1;
146 int session_destroy(struct ltt_session
*session
)
149 if (session
== NULL
) {
150 ERR("Session pointer was null on session destroy");
154 DBG("Destroying session %s", session
->name
);
155 del_session_list(session
);
156 pthread_mutex_destroy(&session
->lock
);
163 * Create a brand new session and add it to the session list.
165 int session_create(char *name
, char *path
, uid_t uid
, gid_t gid
)
168 struct ltt_session
*new_session
;
170 new_session
= session_find_by_name(name
);
171 if (new_session
!= NULL
) {
172 ret
= LTTCOMM_EXIST_SESS
;
176 /* Allocate session data structure */
177 new_session
= zmalloc(sizeof(struct ltt_session
));
178 if (new_session
== NULL
) {
184 /* Define session name */
186 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
191 ERR("No session name given");
196 /* Define session system path */
198 if (snprintf(new_session
->path
, PATH_MAX
, "%s", path
) < 0) {
203 ERR("No session path given");
208 /* Init kernel session */
209 new_session
->kernel_session
= NULL
;
210 new_session
->ust_session
= NULL
;
213 pthread_mutex_init(&new_session
->lock
, NULL
);
215 new_session
->uid
= uid
;
216 new_session
->gid
= gid
;
218 ret
= run_as_mkdir_recursive(new_session
->path
, S_IRWXU
| S_IRWXG
,
219 new_session
->uid
, new_session
->gid
);
221 if (ret
!= -EEXIST
) {
222 ERR("Trace directory creation error");
223 ret
= LTTCOMM_CREATE_DIR_FAIL
;
228 /* Add new session to the session list */
230 new_session
->id
= add_session_list(new_session
);
231 session_unlock_list();
233 DBG("Tracing session %s created in %s with ID %d by UID %d GID %d",
234 name
, path
, new_session
->id
,
235 new_session
->uid
, new_session
->gid
);
241 if (new_session
!= NULL
) {