2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <lttng-share.h>
27 #include <lttng-sessiond-comm.h>
28 #include <lttng/lttng-consumer.h>
30 #include "hashtable.h"
31 #include "ust-consumer.h"
34 * Send all stream fds of UST channel to the consumer.
36 static int send_channel_streams(int sock
,
37 struct ust_app_channel
*uchan
,
41 struct lttcomm_consumer_msg lum
;
42 struct ltt_ust_stream
*stream
, *tmp
;
44 DBG("Sending streams of channel %s to UST consumer", uchan
->name
);
47 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
50 * We need to keep shm_fd open while we transfer the stream file
51 * descriptors to make sure this key stays unique within the
52 * session daemon. We can free the channel shm_fd without
53 * problem after we finished sending stream fds for that
56 lum
.u
.channel
.channel_key
= uchan
->obj
->shm_fd
;
57 lum
.u
.channel
.max_sb_size
= uchan
->attr
.subbuf_size
;
58 lum
.u
.channel
.mmap_len
= uchan
->obj
->memory_map_size
;
59 DBG("Sending channel %d to consumer", lum
.u
.channel
.channel_key
);
60 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
62 perror("send consumer channel");
65 fd
= uchan
->obj
->shm_fd
;
66 ret
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
68 perror("send consumer channel ancillary data");
72 cds_list_for_each_entry_safe(stream
, tmp
, &uchan
->streams
.head
, list
) {
75 if (!stream
->obj
->shm_fd
) {
78 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
79 lum
.u
.stream
.channel_key
= uchan
->obj
->shm_fd
;
80 lum
.u
.stream
.stream_key
= stream
->obj
->shm_fd
;
81 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
83 * FIXME Hack alert! we force MMAP for now. Mixup
84 * between EVENT and UST enums elsewhere.
86 lum
.u
.stream
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
87 lum
.u
.stream
.mmap_len
= stream
->obj
->memory_map_size
;
88 lum
.u
.stream
.uid
= uid
;
89 lum
.u
.stream
.gid
= gid
;
90 strncpy(lum
.u
.stream
.path_name
, stream
->pathname
, PATH_MAX
- 1);
91 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
92 DBG("Sending stream %d to consumer", lum
.u
.stream
.stream_key
);
93 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
95 perror("send consumer stream");
99 fds
[0] = stream
->obj
->shm_fd
;
100 fds
[1] = stream
->obj
->wait_fd
;
101 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
103 perror("send consumer stream ancillary data");
108 DBG("consumer channel streams sent");
117 * Send all stream fds of the UST session to the consumer.
119 int ust_consumer_send_session(int consumer_fd
, struct ust_app_session
*usess
)
122 int sock
= consumer_fd
;
123 struct cds_lfht_iter iter
;
124 struct cds_lfht_node
*node
;
125 struct lttcomm_consumer_msg lum
;
126 struct ust_app_channel
*uchan
;
128 DBG("Sending metadata stream fd");
130 if (consumer_fd
< 0) {
131 ERR("Consumer has negative file descriptor");
135 if (usess
->metadata
->obj
->shm_fd
!= 0) {
139 /* Send metadata channel fd */
140 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
141 lum
.u
.channel
.channel_key
= usess
->metadata
->obj
->shm_fd
;
142 lum
.u
.channel
.max_sb_size
= usess
->metadata
->attr
.subbuf_size
;
143 lum
.u
.channel
.mmap_len
= usess
->metadata
->obj
->memory_map_size
;
144 DBG("Sending metadata channel %d to consumer", lum
.u
.channel
.channel_key
);
145 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
147 perror("send consumer channel");
150 fd
= usess
->metadata
->obj
->shm_fd
;
151 ret
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
153 perror("send consumer metadata channel");
157 /* Send metadata stream fd */
158 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
159 lum
.u
.stream
.channel_key
= usess
->metadata
->obj
->shm_fd
;
160 lum
.u
.stream
.stream_key
= usess
->metadata
->stream_obj
->shm_fd
;
161 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
162 lum
.u
.stream
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
163 lum
.u
.stream
.mmap_len
= usess
->metadata
->stream_obj
->memory_map_size
;
164 lum
.u
.stream
.uid
= usess
->uid
;
165 lum
.u
.stream
.gid
= usess
->gid
;
166 strncpy(lum
.u
.stream
.path_name
, usess
->metadata
->pathname
, PATH_MAX
- 1);
167 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
168 DBG("Sending metadata stream %d to consumer", lum
.u
.stream
.stream_key
);
169 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
171 perror("send consumer metadata stream");
174 fds
[0] = usess
->metadata
->stream_obj
->shm_fd
;
175 fds
[1] = usess
->metadata
->stream_obj
->wait_fd
;
176 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
178 perror("send consumer stream");
183 /* Send each channel fd streams of session */
185 hashtable_get_first(usess
->channels
, &iter
);
186 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
187 uchan
= caa_container_of(node
, struct ust_app_channel
, node
);
189 ret
= send_channel_streams(sock
, uchan
, usess
->uid
,
195 hashtable_get_next(usess
->channels
, &iter
);
199 DBG("consumer fds (metadata and channel streams) sent");