9a4c47d839a60d14d78df5f599f55bf4936c6310
[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 cds_lfht_iter iter;
42 struct cds_lfht_node *node;
43
44 DBG("Sending streams of channel %s to UST consumer", uchan->name);
45
46 /* Send channel */
47 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
48
49 /*
50 * We need to keep shm_fd open to make sure this key stays unique within
51 * the session daemon.
52 */
53 lum.u.channel.channel_key = uchan->obj->shm_fd;
54 lum.u.channel.max_sb_size = uchan->attr.subbuf_size;
55 lum.u.channel.mmap_len = uchan->obj->memory_map_size;
56 DBG("Sending channel %d to consumer", lum.u.channel.channel_key);
57 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
58 if (ret < 0) {
59 perror("send consumer channel");
60 goto error;
61 }
62 fd = uchan->obj->shm_fd;
63 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
64 if (ret < 0) {
65 perror("send consumer channel ancillary data");
66 goto error;
67 }
68
69 rcu_read_lock();
70 hashtable_get_first(uchan->streams, &iter);
71 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
72 struct ltt_ust_stream *stream =
73 caa_container_of(node, struct ltt_ust_stream, node);
74 int fds[2];
75
76 if (!stream->obj->shm_fd) {
77 goto next;
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 lum.u.stream.output = uchan->attr.output;
84 lum.u.stream.mmap_len = stream->obj->memory_map_size;
85 strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
86 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
87 DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
88 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
89 if (ret < 0) {
90 perror("send consumer stream");
91 goto error;
92 }
93
94 fds[0] = stream->obj->shm_fd;
95 fds[1] = stream->obj->wait_fd;
96 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
97 if (ret < 0) {
98 perror("send consumer stream ancillary data");
99 goto error;
100 }
101
102 next:
103 hashtable_get_next(uchan->streams, &iter);
104 }
105 rcu_read_unlock();
106
107 DBG("consumer channel streams sent");
108
109 return 0;
110
111 error:
112 return ret;
113 }
114
115 /*
116 * Send all stream fds of the UST session to the consumer.
117 */
118 int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
119 {
120 int ret = 0;
121 int sock = consumer_fd;
122 struct cds_lfht_iter iter;
123 struct cds_lfht_node *node;
124 struct lttcomm_consumer_msg lum;
125 struct ust_app_channel *uchan;
126
127 DBG("Sending metadata stream fd");
128
129 if (usess->metadata->obj->shm_fd != 0) {
130 int fd;
131 int fds[2];
132
133 /* Send metadata channel fd */
134 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
135 lum.u.channel.channel_key = usess->metadata->obj->shm_fd;
136 lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size;
137 lum.u.channel.mmap_len = 0; /* for kernel */
138 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
139 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
140 if (ret < 0) {
141 perror("send consumer channel");
142 goto error;
143 }
144 fd = usess->metadata->obj->shm_fd;
145 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
146 if (ret < 0) {
147 perror("send consumer metadata channel");
148 goto error;
149 }
150
151 /* Send metadata stream fd */
152 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
153 lum.u.stream.channel_key = usess->metadata->obj->shm_fd;
154 lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd;
155 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
156 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
157 lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size;
158 strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1);
159 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
160 DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
161 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
162 if (ret < 0) {
163 perror("send consumer metadata stream");
164 goto error;
165 }
166 fds[0] = usess->metadata->stream_obj->shm_fd;
167 fds[1] = usess->metadata->stream_obj->wait_fd;
168 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
169 if (ret < 0) {
170 perror("send consumer stream");
171 goto error;
172 }
173 }
174
175 /* Send each channel fd streams of session */
176 rcu_read_lock();
177 hashtable_get_first(usess->channels, &iter);
178 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
179 uchan = caa_container_of(node, struct ust_app_channel, node);
180
181 ret = send_channel_streams(sock, uchan);
182 if (ret < 0) {
183 goto error;
184 }
185 hashtable_get_next(usess->channels, &iter);
186 }
187 rcu_read_unlock();
188
189 DBG("consumer fds (metadata and channel streams) sent");
190
191 return 0;
192
193 error:
194 return ret;
195 }
This page took 0.048161 seconds and 4 git commands to generate.