| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@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 | #define _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <common/common.h> |
| 22 | #include <common/utils.h> |
| 23 | |
| 24 | #include "fd-limit.h" |
| 25 | #include "lttng-sessiond.h" |
| 26 | #include "ust-thread.h" |
| 27 | #include "health-sessiond.h" |
| 28 | #include "testpoint.h" |
| 29 | #include "utils.h" |
| 30 | #include "thread.h" |
| 31 | |
| 32 | struct thread_notifiers { |
| 33 | struct lttng_pipe *quit_pipe; |
| 34 | int apps_cmd_notify_pipe_read_fd; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * This thread manage application notify communication. |
| 39 | */ |
| 40 | static void *thread_application_notification(void *data) |
| 41 | { |
| 42 | int i, ret, pollfd, err = -1; |
| 43 | ssize_t size_ret; |
| 44 | uint32_t revents, nb_fd; |
| 45 | struct lttng_poll_event events; |
| 46 | struct thread_notifiers *notifiers = data; |
| 47 | const int quit_pipe_read_fd = lttng_pipe_get_readfd(notifiers->quit_pipe); |
| 48 | |
| 49 | DBG("[ust-thread] Manage application notify command"); |
| 50 | |
| 51 | rcu_register_thread(); |
| 52 | rcu_thread_online(); |
| 53 | |
| 54 | health_register(health_sessiond, |
| 55 | HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY); |
| 56 | |
| 57 | if (testpoint(sessiond_thread_app_manage_notify)) { |
| 58 | goto error_testpoint; |
| 59 | } |
| 60 | |
| 61 | health_code_update(); |
| 62 | |
| 63 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 64 | if (ret < 0) { |
| 65 | goto error_poll_create; |
| 66 | } |
| 67 | |
| 68 | /* Add notify pipe to the pollset. */ |
| 69 | ret = lttng_poll_add(&events, notifiers->apps_cmd_notify_pipe_read_fd, |
| 70 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 71 | if (ret < 0) { |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | ret = lttng_poll_add(&events, quit_pipe_read_fd, |
| 76 | LPOLLIN | LPOLLERR); |
| 77 | if (ret < 0) { |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | health_code_update(); |
| 82 | |
| 83 | while (1) { |
| 84 | DBG3("[ust-thread] Manage notify polling"); |
| 85 | |
| 86 | /* Inifinite blocking call, waiting for transmission */ |
| 87 | restart: |
| 88 | health_poll_entry(); |
| 89 | ret = lttng_poll_wait(&events, -1); |
| 90 | DBG3("[ust-thread] Manage notify return from poll on %d fds", |
| 91 | LTTNG_POLL_GETNB(&events)); |
| 92 | health_poll_exit(); |
| 93 | if (ret < 0) { |
| 94 | /* |
| 95 | * Restart interrupted system call. |
| 96 | */ |
| 97 | if (errno == EINTR) { |
| 98 | goto restart; |
| 99 | } |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | nb_fd = ret; |
| 104 | |
| 105 | for (i = 0; i < nb_fd; i++) { |
| 106 | health_code_update(); |
| 107 | |
| 108 | /* Fetch once the poll data */ |
| 109 | revents = LTTNG_POLL_GETEV(&events, i); |
| 110 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 111 | |
| 112 | if (!revents) { |
| 113 | /* No activity for this FD (poll implementation). */ |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | /* Thread quit pipe has been closed. Killing thread. */ |
| 118 | if (pollfd == quit_pipe_read_fd) { |
| 119 | err = 0; |
| 120 | goto exit; |
| 121 | } else if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) { |
| 122 | /* Inspect the apps cmd pipe */ |
| 123 | int sock; |
| 124 | |
| 125 | if (revents & LPOLLIN) { |
| 126 | /* Get socket from dispatch thread. */ |
| 127 | size_ret = lttng_read(notifiers->apps_cmd_notify_pipe_read_fd, |
| 128 | &sock, sizeof(sock)); |
| 129 | if (size_ret < sizeof(sock)) { |
| 130 | PERROR("read apps notify pipe"); |
| 131 | goto error; |
| 132 | } |
| 133 | health_code_update(); |
| 134 | |
| 135 | ret = lttng_poll_add(&events, sock, |
| 136 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 137 | if (ret < 0) { |
| 138 | /* |
| 139 | * It's possible we've reached the max poll fd allowed. |
| 140 | * Let's close the socket but continue normal execution. |
| 141 | */ |
| 142 | ret = close(sock); |
| 143 | if (ret) { |
| 144 | PERROR("close notify socket %d", sock); |
| 145 | } |
| 146 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 147 | continue; |
| 148 | } |
| 149 | DBG3("UST thread notify added sock %d to pollset", sock); |
| 150 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 151 | ERR("Apps notify command pipe error"); |
| 152 | goto error; |
| 153 | } else { |
| 154 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 155 | goto error; |
| 156 | } |
| 157 | } else { |
| 158 | /* |
| 159 | * At this point, we know that a registered application |
| 160 | * triggered the event. |
| 161 | */ |
| 162 | if (revents & (LPOLLIN | LPOLLPRI)) { |
| 163 | ret = ust_app_recv_notify(pollfd); |
| 164 | if (ret < 0) { |
| 165 | /* Removing from the poll set */ |
| 166 | ret = lttng_poll_del(&events, pollfd); |
| 167 | if (ret < 0) { |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | /* The socket is closed after a grace period here. */ |
| 172 | ust_app_notify_sock_unregister(pollfd); |
| 173 | } |
| 174 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 175 | /* Removing from the poll set */ |
| 176 | ret = lttng_poll_del(&events, pollfd); |
| 177 | if (ret < 0) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | /* The socket is closed after a grace period here. */ |
| 182 | ust_app_notify_sock_unregister(pollfd); |
| 183 | } else { |
| 184 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 185 | goto error; |
| 186 | } |
| 187 | health_code_update(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | exit: |
| 193 | error: |
| 194 | lttng_poll_clean(&events); |
| 195 | error_poll_create: |
| 196 | error_testpoint: |
| 197 | |
| 198 | DBG("Application notify communication apps thread cleanup complete"); |
| 199 | if (err) { |
| 200 | health_error(); |
| 201 | ERR("Health error occurred in %s", __func__); |
| 202 | } |
| 203 | health_unregister(health_sessiond); |
| 204 | rcu_thread_offline(); |
| 205 | rcu_unregister_thread(); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | static bool shutdown_application_notification_thread(void *data) |
| 210 | { |
| 211 | struct thread_notifiers *notifiers = data; |
| 212 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); |
| 213 | |
| 214 | return notify_thread_pipe(write_fd) == 1; |
| 215 | } |
| 216 | |
| 217 | static void cleanup_application_notification_thread(void *data) |
| 218 | { |
| 219 | struct thread_notifiers *notifiers = data; |
| 220 | |
| 221 | lttng_pipe_destroy(notifiers->quit_pipe); |
| 222 | free(notifiers); |
| 223 | } |
| 224 | |
| 225 | bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd) |
| 226 | { |
| 227 | struct lttng_thread *thread; |
| 228 | struct thread_notifiers *notifiers; |
| 229 | struct lttng_pipe *quit_pipe; |
| 230 | |
| 231 | notifiers = zmalloc(sizeof(*notifiers)); |
| 232 | if (!notifiers) { |
| 233 | goto error; |
| 234 | } |
| 235 | notifiers->apps_cmd_notify_pipe_read_fd = apps_cmd_notify_pipe_read_fd; |
| 236 | |
| 237 | quit_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 238 | if (!quit_pipe) { |
| 239 | goto error; |
| 240 | } |
| 241 | notifiers->quit_pipe = quit_pipe; |
| 242 | |
| 243 | thread = lttng_thread_create("Application notification", |
| 244 | thread_application_notification, |
| 245 | shutdown_application_notification_thread, |
| 246 | cleanup_application_notification_thread, |
| 247 | notifiers); |
| 248 | if (!thread) { |
| 249 | goto error; |
| 250 | } |
| 251 | lttng_thread_put(thread); |
| 252 | return true; |
| 253 | error: |
| 254 | cleanup_application_notification_thread(notifiers); |
| 255 | return false; |
| 256 | } |