Commit | Line | Data |
---|---|---|
7591bab1 MD |
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 | ||
7591bab1 MD |
20 | #define _LGPL_SOURCE |
21 | #include <common/common.h> | |
22 | #include <urcu/rculist.h> | |
23 | ||
24 | #include "lttng-relayd.h" | |
25 | #include "ctf-trace.h" | |
26 | #include "session.h" | |
27 | #include "viewer-session.h" | |
28 | #include "viewer-stream.h" | |
29 | #include "stream.h" | |
30 | ||
31 | struct relay_viewer_session *viewer_session_create(void) | |
32 | { | |
33 | struct relay_viewer_session *vsession; | |
34 | ||
35 | vsession = zmalloc(sizeof(*vsession)); | |
36 | if (!vsession) { | |
37 | goto end; | |
38 | } | |
39 | CDS_INIT_LIST_HEAD(&vsession->session_list); | |
40 | end: | |
41 | return vsession; | |
42 | } | |
43 | ||
44 | /* The existence of session must be guaranteed by the caller. */ | |
45 | int viewer_session_attach(struct relay_viewer_session *vsession, | |
46 | struct relay_session *session) | |
47 | { | |
48 | int ret = 0; | |
49 | ||
50 | /* Will not fail, as per the ownership guarantee. */ | |
51 | if (!session_get(session)) { | |
52 | ret = -1; | |
53 | goto end; | |
54 | } | |
55 | pthread_mutex_lock(&session->lock); | |
56 | if (session->viewer_attached) { | |
57 | ret = -1; | |
58 | } else { | |
59 | session->viewer_attached = true; | |
60 | } | |
61 | ||
62 | if (!ret) { | |
63 | pthread_mutex_lock(&vsession->session_list_lock); | |
64 | /* Ownership is transfered to the list. */ | |
65 | cds_list_add_rcu(&session->viewer_session_node, | |
66 | &vsession->session_list); | |
67 | pthread_mutex_unlock(&vsession->session_list_lock); | |
68 | } else { | |
69 | /* Put our local ref. */ | |
70 | session_put(session); | |
71 | } | |
72 | /* Safe since we know the session exists. */ | |
73 | pthread_mutex_unlock(&session->lock); | |
74 | end: | |
75 | return ret; | |
76 | } | |
77 | ||
78 | /* The existence of session must be guaranteed by the caller. */ | |
79 | static int viewer_session_detach(struct relay_viewer_session *vsession, | |
80 | struct relay_session *session) | |
81 | { | |
82 | int ret = 0; | |
83 | ||
84 | pthread_mutex_lock(&session->lock); | |
85 | if (!session->viewer_attached) { | |
86 | ret = -1; | |
87 | } else { | |
88 | session->viewer_attached = false; | |
89 | } | |
90 | ||
91 | if (!ret) { | |
92 | pthread_mutex_lock(&vsession->session_list_lock); | |
93 | cds_list_del_rcu(&session->viewer_session_node); | |
94 | pthread_mutex_unlock(&vsession->session_list_lock); | |
95 | /* Release reference held by the list. */ | |
96 | session_put(session); | |
97 | } | |
98 | /* Safe since we know the session exists. */ | |
99 | pthread_mutex_unlock(&session->lock); | |
100 | return ret; | |
101 | } | |
102 | ||
103 | void viewer_session_destroy(struct relay_viewer_session *vsession) | |
104 | { | |
105 | free(vsession); | |
106 | } | |
107 | ||
108 | void viewer_session_close(struct relay_viewer_session *vsession) | |
109 | { | |
110 | struct relay_session *session; | |
111 | ||
112 | rcu_read_lock(); | |
113 | cds_list_for_each_entry_rcu(session, | |
114 | &vsession->session_list, viewer_session_node) { | |
115 | struct lttng_ht_iter iter; | |
116 | struct relay_viewer_stream *vstream; | |
117 | ||
118 | /* | |
119 | * TODO: improvement: create more efficient list of | |
120 | * vstream per session. | |
121 | */ | |
122 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, | |
123 | vstream, stream_n.node) { | |
124 | if (!viewer_stream_get(vstream)) { | |
125 | continue; | |
126 | } | |
127 | if (vstream->stream->trace->session != session) { | |
128 | viewer_stream_put(vstream); | |
129 | continue; | |
130 | } | |
131 | /* Put local reference. */ | |
132 | viewer_stream_put(vstream); | |
133 | /* | |
134 | * We have reached one of the viewer stream's lifetime | |
135 | * end condition. | |
136 | */ | |
137 | viewer_stream_put(vstream); | |
138 | } | |
139 | ||
140 | viewer_session_detach(vsession, session); | |
141 | } | |
142 | rcu_read_unlock(); | |
143 | } | |
144 | ||
145 | /* | |
146 | * Check if a connection is attached to a session. | |
147 | * Return 1 if attached, 0 if not attached, a negative value on error. | |
148 | */ | |
149 | int viewer_session_is_attached(struct relay_viewer_session *vsession, | |
150 | struct relay_session *session) | |
151 | { | |
152 | struct relay_session *iter; | |
153 | int found = 0; | |
154 | ||
155 | pthread_mutex_lock(&session->lock); | |
156 | if (!vsession) { | |
157 | goto end; | |
158 | } | |
159 | if (!session->viewer_attached) { | |
160 | goto end; | |
161 | } | |
162 | rcu_read_lock(); | |
163 | cds_list_for_each_entry_rcu(iter, | |
164 | &vsession->session_list, | |
165 | viewer_session_node) { | |
166 | if (session == iter) { | |
167 | found = 1; | |
168 | goto end_rcu_unlock; | |
169 | } | |
170 | } | |
171 | end_rcu_unlock: | |
172 | rcu_read_unlock(); | |
173 | end: | |
174 | pthread_mutex_unlock(&session->lock); | |
175 | return found; | |
176 | } |