Port: Use LTTNG_NAME_MAX instead of NAME_MAX
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21 #define _LGPL_SOURCE
22 #include <common/common.h>
23 #include <urcu/rculist.h>
24
25 #include "lttng-relayd.h"
26 #include "ctf-trace.h"
27 #include "session.h"
28 #include "viewer-session.h"
29 #include "viewer-stream.h"
30 #include "stream.h"
31
32 struct relay_viewer_session *viewer_session_create(void)
33 {
34 struct relay_viewer_session *vsession;
35
36 vsession = zmalloc(sizeof(*vsession));
37 if (!vsession) {
38 goto end;
39 }
40 CDS_INIT_LIST_HEAD(&vsession->session_list);
41 end:
42 return vsession;
43 }
44
45 /* The existence of session must be guaranteed by the caller. */
46 int viewer_session_attach(struct relay_viewer_session *vsession,
47 struct relay_session *session)
48 {
49 int ret = 0;
50
51 /* Will not fail, as per the ownership guarantee. */
52 if (!session_get(session)) {
53 ret = -1;
54 goto end;
55 }
56 pthread_mutex_lock(&session->lock);
57 if (session->viewer_attached) {
58 ret = -1;
59 } else {
60 session->viewer_attached = true;
61 }
62
63 if (!ret) {
64 pthread_mutex_lock(&vsession->session_list_lock);
65 /* Ownership is transfered to the list. */
66 cds_list_add_rcu(&session->viewer_session_node,
67 &vsession->session_list);
68 pthread_mutex_unlock(&vsession->session_list_lock);
69 } else {
70 /* Put our local ref. */
71 session_put(session);
72 }
73 /* Safe since we know the session exists. */
74 pthread_mutex_unlock(&session->lock);
75 end:
76 return ret;
77 }
78
79 /* The existence of session must be guaranteed by the caller. */
80 static int viewer_session_detach(struct relay_viewer_session *vsession,
81 struct relay_session *session)
82 {
83 int ret = 0;
84
85 pthread_mutex_lock(&session->lock);
86 if (!session->viewer_attached) {
87 ret = -1;
88 } else {
89 session->viewer_attached = false;
90 }
91
92 if (!ret) {
93 pthread_mutex_lock(&vsession->session_list_lock);
94 cds_list_del_rcu(&session->viewer_session_node);
95 pthread_mutex_unlock(&vsession->session_list_lock);
96 /* Release reference held by the list. */
97 session_put(session);
98 }
99 /* Safe since we know the session exists. */
100 pthread_mutex_unlock(&session->lock);
101 return ret;
102 }
103
104 void viewer_session_destroy(struct relay_viewer_session *vsession)
105 {
106 free(vsession);
107 }
108
109 void viewer_session_close(struct relay_viewer_session *vsession)
110 {
111 struct relay_session *session;
112
113 rcu_read_lock();
114 cds_list_for_each_entry_rcu(session,
115 &vsession->session_list, viewer_session_node) {
116 struct lttng_ht_iter iter;
117 struct relay_viewer_stream *vstream;
118
119 /*
120 * TODO: improvement: create more efficient list of
121 * vstream per session.
122 */
123 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter,
124 vstream, stream_n.node) {
125 if (!viewer_stream_get(vstream)) {
126 continue;
127 }
128 if (vstream->stream->trace->session != session) {
129 viewer_stream_put(vstream);
130 continue;
131 }
132 /* Put local reference. */
133 viewer_stream_put(vstream);
134 /*
135 * We have reached one of the viewer stream's lifetime
136 * end condition.
137 */
138 viewer_stream_put(vstream);
139 }
140
141 viewer_session_detach(vsession, session);
142 }
143 rcu_read_unlock();
144 }
145
146 /*
147 * Check if a connection is attached to a session.
148 * Return 1 if attached, 0 if not attached, a negative value on error.
149 */
150 int viewer_session_is_attached(struct relay_viewer_session *vsession,
151 struct relay_session *session)
152 {
153 struct relay_session *iter;
154 int found = 0;
155
156 pthread_mutex_lock(&session->lock);
157 if (!vsession) {
158 goto end;
159 }
160 if (!session->viewer_attached) {
161 goto end;
162 }
163 rcu_read_lock();
164 cds_list_for_each_entry_rcu(iter,
165 &vsession->session_list,
166 viewer_session_node) {
167 if (session == iter) {
168 found = 1;
169 goto end_rcu_unlock;
170 }
171 }
172 end_rcu_unlock:
173 rcu_read_unlock();
174 end:
175 pthread_mutex_unlock(&session->lock);
176 return found;
177 }
This page took 0.034548 seconds and 5 git commands to generate.