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