2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
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
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., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/uri.h>
34 * Allocate and assign data to a consumer_output object.
36 * Return pointer to structure.
38 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
40 struct consumer_output
*output
= NULL
;
42 output
= zmalloc(sizeof(struct consumer_output
));
44 PERROR("zmalloc consumer_output");
48 /* By default, consumer output is enabled */
51 output
->net_seq_index
= -1;
53 * Important to keep it to a negative value on creation since it was zeroed
54 * during allocation and the file descriptor 0 is a valid one.
63 * Delete the consumer_output object from the list and free the ptr.
65 void consumer_destroy_output(struct consumer_output
*obj
)
72 (void) close(obj
->sock
);
78 * Copy consumer output and returned the newly allocated copy.
80 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
82 struct consumer_output
*output
;
86 output
= consumer_create_output(obj
->type
);
91 memcpy(output
, obj
, sizeof(struct consumer_output
));
98 * Set network URI to the consumer output object.
100 * Return 0 on success. Negative value on error.
102 int consumer_set_network_uri(struct consumer_output
*obj
,
103 struct lttng_uri
*uri
)
106 char tmp_path
[PATH_MAX
];
107 char hostname
[HOST_NAME_MAX
];
108 struct lttng_uri
*dst_uri
= NULL
;
110 /* Code flow error safety net. */
114 switch (uri
->stype
) {
115 case LTTNG_STREAM_CONTROL
:
116 dst_uri
= &obj
->dst
.net
.control
;
117 obj
->dst
.net
.control_isset
= 1;
118 if (uri
->port
== 0) {
119 /* Assign default port. */
120 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
123 case LTTNG_STREAM_DATA
:
124 dst_uri
= &obj
->dst
.net
.data
;
125 obj
->dst
.net
.data_isset
= 1;
126 if (uri
->port
== 0) {
127 /* Assign default port. */
128 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
132 ERR("Set network uri type unknown %d", uri
->stype
);
136 ret
= uri_compare(dst_uri
, uri
);
138 /* Same URI, don't touch it and return success. */
139 DBG3("URI network compare are the same");
143 /* URIs were not equal, replacing it. */
144 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
145 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
146 obj
->type
= CONSUMER_DST_NET
;
148 /* Handle subdir and add hostname in front. */
149 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
150 /* Get hostname to append it in the pathname */
151 ret
= gethostname(hostname
, sizeof(hostname
));
153 PERROR("gethostname. Fallback on default localhost");
154 strncpy(hostname
, "localhost", sizeof(hostname
));
156 hostname
[sizeof(hostname
) - 1] = '\0';
158 /* Setup consumer subdir if none present in the control URI */
159 if (strlen(dst_uri
->subdir
) == 0) {
160 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
161 hostname
, obj
->subdir
);
163 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
164 hostname
, dst_uri
->subdir
);
167 PERROR("snprintf set consumer uri subdir");
171 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
172 DBG3("Consumer set network uri subdir path %s", tmp_path
);
183 * Send file descriptor to consumer via sock.
185 int consumer_send_fds(int sock
, int *fds
, size_t nb_fd
)
192 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, nb_fd
);
194 PERROR("send consumer fds");
203 * Consumer send channel communication message structure to consumer.
205 int consumer_send_channel(int sock
, struct lttcomm_consumer_msg
*msg
)
212 ret
= lttcomm_send_unix_sock(sock
, msg
,
213 sizeof(struct lttcomm_consumer_msg
));
215 PERROR("send consumer channel");
224 * Init channel communication message structure.
226 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
227 enum lttng_consumer_command cmd
,
229 uint64_t max_sb_size
,
235 /* TODO: Args validation */
237 /* Zeroed structure */
238 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
242 msg
->u
.channel
.channel_key
= channel_key
;
243 msg
->u
.channel
.max_sb_size
= max_sb_size
;
244 msg
->u
.channel
.mmap_len
= mmap_len
;
248 * Init stream communication message structure.
250 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
251 enum lttng_consumer_command cmd
,
255 enum lttng_event_output output
,
260 unsigned int metadata_flag
,
262 const char *pathname
)
266 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
268 /* TODO: Args validation */
271 msg
->u
.stream
.channel_key
= channel_key
;
272 msg
->u
.stream
.stream_key
= stream_key
;
273 msg
->u
.stream
.state
= state
;
274 msg
->u
.stream
.output
= output
;
275 msg
->u
.stream
.mmap_len
= mmap_len
;
276 msg
->u
.stream
.uid
= uid
;
277 msg
->u
.stream
.gid
= gid
;
278 msg
->u
.stream
.net_index
= net_index
;
279 msg
->u
.stream
.metadata_flag
= metadata_flag
;
280 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
281 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
282 strncpy(msg
->u
.stream
.path_name
, pathname
,
283 sizeof(msg
->u
.stream
.path_name
));
284 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
288 * Send stream communication structure to the consumer.
290 int consumer_send_stream(int sock
, struct consumer_output
*dst
,
291 struct lttcomm_consumer_msg
*msg
, int *fds
, size_t nb_fd
)
299 case CONSUMER_DST_NET
:
300 /* Consumer should send the stream on the network. */
301 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
303 case CONSUMER_DST_LOCAL
:
304 /* Add stream file name to stream path */
305 strncat(msg
->u
.stream
.path_name
, "/", sizeof(msg
->u
.stream
.path_name
));
306 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
307 sizeof(msg
->u
.stream
.path_name
));
308 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
309 /* Indicate that the stream is NOT network */
310 msg
->u
.stream
.net_index
= -1;
313 ERR("Consumer unknown output type (%d)", dst
->type
);
319 ret
= lttcomm_send_unix_sock(sock
, msg
,
320 sizeof(struct lttcomm_consumer_msg
));
322 PERROR("send consumer stream");
326 ret
= consumer_send_fds(sock
, fds
, nb_fd
);