Cygwin: Pass file paths instead of file descriptors over UNIX sockets
[lttng-tools.git] / src / bin / 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
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
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.
12 *
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.
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 <common/common.h>
26 #include <common/consumer.h>
27 #include <common/defaults.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29
30 #include "ust-consumer.h"
31
32 /*
33 * Send all stream fds of UST channel to the consumer.
34 */
35 static int send_channel_streams(int sock,
36 struct ust_app_channel *uchan,
37 uid_t uid, gid_t gid)
38 {
39 int ret;
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
65 DBG("Sending channel shm path: %s\n", uchan->obj->shm_path);
66 ret = lttcomm_send_string(sock,
67 uchan->obj->shm_path,
68 strlen(uchan->obj->shm_path));
69 if (ret < 0) {
70 PERROR("send consumer channel shm path");
71 goto error;
72 }
73
74 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
75
76 if (!stream->obj->shm_fd) {
77 continue;
78 }
79 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
80 lum.u.stream.channel_key = uchan->obj->shm_fd;
81 lum.u.stream.stream_key = stream->obj->shm_fd;
82 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
83 /*
84 * FIXME Hack alert! we force MMAP for now. Mixup
85 * between EVENT and UST enums elsewhere.
86 */
87 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
88 lum.u.stream.mmap_len = stream->obj->memory_map_size;
89 lum.u.stream.uid = uid;
90 lum.u.stream.gid = gid;
91 strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
92 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
93 DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
94 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
95 if (ret < 0) {
96 PERROR("send consumer stream");
97 goto error;
98 }
99
100 DBG("Sending stream shm path: %s\n", stream->obj->shm_path);
101 ret = lttcomm_send_string(sock,
102 stream->obj->shm_path,
103 strlen(stream->obj->shm_path));
104 if (ret < 0) {
105 PERROR("send consumer stream shm path");
106 goto error;
107 }
108
109 DBG("Sending stream wait pipe path: %s\n", stream->obj->wait_pipe_path);
110 ret = lttcomm_send_string(sock,
111 stream->obj->wait_pipe_path,
112 strlen(stream->obj->wait_pipe_path));
113
114 if (ret < 0) {
115 PERROR("send consumer stream wait pipe path");
116 goto error;
117 }
118 }
119
120 DBG("consumer channel streams sent");
121
122 return 0;
123
124 error:
125 return ret;
126 }
127
128 /*
129 * Send all stream fds of the UST session to the consumer.
130 */
131 int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
132 {
133 int ret = 0;
134 int sock = consumer_fd;
135 struct lttng_ht_iter iter;
136 struct lttcomm_consumer_msg lum;
137 struct ust_app_channel *ua_chan;
138
139 DBG("Sending metadata stream fd");
140
141 if (consumer_fd < 0) {
142 ERR("Consumer has negative file descriptor");
143 return -EINVAL;
144 }
145
146 if (usess->metadata->obj->shm_fd != 0) {
147 /* Send metadata channel fd */
148 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
149 lum.u.channel.channel_key = usess->metadata->obj->shm_fd;
150 lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size;
151 lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size;
152 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
153 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
154 if (ret < 0) {
155 PERROR("send consumer channel");
156 goto error;
157 }
158
159 DBG("Sending metadata channel shm path: %s\n", usess->metadata->obj->shm_path);
160 ret = lttcomm_send_string(sock,
161 usess->metadata->obj->shm_path,
162 strlen(usess->metadata->obj->shm_path));
163 if (ret < 0) {
164 PERROR("send consumer metadata channel");
165 goto error;
166 }
167
168 /* Send metadata stream fd */
169 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
170 lum.u.stream.channel_key = usess->metadata->obj->shm_fd;
171 lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd;
172 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
173 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
174 lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size;
175 lum.u.stream.uid = usess->uid;
176 lum.u.stream.gid = usess->gid;
177 strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1);
178 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
179 DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
180 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
181 if (ret < 0) {
182 PERROR("send consumer metadata stream");
183 goto error;
184 }
185
186 DBG("Sending metadata stream shm path: %s\n",
187 usess->metadata->stream_obj->shm_path);
188 ret = lttcomm_send_string(sock,
189 usess->metadata->stream_obj->shm_path,
190 strlen(usess->metadata->stream_obj->shm_path));
191
192 if (ret < 0) {
193 PERROR("send consumer shm stream");
194 goto error;
195 }
196
197 DBG("Sending metadata stream wait pipe path: %s\n",
198 usess->metadata->stream_obj->wait_pipe_path);
199 ret = lttcomm_send_string(sock,
200 usess->metadata->stream_obj->wait_pipe_path,
201 strlen(usess->metadata->stream_obj->wait_pipe_path));
202
203 if (ret < 0) {
204 PERROR("send consumer shm stream");
205 goto error;
206 }
207
208 }
209
210 /* Send each channel fd streams of session */
211 rcu_read_lock();
212 cds_lfht_for_each_entry(usess->channels->ht, &iter.iter, ua_chan,
213 node.node) {
214 /*
215 * Indicate that the channel was not created on the tracer side so skip
216 * sending unexisting streams.
217 */
218 if (ua_chan->obj == NULL) {
219 continue;
220 }
221
222 ret = send_channel_streams(sock, ua_chan, usess->uid, usess->gid);
223 if (ret < 0) {
224 rcu_read_unlock();
225 goto error;
226 }
227 }
228 rcu_read_unlock();
229
230 DBG("consumer fds (metadata and channel streams) sent");
231
232 return 0;
233
234 error:
235 return ret;
236 }
This page took 0.033843 seconds and 4 git commands to generate.