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