Fix: bad file descriptor on close in health check
[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,
a24f7994 48 .next_uuid = 0,
b5541356 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);
a24f7994 60 return ltt_session_list.next_uuid++;
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);
5b74c7b1
DG
71}
72
b5541356 73/*
050349bb 74 * Return a pointer to the session list.
b5541356 75 */
54d01ffb 76struct ltt_session_list *session_get_list(void)
b5541356
DG
77{
78 return &ltt_session_list;
79}
80
81/*
6c9cc2ab 82 * Acquire session list lock
b5541356 83 */
54d01ffb 84void session_lock_list(void)
b5541356 85{
6c9cc2ab 86 pthread_mutex_lock(&ltt_session_list.lock);
b5541356
DG
87}
88
89/*
6c9cc2ab 90 * Release session list lock
b5541356 91 */
54d01ffb 92void session_unlock_list(void)
b5541356 93{
6c9cc2ab 94 pthread_mutex_unlock(&ltt_session_list.lock);
b5541356
DG
95}
96
97/*
6c9cc2ab 98 * Acquire session lock
b5541356 99 */
54d01ffb 100void session_lock(struct ltt_session *session)
b5541356 101{
6c9cc2ab
DG
102 pthread_mutex_lock(&session->lock);
103}
b5541356 104
6c9cc2ab
DG
105/*
106 * Release session lock
107 */
54d01ffb 108void session_unlock(struct ltt_session *session)
6c9cc2ab
DG
109{
110 pthread_mutex_unlock(&session->lock);
b5541356
DG
111}
112
5b74c7b1 113/*
74babd95
DG
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.
5b74c7b1 117 */
54d01ffb 118struct ltt_session *session_find_by_name(char *name)
5b74c7b1 119{
5b74c7b1
DG
120 struct ltt_session *iter;
121
5f822d0a
DG
122 DBG2("Trying to find session by name %s", name);
123
5b74c7b1 124 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
1b110e1b 125 if (strncmp(iter->name, name, NAME_MAX) == 0) {
74babd95 126 goto found;
5b74c7b1
DG
127 }
128 }
129
74babd95 130 iter = NULL;
5b74c7b1 131
74babd95 132found:
5b74c7b1
DG
133 return iter;
134}
135
136/*
050349bb 137 * Delete session from the session list and free the memory.
5b74c7b1 138 *
050349bb 139 * Return -1 if no session is found. On success, return 1;
5b74c7b1 140 */
271933a4 141int session_destroy(struct ltt_session *session)
5b74c7b1 142{
271933a4
DG
143 /* Safety check */
144 if (session == NULL) {
145 ERR("Session pointer was null on session destroy");
146 return LTTCOMM_OK;
5b74c7b1 147 }
271933a4
DG
148
149 DBG("Destroying session %s", session->name);
150 del_session_list(session);
271933a4
DG
151 pthread_mutex_destroy(&session->lock);
152 free(session);
5b74c7b1 153
54d01ffb 154 return LTTCOMM_OK;
5b74c7b1
DG
155}
156
157/*
050349bb 158 * Create a brand new session and add it to the session list.
5b74c7b1 159 */
6df2e2c9 160int session_create(char *name, char *path, uid_t uid, gid_t gid)
5b74c7b1 161{
f3ed775e 162 int ret;
5b74c7b1 163 struct ltt_session *new_session;
e07ae692 164
5b74c7b1 165 /* Allocate session data structure */
ba7f0ae5 166 new_session = zmalloc(sizeof(struct ltt_session));
5b74c7b1 167 if (new_session == NULL) {
df0f840b 168 PERROR("zmalloc");
54d01ffb 169 ret = LTTCOMM_FATAL;
f3ed775e 170 goto error_malloc;
5b74c7b1
DG
171 }
172
f3ed775e 173 /* Define session name */
5b74c7b1 174 if (name != NULL) {
74babd95 175 if (snprintf(new_session->name, NAME_MAX, "%s", name) < 0) {
54d01ffb 176 ret = LTTCOMM_FATAL;
f3ed775e 177 goto error_asprintf;
5b74c7b1
DG
178 }
179 } else {
f3ed775e 180 ERR("No session name given");
54d01ffb 181 ret = LTTCOMM_FATAL;
f3ed775e
DG
182 goto error;
183 }
184
185 /* Define session system path */
186 if (path != NULL) {
74babd95 187 if (snprintf(new_session->path, PATH_MAX, "%s", path) < 0) {
54d01ffb 188 ret = LTTCOMM_FATAL;
f3ed775e 189 goto error_asprintf;
5b74c7b1 190 }
f3ed775e
DG
191 } else {
192 ERR("No session path given");
54d01ffb 193 ret = LTTCOMM_FATAL;
f3ed775e 194 goto error;
5b74c7b1
DG
195 }
196
1d4b027a
DG
197 /* Init kernel session */
198 new_session->kernel_session = NULL;
f6a9efaa 199 new_session->ust_session = NULL;
1657e9bb 200
0177d773
DG
201 /* Init lock */
202 pthread_mutex_init(&new_session->lock, NULL);
5b74c7b1 203
6df2e2c9
MD
204 new_session->uid = uid;
205 new_session->gid = gid;
206
00e2e675
DG
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 }
a304c14c
MD
217 }
218 }
219
b5541356 220 /* Add new session to the session list */
54d01ffb 221 session_lock_list();
a991f516 222 new_session->id = add_session_list(new_session);
54d01ffb 223 session_unlock_list();
b5541356 224
fc4705fe 225 DBG("Tracing session %s created in %s with ID %u by UID %d GID %d",
6df2e2c9
MD
226 name, path, new_session->id,
227 new_session->uid, new_session->gid);
b082db07 228
54d01ffb 229 return LTTCOMM_OK;
5b74c7b1
DG
230
231error:
f3ed775e
DG
232error_asprintf:
233 if (new_session != NULL) {
234 free(new_session);
235 }
5b74c7b1 236
f3ed775e
DG
237error_malloc:
238 return ret;
5b74c7b1 239}
This page took 0.040989 seconds and 4 git commands to generate.