46558417ecc2c47b295a7613ae35c7da7c9412d0
[lttng-tools.git] / lttng-sessiond / ust-consumer.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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
11 * more details.
12 *
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.
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
25 #include <lttngerr.h>
26 #include <lttng-share.h>
27 #include <lttng-sessiond-comm.h>
28 #include <lttng/lttng-consumer.h>
29
30 #include "hashtable.h"
31 #include "ust-consumer.h"
32
33 /*
34 * Send all stream fds of UST channel to the consumer.
35 */
36 static int send_channel_streams(int sock,
37 struct ust_app_channel *uchan)
38 {
39 int ret, fd;
40 struct lttcomm_consumer_msg lum;
41 struct ltt_ust_stream *stream, *tmp;
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 /*
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.
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) {
61 perror("send consumer channel");
62 goto error;
63 }
64 fd = uchan->obj->shm_fd;
65 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
66 if (ret < 0) {
67 perror("send consumer channel ancillary data");
68 goto error;
69 }
70
71 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
72 int fds[2];
73
74 if (!stream->obj->shm_fd) {
75 continue;
76 }
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;
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;
86 lum.u.stream.mmap_len = stream->obj->memory_map_size;
87 strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
88 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
89 DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
90 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
91 if (ret < 0) {
92 perror("send consumer stream");
93 goto error;
94 }
95
96 fds[0] = stream->obj->shm_fd;
97 fds[1] = stream->obj->wait_fd;
98 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
99 if (ret < 0) {
100 perror("send consumer stream ancillary data");
101 goto error;
102 }
103
104 /*
105 * We release the stream object here, as we have passed
106 * it to the consumer.
107 */
108 /* Ensure we don't let the app know (sock = -1). */
109 ustctl_release_object(-1, stream->obj);
110 cds_list_del(&stream->list);
111 free(stream);
112 }
113 /* Ensure we don't let the app know (sock = -1). */
114 ustctl_release_object(-1, uchan->obj);
115
116 DBG("consumer channel streams sent");
117
118 return 0;
119
120 error:
121 return ret;
122 }
123
124 /*
125 * Send all stream fds of the UST session to the consumer.
126 */
127 int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
128 {
129 int ret = 0;
130 int sock = consumer_fd;
131 struct cds_lfht_iter iter;
132 struct cds_lfht_node *node;
133 struct lttcomm_consumer_msg lum;
134 struct ust_app_channel *uchan;
135
136 DBG("Sending metadata stream fd");
137
138 if (usess->metadata->obj->shm_fd != 0) {
139 int fd;
140 int fds[2];
141
142 /* Send metadata channel fd */
143 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
144 lum.u.channel.channel_key = usess->metadata->obj->shm_fd;
145 lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size;
146 lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size;
147 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
148 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
149 if (ret < 0) {
150 perror("send consumer channel");
151 goto error;
152 }
153 fd = usess->metadata->obj->shm_fd;
154 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
155 if (ret < 0) {
156 perror("send consumer metadata channel");
157 goto error;
158 }
159
160 /* Send metadata stream fd */
161 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
162 lum.u.stream.channel_key = usess->metadata->obj->shm_fd;
163 lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd;
164 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
165 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
166 lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size;
167 strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1);
168 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
169 DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
170 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
171 if (ret < 0) {
172 perror("send consumer metadata stream");
173 goto error;
174 }
175 fds[0] = usess->metadata->stream_obj->shm_fd;
176 fds[1] = usess->metadata->stream_obj->wait_fd;
177 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
178 if (ret < 0) {
179 perror("send consumer stream");
180 goto error;
181 }
182 /* Metadata fds passed to consumer, release them. */
183 /* Ensure we don't let the app know (sock = -1). */
184 ustctl_release_object(-1, usess->metadata->stream_obj);
185 ustctl_release_object(-1, usess->metadata->obj);
186 }
187
188 /* Send each channel fd streams of session */
189 rcu_read_lock();
190 hashtable_get_first(usess->channels, &iter);
191 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
192 uchan = caa_container_of(node, struct ust_app_channel, node);
193
194 ret = send_channel_streams(sock, uchan);
195 if (ret < 0) {
196 rcu_read_unlock();
197 goto error;
198 }
199 hashtable_get_next(usess->channels, &iter);
200 }
201 rcu_read_unlock();
202
203 DBG("consumer fds (metadata and channel streams) sent");
204
205 return 0;
206
207 error:
208 return ret;
209 }
This page took 0.032744 seconds and 3 git commands to generate.