Fix DBG message grammatical error
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
CommitLineData
5b74c7b1
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
91d76f53 8 *
5b74c7b1
DG
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
050349bb 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b74c7b1
DG
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
6c9cc2ab 20#include <limits.h>
5b74c7b1
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
a304c14c
MD
24#include <sys/stat.h>
25#include <sys/types.h>
f6a9efaa 26#include <urcu.h>
5b74c7b1 27
10a8a223
DG
28#include <common/sessiond-comm/sessiond-comm.h>
29#include <common/runas.h>
30#include <common/lttngerr.h>
31#include <common/lttng-share.h>
1e307fab 32
5b74c7b1
DG
33#include "session.h"
34
8c0faa1d 35/*
b5541356 36 * NOTES:
8c0faa1d 37 *
b5541356
DG
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
54d01ffb 41 * using session_lock() and session_unlock().
8c0faa1d 42 */
8c0faa1d 43
5b74c7b1 44/*
b5541356 45 * Init tracing session list.
5b74c7b1 46 *
b5541356 47 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 48 */
b5541356
DG
49static 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};
5b74c7b1
DG
54
55/*
050349bb 56 * Add a ltt_session structure to the global list.
5b74c7b1 57 *
050349bb 58 * The caller MUST acquire the session list lock before.
44e96653 59 * Returns the unique identifier for the session.
5b74c7b1 60 */
44e96653 61static int add_session_list(struct ltt_session *ls)
5b74c7b1
DG
62{
63 cds_list_add(&ls->list, &ltt_session_list.head);
44e96653 64 return ++ltt_session_list.count;
5b74c7b1
DG
65}
66
67/*
050349bb 68 * Delete a ltt_session structure to the global list.
b5541356 69 *
050349bb 70 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
71 */
72static void del_session_list(struct ltt_session *ls)
73{
74 cds_list_del(&ls->list);
75 /* Sanity check */
b5541356
DG
76 if (ltt_session_list.count > 0) {
77 ltt_session_list.count--;
5b74c7b1
DG
78 }
79}
80
b5541356 81/*
050349bb 82 * Return a pointer to the session list.
b5541356 83 */
54d01ffb 84struct ltt_session_list *session_get_list(void)
b5541356
DG
85{
86 return &ltt_session_list;
87}
88
89/*
6c9cc2ab 90 * Acquire session list lock
b5541356 91 */
54d01ffb 92void session_lock_list(void)
b5541356 93{
6c9cc2ab 94 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
95}
96
97/*
6c9cc2ab 98 * Release session list lock
b5541356 99 */
54d01ffb 100void session_unlock_list(void)
b5541356 101{
6c9cc2ab 102 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
103}
104
105/*
6c9cc2ab 106 * Acquire session lock
b5541356 107 */
54d01ffb 108void session_lock(struct ltt_session *session)
b5541356 109{
6c9cc2ab
DG
110 pthread_mutex_lock(&session->lock);
111}
b5541356 112
6c9cc2ab
DG
113/*
114 * Release session lock
115 */
54d01ffb 116void session_unlock(struct ltt_session *session)
6c9cc2ab
DG
117{
118 pthread_mutex_unlock(&session->lock);
b5541356
DG
119}
120
5b74c7b1 121/*
74babd95
DG
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.
5b74c7b1 125 */
54d01ffb 126struct ltt_session *session_find_by_name(char *name)
5b74c7b1 127{
5b74c7b1
DG
128 struct ltt_session *iter;
129
5f822d0a
DG
130 DBG2("Trying to find session by name %s", name);
131
5b74c7b1 132 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 133 if (strncmp(iter->name, name, NAME_MAX) == 0) {
74babd95 134 goto found;
5b74c7b1
DG
135 }
136 }
137
74babd95 138 iter = NULL;
5b74c7b1 139
74babd95 140found:
5b74c7b1
DG
141 return iter;
142}
143
144/*
050349bb 145 * Delete session from the session list and free the memory.
5b74c7b1 146 *
050349bb 147 * Return -1 if no session is found. On success, return 1;
5b74c7b1 148 */
271933a4 149int session_destroy(struct ltt_session *session)
5b74c7b1 150{
271933a4
DG
151 /* Safety check */
152 if (session == NULL) {
153 ERR("Session pointer was null on session destroy");
154 return LTTCOMM_OK;
5b74c7b1 155 }
271933a4
DG
156
157 DBG("Destroying session %s", session->name);
158 del_session_list(session);
271933a4
DG
159 pthread_mutex_destroy(&session->lock);
160 free(session);
5b74c7b1 161
54d01ffb 162 return LTTCOMM_OK;
5b74c7b1
DG
163}
164
165/*
050349bb 166 * Create a brand new session and add it to the session list.
5b74c7b1 167 */
6df2e2c9 168int session_create(char *name, char *path, uid_t uid, gid_t gid)
5b74c7b1 169{
f3ed775e 170 int ret;
5b74c7b1 171 struct ltt_session *new_session;
e07ae692 172
54d01ffb 173 new_session = session_find_by_name(name);
5b74c7b1 174 if (new_session != NULL) {
54d01ffb 175 ret = LTTCOMM_EXIST_SESS;
f3ed775e 176 goto error_exist;
5b74c7b1
DG
177 }
178
179 /* Allocate session data structure */
ba7f0ae5 180 new_session = zmalloc(sizeof(struct ltt_session));
5b74c7b1 181 if (new_session == NULL) {
ba7f0ae5 182 perror("zmalloc");
54d01ffb 183 ret = LTTCOMM_FATAL;
f3ed775e 184 goto error_malloc;
5b74c7b1
DG
185 }
186
f3ed775e 187 /* Define session name */
5b74c7b1 188 if (name != NULL) {
74babd95 189 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
54d01ffb 190 ret = LTTCOMM_FATAL;
f3ed775e 191 goto error_asprintf;
5b74c7b1
DG
192 }
193 } else {
f3ed775e 194 ERR("No session name given");
54d01ffb 195 ret = LTTCOMM_FATAL;
f3ed775e
DG
196 goto error;
197 }
198
199 /* Define session system path */
200 if (path != NULL) {
74babd95 201 if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) {
54d01ffb 202 ret = LTTCOMM_FATAL;
f3ed775e 203 goto error_asprintf;
5b74c7b1 204 }
f3ed775e
DG
205 } else {
206 ERR("No session path given");
54d01ffb 207 ret = LTTCOMM_FATAL;
f3ed775e 208 goto error;
5b74c7b1
DG
209 }
210
1d4b027a
DG
211 /* Init kernel session */
212 new_session->kernel_session = NULL;
f6a9efaa 213 new_session->ust_session = NULL;
1657e9bb 214
0177d773
DG
215 /* Init lock */
216 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 217
6df2e2c9
MD
218 new_session->uid = uid;
219 new_session->gid = gid;
220
e11d277b 221 ret = run_as_mkdir_recursive(new_session->path, S_IRWXU | S_IRWXG,
a304c14c
MD
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
b5541356 231 /* Add new session to the session list */
54d01ffb 232 session_lock_list();
a991f516 233 new_session->id = add_session_list(new_session);
54d01ffb 234 session_unlock_list();
b5541356 235
6df2e2c9
MD
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);
b082db07 239
54d01ffb 240 return LTTCOMM_OK;
5b74c7b1
DG
241
242error:
f3ed775e
DG
243error_asprintf:
244 if (new_session != NULL) {
245 free(new_session);
246 }
5b74c7b1 247
f3ed775e
DG
248error_exist:
249error_malloc:
250 return ret;
5b74c7b1 251}
This page took 0.043073 seconds and 5 git commands to generate.