| 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 | #define _GNU_SOURCE |
| 18 | #include <assert.h> |
| 19 | |
| 20 | #include <common/common.h> |
| 21 | #include <common/utils.h> |
| 22 | |
| 23 | #include "fd-limit.h" |
| 24 | #include "lttng-sessiond.h" |
| 25 | #include "ust-thread.h" |
| 26 | #include "health-sessiond.h" |
| 27 | |
| 28 | /* |
| 29 | * This thread manage application notify communication. |
| 30 | */ |
| 31 | void *ust_thread_manage_notify(void *data) |
| 32 | { |
| 33 | int i, ret, pollfd, err = -1; |
| 34 | ssize_t size_ret; |
| 35 | uint32_t revents, nb_fd; |
| 36 | struct lttng_poll_event events; |
| 37 | |
| 38 | DBG("[ust-thread] Manage application notify command"); |
| 39 | |
| 40 | rcu_register_thread(); |
| 41 | rcu_thread_online(); |
| 42 | |
| 43 | health_register(health_sessiond, |
| 44 | HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY); |
| 45 | |
| 46 | health_code_update(); |
| 47 | |
| 48 | ret = sessiond_set_thread_pollset(&events, 2); |
| 49 | if (ret < 0) { |
| 50 | goto error_poll_create; |
| 51 | } |
| 52 | |
| 53 | /* Add notify pipe to the pollset. */ |
| 54 | ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR); |
| 55 | if (ret < 0) { |
| 56 | goto error; |
| 57 | } |
| 58 | |
| 59 | health_code_update(); |
| 60 | |
| 61 | while (1) { |
| 62 | DBG3("[ust-thread] Manage notify polling on %d fds", |
| 63 | LTTNG_POLL_GETNB(&events)); |
| 64 | |
| 65 | /* Inifinite blocking call, waiting for transmission */ |
| 66 | restart: |
| 67 | health_poll_entry(); |
| 68 | ret = lttng_poll_wait(&events, -1); |
| 69 | health_poll_exit(); |
| 70 | if (ret < 0) { |
| 71 | /* |
| 72 | * Restart interrupted system call. |
| 73 | */ |
| 74 | if (errno == EINTR) { |
| 75 | goto restart; |
| 76 | } |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | nb_fd = ret; |
| 81 | |
| 82 | for (i = 0; i < nb_fd; i++) { |
| 83 | health_code_update(); |
| 84 | |
| 85 | /* Fetch once the poll data */ |
| 86 | revents = LTTNG_POLL_GETEV(&events, i); |
| 87 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 88 | |
| 89 | /* Thread quit pipe has been closed. Killing thread. */ |
| 90 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 91 | if (ret) { |
| 92 | err = 0; |
| 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | /* Inspect the apps cmd pipe */ |
| 97 | if (pollfd == apps_cmd_notify_pipe[0]) { |
| 98 | int sock; |
| 99 | |
| 100 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 101 | ERR("Apps notify command pipe error"); |
| 102 | goto error; |
| 103 | } else if (!(revents & LPOLLIN)) { |
| 104 | /* No POLLIN and not a catched error, stop the thread. */ |
| 105 | ERR("Notify command pipe failed. revent: %u", revents); |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | /* Get socket from dispatch thread. */ |
| 110 | size_ret = lttng_read(apps_cmd_notify_pipe[0], |
| 111 | &sock, sizeof(sock)); |
| 112 | if (size_ret < sizeof(sock)) { |
| 113 | PERROR("read apps notify pipe"); |
| 114 | goto error; |
| 115 | } |
| 116 | health_code_update(); |
| 117 | |
| 118 | ret = lttng_poll_add(&events, sock, |
| 119 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 120 | if (ret < 0) { |
| 121 | /* |
| 122 | * It's possible we've reached the max poll fd allowed. |
| 123 | * Let's close the socket but continue normal execution. |
| 124 | */ |
| 125 | ret = close(sock); |
| 126 | if (ret) { |
| 127 | PERROR("close notify socket %d", sock); |
| 128 | } |
| 129 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 130 | continue; |
| 131 | } |
| 132 | DBG3("UST thread notify added sock %d to pollset", sock); |
| 133 | } else { |
| 134 | /* |
| 135 | * At this point, we know that a registered application |
| 136 | * triggered the event. |
| 137 | */ |
| 138 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 139 | /* Removing from the poll set */ |
| 140 | ret = lttng_poll_del(&events, pollfd); |
| 141 | if (ret < 0) { |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | /* The socket is closed after a grace period here. */ |
| 146 | ust_app_notify_sock_unregister(pollfd); |
| 147 | } else if (revents & (LPOLLIN | LPOLLPRI)) { |
| 148 | ret = ust_app_recv_notify(pollfd); |
| 149 | if (ret < 0) { |
| 150 | /* |
| 151 | * If the notification failed either the application is |
| 152 | * dead or an internal error happened. In both cases, |
| 153 | * we can only continue here. If the application is |
| 154 | * dead, an unregistration will follow or else the |
| 155 | * application will notice that we are not responding |
| 156 | * on that socket and will close it. |
| 157 | */ |
| 158 | continue; |
| 159 | } |
| 160 | } else { |
| 161 | ERR("Unknown poll events %u for sock %d", revents, pollfd); |
| 162 | continue; |
| 163 | } |
| 164 | health_code_update(); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | exit: |
| 170 | error: |
| 171 | lttng_poll_clean(&events); |
| 172 | error_poll_create: |
| 173 | utils_close_pipe(apps_cmd_notify_pipe); |
| 174 | apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1; |
| 175 | DBG("Application notify communication apps thread cleanup complete"); |
| 176 | if (err) { |
| 177 | health_error(); |
| 178 | ERR("Health error occurred in %s", __func__); |
| 179 | } |
| 180 | health_unregister(health_sessiond); |
| 181 | rcu_thread_offline(); |
| 182 | rcu_unregister_thread(); |
| 183 | return NULL; |
| 184 | } |