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