Add pthread id event context
[lttng-ust.git] / tests / ust-basic-tracing / ust-basic-tracing.c
CommitLineData
b0a99af4
MD
1/*
2 * ust-basic-tracing.c - Basic single-session, single-channel, single-process UST tracing
3 *
4 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; only version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#define _LARGEFILE64_SOURCE
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <signal.h>
27#include <unistd.h>
28#include <sys/wait.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <sys/mman.h>
33#include <limits.h>
34#include <urcu/futex.h>
35#include <urcu/uatomic.h>
36#include <assert.h>
37#include <sys/socket.h>
38
39#include "lttng-ust-comm.h"
40#include "../../libringbuffer/backend.h"
41#include "../../libringbuffer/frontend.h"
42
43#define MAX_NR_STREAMS 64
44#define MAX_NR_EVENTS 128
45
46struct object_data {
47 int handle;
48 int shm_fd;
49 int wait_fd;
50 uint64_t memory_map_size;
51};
52
53static int session_handle;
54static struct object_data metadata_stream_data;
55static struct object_data metadata_data;
56static struct object_data channel_data;
57static struct object_data stream_data[MAX_NR_STREAMS];
58static int event_handle[MAX_NR_EVENTS];
8de38cf7 59static int context_handle;
b0a99af4
MD
60
61static int apps_socket = -1;
62static char apps_sock_path[PATH_MAX];
63static char local_apps_wait_shm_path[PATH_MAX];
64
65static volatile int quit_program;
66
67static void handle_signals(int signo)
68{
69 quit_program = 1;
70}
71
72static int send_app_msg(int sock, struct lttcomm_ust_msg *lum)
73{
74 ssize_t len;
75
76 len = lttcomm_send_unix_sock(sock, lum, sizeof(*lum));
77 switch (len) {
78 case sizeof(*lum):
79 printf("message successfully sent\n");
80 break;
81 case -1:
82 if (errno == ECONNRESET) {
83 printf("remote end closed connection\n");
84 return 0;
85 }
86 return -1;
87 default:
88 printf("incorrect message size: %zd\n", len);
89 return -1;
90 }
91 return 0;
92}
93
94static int recv_app_reply(int sock, struct lttcomm_ust_reply *lur,
95 uint32_t expected_handle, uint32_t expected_cmd)
96{
97 ssize_t len;
98
99 memset(lur, 0, sizeof(*lur));
100 len = lttcomm_recv_unix_sock(sock, lur, sizeof(*lur));
101 switch (len) {
102 case 0: /* orderly shutdown */
103 printf("Application has performed an orderly shutdown\n");
104 return -EINVAL;
105 case sizeof(*lur):
106 printf("result message received\n");
107 if (lur->handle != expected_handle) {
108 printf("Unexpected result message handle\n");
109 return -EINVAL;
110 }
111
112 if (lur->cmd != expected_cmd) {
113 printf("Unexpected result message command\n");
114 return -EINVAL;
115 }
116 if (lur->ret_code != LTTCOMM_OK) {
117 printf("remote operation failed with code %d.\n",
118 lur->ret_code);
119 return lur->ret_code;
120 }
121 return 0;
122 case -1:
123 if (errno == ECONNRESET) {
124 printf("remote end closed connection\n");
125 return -EINVAL;
126 }
127 return -1;
128 default:
129 printf("incorrect message size: %zd\n", len);
130 return len > 0 ? -1 : len;
131 }
132}
133
134static int send_app_cmd(int sock,
135 struct lttcomm_ust_msg *lum,
136 struct lttcomm_ust_reply *lur)
137{
138 int ret;
139
140 ret = send_app_msg(sock, lum);
141 if (ret)
142 return ret;
143 ret = recv_app_reply(sock, lur, lum->handle, lum->cmd);
144 if (ret)
145 return ret;
146 return 0;
147}
148
149
150/*
151 * Receives a single fd from socket.
152 *
153 * Returns the size of received data
154 */
155static int lttcomm_recv_fd(int sock)
156{
157 struct iovec iov[1];
158 int ret = 0;
159 int data_fd;
160 struct cmsghdr *cmsg;
161 char recv_fd[CMSG_SPACE(sizeof(int))];
162 struct msghdr msg = { 0 };
af327df1
MD
163 union {
164 unsigned char vc[4];
165 int vi;
166 } tmp;
167 int i;
b0a99af4
MD
168
169 /* Prepare to receive the structures */
170 iov[0].iov_base = &data_fd;
171 iov[0].iov_len = sizeof(data_fd);
172 msg.msg_iov = iov;
173 msg.msg_iovlen = 1;
174 msg.msg_control = recv_fd;
175 msg.msg_controllen = sizeof(recv_fd);
176
177 printf("Waiting to receive fd\n");
178 if ((ret = recvmsg(sock, &msg, 0)) < 0) {
179 perror("recvmsg");
180 goto end;
181 }
182 if (ret != sizeof(data_fd)) {
183 printf("Received %d bytes, expected %ld", ret, sizeof(data_fd));
184 goto end;
185 }
186 cmsg = CMSG_FIRSTHDR(&msg);
187 if (!cmsg) {
188 printf("Invalid control message header\n");
189 ret = -1;
190 goto end;
191 }
192 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
193 printf("Didn't received any fd\n");
194 ret = -1;
195 goto end;
196 }
197 /* this is our fd */
af327df1 198 for (i = 0; i < sizeof(int); i++)
aca7316c 199 tmp.vc[i] = CMSG_DATA(cmsg)[i];
af327df1 200 ret = tmp.vi;
b0a99af4
MD
201 printf("received fd %d\n", ret);
202end:
203 return ret;
204}
205
206
207static
208int open_streams(int sock, int channel_handle, struct object_data *stream_datas,
209 int nr_check)
210{
211 int ret, k = 0;
212
213 for (;;) {
214 struct lttcomm_ust_msg lum;
215 struct lttcomm_ust_reply lur;
216
217 memset(&lum, 0, sizeof(lum));
218 lum.handle = channel_handle;
219 lum.cmd = LTTNG_UST_STREAM;
220 ret = send_app_cmd(sock, &lum, &lur);
221 if (!ret) {
222 assert(k < nr_check);
223 stream_datas[k].handle = lur.ret_val;
224 printf("received stream handle %u\n",
225 stream_datas[k].handle);
226 if (lur.ret_code == LTTCOMM_OK) {
227 ssize_t len;
228
229 stream_datas[k].memory_map_size = lur.u.stream.memory_map_size;
230 /* get shm fd */
231 len = lttcomm_recv_fd(sock);
232 if (len < 0)
233 return -EINVAL;
234 stream_datas[k].shm_fd = len;
235 /* get wait fd */
236 len = lttcomm_recv_fd(sock);
237 if (len < 0)
238 return -EINVAL;
239 stream_datas[k].wait_fd = len;
240 }
241 k++;
242 }
243 if (ret == -ENOENT)
244 break;
245 if (ret)
246 return ret;
247 }
248 return 0;
249}
250
251static
252int close_streams(int sock, struct object_data *stream_datas, int nr_check)
253{
254 int ret, k;
255
256 for (k = 0; k < nr_check; k++) {
257 struct lttcomm_ust_msg lum;
258 struct lttcomm_ust_reply lur;
259
260 if (!stream_datas[k].handle)
261 continue;
262 memset(&lum, 0, sizeof(lum));
263 lum.handle = stream_datas[k].handle;
264 lum.cmd = LTTNG_UST_RELEASE;
265 ret = send_app_cmd(sock, &lum, &lur);
266 if (ret) {
267 printf("Error closing stream\n");
268 return ret;
269 }
270 if (stream_datas[k].shm_fd >= 0) {
271 ret = close(stream_datas[k].shm_fd);
272 if (ret) {
273 printf("Error closing stream shm_fd\n");
274 return ret;
275 }
276 }
277 if (stream_datas[k].wait_fd >= 0) {
278 ret = close(stream_datas[k].wait_fd);
279 if (ret) {
280 printf("Error closing stream wait_fd\n");
281 return ret;
282 }
283 }
284 }
285 return 0;
286}
287
288static
289struct shm_handle *map_channel(struct object_data *chan_data,
290 struct object_data *stream_datas, int nr_check)
291{
292 struct shm_handle *handle;
293 struct channel *chan;
294 int k, ret;
295
296 /* map metadata channel */
297 handle = channel_handle_create(chan_data->shm_fd,
298 chan_data->wait_fd,
299 chan_data->memory_map_size);
300 if (!handle) {
301 printf("create handle error\n");
302 return NULL;
303 }
304 chan_data->shm_fd = -1;
305 chan_data->wait_fd = -1;
306 chan = shmp(handle, handle->chan);
307
308 for (k = 0; k < nr_check; k++) {
309 struct object_data *stream_data = &stream_datas[k];
310
311 if (!stream_data->handle)
312 break;
313 /* map stream */
314 ret = channel_handle_add_stream(handle,
315 stream_data->shm_fd,
316 stream_data->wait_fd,
317 stream_data->memory_map_size);
318 if (ret) {
319 printf("add stream error\n");
320 goto error_destroy;
321 }
322 stream_data->shm_fd = -1;
323 stream_data->wait_fd = -1;
324 }
325 return handle;
326
327error_destroy:
328 channel_destroy(chan, handle, 1);
329 return NULL;
330}
331
332static
333void unmap_channel(struct shm_handle *handle)
334{
335 struct channel *chan;
336
337 chan = shmp(handle, handle->chan);
338 /* unmap channel */
339 channel_destroy(chan, handle, 1);
340}
341
342static
343int consume_stream(struct shm_handle *handle, int cpu, char *outfile)
344{
345 struct channel *chan;
346 struct lib_ring_buffer *buf;
347 int outfd, ret;
348 int shm_fd, wait_fd;
349 uint64_t memory_map_size;
350
351 chan = shmp(handle, handle->chan);
352
353 /* open stream */
354 buf = channel_get_ring_buffer(&chan->backend.config,
355 chan, cpu, handle, &shm_fd, &wait_fd, &memory_map_size);
356 if (!buf)
357 return -ENOENT;
358 ret = lib_ring_buffer_open_read(buf, handle, 1);
359 if (ret) {
360 return -1;
361 }
362
363 /* copy */
364 outfd = open(outfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC,
365 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
366 if (outfd < 0) {
367 perror("open output");
368 return -1;
369 }
370
371 printf("Waiting for buffer data for %s\n", outfile);
372 for (;;) {
373 unsigned long read_size;
374 unsigned long copy_size;
375 char *ptr;
376
377 ret = lib_ring_buffer_get_next_subbuf(buf, handle);
378 printf("get next ret %d\n", ret);
379 if (ret == -ENODATA)
380 break;
381 if (ret == -EAGAIN) {
382 sleep(1);
383 continue;
384 }
385 if (ret) {
386 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret);
387 return -1;
388 }
389 read_size = lib_ring_buffer_get_read_data_size(
390 &chan->backend.config, buf, handle);
391 read_size = PAGE_ALIGN(read_size);
392 ptr = lib_ring_buffer_read_offset_address(
393 &buf->backend, 0, handle);
394 printf("WRITE: copy %lu bytes\n", read_size);
395 copy_size = write(outfd, ptr, read_size);
396 if (copy_size < read_size) {
397 printf("write issue: copied %zd, expected %lu\n", copy_size, read_size);
398 }
399 lib_ring_buffer_put_next_subbuf(buf, handle);
400 }
401
402 ret = close(outfd);
403 if (ret) {
404 perror("close");
405 return -1;
406 }
407
408 /* close stream */
409 lib_ring_buffer_release_read(buf, handle, 1);
410 return 0;
411}
412
413static
414int consume_buffers(const char *outputpath)
415{
416 int k, ret;
417 mode_t old_umask;
418 char pathname[PATH_MAX];
419 struct shm_handle *handle;
420
421 snprintf(pathname, PATH_MAX - 1, "%s", outputpath);
422 old_umask = umask(0);
423 ret = mkdir(pathname, S_IRWXU | S_IRWXG);
424 if (ret && errno != EEXIST) {
425 perror("mkdir");
426 umask(old_umask);
427 return -1;
428 }
429 umask(old_umask);
430
431 /* copy metadata */
432 handle = map_channel(&metadata_data,
433 &metadata_stream_data, 1);
434 if (!handle)
435 return -1;
436 snprintf(pathname, PATH_MAX - 1,
437 "%s/metadata", outputpath);
438 ret = consume_stream(handle, -1, pathname);
439 if (ret && ret != -ENOENT) {
440 printf("Error in consume_stream\n");
441 return ret;
442 }
443 unmap_channel(handle);
444
445 /* copy binary data */
446 handle = map_channel(&channel_data,
447 stream_data, MAX_NR_STREAMS);
448 if (!handle)
449 return -1;
450 for (k = 0; k < MAX_NR_STREAMS; k++) {
451 snprintf(pathname, PATH_MAX - 1,
452 "%s/data_%u", outputpath, k);
453 ret = consume_stream(handle, k, pathname);
454 if (ret && ret != -ENOENT) {
455 printf("Error in consume_stream\n");
456 return ret;
457 }
458 }
459 unmap_channel(handle);
460
461 return 0;
462}
463
464int send_app_msgs(int sock, const char *outputpath,
465 unsigned int nr_events, const char **event_names)
466{
467 struct lttcomm_ust_msg lum;
468 struct lttcomm_ust_reply lur;
469 int ret, k;
470
471 /* Create session */
472 memset(&lum, 0, sizeof(lum));
473 lum.handle = LTTNG_UST_ROOT_HANDLE;
474 lum.cmd = LTTNG_UST_SESSION;
475 ret = send_app_cmd(sock, &lum, &lur);
476 if (ret)
477 return ret;
478 session_handle = lur.ret_val;
479 printf("received session handle %u\n", session_handle);
480
481 /* Create metadata channel */
482 memset(&lum, 0, sizeof(lum));
483 lum.handle = session_handle;
484 lum.cmd = LTTNG_UST_METADATA;
485 lum.u.channel.overwrite = 0;
486 lum.u.channel.subbuf_size = 32768;
487 lum.u.channel.num_subbuf = 4;
488 lum.u.channel.switch_timer_interval = 0;
489 lum.u.channel.read_timer_interval = 0;
490 lum.u.channel.output = LTTNG_UST_MMAP;
491 ret = send_app_cmd(sock, &lum, &lur);
492 if (ret)
493 return ret;
494 metadata_data.handle = lur.ret_val;
495 printf("received metadata handle %u\n", metadata_data.handle);
496 if (lur.ret_code == LTTCOMM_OK) {
497 ssize_t len;
498
499 metadata_data.memory_map_size = lur.u.channel.memory_map_size;
500 /* get shm fd */
501 len = lttcomm_recv_fd(sock);
502 if (len < 0)
503 return -EINVAL;
504 metadata_data.shm_fd = len;
505 /* get wait fd */
506 len = lttcomm_recv_fd(sock);
507 if (len < 0)
508 return -EINVAL;
509 metadata_data.wait_fd = len;
510 }
511
512 ret = open_streams(sock, metadata_data.handle,
513 &metadata_stream_data, 1);
514 if (ret) {
515 printf("Error in open_streams\n");
516 return ret;
517 }
518
519 /* Create data channel */
520 memset(&lum, 0, sizeof(lum));
521 lum.handle = session_handle;
522 lum.cmd = LTTNG_UST_CHANNEL;
523 //lum.u.channel.overwrite = 0;
524 lum.u.channel.overwrite = 1;
525 lum.u.channel.subbuf_size = 32768;
526 lum.u.channel.num_subbuf = 8;
527 //lum.u.channel.num_subbuf = 4;
528 //lum.u.channel.num_subbuf = 2;
529 lum.u.channel.switch_timer_interval = 0;
530 lum.u.channel.read_timer_interval = 0;
531 lum.u.channel.output = LTTNG_UST_MMAP;
532 ret = send_app_cmd(sock, &lum, &lur);
533 if (ret)
534 return ret;
535 channel_data.handle = lur.ret_val;
536 printf("received channel handle %u\n", channel_data.handle);
537 if (lur.ret_code == LTTCOMM_OK) {
538 ssize_t len;
539
540 channel_data.memory_map_size = lur.u.channel.memory_map_size;
541 /* get shm fd */
542 len = lttcomm_recv_fd(sock);
543 if (len < 0)
544 return -EINVAL;
545 channel_data.shm_fd = len;
546 /* get wait fd */
547 len = lttcomm_recv_fd(sock);
548 if (len < 0)
549 return -EINVAL;
550 channel_data.wait_fd = len;
551 }
552
553 /* Create events */
554 for (k = 0; k < nr_events; k++) {
555 memset(&lum, 0, sizeof(lum));
556 lum.handle = channel_data.handle;
557 lum.cmd = LTTNG_UST_EVENT;
558 strncpy(lum.u.event.name, event_names[k],
559 LTTNG_UST_SYM_NAME_LEN);
560 lum.u.event.instrumentation = LTTNG_UST_TRACEPOINT;
561 ret = send_app_cmd(sock, &lum, &lur);
562 if (ret)
563 return ret;
564 event_handle[k] = lur.ret_val;
565 printf("received event handle %u\n", event_handle[k]);
566 }
567
8de38cf7
MD
568 /* Attach pthread_id context */
569 memset(&lum, 0, sizeof(lum));
570 lum.handle = channel_data.handle;
571 lum.cmd = LTTNG_UST_CONTEXT;
572 lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
573 ret = send_app_cmd(sock, &lum, &lur);
574 if (ret)
575 return ret;
576 context_handle = lur.ret_val;
577 printf("received context handle %u\n", context_handle);
578
b0a99af4
MD
579 /* Get references to channel streams */
580 ret = open_streams(sock, channel_data.handle,
581 stream_data, MAX_NR_STREAMS);
582 if (ret) {
583 printf("Error in open_streams\n");
584 return ret;
585 }
586
587 memset(&lum, 0, sizeof(lum));
588 lum.handle = session_handle;
589 lum.cmd = LTTNG_UST_SESSION_START;
590 ret = send_app_cmd(sock, &lum, &lur);
591 if (ret)
592 return ret;
593 printf("Session handle %u started.\n", session_handle);
594
595 /* Tell application registration is done */
596 memset(&lum, 0, sizeof(lum));
597 lum.handle = LTTNG_UST_ROOT_HANDLE;
598 lum.cmd = LTTNG_UST_REGISTER_DONE;
599 ret = send_app_cmd(sock, &lum, &lur);
600 if (ret)
601 return ret;
602 printf("Registration done acknowledged.\n");
603
604 sleep(4);
605
606 ret = consume_buffers(outputpath);
607 if (ret) {
608 printf("Error in consume_buffers\n");
609 return ret;
610 }
611
612 /* Release data channel */
613 /* Release streams */
614 ret = close_streams(sock, stream_data,
615 MAX_NR_STREAMS);
616 if (ret)
617 return ret;
618
8de38cf7
MD
619 /* Release context */
620 memset(&lum, 0, sizeof(lum));
621 lum.handle = context_handle;
622 lum.cmd = LTTNG_UST_RELEASE;
623 ret = send_app_cmd(sock, &lum, &lur);
624 if (ret)
625 return ret;
626
b0a99af4
MD
627 /* Release events */
628 for (k = 0; k < nr_events; k++) {
629 memset(&lum, 0, sizeof(lum));
630 lum.handle = event_handle[k];
631 lum.cmd = LTTNG_UST_RELEASE;
632 ret = send_app_cmd(sock, &lum, &lur);
633 if (ret)
634 return ret;
635 }
636 memset(&lum, 0, sizeof(lum));
637 lum.handle = channel_data.handle;
638 lum.cmd = LTTNG_UST_RELEASE;
639 ret = send_app_cmd(sock, &lum, &lur);
640 if (ret)
641 return ret;
642 if (channel_data.shm_fd >= 0) {
643 ret = close(channel_data.shm_fd);
644 if (ret)
645 return ret;
646 }
647 if (channel_data.wait_fd >= 0) {
648 ret = close(channel_data.wait_fd);
649 if (ret)
650 return ret;
651 }
652
653 /* Release metadata channel */
654 ret = close_streams(sock, &metadata_stream_data, 1);
655 if (ret)
656 return ret;
657
658 memset(&lum, 0, sizeof(lum));
659 lum.handle = metadata_data.handle;
660 lum.cmd = LTTNG_UST_RELEASE;
661 ret = send_app_cmd(sock, &lum, &lur);
662 if (ret)
663 return ret;
664 if (metadata_data.shm_fd >= 0) {
665 ret = close(metadata_data.shm_fd);
666 if (ret)
667 return ret;
668 }
669 if (metadata_data.wait_fd >= 0) {
670 ret = close(metadata_data.wait_fd);
671 if (ret)
672 return ret;
673 }
674
675 /* Release session */
676 memset(&lum, 0, sizeof(lum));
677 lum.handle = session_handle;
678 lum.cmd = LTTNG_UST_RELEASE;
679 ret = send_app_cmd(sock, &lum, &lur);
680 if (ret)
681 return ret;
682
683 return 0;
684}
685
686/*
687 * Using fork to set umask in the child process (not multi-thread safe). We
688 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
689 * the shm and does not let everybody modify it, to ensure safety against
690 * shm_unlink) by simply letting the mmap fail and retrying after a few
691 * seconds. For global shm, everybody has rw access to it until the sessiond
692 * starts.
693 */
694static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
695{
696 int wait_shm_fd, ret;
697 mode_t mode;
698
699 /* Default permissions */
700 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
701
702 /* Change owner of the shm path */
703 if (global) {
704 ret = chown(shm_path, 0, 0);
705 if (ret < 0) {
706 if (errno != ENOENT) {
707 perror("chown wait shm");
708 goto error;
709 }
710 }
711
712 /*
713 * If global session daemon, any application can register so the shm
714 * needs to be set in read-only mode for others.
715 */
716 mode |= S_IROTH;
717 } else {
718 ret = chown(shm_path, getuid(), getgid());
719 if (ret < 0) {
720 if (errno != ENOENT) {
721 perror("chown wait shm");
722 goto error;
723 }
724 }
725 }
726
727 /*
728 * Set permissions to the shm even if we did not create the shm.
729 */
730 ret = chmod(shm_path, mode);
731 if (ret < 0) {
732 if (errno != ENOENT) {
733 perror("chmod wait shm");
734 goto error;
735 }
736 }
737
738 /*
739 * We're alone in a child process, so we can modify the process-wide
740 * umask.
741 */
742 umask(~mode);
743
744 /*
745 * Try creating shm (or get rw access). We don't do an exclusive open,
746 * because we allow other processes to create+ftruncate it concurrently.
747 */
748 wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode);
749 if (wait_shm_fd < 0) {
750 perror("shm_open wait shm");
751 goto error;
752 }
753
754 ret = ftruncate(wait_shm_fd, mmap_size);
755 if (ret < 0) {
756 perror("ftruncate wait shm");
757 exit(EXIT_FAILURE);
758 }
759
760 ret = fchmod(wait_shm_fd, mode);
761 if (ret < 0) {
762 perror("fchmod");
763 exit(EXIT_FAILURE);
764 }
765
766 printf("Got the wait shm fd %d\n", wait_shm_fd);
767
768 return wait_shm_fd;
769
770error:
771 printf("Failing to get the wait shm fd\n");
772
773 return -1;
774}
775
776int update_futex(int fd, int active)
777{
778 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
779 char *wait_shm_mmap;
780 int ret;
781
782 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
783 MAP_SHARED, fd, 0);
784 if (wait_shm_mmap == MAP_FAILED) {
785 perror("mmap");
786 goto error;
787 }
788
789 if (active) {
790 uatomic_set((int32_t *) wait_shm_mmap, 1);
791 futex_async((int32_t *) wait_shm_mmap, FUTEX_WAKE,
792 INT_MAX, NULL, NULL, 0);
793 } else {
794 uatomic_set((int32_t *) wait_shm_mmap, 0);
795 }
796 ret = munmap(wait_shm_mmap, mmap_size);
797 if (ret) {
798 perror("Error unmapping wait shm");
799 goto error;
800 }
801 return 0;
802error:
803 return -1;
804}
805
806/*
807 * Set open files limit to unlimited. This daemon can open a large number of
808 * file descriptors in order to consumer multiple kernel traces.
809 */
810static void set_ulimit(void)
811{
812 int ret;
813 struct rlimit lim;
814
815 /*
816 * If not root, we cannot increase our max open files. But our
817 * scope is then limited to processes from a single user.
818 */
819 if (getuid() != 0)
820 return;
821 /* The kernel does not allowed an infinite limit for open files */
822 lim.rlim_cur = 65535;
823 lim.rlim_max = 65535;
824
825 ret = setrlimit(RLIMIT_NOFILE, &lim);
826 if (ret < 0) {
827 perror("failed to set open files limit");
828 }
829}
830
831/*
832 * Usage:
833 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
834 */
835int main(int argc, const char **argv)
836{
837 const char *home_dir;
838 int ret, wait_shm_fd;
839 struct sigaction act;
fb50c39d 840 mode_t old_umask = 0;
b0a99af4
MD
841 const char *outputpath;
842 const char **event_names;
843 unsigned int nr_events;
844
845 if (argc < 2) {
846 printf("Usage:\n");
847 printf("%s outputpath event_name1 event_name2 ...\n",
848 argv[0]);
849 exit(-1);
850 }
851 outputpath = argv[1];
852 event_names = &argv[2];
853 nr_events = argc - 2;
854
855 set_ulimit();
856
857 /* Ignore sigpipe */
858 memset(&act, 0, sizeof(act));
859 ret = sigemptyset(&act.sa_mask);
860 if (ret == -1) {
861 perror("sigemptyset");
862 return -1;
863 }
864
865 act.sa_handler = SIG_IGN;
866 ret = sigaction(SIGPIPE, &act, NULL);
867 if (ret == -1) {
868 perror("sigaction");
869 return -1;
870 }
871
872 /* Handle SIGTERM */
873 act.sa_handler = handle_signals;
874 ret = sigaction(SIGTERM, &act, NULL);
875 if (ret == -1) {
876 perror("sigaction");
877 return -1;
878 }
879 /* Handle SIGINT */
880 ret = sigaction(SIGINT, &act, NULL);
881 if (ret == -1) {
882 perror("sigaction");
883 return -1;
884 }
885
886 if (geteuid() == 0) {
887 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
888 if (ret && errno != EEXIST) {
889 perror("mkdir");
890 return -1;
891 }
892 wait_shm_fd = get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
893 sysconf(_SC_PAGE_SIZE), 1);
894 if (wait_shm_fd < 0) {
895 perror("global wait shm error");
896 return -1;
897 }
898 strcpy(apps_sock_path, DEFAULT_GLOBAL_APPS_UNIX_SOCK);
899 old_umask = umask(0);
900 } else {
901 snprintf(local_apps_wait_shm_path, PATH_MAX,
902 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
903 wait_shm_fd = get_wait_shm(local_apps_wait_shm_path,
904 sysconf(_SC_PAGE_SIZE), 0);
905 if (wait_shm_fd < 0) {
906 perror("local wait shm error");
907 return -1;
908 }
909 home_dir = (const char *) getenv("HOME");
910 if (!home_dir) {
911 perror("getenv error");
912 return -ENOENT;
913 }
914 snprintf(apps_sock_path, PATH_MAX,
915 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
916 }
917
918 ret = lttcomm_create_unix_sock(apps_sock_path);
919 if (ret < 0) {
920 perror("create error");
921 return ret;
922 }
923 apps_socket = ret;
924
925 if (getuid() == 0) {
926 /* File permission MUST be 666 */
927 ret = chmod(apps_sock_path,
928 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
929 if (ret < 0) {
930 printf("Set file permissions failed: %s\n", apps_sock_path);
931 perror("chmod");
932 goto end;
933 }
934 umask(old_umask);
935 }
936 ret = lttcomm_listen_unix_sock(apps_socket);
937 if (ret < 0) {
938 perror("listen error");
939 return ret;
940 }
941
942 /* wake up futexes */
943 ret = update_futex(wait_shm_fd, 1);
944 if (ret) {
945 fprintf(stderr, "Error wakeup futex\n");
946 return -1;
947 }
948
949 for (;;) {
950 int sock;
951 ssize_t len;
952 struct {
953 uint32_t major;
954 uint32_t minor;
955 pid_t pid;
956 pid_t ppid;
957 uid_t uid;
958 gid_t gid;
959 char name[16]; /* Process name */
960 } reg_msg;
961 char bufname[17];
962
963 if (quit_program)
964 break;
965
966 printf("Accepting application registration\n");
967 sock = lttcomm_accept_unix_sock(apps_socket);
968 if (sock < 0) {
969 perror("accept error");
970 goto end;
971 }
972
973 /*
974 * Basic recv here to handle the very simple data
975 * that the libust send to register (reg_msg).
976 */
977 len = lttcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
978 if (len < 0 || len != sizeof(reg_msg)) {
979 perror("lttcomm_recv_unix_sock");
980 continue;
981 }
982 memcpy(bufname, reg_msg.name, 16);
983 bufname[16] = '\0';
984 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
985 bufname, reg_msg.pid, reg_msg.ppid, reg_msg.uid,
986 reg_msg.gid, reg_msg.major, reg_msg.minor);
987 ret = send_app_msgs(sock, outputpath, nr_events, event_names);
988 if (ret) {
989 printf("Error in send_app_msgs.\n");
990 sleep(1);
991 }
992 close(sock);
993 }
994
995end:
996 printf("quitting.\n");
997 /* Let applications know we are not responding anymore */
998 ret = update_futex(wait_shm_fd, 0);
999 if (ret) {
1000 fprintf(stderr, "Error wakeup futex\n");
1001 return -1;
1002 }
1003
1004 return 0;
1005}
This page took 0.059368 seconds and 4 git commands to generate.