2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
28 #include <common/common.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
36 * No ltt_session.lock is taken here because those data structure are widely
37 * spread across the lttng-tools code base so before caling functions below
38 * that can read/write a session, the caller MUST acquire the session lock
39 * using session_lock() and session_unlock().
43 * Init tracing session list.
45 * Please see session.h for more explanation and correct usage of the list.
47 static struct ltt_session_list ltt_session_list
= {
48 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
49 .lock
= PTHREAD_MUTEX_INITIALIZER
,
54 * Add a ltt_session structure to the global list.
56 * The caller MUST acquire the session list lock before.
57 * Returns the unique identifier for the session.
59 static int add_session_list(struct ltt_session
*ls
)
61 cds_list_add(&ls
->list
, <t_session_list
.head
);
62 return ++ltt_session_list
.count
;
66 * Delete a ltt_session structure to the global list.
68 * The caller MUST acquire the session list lock before.
70 static void del_session_list(struct ltt_session
*ls
)
72 cds_list_del(&ls
->list
);
74 if (ltt_session_list
.count
> 0) {
75 ltt_session_list
.count
--;
80 * Return a pointer to the session list.
82 struct ltt_session_list
*session_get_list(void)
84 return <t_session_list
;
88 * Acquire session list lock
90 void session_lock_list(void)
92 pthread_mutex_lock(<t_session_list
.lock
);
96 * Release session list lock
98 void session_unlock_list(void)
100 pthread_mutex_unlock(<t_session_list
.lock
);
104 * Acquire session lock
106 void session_lock(struct ltt_session
*session
)
108 pthread_mutex_lock(&session
->lock
);
112 * Release session lock
114 void session_unlock(struct ltt_session
*session
)
116 pthread_mutex_unlock(&session
->lock
);
120 * Return a ltt_session structure ptr that matches name. If no session found,
121 * NULL is returned. This must be called with the session lock held using
122 * session_lock_list and session_unlock_list.
124 struct ltt_session
*session_find_by_name(char *name
)
126 struct ltt_session
*iter
;
128 DBG2("Trying to find session by name %s", name
);
130 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
131 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
143 * Delete session from the session list and free the memory.
145 * Return -1 if no session is found. On success, return 1;
147 int session_destroy(struct ltt_session
*session
)
150 if (session
== NULL
) {
151 ERR("Session pointer was null on session destroy");
155 DBG("Destroying session %s", session
->name
);
156 del_session_list(session
);
157 pthread_mutex_destroy(&session
->lock
);
164 * Create a brand new session and add it to the session list.
166 int session_create(char *name
, char *path
, uid_t uid
, gid_t gid
)
169 struct ltt_session
*new_session
;
171 new_session
= session_find_by_name(name
);
172 if (new_session
!= NULL
) {
173 ret
= LTTCOMM_EXIST_SESS
;
177 /* Allocate session data structure */
178 new_session
= zmalloc(sizeof(struct ltt_session
));
179 if (new_session
== NULL
) {
185 /* Define session name */
187 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
192 ERR("No session name given");
197 /* Define session system path */
199 if (snprintf(new_session
->path
, PATH_MAX
, "%s", path
) < 0) {
204 ERR("No session path given");
209 /* Init kernel session */
210 new_session
->kernel_session
= NULL
;
211 new_session
->ust_session
= NULL
;
214 pthread_mutex_init(&new_session
->lock
, NULL
);
216 new_session
->uid
= uid
;
217 new_session
->gid
= gid
;
219 ret
= run_as_mkdir_recursive(new_session
->path
, S_IRWXU
| S_IRWXG
,
220 new_session
->uid
, new_session
->gid
);
222 if (ret
!= -EEXIST
) {
223 ERR("Trace directory creation error");
224 ret
= LTTCOMM_CREATE_FAIL
;
229 /* Add new session to the session list */
231 new_session
->id
= add_session_list(new_session
);
232 session_unlock_list();
234 DBG("Tracing session %s created in %s with ID %d by UID %d GID %d",
235 name
, path
, new_session
->id
,
236 new_session
->uid
, new_session
->gid
);
242 if (new_session
!= NULL
) {