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