Fix: remove unused code in session daemon
[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 <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.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 .next_uuid = 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 uint64_t add_session_list(struct ltt_session *ls)
59 {
60 assert(ls);
61
62 cds_list_add(&ls->list, &ltt_session_list.head);
63 return ltt_session_list.next_uuid++;
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 */
71 static void del_session_list(struct ltt_session *ls)
72 {
73 assert(ls);
74
75 cds_list_del(&ls->list);
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 assert(session);
108
109 pthread_mutex_lock(&session->lock);
110 }
111
112 /*
113 * Release session lock
114 */
115 void session_unlock(struct ltt_session *session)
116 {
117 assert(session);
118
119 pthread_mutex_unlock(&session->lock);
120 }
121
122 /*
123 * Return a ltt_session structure ptr that matches name. If no session found,
124 * NULL is returned. This must be called with the session lock held using
125 * session_lock_list and session_unlock_list.
126 */
127 struct ltt_session *session_find_by_name(char *name)
128 {
129 struct ltt_session *iter;
130
131 assert(name);
132
133 DBG2("Trying to find session by name %s", name);
134
135 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
136 if (strncmp(iter->name, name, NAME_MAX) == 0) {
137 goto found;
138 }
139 }
140
141 iter = NULL;
142
143 found:
144 return iter;
145 }
146
147 /*
148 * Delete session from the session list and free the memory.
149 *
150 * Return -1 if no session is found. On success, return 1;
151 * Should *NOT* be called with RCU read-side lock held.
152 */
153 int session_destroy(struct ltt_session *session)
154 {
155 /* Safety check */
156 assert(session);
157
158 DBG("Destroying session %s", session->name);
159 del_session_list(session);
160 pthread_mutex_destroy(&session->lock);
161
162 consumer_destroy_output(session->consumer);
163 snapshot_destroy(&session->snapshot);
164 free(session);
165
166 return LTTNG_OK;
167 }
168
169 /*
170 * Create a brand new session and add it to the session list.
171 */
172 int session_create(char *name, uid_t uid, gid_t gid)
173 {
174 int ret;
175 struct ltt_session *new_session;
176
177 /* Allocate session data structure */
178 new_session = zmalloc(sizeof(struct ltt_session));
179 if (new_session == NULL) {
180 PERROR("zmalloc");
181 ret = LTTNG_ERR_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 = LTTNG_ERR_FATAL;
189 goto error_asprintf;
190 }
191 } else {
192 ERR("No session name given");
193 ret = LTTNG_ERR_FATAL;
194 goto error;
195 }
196
197 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
198 if (ret < 0) {
199 if (errno == ENAMETOOLONG) {
200 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
201 } else {
202 ret = LTTNG_ERR_FATAL;
203 goto error;
204 }
205 }
206
207 /* Init kernel session */
208 new_session->kernel_session = NULL;
209 new_session->ust_session = NULL;
210
211 /* Init lock */
212 pthread_mutex_init(&new_session->lock, NULL);
213
214 new_session->uid = uid;
215 new_session->gid = gid;
216
217 ret = snapshot_init(&new_session->snapshot);
218 if (ret < 0) {
219 ret = LTTNG_ERR_NOMEM;
220 goto error;
221 }
222
223 /* Add new session to the session list */
224 session_lock_list();
225 new_session->id = add_session_list(new_session);
226 session_unlock_list();
227
228 /*
229 * Consumer is let to NULL since the create_session_uri command will set it
230 * up and, if valid, assign it to the session.
231 */
232
233 DBG("Tracing session %s created with ID %" PRIu64 " by UID %d GID %d",
234 name, new_session->id, new_session->uid, new_session->gid);
235
236 return LTTNG_OK;
237
238 error:
239 error_asprintf:
240 free(new_session);
241
242 error_malloc:
243 return ret;
244 }
245
246 /*
247 * Check if the UID or GID match the session. Root user has access to all
248 * sessions.
249 */
250 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
251 {
252 assert(session);
253
254 if (uid != session->uid && gid != session->gid && uid != 0) {
255 return 0;
256 } else {
257 return 1;
258 }
259 }
This page took 0.035084 seconds and 4 git commands to generate.