Initial import of the new binary lttng-relayd
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.c
CommitLineData
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 */
35static int send_channel_streams(int sock,
6df2e2c9
MD
36 struct ust_app_channel *uchan,
37 uid_t uid, gid_t gid)
48842b30 38{
8010679a 39 int ret, fd;
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 }
8010679a
MD
64 fd = uchan->obj->shm_fd;
65 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
48842b30 66 if (ret < 0) {
df0f840b 67 PERROR("send consumer channel ancillary data");
48842b30
DG
68 goto error;
69 }
70
d80a6244 71 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
48842b30
DG
72 int fds[2];
73
74 if (!stream->obj->shm_fd) {
5af2f756 75 continue;
48842b30 76 }
48842b30
DG
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;
d41f73b7
MD
81 /*
82 * FIXME Hack alert! we force MMAP for now. Mixup
83 * between EVENT and UST enums elsewhere.
84 */
85 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
48842b30 86 lum.u.stream.mmap_len = stream->obj->memory_map_size;
6df2e2c9
MD
87 lum.u.stream.uid = uid;
88 lum.u.stream.gid = gid;
48842b30
DG
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));
93 if (ret < 0) {
df0f840b 94 PERROR("send consumer stream");
48842b30
DG
95 goto error;
96 }
97
98 fds[0] = stream->obj->shm_fd;
99 fds[1] = stream->obj->wait_fd;
100 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
101 if (ret < 0) {
df0f840b 102 PERROR("send consumer stream ancillary data");
48842b30
DG
103 goto error;
104 }
48842b30 105 }
48842b30
DG
106
107 DBG("consumer channel streams sent");
108
109 return 0;
110
111error:
112 return ret;
113}
114
115/*
116 * Send all stream fds of the UST session to the consumer.
117 */
118int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
119{
120 int ret = 0;
121 int sock = consumer_fd;
bec39940 122 struct lttng_ht_iter iter;
48842b30 123 struct lttcomm_consumer_msg lum;
bec39940 124 struct ust_app_channel *ua_chan;
48842b30
DG
125
126 DBG("Sending metadata stream fd");
127
7753dea8
MD
128 if (consumer_fd < 0) {
129 ERR("Consumer has negative file descriptor");
130 return -EINVAL;
131 }
132
48842b30 133 if (usess->metadata->obj->shm_fd != 0) {
8010679a 134 int fd;
48842b30
DG
135 int fds[2];
136
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;
3ee75e9c 141 lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size;
ee6b7fb2 142 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
48842b30
DG
143 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
144 if (ret < 0) {
df0f840b 145 PERROR("send consumer channel");
48842b30
DG
146 goto error;
147 }
8010679a
MD
148 fd = usess->metadata->obj->shm_fd;
149 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
48842b30 150 if (ret < 0) {
df0f840b 151 PERROR("send consumer metadata channel");
48842b30
DG
152 goto error;
153 }
154
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;
6df2e2c9
MD
162 lum.u.stream.uid = usess->uid;
163 lum.u.stream.gid = usess->gid;
48842b30
DG
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));
168 if (ret < 0) {
df0f840b 169 PERROR("send consumer metadata stream");
48842b30
DG
170 goto error;
171 }
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);
175 if (ret < 0) {
df0f840b 176 PERROR("send consumer stream");
48842b30
DG
177 goto error;
178 }
179 }
180
181 /* Send each channel fd streams of session */
182 rcu_read_lock();
bec39940
DG
183 cds_lfht_for_each_entry(usess->channels->ht, &iter.iter, ua_chan,
184 node.node) {
aeb96892
DG
185 /*
186 * Indicate that the channel was not created on the tracer side so skip
187 * sending unexisting streams.
188 */
189 if (ua_chan->obj == NULL) {
190 continue;
191 }
192
bec39940 193 ret = send_channel_streams(sock, ua_chan, usess->uid, usess->gid);
48842b30 194 if (ret < 0) {
5485f822 195 rcu_read_unlock();
48842b30
DG
196 goto error;
197 }
48842b30
DG
198 }
199 rcu_read_unlock();
200
201 DBG("consumer fds (metadata and channel streams) sent");
202
203 return 0;
204
205error:
206 return ret;
207}
This page took 0.052875 seconds and 4 git commands to generate.