c437216068c137177c135f07ec61dc52ead067a1
[lttng-tools.git] / tests / regression / tools / notification / consumer_testpoints.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/compat/getenv.h>
9 #include <common/consumer/consumer.h>
10 #include <common/pipe.h>
11 #include <common/error.h>
12 #include <unistd.h>
13 #include <stdbool.h>
14 #include <lttng/constant.h>
15 #include <fcntl.h>
16 #include <dlfcn.h>
17 #include <assert.h>
18 #include <stdio.h>
19
20 static char *pause_pipe_path;
21 static struct lttng_pipe *pause_pipe;
22 static int *data_consumption_state;
23 static enum lttng_consumer_type (*lttng_consumer_get_type)(void);
24
25 int lttng_opt_verbose;
26 int lttng_opt_mi;
27 int lttng_opt_quiet;
28
29 static
30 void __attribute__((destructor)) pause_pipe_fini(void)
31 {
32 int ret;
33
34 if (pause_pipe_path) {
35 ret = unlink(pause_pipe_path);
36 if (ret) {
37 PERROR("unlink pause pipe");
38 }
39 }
40
41 free(pause_pipe_path);
42 lttng_pipe_destroy(pause_pipe);
43 }
44
45 /*
46 * We use this testpoint, invoked at the start of the consumerd's data handling
47 * thread to create a named pipe/FIFO which a test application can use to either
48 * pause or resume the consumption of data.
49 */
50 int __testpoint_consumerd_thread_data(void)
51 {
52 int ret = 0;
53 const char *pause_pipe_path_prefix, *domain;
54
55 pause_pipe_path_prefix = lttng_secure_getenv(
56 "CONSUMER_PAUSE_PIPE_PATH");
57 if (!pause_pipe_path_prefix) {
58 ret = -1;
59 goto end;
60 }
61
62 /*
63 * These symbols are exclusive to the consumerd process, hence we can't
64 * rely on their presence in the sessiond. Not looking-up these symbols
65 * dynamically would not allow this shared object to be LD_PRELOAD-ed
66 * when launching the session daemon.
67 */
68 data_consumption_state = dlsym(NULL, "data_consumption_paused");
69 assert(data_consumption_state);
70 lttng_consumer_get_type = dlsym(NULL, "lttng_consumer_get_type");
71 assert(lttng_consumer_get_type);
72
73 switch (lttng_consumer_get_type()) {
74 case LTTNG_CONSUMER_KERNEL:
75 domain = "kernel";
76 break;
77 case LTTNG_CONSUMER32_UST:
78 domain = "ust32";
79 break;
80 case LTTNG_CONSUMER64_UST:
81 domain = "ust64";
82 break;
83 default:
84 abort();
85 }
86
87 ret = asprintf(&pause_pipe_path, "%s-%s", pause_pipe_path_prefix,
88 domain);
89 if (ret < 1) {
90 ERR("Failed to allocate pause pipe path");
91 goto end;
92 }
93
94 DBG("Creating pause pipe at %s", pause_pipe_path);
95 pause_pipe = lttng_pipe_named_open(pause_pipe_path,
96 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, O_NONBLOCK);
97 if (!pause_pipe) {
98 ERR("Failed to create pause pipe at %s", pause_pipe_path);
99 ret = -1;
100 goto end;
101 }
102
103 /* Only the read end of the pipe is useful to us. */
104 ret = lttng_pipe_write_close(pause_pipe);
105 end:
106 return ret;
107 }
108
109 int __testpoint_consumerd_thread_data_poll(void)
110 {
111 int ret = 0;
112 uint8_t value;
113 bool value_read = false;
114
115 if (!pause_pipe) {
116 ret = -1;
117 goto end;
118 }
119
120 /* Purge pipe and only consider the freshest value. */
121 do {
122 ret = lttng_pipe_read(pause_pipe, &value, sizeof(value));
123 if (ret == sizeof(value)) {
124 value_read = true;
125 }
126 } while (ret == sizeof(value));
127
128 ret = (errno == EAGAIN) ? 0 : -errno;
129
130 if (value_read) {
131 *data_consumption_state = !!value;
132 DBG("Message received on pause pipe: %s data consumption",
133 *data_consumption_state ? "paused" : "resumed");
134 }
135 end:
136 return ret;
137 }
This page took 0.042968 seconds and 3 git commands to generate.