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