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