2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
25 #include <common/common.h>
26 #include <common/consumer.h>
27 #include <common/defaults.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
, const char *path
,
37 uid_t uid
, gid_t gid
, struct consumer_output
*consumer
)
40 char tmp_path
[PATH_MAX
];
42 struct lttcomm_consumer_msg lum
;
43 struct ltt_ust_stream
*stream
, *tmp
;
45 DBG("Sending streams of channel %s to UST consumer", uchan
->name
);
47 consumer_init_channel_comm_msg(&lum
,
48 LTTNG_CONSUMER_ADD_CHANNEL
,
50 uchan
->attr
.subbuf_size
,
51 uchan
->obj
->memory_map_size
,
54 ret
= consumer_send_channel(sock
, &lum
);
59 fd
= uchan
->obj
->shm_fd
;
60 ret
= consumer_send_fds(sock
, &fd
, 1);
65 /* Get the right path name destination */
66 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
67 /* Set application path to the destination path */
68 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
69 consumer
->dst
.trace_path
, path
);
71 PERROR("snprintf stream path");
75 DBG3("UST local consumer tracefile path: %s", pathname
);
77 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
78 consumer
->subdir
, path
);
80 PERROR("snprintf stream path");
84 DBG3("UST network consumer subdir path: %s", pathname
);
87 cds_list_for_each_entry_safe(stream
, tmp
, &uchan
->streams
.head
, list
) {
90 if (!stream
->obj
->shm_fd
) {
94 consumer_init_stream_comm_msg(&lum
,
95 LTTNG_CONSUMER_ADD_STREAM
,
98 LTTNG_CONSUMER_ACTIVE_STREAM
,
99 DEFAULT_UST_CHANNEL_OUTPUT
,
100 stream
->obj
->memory_map_size
,
103 consumer
->net_seq_index
,
104 0, /* Metadata flag unset */
108 /* Send stream and file descriptor */
109 fds
[0] = stream
->obj
->shm_fd
;
110 fds
[1] = stream
->obj
->wait_fd
;
111 ret
= consumer_send_stream(sock
, consumer
, &lum
, fds
, 2);
117 DBG("UST consumer channel streams sent");
126 * Send all stream fds of the UST session to the consumer.
128 int ust_consumer_send_session(int consumer_fd
, struct ust_app_session
*usess
,
129 struct consumer_output
*consumer
)
132 int sock
= consumer_fd
;
133 char tmp_path
[PATH_MAX
];
134 const char *pathname
;
135 struct lttng_ht_iter iter
;
136 struct lttcomm_consumer_msg lum
;
137 struct ust_app_channel
*ua_chan
;
139 DBG("Sending metadata stream fd");
141 if (consumer_fd
< 0) {
142 ERR("Consumer has negative file descriptor");
146 if (usess
->metadata
->obj
->shm_fd
!= 0) {
150 consumer_init_channel_comm_msg(&lum
,
151 LTTNG_CONSUMER_ADD_CHANNEL
,
152 usess
->metadata
->obj
->shm_fd
,
153 usess
->metadata
->attr
.subbuf_size
,
154 usess
->metadata
->obj
->memory_map_size
,
157 ret
= consumer_send_channel(sock
, &lum
);
162 fd
= usess
->metadata
->obj
->shm_fd
;
163 ret
= consumer_send_fds(sock
, &fd
, 1);
168 /* Get correct path name destination */
169 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
170 /* Set application path to the destination path */
171 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
172 consumer
->dst
.trace_path
, usess
->path
);
174 PERROR("snprintf stream path");
179 /* Create directory */
180 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
181 usess
->uid
, usess
->gid
);
183 if (ret
!= -EEXIST
) {
184 ERR("Trace directory creation error");
189 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
190 consumer
->subdir
, usess
->path
);
192 PERROR("snprintf metadata path");
198 consumer_init_stream_comm_msg(&lum
,
199 LTTNG_CONSUMER_ADD_STREAM
,
200 usess
->metadata
->obj
->shm_fd
,
201 usess
->metadata
->stream_obj
->shm_fd
,
202 LTTNG_CONSUMER_ACTIVE_STREAM
,
203 DEFAULT_UST_CHANNEL_OUTPUT
,
204 usess
->metadata
->stream_obj
->memory_map_size
,
207 consumer
->net_seq_index
,
208 1, /* Flag metadata set */
212 /* Send stream and file descriptor */
213 fds
[0] = usess
->metadata
->stream_obj
->shm_fd
;
214 fds
[1] = usess
->metadata
->stream_obj
->wait_fd
;
215 ret
= consumer_send_stream(sock
, consumer
, &lum
, fds
, 2);
221 /* Send each channel fd streams of session */
223 cds_lfht_for_each_entry(usess
->channels
->ht
, &iter
.iter
, ua_chan
,
226 * Indicate that the channel was not created on the tracer side so skip
227 * sending unexisting streams.
229 if (ua_chan
->obj
== NULL
) {
233 ret
= send_channel_streams(sock
, ua_chan
, usess
->path
, usess
->uid
,
234 usess
->gid
, consumer
);
242 DBG("consumer fds (metadata and channel streams) sent");
251 * Send relayd socket to consumer associated with a session name.
253 * On success return positive value. On error, negative value.
255 int ust_consumer_send_relayd_socket(int consumer_sock
,
256 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
257 enum lttng_stream_type type
)
260 struct lttcomm_consumer_msg msg
;
262 /* Code flow error. Safety net. */
265 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
266 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
267 msg
.u
.relayd_sock
.type
= type
;
268 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
270 DBG2("Sending relayd sock info to consumer");
271 ret
= lttcomm_send_unix_sock(consumer_sock
, &msg
, sizeof(msg
));
273 PERROR("send consumer relayd socket info");
277 DBG2("Sending relayd socket file descriptor to consumer");
278 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
283 DBG("UST consumer relayd socket sent");