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.
25 #include <common/lttngerr.h>
26 #include <common/lttng-share.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/consumer.h>
30 #include "ust-consumer.h"
33 * Send all stream fds of UST channel to the consumer.
35 static int send_channel_streams(int sock
,
36 struct ust_app_channel
*uchan
,
40 struct lttcomm_consumer_msg lum
;
41 struct ltt_ust_stream
*stream
, *tmp
;
43 DBG("Sending streams of channel %s to UST consumer", uchan
->name
);
46 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
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
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
));
61 perror("send consumer channel");
64 fd
= uchan
->obj
->shm_fd
;
65 ret
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
67 perror("send consumer channel ancillary data");
71 cds_list_for_each_entry_safe(stream
, tmp
, &uchan
->streams
.head
, list
) {
74 if (!stream
->obj
->shm_fd
) {
77 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
78 lum
.u
.stream
.channel_key
= uchan
->obj
->shm_fd
;
79 lum
.u
.stream
.stream_key
= stream
->obj
->shm_fd
;
80 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
82 * FIXME Hack alert! we force MMAP for now. Mixup
83 * between EVENT and UST enums elsewhere.
85 lum
.u
.stream
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
86 lum
.u
.stream
.mmap_len
= stream
->obj
->memory_map_size
;
87 lum
.u
.stream
.uid
= uid
;
88 lum
.u
.stream
.gid
= gid
;
89 strncpy(lum
.u
.stream
.path_name
, stream
->pathname
, PATH_MAX
- 1);
90 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
91 DBG("Sending stream %d to consumer", lum
.u
.stream
.stream_key
);
92 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
94 perror("send consumer stream");
98 fds
[0] = stream
->obj
->shm_fd
;
99 fds
[1] = stream
->obj
->wait_fd
;
100 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
102 perror("send consumer stream ancillary data");
107 DBG("consumer channel streams sent");
116 * Send all stream fds of the UST session to the consumer.
118 int ust_consumer_send_session(int consumer_fd
, struct ust_app_session
*usess
)
121 int sock
= consumer_fd
;
122 struct lttng_ht_iter iter
;
123 struct lttcomm_consumer_msg lum
;
124 struct ust_app_channel
*ua_chan
;
126 DBG("Sending metadata stream fd");
128 if (consumer_fd
< 0) {
129 ERR("Consumer has negative file descriptor");
133 if (usess
->metadata
->obj
->shm_fd
!= 0) {
137 /* Send metadata channel fd */
138 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
139 lum
.u
.channel
.channel_key
= usess
->metadata
->obj
->shm_fd
;
140 lum
.u
.channel
.max_sb_size
= usess
->metadata
->attr
.subbuf_size
;
141 lum
.u
.channel
.mmap_len
= usess
->metadata
->obj
->memory_map_size
;
142 DBG("Sending metadata channel %d to consumer", lum
.u
.channel
.channel_key
);
143 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
145 perror("send consumer channel");
148 fd
= usess
->metadata
->obj
->shm_fd
;
149 ret
= lttcomm_send_fds_unix_sock(sock
, &fd
, 1);
151 perror("send consumer metadata channel");
155 /* Send metadata stream fd */
156 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
157 lum
.u
.stream
.channel_key
= usess
->metadata
->obj
->shm_fd
;
158 lum
.u
.stream
.stream_key
= usess
->metadata
->stream_obj
->shm_fd
;
159 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
160 lum
.u
.stream
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
161 lum
.u
.stream
.mmap_len
= usess
->metadata
->stream_obj
->memory_map_size
;
162 lum
.u
.stream
.uid
= usess
->uid
;
163 lum
.u
.stream
.gid
= usess
->gid
;
164 strncpy(lum
.u
.stream
.path_name
, usess
->metadata
->pathname
, PATH_MAX
- 1);
165 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
166 DBG("Sending metadata stream %d to consumer", lum
.u
.stream
.stream_key
);
167 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
169 perror("send consumer metadata stream");
172 fds
[0] = usess
->metadata
->stream_obj
->shm_fd
;
173 fds
[1] = usess
->metadata
->stream_obj
->wait_fd
;
174 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
176 perror("send consumer stream");
181 /* Send each channel fd streams of session */
183 cds_lfht_for_each_entry(usess
->channels
->ht
, &iter
.iter
, ua_chan
,
185 ret
= send_channel_streams(sock
, ua_chan
, usess
->uid
, usess
->gid
);
193 DBG("consumer fds (metadata and channel streams) sent");