Create all trace directories and files with client user credentials
[lttng-tools.git] / lttng-sessiond / session.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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
7 * of the License.
8 *
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.
13 *
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.
17 */
18
19#define _GNU_SOURCE
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <urcu.h>
25
26#include <lttng-sessiond-comm.h>
27#include <lttngerr.h>
28
29#include "hashtable.h"
30#include "session.h"
31
32#include "../hashtable/hash.h"
33
34/*
35 * NOTES:
36 *
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().
41 */
42
43/*
44 * Init tracing session list.
45 *
46 * Please see session.h for more explanation and correct usage of the list.
47 */
48static struct ltt_session_list ltt_session_list = {
49 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
50 .lock = PTHREAD_MUTEX_INITIALIZER,
51 .count = 0,
52};
53
54/*
55 * Add a ltt_session structure to the global list.
56 *
57 * The caller MUST acquire the session list lock before.
58 * Returns the unique identifier for the session.
59 */
60static int add_session_list(struct ltt_session *ls)
61{
62 cds_list_add(&ls->list, &ltt_session_list.head);
63 return ++ltt_session_list.count;
64}
65
66/*
67 * Delete a ltt_session structure to the global list.
68 *
69 * The caller MUST acquire the session list lock before.
70 */
71static void del_session_list(struct ltt_session *ls)
72{
73 cds_list_del(&ls->list);
74 /* Sanity check */
75 if (ltt_session_list.count > 0) {
76 ltt_session_list.count--;
77 }
78}
79
80/*
81 * Return a pointer to the session list.
82 */
83struct ltt_session_list *session_get_list(void)
84{
85 return &ltt_session_list;
86}
87
88/*
89 * Acquire session list lock
90 */
91void session_lock_list(void)
92{
93 pthread_mutex_lock(&ltt_session_list.lock);
94}
95
96/*
97 * Release session list lock
98 */
99void session_unlock_list(void)
100{
101 pthread_mutex_unlock(&ltt_session_list.lock);
102}
103
104/*
105 * Acquire session lock
106 */
107void session_lock(struct ltt_session *session)
108{
109 pthread_mutex_lock(&session->lock);
110}
111
112/*
113 * Release session lock
114 */
115void session_unlock(struct ltt_session *session)
116{
117 pthread_mutex_unlock(&session->lock);
118}
119
120/*
121 * Return a ltt_session structure ptr that matches name. If no session found,
122 * NULL is returned. This must be called with the session lock held using
123 * session_lock_list and session_unlock_list.
124 */
125struct ltt_session *session_find_by_name(char *name)
126{
127 struct ltt_session *iter;
128
129 DBG2("Trying to find session by name %s", name);
130
131 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
132 if (strncmp(iter->name, name, NAME_MAX) == 0) {
133 goto found;
134 }
135 }
136
137 iter = NULL;
138
139found:
140 return iter;
141}
142
143/*
144 * Delete session from the session list and free the memory.
145 *
146 * Return -1 if no session is found. On success, return 1;
147 */
148int session_destroy(struct ltt_session *session)
149{
150 /* Safety check */
151 if (session == NULL) {
152 ERR("Session pointer was null on session destroy");
153 return LTTCOMM_OK;
154 }
155
156 DBG("Destroying session %s", session->name);
157 del_session_list(session);
158 pthread_mutex_destroy(&session->lock);
159 free(session);
160
161 return LTTCOMM_OK;
162}
163
164/*
165 * Create a brand new session and add it to the session list.
166 */
167int session_create(char *name, char *path, uid_t uid, gid_t gid)
168{
169 int ret;
170 struct ltt_session *new_session;
171
172 new_session = session_find_by_name(name);
173 if (new_session != NULL) {
174 ret = LTTCOMM_EXIST_SESS;
175 goto error_exist;
176 }
177
178 /* Allocate session data structure */
179 new_session = zmalloc(sizeof(struct ltt_session));
180 if (new_session == NULL) {
181 perror("zmalloc");
182 ret = LTTCOMM_FATAL;
183 goto error_malloc;
184 }
185
186 /* Define session name */
187 if (name != NULL) {
188 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
189 ret = LTTCOMM_FATAL;
190 goto error_asprintf;
191 }
192 } else {
193 ERR("No session name given");
194 ret = LTTCOMM_FATAL;
195 goto error;
196 }
197
198 /* Define session system path */
199 if (path != NULL) {
200 if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) {
201 ret = LTTCOMM_FATAL;
202 goto error_asprintf;
203 }
204 } else {
205 ERR("No session path given");
206 ret = LTTCOMM_FATAL;
207 goto error;
208 }
209
210 /* Init kernel session */
211 new_session->kernel_session = NULL;
212 new_session->ust_session = NULL;
213
214 /* Init lock */
215 pthread_mutex_init(&new_session->lock, NULL);
216
217 new_session->uid = uid;
218 new_session->gid = gid;
219
220 /* Add new session to the session list */
221 session_lock_list();
222 new_session->id = add_session_list(new_session);
223 session_unlock_list();
224
225 DBG("Tracing session %s created in %s with ID %d by UID %d GID %d",
226 name, path, new_session->id,
227 new_session->uid, new_session->gid);
228
229 return LTTCOMM_OK;
230
231error:
232error_asprintf:
233 if (new_session != NULL) {
234 free(new_session);
235 }
236
237error_exist:
238error_malloc:
239 return ret;
240}
This page took 0.023945 seconds and 4 git commands to generate.