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