Add cleanup function for UST app
[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 to make sure this key stays unique within
50 * the session daemon.
51 */
52 lum.u.channel.channel_key = uchan->obj->shm_fd;
53 lum.u.channel.max_sb_size = uchan->attr.subbuf_size;
54 lum.u.channel.mmap_len = uchan->obj->memory_map_size;
55 DBG("Sending channel %d to consumer", lum.u.channel.channel_key);
56 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
57 if (ret < 0) {
58 perror("send consumer channel");
59 goto error;
60 }
61 fd = uchan->obj->shm_fd;
62 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
63 if (ret < 0) {
64 perror("send consumer channel ancillary data");
65 goto error;
66 }
67
68 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
69 int fds[2];
70
71 if (!stream->obj->shm_fd) {
72 continue;
73 }
74 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
75 lum.u.stream.channel_key = uchan->obj->shm_fd;
76 lum.u.stream.stream_key = stream->obj->shm_fd;
77 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
78 /*
79 * FIXME Hack alert! we force MMAP for now. Mixup
80 * between EVENT and UST enums elsewhere.
81 */
82 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
83 lum.u.stream.mmap_len = stream->obj->memory_map_size;
84 strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
85 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
86 DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
87 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
88 if (ret < 0) {
89 perror("send consumer stream");
90 goto error;
91 }
92
93 fds[0] = stream->obj->shm_fd;
94 fds[1] = stream->obj->wait_fd;
95 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
96 if (ret < 0) {
97 perror("send consumer stream ancillary data");
98 goto error;
99 }
100
101 /*
102 * We release the stream object here, as we have passed
103 * it to the consumer.
104 */
105 /* Ensure we don't let the app know (sock = -1). */
106 ustctl_release_object(-1, stream->obj);
107 cds_list_del(&stream->list);
108 free(stream);
109 }
110 /* Ensure we don't let the app know (sock = -1). */
111 ustctl_release_object(-1, uchan->obj);
112
113 DBG("consumer channel streams sent");
114
115 return 0;
116
117 error:
118 return ret;
119 }
120
121 /*
122 * Send all stream fds of the UST session to the consumer.
123 */
124 int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
125 {
126 int ret = 0;
127 int sock = consumer_fd;
128 struct cds_lfht_iter iter;
129 struct cds_lfht_node *node;
130 struct lttcomm_consumer_msg lum;
131 struct ust_app_channel *uchan;
132
133 DBG("Sending metadata stream fd");
134
135 if (usess->metadata->obj->shm_fd != 0) {
136 int fd;
137 int fds[2];
138
139 /* Send metadata channel fd */
140 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
141 lum.u.channel.channel_key = usess->metadata->obj->shm_fd;
142 lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size;
143 lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size;
144 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
145 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
146 if (ret < 0) {
147 perror("send consumer channel");
148 goto error;
149 }
150 fd = usess->metadata->obj->shm_fd;
151 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
152 if (ret < 0) {
153 perror("send consumer metadata channel");
154 goto error;
155 }
156
157 /* Send metadata stream fd */
158 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
159 lum.u.stream.channel_key = usess->metadata->obj->shm_fd;
160 lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd;
161 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
162 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
163 lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size;
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) {
169 perror("send consumer metadata stream");
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) {
176 perror("send consumer stream");
177 goto error;
178 }
179 /* Metadata fds passed to consumer, release them. */
180 /* Ensure we don't let the app know (sock = -1). */
181 ustctl_release_object(-1, usess->metadata->stream_obj);
182 ustctl_release_object(-1, usess->metadata->obj);
183 }
184
185 /* Send each channel fd streams of session */
186 rcu_read_lock();
187 hashtable_get_first(usess->channels, &iter);
188 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
189 uchan = caa_container_of(node, struct ust_app_channel, node);
190
191 ret = send_channel_streams(sock, uchan);
192 if (ret < 0) {
193 rcu_read_unlock();
194 goto error;
195 }
196 hashtable_get_next(usess->channels, &iter);
197 }
198 rcu_read_unlock();
199
200 DBG("consumer fds (metadata and channel streams) sent");
201
202 return 0;
203
204 error:
205 return ret;
206 }
This page took 0.034373 seconds and 5 git commands to generate.