Fix multiple error handling for UST tracing
[lttng-tools.git] / lttng-sessiond / ust-consumer.c
CommitLineData
48842b30
DG
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>
bec39940 26#include <lttng-ht.h>
48842b30
DG
27#include <lttng-share.h>
28#include <lttng-sessiond-comm.h>
29#include <lttng/lttng-consumer.h>
30
48842b30
DG
31#include "ust-consumer.h"
32
33/*
34 * Send all stream fds of UST channel to the consumer.
35 */
36static int send_channel_streams(int sock,
6df2e2c9
MD
37 struct ust_app_channel *uchan,
38 uid_t uid, gid_t gid)
48842b30 39{
8010679a 40 int ret, fd;
48842b30 41 struct lttcomm_consumer_msg lum;
d80a6244 42 struct ltt_ust_stream *stream, *tmp;
48842b30
DG
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 /*
7ad0a0cb
MD
50 * We need to keep shm_fd open while we transfer the stream file
51 * descriptors to make sure this key stays unique within the
52 * session daemon. We can free the channel shm_fd without
53 * problem after we finished sending stream fds for that
54 * channel.
48842b30
DG
55 */
56 lum.u.channel.channel_key = uchan->obj->shm_fd;
57 lum.u.channel.max_sb_size = uchan->attr.subbuf_size;
58 lum.u.channel.mmap_len = uchan->obj->memory_map_size;
59 DBG("Sending channel %d to consumer", lum.u.channel.channel_key);
60 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
61 if (ret < 0) {
62 perror("send consumer channel");
63 goto error;
64 }
8010679a
MD
65 fd = uchan->obj->shm_fd;
66 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
48842b30
DG
67 if (ret < 0) {
68 perror("send consumer channel ancillary data");
69 goto error;
70 }
71
d80a6244 72 cds_list_for_each_entry_safe(stream, tmp, &uchan->streams.head, list) {
48842b30
DG
73 int fds[2];
74
75 if (!stream->obj->shm_fd) {
5af2f756 76 continue;
48842b30 77 }
48842b30
DG
78 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
79 lum.u.stream.channel_key = uchan->obj->shm_fd;
80 lum.u.stream.stream_key = stream->obj->shm_fd;
81 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
d41f73b7
MD
82 /*
83 * FIXME Hack alert! we force MMAP for now. Mixup
84 * between EVENT and UST enums elsewhere.
85 */
86 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
48842b30 87 lum.u.stream.mmap_len = stream->obj->memory_map_size;
6df2e2c9
MD
88 lum.u.stream.uid = uid;
89 lum.u.stream.gid = gid;
48842b30
DG
90 strncpy(lum.u.stream.path_name, stream->pathname, PATH_MAX - 1);
91 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
92 DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
93 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
94 if (ret < 0) {
95 perror("send consumer stream");
96 goto error;
97 }
98
99 fds[0] = stream->obj->shm_fd;
100 fds[1] = stream->obj->wait_fd;
101 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
102 if (ret < 0) {
103 perror("send consumer stream ancillary data");
104 goto error;
105 }
48842b30 106 }
48842b30
DG
107
108 DBG("consumer channel streams sent");
109
110 return 0;
111
112error:
113 return ret;
114}
115
116/*
117 * Send all stream fds of the UST session to the consumer.
118 */
119int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
120{
121 int ret = 0;
122 int sock = consumer_fd;
bec39940 123 struct lttng_ht_iter iter;
48842b30 124 struct lttcomm_consumer_msg lum;
bec39940 125 struct ust_app_channel *ua_chan;
48842b30
DG
126
127 DBG("Sending metadata stream fd");
128
7753dea8
MD
129 if (consumer_fd < 0) {
130 ERR("Consumer has negative file descriptor");
131 return -EINVAL;
132 }
133
48842b30 134 if (usess->metadata->obj->shm_fd != 0) {
8010679a 135 int fd;
48842b30
DG
136 int fds[2];
137
138 /* Send metadata channel fd */
139 lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
140 lum.u.channel.channel_key = usess->metadata->obj->shm_fd;
141 lum.u.channel.max_sb_size = usess->metadata->attr.subbuf_size;
3ee75e9c 142 lum.u.channel.mmap_len = usess->metadata->obj->memory_map_size;
ee6b7fb2 143 DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
48842b30
DG
144 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
145 if (ret < 0) {
146 perror("send consumer channel");
147 goto error;
148 }
8010679a
MD
149 fd = usess->metadata->obj->shm_fd;
150 ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
48842b30
DG
151 if (ret < 0) {
152 perror("send consumer metadata channel");
153 goto error;
154 }
155
156 /* Send metadata stream fd */
157 lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
158 lum.u.stream.channel_key = usess->metadata->obj->shm_fd;
159 lum.u.stream.stream_key = usess->metadata->stream_obj->shm_fd;
160 lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
161 lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
162 lum.u.stream.mmap_len = usess->metadata->stream_obj->memory_map_size;
6df2e2c9
MD
163 lum.u.stream.uid = usess->uid;
164 lum.u.stream.gid = usess->gid;
48842b30
DG
165 strncpy(lum.u.stream.path_name, usess->metadata->pathname, PATH_MAX - 1);
166 lum.u.stream.path_name[PATH_MAX - 1] = '\0';
167 DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
168 ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
169 if (ret < 0) {
170 perror("send consumer metadata stream");
171 goto error;
172 }
173 fds[0] = usess->metadata->stream_obj->shm_fd;
174 fds[1] = usess->metadata->stream_obj->wait_fd;
175 ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
176 if (ret < 0) {
177 perror("send consumer stream");
178 goto error;
179 }
180 }
181
182 /* Send each channel fd streams of session */
183 rcu_read_lock();
bec39940
DG
184 cds_lfht_for_each_entry(usess->channels->ht, &iter.iter, ua_chan,
185 node.node) {
186 ret = send_channel_streams(sock, ua_chan, usess->uid, usess->gid);
48842b30 187 if (ret < 0) {
5485f822 188 rcu_read_unlock();
48842b30
DG
189 goto error;
190 }
48842b30
DG
191 }
192 rcu_read_unlock();
193
194 DBG("consumer fds (metadata and channel streams) sent");
195
196 return 0;
197
198error:
199 return ret;
200}
This page took 0.0308 seconds and 4 git commands to generate.