tests: trigger action firing policy
[lttng-tools.git] / tests / regression / tools / notification / consumer_testpoints.c
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
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
20static char *pause_pipe_path;
21static struct lttng_pipe *pause_pipe;
22static int *data_consumption_state;
23static enum lttng_consumer_type (*lttng_consumer_get_type)(void);
24
25int lttng_opt_verbose;
26int lttng_opt_mi;
27int lttng_opt_quiet;
28
29static
30void __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 */
8abfcbd1 50int __testpoint_consumerd_thread_data(void);
434f8068
JR
51int __testpoint_consumerd_thread_data(void)
52{
53 int ret = 0;
54 const char *pause_pipe_path_prefix, *domain;
55
56 pause_pipe_path_prefix = lttng_secure_getenv(
57 "CONSUMER_PAUSE_PIPE_PATH");
58 if (!pause_pipe_path_prefix) {
59 ret = -1;
60 goto end;
61 }
62
63 /*
64 * These symbols are exclusive to the consumerd process, hence we can't
65 * rely on their presence in the sessiond. Not looking-up these symbols
66 * dynamically would not allow this shared object to be LD_PRELOAD-ed
67 * when launching the session daemon.
68 */
69 data_consumption_state = dlsym(NULL, "data_consumption_paused");
70 assert(data_consumption_state);
71 lttng_consumer_get_type = dlsym(NULL, "lttng_consumer_get_type");
72 assert(lttng_consumer_get_type);
73
74 switch (lttng_consumer_get_type()) {
75 case LTTNG_CONSUMER_KERNEL:
76 domain = "kernel";
77 break;
78 case LTTNG_CONSUMER32_UST:
79 domain = "ust32";
80 break;
81 case LTTNG_CONSUMER64_UST:
82 domain = "ust64";
83 break;
84 default:
85 abort();
86 }
87
88 ret = asprintf(&pause_pipe_path, "%s-%s", pause_pipe_path_prefix,
89 domain);
90 if (ret < 1) {
91 ERR("Failed to allocate pause pipe path");
92 goto end;
93 }
94
95 DBG("Creating pause pipe at %s", pause_pipe_path);
96 pause_pipe = lttng_pipe_named_open(pause_pipe_path,
97 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, O_NONBLOCK);
98 if (!pause_pipe) {
99 ERR("Failed to create pause pipe at %s", pause_pipe_path);
100 ret = -1;
101 goto end;
102 }
103
104 /* Only the read end of the pipe is useful to us. */
105 ret = lttng_pipe_write_close(pause_pipe);
106end:
107 return ret;
108}
109
8abfcbd1 110int __testpoint_consumerd_thread_data_poll(void);
434f8068
JR
111int __testpoint_consumerd_thread_data_poll(void)
112{
113 int ret = 0;
114 uint8_t value;
115 bool value_read = false;
116
117 if (!pause_pipe) {
118 ret = -1;
119 goto end;
120 }
121
122 /* Purge pipe and only consider the freshest value. */
123 do {
124 ret = lttng_pipe_read(pause_pipe, &value, sizeof(value));
125 if (ret == sizeof(value)) {
126 value_read = true;
127 }
128 } while (ret == sizeof(value));
129
130 ret = (errno == EAGAIN) ? 0 : -errno;
131
132 if (value_read) {
133 *data_consumption_state = !!value;
134 DBG("Message received on pause pipe: %s data consumption",
135 *data_consumption_state ? "paused" : "resumed");
136 }
137end:
138 return ret;
139}
This page took 0.06212 seconds and 4 git commands to generate.