Add FD_CLOEXEC option to all anonymous FDs
[lttng-tools.git] / ltt-sessiond / kernel-ctl.c
CommitLineData
20fe2104
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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
8c0faa1d 19#define _GNU_SOURCE
20fe2104 20#include <errno.h>
7b395890 21#include <fcntl.h>
20fe2104
DG
22#include <stdlib.h>
23#include <stdio.h>
f34daff7 24#include <string.h>
8c0faa1d 25#include <unistd.h>
20fe2104 26
8c0faa1d 27#include "lttngerr.h"
20fe2104
DG
28#include "ltt-sessiond.h"
29#include "libkernelctl.h"
30#include "kernel-ctl.h"
20fe2104
DG
31
32/*
33 * kernel_create_session
34 *
54012638
DG
35 * Create a new kernel session, register it to the kernel tracer and add it to
36 * the session daemon session.
20fe2104 37 */
8c0faa1d 38int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
39{
40 int ret;
41 struct ltt_kernel_session *lks;
42
54012638
DG
43 /* Allocate data structure */
44 lks = trace_create_kernel_session();
20fe2104 45 if (lks == NULL) {
54012638 46 ret = -1;
20fe2104
DG
47 goto error;
48 }
49
54012638 50 /* Kernel tracer session creation */
20fe2104
DG
51 ret = kernctl_create_session(tracer_fd);
52 if (ret < 0) {
54012638 53 perror("ioctl kernel create session");
20fe2104
DG
54 goto error;
55 }
56
20fe2104 57 lks->fd = ret;
7b395890
DG
58 /* Prevent fd duplication after execlp() */
59 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
60 if (ret < 0) {
61 perror("fcntl session fd");
62 }
63
f3ed775e 64 lks->kconsumer_fds_sent = 0;
8c0faa1d
DG
65 session->kernel_session = lks;
66 session->kern_session_count++;
8c0faa1d
DG
67
68 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
69
70 return 0;
71
72error:
73 return ret;
74}
75
76/*
77 * kernel_create_channel
78 *
54012638
DG
79 * Create a kernel channel, register it to the kernel tracer and add it to the
80 * kernel session.
20fe2104 81 */
f3ed775e 82int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan)
20fe2104
DG
83{
84 int ret;
85 struct ltt_kernel_channel *lkc;
20fe2104 86
54012638 87 /* Allocate kernel channel */
f3ed775e 88 lkc = trace_create_kernel_channel(chan);
54012638 89 if (lkc == NULL) {
20fe2104
DG
90 goto error;
91 }
92
54012638 93 /* Kernel tracer channel creation */
f3ed775e 94 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 95 if (ret < 0) {
54012638 96 perror("ioctl kernel create channel");
20fe2104
DG
97 goto error;
98 }
99
54012638 100 /* Setup the channel fd */
20fe2104 101 lkc->fd = ret;
7b395890
DG
102 /* Prevent fd duplication after execlp() */
103 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
104 if (ret < 0) {
105 perror("fcntl session fd");
106 }
107
54012638 108 /* Add channel to session */
8c0faa1d
DG
109 cds_list_add(&lkc->list, &session->channel_list.head);
110 session->channel_count++;
20fe2104 111
f3ed775e
DG
112 DBG("Kernel channel %s created (fd: %d and path: %s)",
113 lkc->channel->name, lkc->fd, lkc->pathname);
20fe2104
DG
114
115 return 0;
116
117error:
54012638 118 return -1;
20fe2104 119}
f34daff7
DG
120
121/*
f3ed775e 122 * kernel_create_event
f34daff7 123 *
54012638
DG
124 * Create a kernel event, enable it to the kernel tracer and add it to the
125 * channel event list of the kernel session.
f34daff7 126 */
f3ed775e 127int kernel_create_event(struct ltt_kernel_channel *channel, struct lttng_event *ev)
f34daff7
DG
128{
129 int ret;
130 struct ltt_kernel_event *event;
f34daff7 131
f3ed775e 132 event = trace_create_kernel_event(ev);
54012638 133 if (event == NULL) {
f34daff7
DG
134 goto error;
135 }
136
f3ed775e
DG
137 ret = kernctl_create_event(channel->fd, event->event);
138 if (ret < 0) {
139 ERR("Unable to enable event %s for channel %s", ev->name, channel->channel->name);
140 goto error;
8c0faa1d 141 }
f34daff7 142
f3ed775e 143 event->fd = ret;
7b395890
DG
144 /* Prevent fd duplication after execlp() */
145 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
146 if (ret < 0) {
147 perror("fcntl session fd");
148 }
149
f3ed775e
DG
150 /* Add event to event list */
151 cds_list_add(&event->list, &channel->events_list.head);
152 DBG("Event %s enabled (fd: %d)", ev->name, event->fd);
153
f34daff7
DG
154 return 0;
155
156error:
54012638 157 return -1;
f34daff7 158}
aaf26714
DG
159
160/*
161 * kernel_open_metadata
162 *
54012638
DG
163 * Create kernel metadata, open from the kernel tracer and add it to the
164 * kernel session.
aaf26714
DG
165 */
166int kernel_open_metadata(struct ltt_kernel_session *session)
167{
168 int ret;
169 struct ltt_kernel_metadata *lkm;
aaf26714 170
54012638
DG
171 /* Allocate kernel metadata */
172 lkm = trace_create_kernel_metadata();
173 if (lkm == NULL) {
aaf26714
DG
174 goto error;
175 }
176
54012638 177 /* Kernel tracer metadata creation */
f3ed775e 178 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
179 if (ret < 0) {
180 goto error;
181 }
182
8c0faa1d 183 lkm->fd = ret;
7b395890
DG
184 /* Prevent fd duplication after execlp() */
185 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
186 if (ret < 0) {
187 perror("fcntl session fd");
188 }
189
aaf26714 190 session->metadata = lkm;
8c0faa1d 191
54012638 192 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
193
194 return 0;
195
196error:
54012638 197 return -1;
8c0faa1d
DG
198}
199
200/*
201 * kernel_start_session
202 *
203 * Start tracing session.
204 */
205int kernel_start_session(struct ltt_kernel_session *session)
206{
207 int ret;
208
209 ret = kernctl_start_session(session->fd);
210 if (ret < 0) {
f3ed775e 211 perror("ioctl start session");
8c0faa1d
DG
212 goto error;
213 }
214
215 DBG("Kernel session started");
216
217 return 0;
218
219error:
220 return ret;
221}
222
f3ed775e
DG
223/*
224 * kernel_wait_quiescent
225 *
226 * Make a kernel wait to make sure in-flight probe have completed.
227 */
228void kernel_wait_quiescent(int fd)
229{
230 int ret;
231
232 DBG("Kernel quiescent wait on %d", fd);
233
234 ret = kernctl_wait_quiescent(fd);
235 if (ret < 0) {
236 perror("wait quiescent ioctl");
237 ERR("Kernel quiescent wait failed");
238 }
239}
240
241/*
242 * kernel_metadata_flush_buffer
243 *
244 * Force flush buffer of metadata.
245 */
246int kernel_metadata_flush_buffer(int fd)
247{
248 int ret;
249
250 ret = kernctl_buffer_flush(fd);
251 if (ret < 0) {
252 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
253 }
254
255 return 0;
256}
257
258/*
259 * kernel_flush_buffer
260 *
261 * Force flush buffer for channel.
262 */
263int kernel_flush_buffer(struct ltt_kernel_channel *channel)
264{
265 int ret;
266 struct ltt_kernel_stream *stream;
267
268 DBG("Flush buffer for channel %s", channel->channel->name);
269
270 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
271 DBG("Flushing channel stream %d", stream->fd);
272 ret = kernctl_buffer_flush(stream->fd);
273 if (ret < 0) {
274 perror("ioctl");
275 ERR("Fail to flush buffer for stream %d (ret: %d)",
276 stream->fd, ret);
277 }
278 }
279
280 return 0;
281}
282
8c0faa1d
DG
283/*
284 * kernel_stop_session
285 *
286 * Stop tracing session.
287 */
288int kernel_stop_session(struct ltt_kernel_session *session)
289{
290 int ret;
291
292 ret = kernctl_stop_session(session->fd);
293 if (ret < 0) {
294 goto error;
295 }
296
297 DBG("Kernel session stopped");
298
299 return 0;
300
301error:
302 return ret;
303}
304
305/*
f3ed775e 306 * kernel_open_channel_stream
8c0faa1d 307 *
f3ed775e 308 * Open stream of channel, register it to the kernel tracer and add it
54012638 309 * to the stream list of the channel.
8c0faa1d
DG
310 *
311 * Return the number of created stream. Else, a negative value.
312 */
f3ed775e 313int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
314{
315 int ret;
316 struct ltt_kernel_stream *lks;
317
318 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
54012638 319 lks = trace_create_kernel_stream();
8c0faa1d 320 if (lks == NULL) {
54012638 321 close(ret);
8c0faa1d
DG
322 goto error;
323 }
324
325 lks->fd = ret;
7b395890
DG
326 /* Prevent fd duplication after execlp() */
327 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
328 if (ret < 0) {
329 perror("fcntl session fd");
330 }
331
8c0faa1d
DG
332 ret = asprintf(&lks->pathname, "%s/trace_%d",
333 channel->pathname, channel->stream_count);
334 if (ret < 0) {
335 perror("asprintf kernel create stream");
336 goto error;
337 }
8c0faa1d 338
54012638 339 /* Add stream to channe stream list */
8c0faa1d
DG
340 cds_list_add(&lks->list, &channel->stream_list.head);
341 channel->stream_count++;
8c0faa1d 342
54012638
DG
343 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
344 channel->stream_count, lks->fd, lks->state, lks->pathname);
345 }
8c0faa1d
DG
346
347 return channel->stream_count;
348
349error:
54012638 350 return -1;
8c0faa1d
DG
351}
352
353/*
f3ed775e 354 * kernel_open_metadata_stream
8c0faa1d 355 *
f3ed775e 356 * Open the metadata stream and set it to the kernel session.
8c0faa1d 357 */
f3ed775e 358int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
359{
360 int ret;
361
362 ret = kernctl_create_stream(session->metadata->fd);
363 if (ret < 0) {
364 perror("kernel create metadata stream");
8c0faa1d
DG
365 goto error;
366 }
367
368 DBG("Kernel metadata stream created (fd: %d)", ret);
369 session->metadata_stream_fd = ret;
7b395890
DG
370 /* Prevent fd duplication after execlp() */
371 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
372 if (ret < 0) {
373 perror("fcntl session fd");
374 }
aaf26714
DG
375
376 return 0;
377
378error:
54012638 379 return -1;
aaf26714 380}
2ef84c95
DG
381
382/*
383 * kernel_list_events
384 *
385 * Get the event list from the kernel tracer and return that list in the CTF
386 * format.
387 */
388ssize_t kernel_list_events(int tracer_fd, char **list)
389{
390 int fd;
391 char *buf, *line = NULL;
392 size_t nb, nbmem, total = 0;
393 ssize_t size;
394 FILE *fp;
395
396 fd = kernctl_tracepoint_list(tracer_fd);
397 if (fd < 0) {
398 perror("kernel tracepoint list");
399 goto error;
400 }
401
402 fp = fdopen(fd, "r");
403 if (fp == NULL) {
404 perror("kernel tracepoint list fdopen");
405 goto error;
406 }
407
408 /*
409 * Init memory size counter
410 * See kernel-ctl.h for explanation of this value
411 */
412 nbmem = KERNEL_EVENT_LIST_SIZE;
413 buf = malloc(nbmem);
414
415 while ((size = getline(&line, &nb, fp)) != -1) {
416 if (total + size > nbmem) {
d686b40f 417 DBG("Reallocating event list from %zd to %zd bytes", nbmem,
2ef84c95
DG
418 total + size + KERNEL_EVENT_LIST_SIZE);
419 /* Adding the default size again */
420 nbmem = total + size + KERNEL_EVENT_LIST_SIZE;
421 buf = realloc(buf, nbmem);
422 if (buf == NULL) {
423 perror("realloc list events");
424 goto error;
425 }
426 }
427 memcpy(buf + total, line, size);
428 total += size;
429 }
430
431 *list = buf;
432
433 DBG("Kernel list events done");
434
435 return total;
436
437error:
438 return -1;
439}
This page took 0.065368 seconds and 4 git commands to generate.