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