Commit | Line | Data |
---|---|---|
48842b30 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. | |
48842b30 | 7 | * |
d14d33bf AM |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
48842b30 | 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. | |
48842b30 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | ||
990570ed | 25 | #include <common/common.h> |
db758600 | 26 | #include <common/consumer.h> |
990570ed | 27 | #include <common/defaults.h> |
10a8a223 | 28 | #include <common/sessiond-comm/sessiond-comm.h> |
48842b30 | 29 | |
48842b30 DG |
30 | #include "ust-consumer.h" |
31 | ||
32 | /* | |
33 | * Send all stream fds of UST channel to the consumer. | |
34 | */ | |
35 | static int send_channel_streams(int sock, | |
6df2e2c9 MD |
36 | struct ust_app_channel *uchan, |
37 | uid_t uid, gid_t gid) | |
48842b30 | 38 | { |
6684bfa4 | 39 | int ret; |
48842b30 | 40 | struct lttcomm_consumer_msg lum; |
d80a6244 | 41 | struct ltt_ust_stream *stream, *tmp; |
48842b30 DG |
42 | |
43 | DBG("Sending streams of channel %s to UST consumer", uchan->name); | |
44 | ||
45 | /* Send channel */ | |
46 | lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
47 | ||
48 | /* | |
7ad0a0cb MD |
49 | * We need to keep shm_fd open while we transfer the stream file |
50 | * descriptors to make sure this key stays unique within the | |
51 | * session daemon. We can free the channel shm_fd without | |
52 | * problem after we finished sending stream fds for that | |
53 | * channel. | |
48842b30 DG |
54 | */ |
55 | lum.u.channel.channel_key = uchan->obj->shm_fd; | |
56 | lum.u.channel.max_sb_size = uchan->attr.subbuf_size; | |
57 | lum.u.channel.mmap_len = uchan->obj->memory_map_size; | |
58 | DBG("Sending channel %d to consumer", lum.u.channel.channel_key); | |
59 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); | |
60 | if (ret < 0) { | |
df0f840b | 61 | PERROR("send consumer channel"); |
48842b30 DG |
62 | goto error; |
63 | } | |
6684bfa4 CB |
64 | |
65 | DBG("Sending channel shm path: %s\n", uchan->obj->shm_path); | |
66 | ret = lttcomm_send_string(sock, | |
67 | uchan->obj->shm_path, | |
68 | strlen(uchan->obj->shm_path)); | |
48842b30 | 69 | if (ret < 0) { |
6684bfa4 | 70 | PERROR("send consumer channel shm path"); |
48842b30 DG |
71 | goto error; |
72 | } | |
73 | ||
d80a6244 | 74 | cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) { |
48842b30 DG |
75 | |
76 | if (!stream->obj->shm_fd) { | |
5af2f756 | 77 | continue; |
48842b30 | 78 | } |
48842b30 DG |
79 | lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
80 | lum.u.stream.channel_key = uchan->obj->shm_fd; | |
81 | lum.u.stream.stream_key = stream->obj->shm_fd; | |
82 | lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; | |
d41f73b7 MD |
83 | /* |
84 | * FIXME Hack alert! we force MMAP for now. Mixup | |
85 | * between EVENT and UST enums elsewhere. | |
86 | */ | |
87 | lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT; | |
48842b30 | 88 | lum.u.stream.mmap_len = stream->obj->memory_map_size; |
6df2e2c9 MD |
89 | lum.u.stream.uid = uid; |
90 | lum.u.stream.gid = gid; | |
48842b30 DG |
91 | strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1); |
92 | lum.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
93 | DBG("Sending stream %d to consumer", lum.u.stream.stream_key); | |
94 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); | |
95 | if (ret < 0) { | |
df0f840b | 96 | PERROR("send consumer stream"); |
48842b30 DG |
97 | goto error; |
98 | } | |
99 | ||
6684bfa4 CB |
100 | DBG("Sending stream shm path: %s\n", stream->obj->shm_path); |
101 | ret = lttcomm_send_string(sock, | |
102 | stream->obj->shm_path, | |
103 | strlen(stream->obj->shm_path)); | |
48842b30 | 104 | if (ret < 0) { |
6684bfa4 CB |
105 | PERROR("send consumer stream shm path"); |
106 | goto error; | |
107 | } | |
108 | ||
109 | DBG("Sending stream wait pipe path: %s\n", stream->obj->wait_pipe_path); | |
110 | ret = lttcomm_send_string(sock, | |
111 | stream->obj->wait_pipe_path, | |
112 | strlen(stream->obj->wait_pipe_path)); | |
113 | ||
114 | if (ret < 0) { | |
115 | PERROR("send consumer stream wait pipe path"); | |
48842b30 DG |
116 | goto error; |
117 | } | |
48842b30 | 118 | } |
48842b30 DG |
119 | |
120 | DBG("consumer channel streams sent"); | |
121 | ||
122 | return 0; | |
123 | ||
124 | error: | |
125 | return ret; | |
126 | } | |
127 | ||
128 | /* | |
129 | * Send all stream fds of the UST session to the consumer. | |
130 | */ | |
131 | int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) | |
132 | { | |
133 | int ret = 0; | |
134 | int sock = consumer_fd; | |
bec39940 | 135 | struct lttng_ht_iter iter; |
48842b30 | 136 | struct lttcomm_consumer_msg lum; |
bec39940 | 137 | struct ust_app_channel *ua_chan; |
48842b30 DG |
138 | |
139 | DBG("Sending metadata stream fd"); | |
140 | ||
7753dea8 MD |
141 | if (consumer_fd < 0) { |
142 | ERR("Consumer has negative file descriptor"); | |
143 | return -EINVAL; | |
144 | } | |
145 | ||
48842b30 | 146 | if (usess->metadata->obj->shm_fd != 0) { |
48842b30 DG |
147 | /* Send metadata channel fd */ |
148 | lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
149 | lum.u.channel.channel_key = usess->metadata->obj->shm_fd; | |
150 | lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size; | |
3ee75e9c | 151 | lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size; |
ee6b7fb2 | 152 | DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key); |
48842b30 DG |
153 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); |
154 | if (ret < 0) { | |
df0f840b | 155 | PERROR("send consumer channel"); |
48842b30 DG |
156 | goto error; |
157 | } | |
6684bfa4 CB |
158 | |
159 | DBG("Sending metadata channel shm path: %s\n", usess->metadata->obj->shm_path); | |
160 | ret = lttcomm_send_string(sock, | |
161 | usess->metadata->obj->shm_path, | |
162 | strlen(usess->metadata->obj->shm_path)); | |
48842b30 | 163 | if (ret < 0) { |
df0f840b | 164 | PERROR("send consumer metadata channel"); |
48842b30 DG |
165 | goto error; |
166 | } | |
167 | ||
168 | /* Send metadata stream fd */ | |
169 | lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM; | |
170 | lum.u.stream.channel_key = usess->metadata->obj->shm_fd; | |
171 | lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd; | |
172 | lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; | |
173 | lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT; | |
174 | lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size; | |
6df2e2c9 MD |
175 | lum.u.stream.uid = usess->uid; |
176 | lum.u.stream.gid = usess->gid; | |
48842b30 DG |
177 | strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1); |
178 | lum.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
179 | DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key); | |
180 | ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); | |
181 | if (ret < 0) { | |
df0f840b | 182 | PERROR("send consumer metadata stream"); |
48842b30 DG |
183 | goto error; |
184 | } | |
6684bfa4 CB |
185 | |
186 | DBG("Sending metadata stream shm path: %s\n", | |
187 | usess->metadata->stream_obj->shm_path); | |
188 | ret = lttcomm_send_string(sock, | |
189 | usess->metadata->stream_obj->shm_path, | |
190 | strlen(usess->metadata->stream_obj->shm_path)); | |
191 | ||
48842b30 | 192 | if (ret < 0) { |
6684bfa4 CB |
193 | PERROR("send consumer shm stream"); |
194 | goto error; | |
195 | } | |
196 | ||
197 | DBG("Sending metadata stream wait pipe path: %s\n", | |
198 | usess->metadata->stream_obj->wait_pipe_path); | |
199 | ret = lttcomm_send_string(sock, | |
200 | usess->metadata->stream_obj->wait_pipe_path, | |
201 | strlen(usess->metadata->stream_obj->wait_pipe_path)); | |
202 | ||
203 | if (ret < 0) { | |
204 | PERROR("send consumer shm stream"); | |
48842b30 DG |
205 | goto error; |
206 | } | |
6684bfa4 | 207 | |
48842b30 DG |
208 | } |
209 | ||
210 | /* Send each channel fd streams of session */ | |
211 | rcu_read_lock(); | |
bec39940 DG |
212 | cds_lfht_for_each_entry(usess->channels->ht, &iter.iter, ua_chan, |
213 | node.node) { | |
aeb96892 DG |
214 | /* |
215 | * Indicate that the channel was not created on the tracer side so skip | |
216 | * sending unexisting streams. | |
217 | */ | |
218 | if (ua_chan->obj == NULL) { | |
219 | continue; | |
220 | } | |
221 | ||
bec39940 | 222 | ret = send_channel_streams(sock, ua_chan, usess->uid, usess->gid); |
48842b30 | 223 | if (ret < 0) { |
5485f822 | 224 | rcu_read_unlock(); |
48842b30 DG |
225 | goto error; |
226 | } | |
48842b30 DG |
227 | } |
228 | rcu_read_unlock(); | |
229 | ||
230 | DBG("consumer fds (metadata and channel streams) sent"); | |
231 | ||
232 | return 0; | |
233 | ||
234 | error: | |
235 | return ret; | |
236 | } |