| 1 | /* |
| 2 | * lttng-ust-comm.c |
| 3 | * |
| 4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; only |
| 10 | * version 2.1 of the License. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <unistd.h> |
| 25 | #include <errno.h> |
| 26 | #include <ust/lttng-ust-abi.h> |
| 27 | #include <lttng-ust-comm.h> |
| 28 | #include <ust/usterr-signal-safe.h> |
| 29 | #include <pthread.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | /* |
| 33 | * communication thread mutex. Held when handling a command, also held |
| 34 | * by fork() to deal with removal of threads, and by exit path. |
| 35 | */ |
| 36 | static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 37 | |
| 38 | /* Should the ust comm thread quit ? */ |
| 39 | static int lttng_ust_comm_should_quit; |
| 40 | |
| 41 | /* |
| 42 | * Info about socket and associated listener thread. |
| 43 | */ |
| 44 | struct sock_info { |
| 45 | char sock_path[PATH_MAX]; |
| 46 | int socket; |
| 47 | pthread_t ust_listener; /* listener thread */ |
| 48 | int root_handle; |
| 49 | }; |
| 50 | |
| 51 | /* Socket from app (connect) to session daemon (listen) for communication */ |
| 52 | struct sock_info global_apps = { |
| 53 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, |
| 54 | .socket = -1, |
| 55 | .root_handle = -1, |
| 56 | }; |
| 57 | |
| 58 | /* TODO: allow global_apps_sock_path override */ |
| 59 | |
| 60 | struct sock_info local_apps = { |
| 61 | .socket = -1, |
| 62 | .root_handle = -1, |
| 63 | }; |
| 64 | |
| 65 | static |
| 66 | int setup_local_apps_socket(void) |
| 67 | { |
| 68 | const char *home_dir; |
| 69 | |
| 70 | home_dir = (const char *) getenv("HOME"); |
| 71 | if (!home_dir) |
| 72 | return -ENOENT; |
| 73 | snprintf(local_apps.sock_path, PATH_MAX, |
| 74 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static |
| 79 | int register_app_to_sessiond(int socket) |
| 80 | { |
| 81 | ssize_t ret; |
| 82 | struct { |
| 83 | uint32_t major; |
| 84 | uint32_t minor; |
| 85 | pid_t pid; |
| 86 | uid_t uid; |
| 87 | } reg_msg; |
| 88 | |
| 89 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
| 90 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; |
| 91 | reg_msg.pid = getpid(); |
| 92 | reg_msg.uid = getuid(); |
| 93 | |
| 94 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); |
| 95 | if (ret >= 0 && ret != sizeof(reg_msg)) |
| 96 | return -EIO; |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static |
| 101 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
| 102 | { |
| 103 | ssize_t len; |
| 104 | |
| 105 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
| 106 | switch (len) { |
| 107 | case sizeof(*lur): |
| 108 | DBG("message successfully sent"); |
| 109 | return 0; |
| 110 | case -1: |
| 111 | if (errno == ECONNRESET) { |
| 112 | printf("remote end closed connection\n"); |
| 113 | return 0; |
| 114 | } |
| 115 | return -1; |
| 116 | default: |
| 117 | printf("incorrect message size: %zd\n", len); |
| 118 | return -1; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static |
| 123 | int handle_message(int sock, struct lttcomm_ust_msg *lum) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | const struct objd_ops *ops; |
| 127 | struct lttcomm_ust_reply lur; |
| 128 | |
| 129 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 130 | |
| 131 | memset(&lur, 0, sizeof(lur)); |
| 132 | |
| 133 | if (lttng_ust_comm_should_quit) { |
| 134 | ret = -EPERM; |
| 135 | goto end; |
| 136 | } |
| 137 | |
| 138 | ops = objd_ops(lum->handle); |
| 139 | if (!ops) { |
| 140 | ret = -ENOENT; |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | switch (lum->cmd) { |
| 145 | case LTTNG_UST_RELEASE: |
| 146 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 147 | ret = -EPERM; |
| 148 | else |
| 149 | ret = objd_unref(lum->handle); |
| 150 | break; |
| 151 | default: |
| 152 | if (ops->cmd) |
| 153 | ret = ops->cmd(lum->handle, lum->cmd, |
| 154 | (unsigned long) &lum->u); |
| 155 | else |
| 156 | ret = -ENOSYS; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | end: |
| 161 | lur.handle = lum->handle; |
| 162 | lur.cmd = lum->cmd; |
| 163 | lur.ret_val = ret; |
| 164 | if (ret >= 0) { |
| 165 | lur.ret_code = LTTCOMM_OK; |
| 166 | } else { |
| 167 | lur.ret_code = LTTCOMM_SESSION_FAIL; |
| 168 | } |
| 169 | ret = send_reply(sock, &lur); |
| 170 | |
| 171 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | void cleanup_sock_info(struct sock_info *sock_info) |
| 177 | { |
| 178 | int ret; |
| 179 | |
| 180 | if (sock_info->socket != -1) { |
| 181 | ret = close(sock_info->socket); |
| 182 | if (ret) { |
| 183 | ERR("Error closing local apps socket"); |
| 184 | } |
| 185 | sock_info->socket = -1; |
| 186 | } |
| 187 | if (sock_info->root_handle != -1) { |
| 188 | ret = objd_unref(sock_info->root_handle); |
| 189 | if (ret) { |
| 190 | ERR("Error unref root handle"); |
| 191 | } |
| 192 | sock_info->root_handle = -1; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * This thread does not allocate any resource, except within |
| 198 | * handle_message, within mutex protection. This mutex protects against |
| 199 | * fork and exit. |
| 200 | * The other moment it allocates resources is at socket connexion, which |
| 201 | * is also protected by the mutex. |
| 202 | */ |
| 203 | static |
| 204 | void *ust_listener_thread(void *arg) |
| 205 | { |
| 206 | struct sock_info *sock_info = arg; |
| 207 | int sock, ret; |
| 208 | |
| 209 | /* Restart trying to connect to the session daemon */ |
| 210 | restart: |
| 211 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 212 | |
| 213 | if (lttng_ust_comm_should_quit) { |
| 214 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 215 | goto quit; |
| 216 | } |
| 217 | |
| 218 | if (sock_info->socket != -1) { |
| 219 | ret = close(sock_info->socket); |
| 220 | if (ret) { |
| 221 | ERR("Error closing local apps socket"); |
| 222 | } |
| 223 | sock_info->socket = -1; |
| 224 | } |
| 225 | |
| 226 | /* Check for sessiond availability with pipe TODO */ |
| 227 | |
| 228 | /* Register */ |
| 229 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
| 230 | if (ret < 0) { |
| 231 | ERR("Error connecting to global apps socket"); |
| 232 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 233 | sleep(5); |
| 234 | goto restart; |
| 235 | } |
| 236 | |
| 237 | sock_info->socket = sock = ret; |
| 238 | |
| 239 | /* |
| 240 | * Create only one root handle per listener thread for the whole |
| 241 | * process lifetime. |
| 242 | */ |
| 243 | if (sock_info->root_handle == -1) { |
| 244 | ret = lttng_abi_create_root_handle(); |
| 245 | if (ret) { |
| 246 | ERR("Error creating root handle"); |
| 247 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 248 | goto quit; |
| 249 | } |
| 250 | sock_info->root_handle = ret; |
| 251 | } |
| 252 | |
| 253 | ret = register_app_to_sessiond(sock); |
| 254 | if (ret < 0) { |
| 255 | ERR("Error registering app to local apps socket"); |
| 256 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 257 | sleep(5); |
| 258 | goto restart; |
| 259 | } |
| 260 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 261 | |
| 262 | for (;;) { |
| 263 | ssize_t len; |
| 264 | struct lttcomm_ust_msg lum; |
| 265 | |
| 266 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
| 267 | switch (len) { |
| 268 | case 0: /* orderly shutdown */ |
| 269 | DBG("ltt-sessiond has performed an orderly shutdown\n"); |
| 270 | goto end; |
| 271 | case sizeof(lum): |
| 272 | DBG("message received\n"); |
| 273 | ret = handle_message(sock, &lum); |
| 274 | if (ret < 0) { |
| 275 | ERR("Error handling message\n"); |
| 276 | } |
| 277 | continue; |
| 278 | case -1: |
| 279 | if (errno == ECONNRESET) { |
| 280 | ERR("remote end closed connection\n"); |
| 281 | goto end; |
| 282 | } |
| 283 | goto end; |
| 284 | default: |
| 285 | ERR("incorrect message size: %zd\n", len); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | end: |
| 291 | goto restart; /* try to reconnect */ |
| 292 | quit: |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* |
| 298 | * sessiond monitoring thread: monitor presence of global and per-user |
| 299 | * sessiond by polling the application common named pipe. |
| 300 | */ |
| 301 | /* TODO */ |
| 302 | |
| 303 | void __attribute__((constructor)) lttng_ust_comm_init(void) |
| 304 | { |
| 305 | int ret; |
| 306 | |
| 307 | init_usterr(); |
| 308 | |
| 309 | ret = setup_local_apps_socket(); |
| 310 | if (ret) { |
| 311 | ERR("Error setting up to local apps socket"); |
| 312 | } |
| 313 | #if 0 |
| 314 | ret = pthread_create(&global_apps.ust_listener, NULL, |
| 315 | ust_listener_thread, &global_apps); |
| 316 | #endif //0 |
| 317 | ret = pthread_create(&local_apps.ust_listener, NULL, |
| 318 | ust_listener_thread, &local_apps); |
| 319 | } |
| 320 | |
| 321 | void __attribute__((destructor)) lttng_ust_comm_exit(void) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | /* |
| 326 | * Using pthread_cancel here because: |
| 327 | * A) we don't want to hang application teardown. |
| 328 | * B) the thread is not allocating any resource. |
| 329 | */ |
| 330 | |
| 331 | /* |
| 332 | * Require the communication thread to quit. Synchronize with |
| 333 | * mutexes to ensure it is not in a mutex critical section when |
| 334 | * pthread_cancel is later called. |
| 335 | */ |
| 336 | pthread_mutex_lock(<tng_ust_comm_mutex); |
| 337 | lttng_ust_comm_should_quit = 1; |
| 338 | pthread_mutex_unlock(<tng_ust_comm_mutex); |
| 339 | |
| 340 | #if 0 |
| 341 | ret = pthread_cancel(global_apps.ust_listener); |
| 342 | if (ret) { |
| 343 | ERR("Error cancelling global ust listener thread"); |
| 344 | } |
| 345 | #endif //0 |
| 346 | |
| 347 | cleanup_sock_info(&global_apps); |
| 348 | |
| 349 | ret = pthread_cancel(local_apps.ust_listener); |
| 350 | if (ret) { |
| 351 | ERR("Error cancelling local ust listener thread"); |
| 352 | } |
| 353 | |
| 354 | cleanup_sock_info(&local_apps); |
| 355 | |
| 356 | lttng_ust_abi_exit(); |
| 357 | ltt_events_exit(); |
| 358 | } |