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 <lttng-sessiond-comm.h>
32 #include "../common/hashtable.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
,
56 * Add a ltt_session structure to the global list.
58 * The caller MUST acquire the session list lock before.
59 * Returns the unique identifier for the session.
61 static int add_session_list(struct ltt_session
*ls
)
63 cds_list_add(&ls
->list
, <t_session_list
.head
);
64 return ++ltt_session_list
.count
;
68 * Delete a ltt_session structure to the global list.
70 * The caller MUST acquire the session list lock before.
72 static void del_session_list(struct ltt_session
*ls
)
74 cds_list_del(&ls
->list
);
76 if (ltt_session_list
.count
> 0) {
77 ltt_session_list
.count
--;
82 * Return a pointer to the session list.
84 struct ltt_session_list
*session_get_list(void)
86 return <t_session_list
;
90 * Acquire session list lock
92 void session_lock_list(void)
94 pthread_mutex_lock(<t_session_list
.lock
);
98 * Release session list lock
100 void session_unlock_list(void)
102 pthread_mutex_unlock(<t_session_list
.lock
);
106 * Acquire session lock
108 void session_lock(struct ltt_session
*session
)
110 pthread_mutex_lock(&session
->lock
);
114 * Release session lock
116 void session_unlock(struct ltt_session
*session
)
118 pthread_mutex_unlock(&session
->lock
);
122 * Return a ltt_session structure ptr that matches name. If no session found,
123 * NULL is returned. This must be called with the session lock held using
124 * session_lock_list and session_unlock_list.
126 struct ltt_session
*session_find_by_name(char *name
)
128 struct ltt_session
*iter
;
130 DBG2("Trying to find session by name %s", name
);
132 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
133 if (strncmp(iter
->name
, name
, NAME_MAX
) == 0) {
145 * Delete session from the session list and free the memory.
147 * Return -1 if no session is found. On success, return 1;
149 int session_destroy(struct ltt_session
*session
)
152 if (session
== NULL
) {
153 ERR("Session pointer was null on session destroy");
157 DBG("Destroying session %s", session
->name
);
158 del_session_list(session
);
159 pthread_mutex_destroy(&session
->lock
);
166 * Create a brand new session and add it to the session list.
168 int session_create(char *name
, char *path
, uid_t uid
, gid_t gid
)
171 struct ltt_session
*new_session
;
173 new_session
= session_find_by_name(name
);
174 if (new_session
!= NULL
) {
175 ret
= LTTCOMM_EXIST_SESS
;
179 /* Allocate session data structure */
180 new_session
= zmalloc(sizeof(struct ltt_session
));
181 if (new_session
== NULL
) {
187 /* Define session name */
189 if (snprintf(new_session
->name
, NAME_MAX
, "%s", name
) < 0) {
194 ERR("No session name given");
199 /* Define session system path */
201 if (snprintf(new_session
->path
, PATH_MAX
, "%s", path
) < 0) {
206 ERR("No session path given");
211 /* Init kernel session */
212 new_session
->kernel_session
= NULL
;
213 new_session
->ust_session
= NULL
;
216 pthread_mutex_init(&new_session
->lock
, NULL
);
218 new_session
->uid
= uid
;
219 new_session
->gid
= gid
;
221 ret
= mkdir_recursive_run_as(new_session
->path
, S_IRWXU
| S_IRWXG
,
222 new_session
->uid
, new_session
->gid
);
224 if (ret
!= -EEXIST
) {
225 ERR("Trace directory creation error");
226 ret
= LTTCOMM_CREATE_FAIL
;
231 /* Add new session to the session list */
233 new_session
->id
= add_session_list(new_session
);
234 session_unlock_list();
236 DBG("Tracing session %s created in %s with ID %d by UID %d GID %d",
237 name
, path
, new_session
->id
,
238 new_session
->uid
, new_session
->gid
);
244 if (new_session
!= NULL
) {