| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #include <common/compat/getenv.h> |
| 19 | #include <common/consumer/consumer.h> |
| 20 | #include <common/pipe.h> |
| 21 | #include <common/error.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <lttng/constant.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <dlfcn.h> |
| 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | static char *pause_pipe_path; |
| 31 | static struct lttng_pipe *pause_pipe; |
| 32 | static int *data_consumption_state; |
| 33 | static enum lttng_consumer_type (*lttng_consumer_get_type)(void); |
| 34 | |
| 35 | int lttng_opt_verbose; |
| 36 | int lttng_opt_mi; |
| 37 | int lttng_opt_quiet; |
| 38 | |
| 39 | static |
| 40 | void __attribute__((destructor)) pause_pipe_fini(void) |
| 41 | { |
| 42 | int ret; |
| 43 | |
| 44 | if (pause_pipe_path) { |
| 45 | ret = unlink(pause_pipe_path); |
| 46 | if (ret) { |
| 47 | PERROR("unlink pause pipe"); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | free(pause_pipe_path); |
| 52 | lttng_pipe_destroy(pause_pipe); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * We use this testpoint, invoked at the start of the consumerd's data handling |
| 57 | * thread to create a named pipe/FIFO which a test application can use to either |
| 58 | * pause or resume the consumption of data. |
| 59 | */ |
| 60 | int __testpoint_consumerd_thread_data(void) |
| 61 | { |
| 62 | int ret = 0; |
| 63 | const char *pause_pipe_path_prefix, *domain; |
| 64 | |
| 65 | pause_pipe_path_prefix = lttng_secure_getenv( |
| 66 | "CONSUMER_PAUSE_PIPE_PATH"); |
| 67 | if (!pause_pipe_path_prefix) { |
| 68 | ret = -1; |
| 69 | goto end; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * These symbols are exclusive to the consumerd process, hence we can't |
| 74 | * rely on their presence in the sessiond. Not looking-up these symbols |
| 75 | * dynamically would not allow this shared object to be LD_PRELOAD-ed |
| 76 | * when launching the session daemon. |
| 77 | */ |
| 78 | data_consumption_state = dlsym(NULL, "data_consumption_paused"); |
| 79 | assert(data_consumption_state); |
| 80 | lttng_consumer_get_type = dlsym(NULL, "lttng_consumer_get_type"); |
| 81 | assert(lttng_consumer_get_type); |
| 82 | |
| 83 | switch (lttng_consumer_get_type()) { |
| 84 | case LTTNG_CONSUMER_KERNEL: |
| 85 | domain = "kernel"; |
| 86 | break; |
| 87 | case LTTNG_CONSUMER32_UST: |
| 88 | domain = "ust32"; |
| 89 | break; |
| 90 | case LTTNG_CONSUMER64_UST: |
| 91 | domain = "ust64"; |
| 92 | break; |
| 93 | default: |
| 94 | abort(); |
| 95 | } |
| 96 | |
| 97 | ret = asprintf(&pause_pipe_path, "%s-%s", pause_pipe_path_prefix, |
| 98 | domain); |
| 99 | if (ret < 1) { |
| 100 | ERR("Failed to allocate pause pipe path"); |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | DBG("Creating pause pipe at %s", pause_pipe_path); |
| 105 | pause_pipe = lttng_pipe_named_open(pause_pipe_path, |
| 106 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, O_NONBLOCK); |
| 107 | if (!pause_pipe) { |
| 108 | ERR("Failed to create pause pipe at %s", pause_pipe_path); |
| 109 | ret = -1; |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | /* Only the read end of the pipe is useful to us. */ |
| 114 | ret = lttng_pipe_write_close(pause_pipe); |
| 115 | end: |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | int __testpoint_consumerd_thread_data_poll(void) |
| 120 | { |
| 121 | int ret = 0; |
| 122 | uint8_t value; |
| 123 | bool value_read = false; |
| 124 | |
| 125 | if (!pause_pipe) { |
| 126 | ret = -1; |
| 127 | goto end; |
| 128 | } |
| 129 | |
| 130 | /* Purge pipe and only consider the freshest value. */ |
| 131 | do { |
| 132 | ret = lttng_pipe_read(pause_pipe, &value, sizeof(value)); |
| 133 | if (ret == sizeof(value)) { |
| 134 | value_read = true; |
| 135 | } |
| 136 | } while (ret == sizeof(value)); |
| 137 | |
| 138 | ret = (errno == EAGAIN) ? 0 : -errno; |
| 139 | |
| 140 | if (value_read) { |
| 141 | *data_consumption_state = !!value; |
| 142 | DBG("Message received on pause pipe: %s data consumption", |
| 143 | *data_consumption_state ? "paused" : "resumed"); |
| 144 | } |
| 145 | end: |
| 146 | return ret; |
| 147 | } |