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