| 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 | #define _LGPL_SOURCE |
| 23 | #define _GNU_SOURCE |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
| 32 | #include <errno.h> |
| 33 | #include <pthread.h> |
| 34 | #include <semaphore.h> |
| 35 | #include <time.h> |
| 36 | #include <assert.h> |
| 37 | #include <signal.h> |
| 38 | #include <limits.h> |
| 39 | #include <urcu/uatomic.h> |
| 40 | #include <urcu/futex.h> |
| 41 | #include <urcu/compiler.h> |
| 42 | |
| 43 | #include <lttng/ust-events.h> |
| 44 | #include <lttng/ust-abi.h> |
| 45 | #include <lttng/ust.h> |
| 46 | #include <lttng/ust-error.h> |
| 47 | #include <lttng/ust-ctl.h> |
| 48 | #include <urcu/tls-compat.h> |
| 49 | #include <ust-comm.h> |
| 50 | #include <ust-fd.h> |
| 51 | #include <usterr-signal-safe.h> |
| 52 | #include <helper.h> |
| 53 | #include "tracepoint-internal.h" |
| 54 | #include "lttng-tracer-core.h" |
| 55 | #include "compat.h" |
| 56 | #include "../libringbuffer/rb-init.h" |
| 57 | #include "lttng-ust-statedump.h" |
| 58 | #include "clock.h" |
| 59 | #include "../libringbuffer/getcpu.h" |
| 60 | #include "getenv.h" |
| 61 | |
| 62 | /* |
| 63 | * Has lttng ust comm constructor been called ? |
| 64 | */ |
| 65 | static int initialized; |
| 66 | |
| 67 | /* |
| 68 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
| 69 | * Held when handling a command, also held by fork() to deal with |
| 70 | * removal of threads, and by exit path. |
| 71 | * |
| 72 | * The UST lock is the centralized mutex across UST tracing control and |
| 73 | * probe registration. |
| 74 | * |
| 75 | * ust_exit_mutex must never nest in ust_mutex. |
| 76 | * |
| 77 | * ust_fork_mutex must never nest in ust_mutex. |
| 78 | * |
| 79 | * ust_mutex_nest is a per-thread nesting counter, allowing the perf |
| 80 | * counter lazy initialization called by events within the statedump, |
| 81 | * which traces while the ust_mutex is held. |
| 82 | * |
| 83 | * ust_lock nests within the dynamic loader lock (within glibc) because |
| 84 | * it is taken within the library constructor. |
| 85 | */ |
| 86 | static pthread_mutex_t ust_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 87 | |
| 88 | /* Allow nesting the ust_mutex within the same thread. */ |
| 89 | static DEFINE_URCU_TLS(int, ust_mutex_nest); |
| 90 | |
| 91 | /* |
| 92 | * ust_exit_mutex protects thread_active variable wrt thread exit. It |
| 93 | * cannot be done by ust_mutex because pthread_cancel(), which takes an |
| 94 | * internal libc lock, cannot nest within ust_mutex. |
| 95 | * |
| 96 | * It never nests within a ust_mutex. |
| 97 | */ |
| 98 | static pthread_mutex_t ust_exit_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 99 | |
| 100 | /* |
| 101 | * ust_fork_mutex protects base address statedump tracing against forks. It |
| 102 | * prevents the dynamic loader lock to be taken (by base address statedump |
| 103 | * tracing) while a fork is happening, thus preventing deadlock issues with |
| 104 | * the dynamic loader lock. |
| 105 | */ |
| 106 | static pthread_mutex_t ust_fork_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 107 | |
| 108 | /* Should the ust comm thread quit ? */ |
| 109 | static int lttng_ust_comm_should_quit; |
| 110 | |
| 111 | /* |
| 112 | * This variable can be tested by applications to check whether |
| 113 | * lttng-ust is loaded. They simply have to define their own |
| 114 | * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the |
| 115 | * library constructor. |
| 116 | */ |
| 117 | int lttng_ust_loaded __attribute__((weak)); |
| 118 | |
| 119 | /* |
| 120 | * Return 0 on success, -1 if should quit. |
| 121 | * The lock is taken in both cases. |
| 122 | * Signal-safe. |
| 123 | */ |
| 124 | int ust_lock(void) |
| 125 | { |
| 126 | sigset_t sig_all_blocked, orig_mask; |
| 127 | int ret, oldstate; |
| 128 | |
| 129 | ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); |
| 130 | if (ret) { |
| 131 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 132 | } |
| 133 | if (oldstate != PTHREAD_CANCEL_ENABLE) { |
| 134 | ERR("pthread_setcancelstate: unexpected oldstate"); |
| 135 | } |
| 136 | sigfillset(&sig_all_blocked); |
| 137 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask); |
| 138 | if (ret) { |
| 139 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 140 | } |
| 141 | if (!URCU_TLS(ust_mutex_nest)++) |
| 142 | pthread_mutex_lock(&ust_mutex); |
| 143 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); |
| 144 | if (ret) { |
| 145 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 146 | } |
| 147 | if (lttng_ust_comm_should_quit) { |
| 148 | return -1; |
| 149 | } else { |
| 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * ust_lock_nocheck() can be used in constructors/destructors, because |
| 156 | * they are already nested within the dynamic loader lock, and therefore |
| 157 | * have exclusive access against execution of liblttng-ust destructor. |
| 158 | * Signal-safe. |
| 159 | */ |
| 160 | void ust_lock_nocheck(void) |
| 161 | { |
| 162 | sigset_t sig_all_blocked, orig_mask; |
| 163 | int ret, oldstate; |
| 164 | |
| 165 | ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); |
| 166 | if (ret) { |
| 167 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 168 | } |
| 169 | if (oldstate != PTHREAD_CANCEL_ENABLE) { |
| 170 | ERR("pthread_setcancelstate: unexpected oldstate"); |
| 171 | } |
| 172 | sigfillset(&sig_all_blocked); |
| 173 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask); |
| 174 | if (ret) { |
| 175 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 176 | } |
| 177 | if (!URCU_TLS(ust_mutex_nest)++) |
| 178 | pthread_mutex_lock(&ust_mutex); |
| 179 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); |
| 180 | if (ret) { |
| 181 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Signal-safe. |
| 187 | */ |
| 188 | void ust_unlock(void) |
| 189 | { |
| 190 | sigset_t sig_all_blocked, orig_mask; |
| 191 | int ret, oldstate; |
| 192 | |
| 193 | sigfillset(&sig_all_blocked); |
| 194 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask); |
| 195 | if (ret) { |
| 196 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 197 | } |
| 198 | if (!--URCU_TLS(ust_mutex_nest)) |
| 199 | pthread_mutex_unlock(&ust_mutex); |
| 200 | ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL); |
| 201 | if (ret) { |
| 202 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 203 | } |
| 204 | ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 205 | if (ret) { |
| 206 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 207 | } |
| 208 | if (oldstate != PTHREAD_CANCEL_DISABLE) { |
| 209 | ERR("pthread_setcancelstate: unexpected oldstate"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Wait for either of these before continuing to the main |
| 215 | * program: |
| 216 | * - the register_done message from sessiond daemon |
| 217 | * (will let the sessiond daemon enable sessions before main |
| 218 | * starts.) |
| 219 | * - sessiond daemon is not reachable. |
| 220 | * - timeout (ensuring applications are resilient to session |
| 221 | * daemon problems). |
| 222 | */ |
| 223 | static sem_t constructor_wait; |
| 224 | /* |
| 225 | * Doing this for both the global and local sessiond. |
| 226 | */ |
| 227 | static int sem_count = { 2 }; |
| 228 | |
| 229 | /* |
| 230 | * Counting nesting within lttng-ust. Used to ensure that calling fork() |
| 231 | * from liblttng-ust does not execute the pre/post fork handlers. |
| 232 | */ |
| 233 | static DEFINE_URCU_TLS(int, lttng_ust_nest_count); |
| 234 | |
| 235 | /* |
| 236 | * Info about socket and associated listener thread. |
| 237 | */ |
| 238 | struct sock_info { |
| 239 | const char *name; |
| 240 | pthread_t ust_listener; /* listener thread */ |
| 241 | int root_handle; |
| 242 | int constructor_sem_posted; |
| 243 | int allowed; |
| 244 | int global; |
| 245 | int thread_active; |
| 246 | |
| 247 | char sock_path[PATH_MAX]; |
| 248 | int socket; |
| 249 | int notify_socket; |
| 250 | |
| 251 | char wait_shm_path[PATH_MAX]; |
| 252 | char *wait_shm_mmap; |
| 253 | /* Keep track of lazy state dump not performed yet. */ |
| 254 | int statedump_pending; |
| 255 | }; |
| 256 | |
| 257 | /* Socket from app (connect) to session daemon (listen) for communication */ |
| 258 | struct sock_info global_apps = { |
| 259 | .name = "global", |
| 260 | .global = 1, |
| 261 | |
| 262 | .root_handle = -1, |
| 263 | .allowed = 1, |
| 264 | .thread_active = 0, |
| 265 | |
| 266 | .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME, |
| 267 | .socket = -1, |
| 268 | .notify_socket = -1, |
| 269 | |
| 270 | .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME, |
| 271 | |
| 272 | .statedump_pending = 0, |
| 273 | }; |
| 274 | |
| 275 | /* TODO: allow global_apps_sock_path override */ |
| 276 | |
| 277 | struct sock_info local_apps = { |
| 278 | .name = "local", |
| 279 | .global = 0, |
| 280 | .root_handle = -1, |
| 281 | .allowed = 0, /* Check setuid bit first */ |
| 282 | .thread_active = 0, |
| 283 | |
| 284 | .socket = -1, |
| 285 | .notify_socket = -1, |
| 286 | |
| 287 | .statedump_pending = 0, |
| 288 | }; |
| 289 | |
| 290 | static int wait_poll_fallback; |
| 291 | |
| 292 | static const char *cmd_name_mapping[] = { |
| 293 | [ LTTNG_UST_RELEASE ] = "Release", |
| 294 | [ LTTNG_UST_SESSION ] = "Create Session", |
| 295 | [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version", |
| 296 | |
| 297 | [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List", |
| 298 | [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State", |
| 299 | [ LTTNG_UST_REGISTER_DONE ] = "Registration Done", |
| 300 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List", |
| 301 | |
| 302 | /* Session FD commands */ |
| 303 | [ LTTNG_UST_CHANNEL ] = "Create Channel", |
| 304 | [ LTTNG_UST_SESSION_START ] = "Start Session", |
| 305 | [ LTTNG_UST_SESSION_STOP ] = "Stop Session", |
| 306 | |
| 307 | /* Channel FD commands */ |
| 308 | [ LTTNG_UST_STREAM ] = "Create Stream", |
| 309 | [ LTTNG_UST_EVENT ] = "Create Event", |
| 310 | |
| 311 | /* Event and Channel FD commands */ |
| 312 | [ LTTNG_UST_CONTEXT ] = "Create Context", |
| 313 | [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer", |
| 314 | |
| 315 | /* Event, Channel and Session commands */ |
| 316 | [ LTTNG_UST_ENABLE ] = "Enable", |
| 317 | [ LTTNG_UST_DISABLE ] = "Disable", |
| 318 | |
| 319 | /* Tracepoint list commands */ |
| 320 | [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint", |
| 321 | [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field", |
| 322 | |
| 323 | /* Event FD commands */ |
| 324 | [ LTTNG_UST_FILTER ] = "Create Filter", |
| 325 | [ LTTNG_UST_EXCLUSION ] = "Add exclusions to event", |
| 326 | }; |
| 327 | |
| 328 | static const char *str_timeout; |
| 329 | static int got_timeout_env; |
| 330 | |
| 331 | extern void lttng_ring_buffer_client_overwrite_init(void); |
| 332 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
| 333 | extern void lttng_ring_buffer_client_discard_init(void); |
| 334 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
| 335 | extern void lttng_ring_buffer_metadata_client_init(void); |
| 336 | extern void lttng_ring_buffer_client_overwrite_exit(void); |
| 337 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
| 338 | extern void lttng_ring_buffer_client_discard_exit(void); |
| 339 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
| 340 | extern void lttng_ring_buffer_metadata_client_exit(void); |
| 341 | |
| 342 | ssize_t lttng_ust_read(int fd, void *buf, size_t len) |
| 343 | { |
| 344 | ssize_t ret; |
| 345 | size_t copied = 0, to_copy = len; |
| 346 | |
| 347 | do { |
| 348 | ret = read(fd, buf + copied, to_copy); |
| 349 | if (ret > 0) { |
| 350 | copied += ret; |
| 351 | to_copy -= ret; |
| 352 | } |
| 353 | } while ((ret > 0 && to_copy > 0) |
| 354 | || (ret < 0 && errno == EINTR)); |
| 355 | if (ret > 0) { |
| 356 | ret = copied; |
| 357 | } |
| 358 | return ret; |
| 359 | } |
| 360 | /* |
| 361 | * Returns the HOME directory path. Caller MUST NOT free(3) the returned |
| 362 | * pointer. |
| 363 | */ |
| 364 | static |
| 365 | const char *get_lttng_home_dir(void) |
| 366 | { |
| 367 | const char *val; |
| 368 | |
| 369 | val = (const char *) lttng_getenv("LTTNG_HOME"); |
| 370 | if (val != NULL) { |
| 371 | return val; |
| 372 | } |
| 373 | return (const char *) lttng_getenv("HOME"); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Force a read (imply TLS fixup for dlopen) of TLS variables. |
| 378 | */ |
| 379 | static |
| 380 | void lttng_fixup_nest_count_tls(void) |
| 381 | { |
| 382 | asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count))); |
| 383 | } |
| 384 | |
| 385 | static |
| 386 | void lttng_fixup_ust_mutex_nest_tls(void) |
| 387 | { |
| 388 | asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest))); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Fixup urcu bp TLS. |
| 393 | */ |
| 394 | static |
| 395 | void lttng_fixup_urcu_bp_tls(void) |
| 396 | { |
| 397 | rcu_read_lock(); |
| 398 | rcu_read_unlock(); |
| 399 | } |
| 400 | |
| 401 | void lttng_ust_fixup_tls(void) |
| 402 | { |
| 403 | lttng_fixup_urcu_bp_tls(); |
| 404 | lttng_fixup_ringbuffer_tls(); |
| 405 | lttng_fixup_vtid_tls(); |
| 406 | lttng_fixup_nest_count_tls(); |
| 407 | lttng_fixup_procname_tls(); |
| 408 | lttng_fixup_ust_mutex_nest_tls(); |
| 409 | lttng_ust_fixup_fd_tracker_tls(); |
| 410 | } |
| 411 | |
| 412 | int lttng_get_notify_socket(void *owner) |
| 413 | { |
| 414 | struct sock_info *info = owner; |
| 415 | |
| 416 | return info->notify_socket; |
| 417 | } |
| 418 | |
| 419 | static |
| 420 | void print_cmd(int cmd, int handle) |
| 421 | { |
| 422 | const char *cmd_name = "Unknown"; |
| 423 | |
| 424 | if (cmd >= 0 && cmd < LTTNG_ARRAY_SIZE(cmd_name_mapping) |
| 425 | && cmd_name_mapping[cmd]) { |
| 426 | cmd_name = cmd_name_mapping[cmd]; |
| 427 | } |
| 428 | DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)", |
| 429 | cmd_name, cmd, |
| 430 | lttng_ust_obj_get_name(handle), handle); |
| 431 | } |
| 432 | |
| 433 | static |
| 434 | int setup_local_apps(void) |
| 435 | { |
| 436 | const char *home_dir; |
| 437 | uid_t uid; |
| 438 | |
| 439 | uid = getuid(); |
| 440 | /* |
| 441 | * Disallow per-user tracing for setuid binaries. |
| 442 | */ |
| 443 | if (uid != geteuid()) { |
| 444 | assert(local_apps.allowed == 0); |
| 445 | return 0; |
| 446 | } |
| 447 | home_dir = get_lttng_home_dir(); |
| 448 | if (!home_dir) { |
| 449 | WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing."); |
| 450 | assert(local_apps.allowed == 0); |
| 451 | return -ENOENT; |
| 452 | } |
| 453 | local_apps.allowed = 1; |
| 454 | snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s", |
| 455 | home_dir, |
| 456 | LTTNG_DEFAULT_HOME_RUNDIR, |
| 457 | LTTNG_UST_SOCK_FILENAME); |
| 458 | snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u", |
| 459 | LTTNG_UST_WAIT_FILENAME, |
| 460 | uid); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Get socket timeout, in ms. |
| 466 | * -1: wait forever. 0: don't wait. >0: timeout, in ms. |
| 467 | */ |
| 468 | static |
| 469 | long get_timeout(void) |
| 470 | { |
| 471 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
| 472 | |
| 473 | if (!got_timeout_env) { |
| 474 | str_timeout = lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT"); |
| 475 | got_timeout_env = 1; |
| 476 | } |
| 477 | if (str_timeout) |
| 478 | constructor_delay_ms = strtol(str_timeout, NULL, 10); |
| 479 | /* All negative values are considered as "-1". */ |
| 480 | if (constructor_delay_ms < -1) |
| 481 | constructor_delay_ms = -1; |
| 482 | return constructor_delay_ms; |
| 483 | } |
| 484 | |
| 485 | /* Timeout for notify socket send and recv. */ |
| 486 | static |
| 487 | long get_notify_sock_timeout(void) |
| 488 | { |
| 489 | return get_timeout(); |
| 490 | } |
| 491 | |
| 492 | /* Timeout for connecting to cmd and notify sockets. */ |
| 493 | static |
| 494 | long get_connect_sock_timeout(void) |
| 495 | { |
| 496 | return get_timeout(); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Return values: -1: wait forever. 0: don't wait. 1: timeout wait. |
| 501 | */ |
| 502 | static |
| 503 | int get_constructor_timeout(struct timespec *constructor_timeout) |
| 504 | { |
| 505 | long constructor_delay_ms; |
| 506 | int ret; |
| 507 | |
| 508 | constructor_delay_ms = get_timeout(); |
| 509 | |
| 510 | switch (constructor_delay_ms) { |
| 511 | case -1:/* fall-through */ |
| 512 | case 0: |
| 513 | return constructor_delay_ms; |
| 514 | default: |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * If we are unable to find the current time, don't wait. |
| 520 | */ |
| 521 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); |
| 522 | if (ret) { |
| 523 | /* Don't wait. */ |
| 524 | return 0; |
| 525 | } |
| 526 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
| 527 | constructor_timeout->tv_nsec += |
| 528 | (constructor_delay_ms % 1000UL) * 1000000UL; |
| 529 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
| 530 | constructor_timeout->tv_sec++; |
| 531 | constructor_timeout->tv_nsec -= 1000000000UL; |
| 532 | } |
| 533 | /* Timeout wait (constructor_delay_ms). */ |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | static |
| 538 | void get_allow_blocking(void) |
| 539 | { |
| 540 | const char *str_allow_blocking = |
| 541 | lttng_getenv("LTTNG_UST_ALLOW_BLOCKING"); |
| 542 | |
| 543 | if (str_allow_blocking) { |
| 544 | DBG("%s environment variable is set", |
| 545 | "LTTNG_UST_ALLOW_BLOCKING"); |
| 546 | lttng_ust_ringbuffer_set_allow_blocking(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | int register_to_sessiond(int socket, enum ustctl_socket_type type) |
| 552 | { |
| 553 | return ustcomm_send_reg_msg(socket, |
| 554 | type, |
| 555 | CAA_BITS_PER_LONG, |
| 556 | lttng_alignof(uint8_t) * CHAR_BIT, |
| 557 | lttng_alignof(uint16_t) * CHAR_BIT, |
| 558 | lttng_alignof(uint32_t) * CHAR_BIT, |
| 559 | lttng_alignof(uint64_t) * CHAR_BIT, |
| 560 | lttng_alignof(unsigned long) * CHAR_BIT); |
| 561 | } |
| 562 | |
| 563 | static |
| 564 | int send_reply(int sock, struct ustcomm_ust_reply *lur) |
| 565 | { |
| 566 | ssize_t len; |
| 567 | |
| 568 | len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
| 569 | switch (len) { |
| 570 | case sizeof(*lur): |
| 571 | DBG("message successfully sent"); |
| 572 | return 0; |
| 573 | default: |
| 574 | if (len == -ECONNRESET) { |
| 575 | DBG("remote end closed connection"); |
| 576 | return 0; |
| 577 | } |
| 578 | if (len < 0) |
| 579 | return len; |
| 580 | DBG("incorrect message size: %zd", len); |
| 581 | return -EINVAL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static |
| 586 | int handle_register_done(struct sock_info *sock_info) |
| 587 | { |
| 588 | int ret; |
| 589 | |
| 590 | if (sock_info->constructor_sem_posted) |
| 591 | return 0; |
| 592 | sock_info->constructor_sem_posted = 1; |
| 593 | if (uatomic_read(&sem_count) <= 0) { |
| 594 | return 0; |
| 595 | } |
| 596 | ret = uatomic_add_return(&sem_count, -1); |
| 597 | if (ret == 0) { |
| 598 | ret = sem_post(&constructor_wait); |
| 599 | assert(!ret); |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Only execute pending statedump after the constructor semaphore has |
| 606 | * been posted by each listener thread. This means statedump will only |
| 607 | * be performed after the "registration done" command is received from |
| 608 | * each session daemon the application is connected to. |
| 609 | * |
| 610 | * This ensures we don't run into deadlock issues with the dynamic |
| 611 | * loader mutex, which is held while the constructor is called and |
| 612 | * waiting on the constructor semaphore. All operations requiring this |
| 613 | * dynamic loader lock need to be postponed using this mechanism. |
| 614 | */ |
| 615 | static |
| 616 | void handle_pending_statedump(struct sock_info *sock_info) |
| 617 | { |
| 618 | int ctor_passed = sock_info->constructor_sem_posted; |
| 619 | |
| 620 | if (ctor_passed && sock_info->statedump_pending) { |
| 621 | sock_info->statedump_pending = 0; |
| 622 | pthread_mutex_lock(&ust_fork_mutex); |
| 623 | lttng_handle_pending_statedump(sock_info); |
| 624 | pthread_mutex_unlock(&ust_fork_mutex); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static |
| 629 | int handle_message(struct sock_info *sock_info, |
| 630 | int sock, struct ustcomm_ust_msg *lum) |
| 631 | { |
| 632 | int ret = 0; |
| 633 | const struct lttng_ust_objd_ops *ops; |
| 634 | struct ustcomm_ust_reply lur; |
| 635 | union ust_args args; |
| 636 | char ctxstr[LTTNG_UST_SYM_NAME_LEN]; /* App context string. */ |
| 637 | ssize_t len; |
| 638 | |
| 639 | memset(&lur, 0, sizeof(lur)); |
| 640 | |
| 641 | if (ust_lock()) { |
| 642 | ret = -LTTNG_UST_ERR_EXITING; |
| 643 | goto error; |
| 644 | } |
| 645 | |
| 646 | ops = objd_ops(lum->handle); |
| 647 | if (!ops) { |
| 648 | ret = -ENOENT; |
| 649 | goto error; |
| 650 | } |
| 651 | |
| 652 | switch (lum->cmd) { |
| 653 | case LTTNG_UST_REGISTER_DONE: |
| 654 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 655 | ret = handle_register_done(sock_info); |
| 656 | else |
| 657 | ret = -EINVAL; |
| 658 | break; |
| 659 | case LTTNG_UST_RELEASE: |
| 660 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 661 | ret = -EPERM; |
| 662 | else |
| 663 | ret = lttng_ust_objd_unref(lum->handle, 1); |
| 664 | break; |
| 665 | case LTTNG_UST_FILTER: |
| 666 | { |
| 667 | /* Receive filter data */ |
| 668 | struct lttng_ust_filter_bytecode_node *bytecode; |
| 669 | |
| 670 | if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) { |
| 671 | ERR("Filter data size is too large: %u bytes", |
| 672 | lum->u.filter.data_size); |
| 673 | ret = -EINVAL; |
| 674 | goto error; |
| 675 | } |
| 676 | |
| 677 | if (lum->u.filter.reloc_offset > lum->u.filter.data_size) { |
| 678 | ERR("Filter reloc offset %u is not within data", |
| 679 | lum->u.filter.reloc_offset); |
| 680 | ret = -EINVAL; |
| 681 | goto error; |
| 682 | } |
| 683 | |
| 684 | bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size); |
| 685 | if (!bytecode) { |
| 686 | ret = -ENOMEM; |
| 687 | goto error; |
| 688 | } |
| 689 | len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, |
| 690 | lum->u.filter.data_size); |
| 691 | switch (len) { |
| 692 | case 0: /* orderly shutdown */ |
| 693 | ret = 0; |
| 694 | free(bytecode); |
| 695 | goto error; |
| 696 | default: |
| 697 | if (len == lum->u.filter.data_size) { |
| 698 | DBG("filter data received"); |
| 699 | break; |
| 700 | } else if (len < 0) { |
| 701 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); |
| 702 | if (len == -ECONNRESET) { |
| 703 | ERR("%s remote end closed connection", sock_info->name); |
| 704 | ret = len; |
| 705 | free(bytecode); |
| 706 | goto error; |
| 707 | } |
| 708 | ret = len; |
| 709 | free(bytecode); |
| 710 | goto error; |
| 711 | } else { |
| 712 | DBG("incorrect filter data message size: %zd", len); |
| 713 | ret = -EINVAL; |
| 714 | free(bytecode); |
| 715 | goto error; |
| 716 | } |
| 717 | } |
| 718 | bytecode->bc.len = lum->u.filter.data_size; |
| 719 | bytecode->bc.reloc_offset = lum->u.filter.reloc_offset; |
| 720 | bytecode->bc.seqnum = lum->u.filter.seqnum; |
| 721 | if (ops->cmd) { |
| 722 | ret = ops->cmd(lum->handle, lum->cmd, |
| 723 | (unsigned long) bytecode, |
| 724 | &args, sock_info); |
| 725 | if (ret) { |
| 726 | free(bytecode); |
| 727 | } |
| 728 | /* don't free bytecode if everything went fine. */ |
| 729 | } else { |
| 730 | ret = -ENOSYS; |
| 731 | free(bytecode); |
| 732 | } |
| 733 | break; |
| 734 | } |
| 735 | case LTTNG_UST_EXCLUSION: |
| 736 | { |
| 737 | /* Receive exclusion names */ |
| 738 | struct lttng_ust_excluder_node *node; |
| 739 | unsigned int count; |
| 740 | |
| 741 | count = lum->u.exclusion.count; |
| 742 | if (count == 0) { |
| 743 | /* There are no names to read */ |
| 744 | ret = 0; |
| 745 | goto error; |
| 746 | } |
| 747 | node = zmalloc(sizeof(*node) + |
| 748 | count * LTTNG_UST_SYM_NAME_LEN); |
| 749 | if (!node) { |
| 750 | ret = -ENOMEM; |
| 751 | goto error; |
| 752 | } |
| 753 | node->excluder.count = count; |
| 754 | len = ustcomm_recv_unix_sock(sock, node->excluder.names, |
| 755 | count * LTTNG_UST_SYM_NAME_LEN); |
| 756 | switch (len) { |
| 757 | case 0: /* orderly shutdown */ |
| 758 | ret = 0; |
| 759 | free(node); |
| 760 | goto error; |
| 761 | default: |
| 762 | if (len == count * LTTNG_UST_SYM_NAME_LEN) { |
| 763 | DBG("Exclusion data received"); |
| 764 | break; |
| 765 | } else if (len < 0) { |
| 766 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); |
| 767 | if (len == -ECONNRESET) { |
| 768 | ERR("%s remote end closed connection", sock_info->name); |
| 769 | ret = len; |
| 770 | free(node); |
| 771 | goto error; |
| 772 | } |
| 773 | ret = len; |
| 774 | free(node); |
| 775 | goto error; |
| 776 | } else { |
| 777 | DBG("Incorrect exclusion data message size: %zd", len); |
| 778 | ret = -EINVAL; |
| 779 | free(node); |
| 780 | goto error; |
| 781 | } |
| 782 | } |
| 783 | if (ops->cmd) { |
| 784 | ret = ops->cmd(lum->handle, lum->cmd, |
| 785 | (unsigned long) node, |
| 786 | &args, sock_info); |
| 787 | if (ret) { |
| 788 | free(node); |
| 789 | } |
| 790 | /* Don't free exclusion data if everything went fine. */ |
| 791 | } else { |
| 792 | ret = -ENOSYS; |
| 793 | free(node); |
| 794 | } |
| 795 | break; |
| 796 | } |
| 797 | case LTTNG_UST_CHANNEL: |
| 798 | { |
| 799 | void *chan_data; |
| 800 | int wakeup_fd; |
| 801 | |
| 802 | len = ustcomm_recv_channel_from_sessiond(sock, |
| 803 | &chan_data, lum->u.channel.len, |
| 804 | &wakeup_fd); |
| 805 | switch (len) { |
| 806 | case 0: /* orderly shutdown */ |
| 807 | ret = 0; |
| 808 | goto error; |
| 809 | default: |
| 810 | if (len == lum->u.channel.len) { |
| 811 | DBG("channel data received"); |
| 812 | break; |
| 813 | } else if (len < 0) { |
| 814 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); |
| 815 | if (len == -ECONNRESET) { |
| 816 | ERR("%s remote end closed connection", sock_info->name); |
| 817 | ret = len; |
| 818 | goto error; |
| 819 | } |
| 820 | ret = len; |
| 821 | goto error; |
| 822 | } else { |
| 823 | DBG("incorrect channel data message size: %zd", len); |
| 824 | ret = -EINVAL; |
| 825 | goto error; |
| 826 | } |
| 827 | } |
| 828 | args.channel.chan_data = chan_data; |
| 829 | args.channel.wakeup_fd = wakeup_fd; |
| 830 | if (ops->cmd) |
| 831 | ret = ops->cmd(lum->handle, lum->cmd, |
| 832 | (unsigned long) &lum->u, |
| 833 | &args, sock_info); |
| 834 | else |
| 835 | ret = -ENOSYS; |
| 836 | break; |
| 837 | } |
| 838 | case LTTNG_UST_STREAM: |
| 839 | { |
| 840 | /* Receive shm_fd, wakeup_fd */ |
| 841 | ret = ustcomm_recv_stream_from_sessiond(sock, |
| 842 | &lum->u.stream.len, |
| 843 | &args.stream.shm_fd, |
| 844 | &args.stream.wakeup_fd); |
| 845 | if (ret) { |
| 846 | goto error; |
| 847 | } |
| 848 | if (ops->cmd) |
| 849 | ret = ops->cmd(lum->handle, lum->cmd, |
| 850 | (unsigned long) &lum->u, |
| 851 | &args, sock_info); |
| 852 | else |
| 853 | ret = -ENOSYS; |
| 854 | break; |
| 855 | } |
| 856 | case LTTNG_UST_CONTEXT: |
| 857 | switch (lum->u.context.ctx) { |
| 858 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
| 859 | { |
| 860 | char *p; |
| 861 | size_t ctxlen, recvlen; |
| 862 | |
| 863 | ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1 |
| 864 | + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len; |
| 865 | if (ctxlen >= LTTNG_UST_SYM_NAME_LEN) { |
| 866 | ERR("Application context string length size is too large: %zu bytes", |
| 867 | ctxlen); |
| 868 | ret = -EINVAL; |
| 869 | goto error; |
| 870 | } |
| 871 | strcpy(ctxstr, "$app."); |
| 872 | p = &ctxstr[strlen("$app.")]; |
| 873 | recvlen = ctxlen - strlen("$app."); |
| 874 | len = ustcomm_recv_unix_sock(sock, p, recvlen); |
| 875 | switch (len) { |
| 876 | case 0: /* orderly shutdown */ |
| 877 | ret = 0; |
| 878 | goto error; |
| 879 | default: |
| 880 | if (len == recvlen) { |
| 881 | DBG("app context data received"); |
| 882 | break; |
| 883 | } else if (len < 0) { |
| 884 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); |
| 885 | if (len == -ECONNRESET) { |
| 886 | ERR("%s remote end closed connection", sock_info->name); |
| 887 | ret = len; |
| 888 | goto error; |
| 889 | } |
| 890 | ret = len; |
| 891 | goto error; |
| 892 | } else { |
| 893 | DBG("incorrect app context data message size: %zd", len); |
| 894 | ret = -EINVAL; |
| 895 | goto error; |
| 896 | } |
| 897 | } |
| 898 | /* Put : between provider and ctxname. */ |
| 899 | p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':'; |
| 900 | args.app_context.ctxname = ctxstr; |
| 901 | break; |
| 902 | } |
| 903 | default: |
| 904 | break; |
| 905 | } |
| 906 | if (ops->cmd) { |
| 907 | ret = ops->cmd(lum->handle, lum->cmd, |
| 908 | (unsigned long) &lum->u, |
| 909 | &args, sock_info); |
| 910 | } else { |
| 911 | ret = -ENOSYS; |
| 912 | } |
| 913 | break; |
| 914 | default: |
| 915 | if (ops->cmd) |
| 916 | ret = ops->cmd(lum->handle, lum->cmd, |
| 917 | (unsigned long) &lum->u, |
| 918 | &args, sock_info); |
| 919 | else |
| 920 | ret = -ENOSYS; |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | lur.handle = lum->handle; |
| 925 | lur.cmd = lum->cmd; |
| 926 | lur.ret_val = ret; |
| 927 | if (ret >= 0) { |
| 928 | lur.ret_code = LTTNG_UST_OK; |
| 929 | } else { |
| 930 | /* |
| 931 | * Use -LTTNG_UST_ERR as wildcard for UST internal |
| 932 | * error that are not caused by the transport, except if |
| 933 | * we already have a more precise error message to |
| 934 | * report. |
| 935 | */ |
| 936 | if (ret > -LTTNG_UST_ERR) { |
| 937 | /* Translate code to UST error. */ |
| 938 | switch (ret) { |
| 939 | case -EEXIST: |
| 940 | lur.ret_code = -LTTNG_UST_ERR_EXIST; |
| 941 | break; |
| 942 | case -EINVAL: |
| 943 | lur.ret_code = -LTTNG_UST_ERR_INVAL; |
| 944 | break; |
| 945 | case -ENOENT: |
| 946 | lur.ret_code = -LTTNG_UST_ERR_NOENT; |
| 947 | break; |
| 948 | case -EPERM: |
| 949 | lur.ret_code = -LTTNG_UST_ERR_PERM; |
| 950 | break; |
| 951 | case -ENOSYS: |
| 952 | lur.ret_code = -LTTNG_UST_ERR_NOSYS; |
| 953 | break; |
| 954 | default: |
| 955 | lur.ret_code = -LTTNG_UST_ERR; |
| 956 | break; |
| 957 | } |
| 958 | } else { |
| 959 | lur.ret_code = ret; |
| 960 | } |
| 961 | } |
| 962 | if (ret >= 0) { |
| 963 | switch (lum->cmd) { |
| 964 | case LTTNG_UST_TRACER_VERSION: |
| 965 | lur.u.version = lum->u.version; |
| 966 | break; |
| 967 | case LTTNG_UST_TRACEPOINT_LIST_GET: |
| 968 | memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint)); |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | DBG("Return value: %d", lur.ret_val); |
| 973 | |
| 974 | ust_unlock(); |
| 975 | |
| 976 | /* |
| 977 | * Performed delayed statedump operations outside of the UST |
| 978 | * lock. We need to take the dynamic loader lock before we take |
| 979 | * the UST lock internally within handle_pending_statedump(). |
| 980 | */ |
| 981 | handle_pending_statedump(sock_info); |
| 982 | |
| 983 | if (ust_lock()) { |
| 984 | ret = -LTTNG_UST_ERR_EXITING; |
| 985 | goto error; |
| 986 | } |
| 987 | |
| 988 | ret = send_reply(sock, &lur); |
| 989 | if (ret < 0) { |
| 990 | DBG("error sending reply"); |
| 991 | goto error; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field |
| 996 | * after the reply. |
| 997 | */ |
| 998 | if (lur.ret_code == LTTNG_UST_OK) { |
| 999 | switch (lum->cmd) { |
| 1000 | case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: |
| 1001 | len = ustcomm_send_unix_sock(sock, |
| 1002 | &args.field_list.entry, |
| 1003 | sizeof(args.field_list.entry)); |
| 1004 | if (len < 0) { |
| 1005 | ret = len; |
| 1006 | goto error; |
| 1007 | } |
| 1008 | if (len != sizeof(args.field_list.entry)) { |
| 1009 | ret = -EINVAL; |
| 1010 | goto error; |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | error: |
| 1016 | ust_unlock(); |
| 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | static |
| 1022 | void cleanup_sock_info(struct sock_info *sock_info, int exiting) |
| 1023 | { |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (sock_info->root_handle != -1) { |
| 1027 | ret = lttng_ust_objd_unref(sock_info->root_handle, 1); |
| 1028 | if (ret) { |
| 1029 | ERR("Error unref root handle"); |
| 1030 | } |
| 1031 | sock_info->root_handle = -1; |
| 1032 | } |
| 1033 | sock_info->constructor_sem_posted = 0; |
| 1034 | |
| 1035 | /* |
| 1036 | * wait_shm_mmap, socket and notify socket are used by listener |
| 1037 | * threads outside of the ust lock, so we cannot tear them down |
| 1038 | * ourselves, because we cannot join on these threads. Leave |
| 1039 | * responsibility of cleaning up these resources to the OS |
| 1040 | * process exit. |
| 1041 | */ |
| 1042 | if (exiting) |
| 1043 | return; |
| 1044 | |
| 1045 | if (sock_info->socket != -1) { |
| 1046 | ret = ustcomm_close_unix_sock(sock_info->socket); |
| 1047 | if (ret) { |
| 1048 | ERR("Error closing ust cmd socket"); |
| 1049 | } |
| 1050 | sock_info->socket = -1; |
| 1051 | } |
| 1052 | if (sock_info->notify_socket != -1) { |
| 1053 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); |
| 1054 | if (ret) { |
| 1055 | ERR("Error closing ust notify socket"); |
| 1056 | } |
| 1057 | sock_info->notify_socket = -1; |
| 1058 | } |
| 1059 | if (sock_info->wait_shm_mmap) { |
| 1060 | long page_size; |
| 1061 | |
| 1062 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1063 | if (page_size <= 0) { |
| 1064 | if (!page_size) { |
| 1065 | errno = EINVAL; |
| 1066 | } |
| 1067 | PERROR("Error in sysconf(_SC_PAGE_SIZE)"); |
| 1068 | } else { |
| 1069 | ret = munmap(sock_info->wait_shm_mmap, page_size); |
| 1070 | if (ret) { |
| 1071 | ERR("Error unmapping wait shm"); |
| 1072 | } |
| 1073 | } |
| 1074 | sock_info->wait_shm_mmap = NULL; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Using fork to set umask in the child process (not multi-thread safe). |
| 1080 | * We deal with the shm_open vs ftruncate race (happening when the |
| 1081 | * sessiond owns the shm and does not let everybody modify it, to ensure |
| 1082 | * safety against shm_unlink) by simply letting the mmap fail and |
| 1083 | * retrying after a few seconds. |
| 1084 | * For global shm, everybody has rw access to it until the sessiond |
| 1085 | * starts. |
| 1086 | */ |
| 1087 | static |
| 1088 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
| 1089 | { |
| 1090 | int wait_shm_fd, ret; |
| 1091 | pid_t pid; |
| 1092 | |
| 1093 | /* |
| 1094 | * Try to open read-only. |
| 1095 | */ |
| 1096 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
| 1097 | if (wait_shm_fd >= 0) { |
| 1098 | int32_t tmp_read; |
| 1099 | ssize_t len; |
| 1100 | size_t bytes_read = 0; |
| 1101 | |
| 1102 | /* |
| 1103 | * Try to read the fd. If unable to do so, try opening |
| 1104 | * it in write mode. |
| 1105 | */ |
| 1106 | do { |
| 1107 | len = read(wait_shm_fd, |
| 1108 | &((char *) &tmp_read)[bytes_read], |
| 1109 | sizeof(tmp_read) - bytes_read); |
| 1110 | if (len > 0) { |
| 1111 | bytes_read += len; |
| 1112 | } |
| 1113 | } while ((len < 0 && errno == EINTR) |
| 1114 | || (len > 0 && bytes_read < sizeof(tmp_read))); |
| 1115 | if (bytes_read != sizeof(tmp_read)) { |
| 1116 | ret = close(wait_shm_fd); |
| 1117 | if (ret) { |
| 1118 | ERR("close wait_shm_fd"); |
| 1119 | } |
| 1120 | goto open_write; |
| 1121 | } |
| 1122 | goto end; |
| 1123 | } else if (wait_shm_fd < 0 && errno != ENOENT) { |
| 1124 | /* |
| 1125 | * Real-only open did not work, and it's not because the |
| 1126 | * entry was not present. It's a failure that prohibits |
| 1127 | * using shm. |
| 1128 | */ |
| 1129 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
| 1130 | goto end; |
| 1131 | } |
| 1132 | |
| 1133 | open_write: |
| 1134 | /* |
| 1135 | * If the open failed because the file did not exist, or because |
| 1136 | * the file was not truncated yet, try creating it ourself. |
| 1137 | */ |
| 1138 | URCU_TLS(lttng_ust_nest_count)++; |
| 1139 | pid = fork(); |
| 1140 | URCU_TLS(lttng_ust_nest_count)--; |
| 1141 | if (pid > 0) { |
| 1142 | int status; |
| 1143 | |
| 1144 | /* |
| 1145 | * Parent: wait for child to return, in which case the |
| 1146 | * shared memory map will have been created. |
| 1147 | */ |
| 1148 | pid = wait(&status); |
| 1149 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1150 | wait_shm_fd = -1; |
| 1151 | goto end; |
| 1152 | } |
| 1153 | /* |
| 1154 | * Try to open read-only again after creation. |
| 1155 | */ |
| 1156 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
| 1157 | if (wait_shm_fd < 0) { |
| 1158 | /* |
| 1159 | * Real-only open did not work. It's a failure |
| 1160 | * that prohibits using shm. |
| 1161 | */ |
| 1162 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
| 1163 | goto end; |
| 1164 | } |
| 1165 | goto end; |
| 1166 | } else if (pid == 0) { |
| 1167 | int create_mode; |
| 1168 | |
| 1169 | /* Child */ |
| 1170 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
| 1171 | if (sock_info->global) |
| 1172 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
| 1173 | /* |
| 1174 | * We're alone in a child process, so we can modify the |
| 1175 | * process-wide umask. |
| 1176 | */ |
| 1177 | umask(~create_mode); |
| 1178 | /* |
| 1179 | * Try creating shm (or get rw access). |
| 1180 | * We don't do an exclusive open, because we allow other |
| 1181 | * processes to create+ftruncate it concurrently. |
| 1182 | */ |
| 1183 | wait_shm_fd = shm_open(sock_info->wait_shm_path, |
| 1184 | O_RDWR | O_CREAT, create_mode); |
| 1185 | if (wait_shm_fd >= 0) { |
| 1186 | ret = ftruncate(wait_shm_fd, mmap_size); |
| 1187 | if (ret) { |
| 1188 | PERROR("ftruncate"); |
| 1189 | _exit(EXIT_FAILURE); |
| 1190 | } |
| 1191 | _exit(EXIT_SUCCESS); |
| 1192 | } |
| 1193 | /* |
| 1194 | * For local shm, we need to have rw access to accept |
| 1195 | * opening it: this means the local sessiond will be |
| 1196 | * able to wake us up. For global shm, we open it even |
| 1197 | * if rw access is not granted, because the root.root |
| 1198 | * sessiond will be able to override all rights and wake |
| 1199 | * us up. |
| 1200 | */ |
| 1201 | if (!sock_info->global && errno != EACCES) { |
| 1202 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
| 1203 | _exit(EXIT_FAILURE); |
| 1204 | } |
| 1205 | /* |
| 1206 | * The shm exists, but we cannot open it RW. Report |
| 1207 | * success. |
| 1208 | */ |
| 1209 | _exit(EXIT_SUCCESS); |
| 1210 | } else { |
| 1211 | return -1; |
| 1212 | } |
| 1213 | end: |
| 1214 | if (wait_shm_fd >= 0 && !sock_info->global) { |
| 1215 | struct stat statbuf; |
| 1216 | |
| 1217 | /* |
| 1218 | * Ensure that our user is the owner of the shm file for |
| 1219 | * local shm. If we do not own the file, it means our |
| 1220 | * sessiond will not have access to wake us up (there is |
| 1221 | * probably a rogue process trying to fake our |
| 1222 | * sessiond). Fallback to polling method in this case. |
| 1223 | */ |
| 1224 | ret = fstat(wait_shm_fd, &statbuf); |
| 1225 | if (ret) { |
| 1226 | PERROR("fstat"); |
| 1227 | goto error_close; |
| 1228 | } |
| 1229 | if (statbuf.st_uid != getuid()) |
| 1230 | goto error_close; |
| 1231 | } |
| 1232 | return wait_shm_fd; |
| 1233 | |
| 1234 | error_close: |
| 1235 | ret = close(wait_shm_fd); |
| 1236 | if (ret) { |
| 1237 | PERROR("Error closing fd"); |
| 1238 | } |
| 1239 | return -1; |
| 1240 | } |
| 1241 | |
| 1242 | static |
| 1243 | char *get_map_shm(struct sock_info *sock_info) |
| 1244 | { |
| 1245 | long page_size; |
| 1246 | int wait_shm_fd, ret; |
| 1247 | char *wait_shm_mmap; |
| 1248 | |
| 1249 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1250 | if (page_size <= 0) { |
| 1251 | if (!page_size) { |
| 1252 | errno = EINVAL; |
| 1253 | } |
| 1254 | PERROR("Error in sysconf(_SC_PAGE_SIZE)"); |
| 1255 | goto error; |
| 1256 | } |
| 1257 | |
| 1258 | lttng_ust_lock_fd_tracker(); |
| 1259 | wait_shm_fd = get_wait_shm(sock_info, page_size); |
| 1260 | if (wait_shm_fd < 0) { |
| 1261 | lttng_ust_unlock_fd_tracker(); |
| 1262 | goto error; |
| 1263 | } |
| 1264 | |
| 1265 | ret = lttng_ust_add_fd_to_tracker(wait_shm_fd); |
| 1266 | if (ret < 0) { |
| 1267 | ret = close(wait_shm_fd); |
| 1268 | if (!ret) { |
| 1269 | PERROR("Error closing fd"); |
| 1270 | } |
| 1271 | lttng_ust_unlock_fd_tracker(); |
| 1272 | goto error; |
| 1273 | } |
| 1274 | |
| 1275 | wait_shm_fd = ret; |
| 1276 | lttng_ust_unlock_fd_tracker(); |
| 1277 | |
| 1278 | wait_shm_mmap = mmap(NULL, page_size, PROT_READ, |
| 1279 | MAP_SHARED, wait_shm_fd, 0); |
| 1280 | |
| 1281 | /* close shm fd immediately after taking the mmap reference */ |
| 1282 | lttng_ust_lock_fd_tracker(); |
| 1283 | ret = close(wait_shm_fd); |
| 1284 | if (!ret) { |
| 1285 | lttng_ust_delete_fd_from_tracker(wait_shm_fd); |
| 1286 | } else { |
| 1287 | PERROR("Error closing fd"); |
| 1288 | } |
| 1289 | lttng_ust_unlock_fd_tracker(); |
| 1290 | |
| 1291 | if (wait_shm_mmap == MAP_FAILED) { |
| 1292 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); |
| 1293 | goto error; |
| 1294 | } |
| 1295 | return wait_shm_mmap; |
| 1296 | |
| 1297 | error: |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | |
| 1301 | static |
| 1302 | void wait_for_sessiond(struct sock_info *sock_info) |
| 1303 | { |
| 1304 | if (ust_lock()) { |
| 1305 | goto quit; |
| 1306 | } |
| 1307 | if (wait_poll_fallback) { |
| 1308 | goto error; |
| 1309 | } |
| 1310 | if (!sock_info->wait_shm_mmap) { |
| 1311 | sock_info->wait_shm_mmap = get_map_shm(sock_info); |
| 1312 | if (!sock_info->wait_shm_mmap) |
| 1313 | goto error; |
| 1314 | } |
| 1315 | ust_unlock(); |
| 1316 | |
| 1317 | DBG("Waiting for %s apps sessiond", sock_info->name); |
| 1318 | /* Wait for futex wakeup */ |
| 1319 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap)) |
| 1320 | goto end_wait; |
| 1321 | |
| 1322 | while (futex_async((int32_t *) sock_info->wait_shm_mmap, |
| 1323 | FUTEX_WAIT, 0, NULL, NULL, 0)) { |
| 1324 | switch (errno) { |
| 1325 | case EWOULDBLOCK: |
| 1326 | /* Value already changed. */ |
| 1327 | goto end_wait; |
| 1328 | case EINTR: |
| 1329 | /* Retry if interrupted by signal. */ |
| 1330 | break; /* Get out of switch. */ |
| 1331 | case EFAULT: |
| 1332 | wait_poll_fallback = 1; |
| 1333 | DBG( |
| 1334 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " |
| 1335 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " |
| 1336 | "Please upgrade your kernel " |
| 1337 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " |
| 1338 | "mainline). LTTng-UST will use polling mode fallback."); |
| 1339 | if (ust_debug()) |
| 1340 | PERROR("futex"); |
| 1341 | goto end_wait; |
| 1342 | } |
| 1343 | } |
| 1344 | end_wait: |
| 1345 | return; |
| 1346 | |
| 1347 | quit: |
| 1348 | ust_unlock(); |
| 1349 | return; |
| 1350 | |
| 1351 | error: |
| 1352 | ust_unlock(); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * This thread does not allocate any resource, except within |
| 1358 | * handle_message, within mutex protection. This mutex protects against |
| 1359 | * fork and exit. |
| 1360 | * The other moment it allocates resources is at socket connection, which |
| 1361 | * is also protected by the mutex. |
| 1362 | */ |
| 1363 | static |
| 1364 | void *ust_listener_thread(void *arg) |
| 1365 | { |
| 1366 | struct sock_info *sock_info = arg; |
| 1367 | int sock, ret, prev_connect_failed = 0, has_waited = 0, fd; |
| 1368 | long timeout; |
| 1369 | |
| 1370 | lttng_ust_fixup_tls(); |
| 1371 | /* |
| 1372 | * If available, add '-ust' to the end of this thread's |
| 1373 | * process name |
| 1374 | */ |
| 1375 | ret = lttng_ust_setustprocname(); |
| 1376 | if (ret) { |
| 1377 | ERR("Unable to set UST process name"); |
| 1378 | } |
| 1379 | |
| 1380 | /* Restart trying to connect to the session daemon */ |
| 1381 | restart: |
| 1382 | if (prev_connect_failed) { |
| 1383 | /* Wait for sessiond availability with pipe */ |
| 1384 | wait_for_sessiond(sock_info); |
| 1385 | if (has_waited) { |
| 1386 | has_waited = 0; |
| 1387 | /* |
| 1388 | * Sleep for 5 seconds before retrying after a |
| 1389 | * sequence of failure / wait / failure. This |
| 1390 | * deals with a killed or broken session daemon. |
| 1391 | */ |
| 1392 | sleep(5); |
| 1393 | } else { |
| 1394 | has_waited = 1; |
| 1395 | } |
| 1396 | prev_connect_failed = 0; |
| 1397 | } |
| 1398 | |
| 1399 | if (ust_lock()) { |
| 1400 | goto quit; |
| 1401 | } |
| 1402 | |
| 1403 | if (sock_info->socket != -1) { |
| 1404 | /* FD tracker is updated by ustcomm_close_unix_sock() */ |
| 1405 | ret = ustcomm_close_unix_sock(sock_info->socket); |
| 1406 | if (ret) { |
| 1407 | ERR("Error closing %s ust cmd socket", |
| 1408 | sock_info->name); |
| 1409 | } |
| 1410 | sock_info->socket = -1; |
| 1411 | } |
| 1412 | if (sock_info->notify_socket != -1) { |
| 1413 | /* FD tracker is updated by ustcomm_close_unix_sock() */ |
| 1414 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); |
| 1415 | if (ret) { |
| 1416 | ERR("Error closing %s ust notify socket", |
| 1417 | sock_info->name); |
| 1418 | } |
| 1419 | sock_info->notify_socket = -1; |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | /* |
| 1424 | * Register. We need to perform both connect and sending |
| 1425 | * registration message before doing the next connect otherwise |
| 1426 | * we may reach unix socket connect queue max limits and block |
| 1427 | * on the 2nd connect while the session daemon is awaiting the |
| 1428 | * first connect registration message. |
| 1429 | */ |
| 1430 | /* Connect cmd socket */ |
| 1431 | lttng_ust_lock_fd_tracker(); |
| 1432 | ret = ustcomm_connect_unix_sock(sock_info->sock_path, |
| 1433 | get_connect_sock_timeout()); |
| 1434 | if (ret < 0) { |
| 1435 | lttng_ust_unlock_fd_tracker(); |
| 1436 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
| 1437 | prev_connect_failed = 1; |
| 1438 | |
| 1439 | /* |
| 1440 | * If we cannot find the sessiond daemon, don't delay |
| 1441 | * constructor execution. |
| 1442 | */ |
| 1443 | ret = handle_register_done(sock_info); |
| 1444 | assert(!ret); |
| 1445 | ust_unlock(); |
| 1446 | goto restart; |
| 1447 | } |
| 1448 | fd = ret; |
| 1449 | ret = lttng_ust_add_fd_to_tracker(fd); |
| 1450 | if (ret < 0) { |
| 1451 | ret = close(fd); |
| 1452 | if (ret) { |
| 1453 | PERROR("close on sock_info->socket"); |
| 1454 | } |
| 1455 | ret = -1; |
| 1456 | lttng_ust_unlock_fd_tracker(); |
| 1457 | ust_unlock(); |
| 1458 | goto quit; |
| 1459 | } |
| 1460 | |
| 1461 | sock_info->socket = ret; |
| 1462 | lttng_ust_unlock_fd_tracker(); |
| 1463 | |
| 1464 | ust_unlock(); |
| 1465 | /* |
| 1466 | * Unlock/relock ust lock because connect is blocking (with |
| 1467 | * timeout). Don't delay constructors on the ust lock for too |
| 1468 | * long. |
| 1469 | */ |
| 1470 | if (ust_lock()) { |
| 1471 | goto quit; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Create only one root handle per listener thread for the whole |
| 1476 | * process lifetime, so we ensure we get ID which is statically |
| 1477 | * assigned to the root handle. |
| 1478 | */ |
| 1479 | if (sock_info->root_handle == -1) { |
| 1480 | ret = lttng_abi_create_root_handle(); |
| 1481 | if (ret < 0) { |
| 1482 | ERR("Error creating root handle"); |
| 1483 | goto quit; |
| 1484 | } |
| 1485 | sock_info->root_handle = ret; |
| 1486 | } |
| 1487 | |
| 1488 | ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD); |
| 1489 | if (ret < 0) { |
| 1490 | ERR("Error registering to %s ust cmd socket", |
| 1491 | sock_info->name); |
| 1492 | prev_connect_failed = 1; |
| 1493 | /* |
| 1494 | * If we cannot register to the sessiond daemon, don't |
| 1495 | * delay constructor execution. |
| 1496 | */ |
| 1497 | ret = handle_register_done(sock_info); |
| 1498 | assert(!ret); |
| 1499 | ust_unlock(); |
| 1500 | goto restart; |
| 1501 | } |
| 1502 | |
| 1503 | ust_unlock(); |
| 1504 | /* |
| 1505 | * Unlock/relock ust lock because connect is blocking (with |
| 1506 | * timeout). Don't delay constructors on the ust lock for too |
| 1507 | * long. |
| 1508 | */ |
| 1509 | if (ust_lock()) { |
| 1510 | goto quit; |
| 1511 | } |
| 1512 | |
| 1513 | /* Connect notify socket */ |
| 1514 | lttng_ust_lock_fd_tracker(); |
| 1515 | ret = ustcomm_connect_unix_sock(sock_info->sock_path, |
| 1516 | get_connect_sock_timeout()); |
| 1517 | if (ret < 0) { |
| 1518 | lttng_ust_unlock_fd_tracker(); |
| 1519 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
| 1520 | prev_connect_failed = 1; |
| 1521 | |
| 1522 | /* |
| 1523 | * If we cannot find the sessiond daemon, don't delay |
| 1524 | * constructor execution. |
| 1525 | */ |
| 1526 | ret = handle_register_done(sock_info); |
| 1527 | assert(!ret); |
| 1528 | ust_unlock(); |
| 1529 | goto restart; |
| 1530 | } |
| 1531 | |
| 1532 | fd = ret; |
| 1533 | ret = lttng_ust_add_fd_to_tracker(fd); |
| 1534 | if (ret < 0) { |
| 1535 | ret = close(fd); |
| 1536 | if (ret) { |
| 1537 | PERROR("close on sock_info->notify_socket"); |
| 1538 | } |
| 1539 | ret = -1; |
| 1540 | lttng_ust_unlock_fd_tracker(); |
| 1541 | ust_unlock(); |
| 1542 | goto quit; |
| 1543 | } |
| 1544 | |
| 1545 | sock_info->notify_socket = ret; |
| 1546 | lttng_ust_unlock_fd_tracker(); |
| 1547 | |
| 1548 | ust_unlock(); |
| 1549 | /* |
| 1550 | * Unlock/relock ust lock because connect is blocking (with |
| 1551 | * timeout). Don't delay constructors on the ust lock for too |
| 1552 | * long. |
| 1553 | */ |
| 1554 | if (ust_lock()) { |
| 1555 | goto quit; |
| 1556 | } |
| 1557 | |
| 1558 | timeout = get_notify_sock_timeout(); |
| 1559 | if (timeout >= 0) { |
| 1560 | /* |
| 1561 | * Give at least 10ms to sessiond to reply to |
| 1562 | * notifications. |
| 1563 | */ |
| 1564 | if (timeout < 10) |
| 1565 | timeout = 10; |
| 1566 | ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket, |
| 1567 | timeout); |
| 1568 | if (ret < 0) { |
| 1569 | WARN("Error setting socket receive timeout"); |
| 1570 | } |
| 1571 | ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket, |
| 1572 | timeout); |
| 1573 | if (ret < 0) { |
| 1574 | WARN("Error setting socket send timeout"); |
| 1575 | } |
| 1576 | } else if (timeout < -1) { |
| 1577 | WARN("Unsupported timeout value %ld", timeout); |
| 1578 | } |
| 1579 | |
| 1580 | ret = register_to_sessiond(sock_info->notify_socket, |
| 1581 | USTCTL_SOCKET_NOTIFY); |
| 1582 | if (ret < 0) { |
| 1583 | ERR("Error registering to %s ust notify socket", |
| 1584 | sock_info->name); |
| 1585 | prev_connect_failed = 1; |
| 1586 | /* |
| 1587 | * If we cannot register to the sessiond daemon, don't |
| 1588 | * delay constructor execution. |
| 1589 | */ |
| 1590 | ret = handle_register_done(sock_info); |
| 1591 | assert(!ret); |
| 1592 | ust_unlock(); |
| 1593 | goto restart; |
| 1594 | } |
| 1595 | sock = sock_info->socket; |
| 1596 | |
| 1597 | ust_unlock(); |
| 1598 | |
| 1599 | for (;;) { |
| 1600 | ssize_t len; |
| 1601 | struct ustcomm_ust_msg lum; |
| 1602 | |
| 1603 | len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
| 1604 | switch (len) { |
| 1605 | case 0: /* orderly shutdown */ |
| 1606 | DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name); |
| 1607 | if (ust_lock()) { |
| 1608 | goto quit; |
| 1609 | } |
| 1610 | /* |
| 1611 | * Either sessiond has shutdown or refused us by closing the socket. |
| 1612 | * In either case, we don't want to delay construction execution, |
| 1613 | * and we need to wait before retry. |
| 1614 | */ |
| 1615 | prev_connect_failed = 1; |
| 1616 | /* |
| 1617 | * If we cannot register to the sessiond daemon, don't |
| 1618 | * delay constructor execution. |
| 1619 | */ |
| 1620 | ret = handle_register_done(sock_info); |
| 1621 | assert(!ret); |
| 1622 | ust_unlock(); |
| 1623 | goto end; |
| 1624 | case sizeof(lum): |
| 1625 | print_cmd(lum.cmd, lum.handle); |
| 1626 | ret = handle_message(sock_info, sock, &lum); |
| 1627 | if (ret) { |
| 1628 | ERR("Error handling message for %s socket", |
| 1629 | sock_info->name); |
| 1630 | /* |
| 1631 | * Close socket if protocol error is |
| 1632 | * detected. |
| 1633 | */ |
| 1634 | goto end; |
| 1635 | } |
| 1636 | continue; |
| 1637 | default: |
| 1638 | if (len < 0) { |
| 1639 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); |
| 1640 | } else { |
| 1641 | DBG("incorrect message size (%s socket): %zd", sock_info->name, len); |
| 1642 | } |
| 1643 | if (len == -ECONNRESET) { |
| 1644 | DBG("%s remote end closed connection", sock_info->name); |
| 1645 | goto end; |
| 1646 | } |
| 1647 | goto end; |
| 1648 | } |
| 1649 | |
| 1650 | } |
| 1651 | end: |
| 1652 | if (ust_lock()) { |
| 1653 | goto quit; |
| 1654 | } |
| 1655 | /* Cleanup socket handles before trying to reconnect */ |
| 1656 | lttng_ust_objd_table_owner_cleanup(sock_info); |
| 1657 | ust_unlock(); |
| 1658 | goto restart; /* try to reconnect */ |
| 1659 | |
| 1660 | quit: |
| 1661 | ust_unlock(); |
| 1662 | |
| 1663 | pthread_mutex_lock(&ust_exit_mutex); |
| 1664 | sock_info->thread_active = 0; |
| 1665 | pthread_mutex_unlock(&ust_exit_mutex); |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Weak symbol to call when the ust malloc wrapper is not loaded. |
| 1671 | */ |
| 1672 | __attribute__((weak)) |
| 1673 | void lttng_ust_malloc_wrapper_init(void) |
| 1674 | { |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * sessiond monitoring thread: monitor presence of global and per-user |
| 1679 | * sessiond by polling the application common named pipe. |
| 1680 | */ |
| 1681 | void __attribute__((constructor)) lttng_ust_init(void) |
| 1682 | { |
| 1683 | struct timespec constructor_timeout; |
| 1684 | sigset_t sig_all_blocked, orig_parent_mask; |
| 1685 | pthread_attr_t thread_attr; |
| 1686 | int timeout_mode; |
| 1687 | int ret; |
| 1688 | |
| 1689 | if (uatomic_xchg(&initialized, 1) == 1) |
| 1690 | return; |
| 1691 | |
| 1692 | /* |
| 1693 | * Fixup interdependency between TLS fixup mutex (which happens |
| 1694 | * to be the dynamic linker mutex) and ust_lock, taken within |
| 1695 | * the ust lock. |
| 1696 | */ |
| 1697 | lttng_ust_fixup_tls(); |
| 1698 | |
| 1699 | lttng_ust_loaded = 1; |
| 1700 | |
| 1701 | /* |
| 1702 | * We want precise control over the order in which we construct |
| 1703 | * our sub-libraries vs starting to receive commands from |
| 1704 | * sessiond (otherwise leading to errors when trying to create |
| 1705 | * sessiond before the init functions are completed). |
| 1706 | */ |
| 1707 | init_usterr(); |
| 1708 | lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */ |
| 1709 | init_tracepoint(); |
| 1710 | lttng_ust_init_fd_tracker(); |
| 1711 | lttng_ust_clock_init(); |
| 1712 | lttng_ust_getcpu_init(); |
| 1713 | lttng_ust_statedump_init(); |
| 1714 | lttng_ring_buffer_metadata_client_init(); |
| 1715 | lttng_ring_buffer_client_overwrite_init(); |
| 1716 | lttng_ring_buffer_client_overwrite_rt_init(); |
| 1717 | lttng_ring_buffer_client_discard_init(); |
| 1718 | lttng_ring_buffer_client_discard_rt_init(); |
| 1719 | lttng_perf_counter_init(); |
| 1720 | /* |
| 1721 | * Invoke ust malloc wrapper init before starting other threads. |
| 1722 | */ |
| 1723 | lttng_ust_malloc_wrapper_init(); |
| 1724 | |
| 1725 | timeout_mode = get_constructor_timeout(&constructor_timeout); |
| 1726 | |
| 1727 | get_allow_blocking(); |
| 1728 | |
| 1729 | ret = sem_init(&constructor_wait, 0, 0); |
| 1730 | if (ret) { |
| 1731 | PERROR("sem_init"); |
| 1732 | } |
| 1733 | |
| 1734 | ret = setup_local_apps(); |
| 1735 | if (ret) { |
| 1736 | DBG("local apps setup returned %d", ret); |
| 1737 | } |
| 1738 | |
| 1739 | /* A new thread created by pthread_create inherits the signal mask |
| 1740 | * from the parent. To avoid any signal being received by the |
| 1741 | * listener thread, we block all signals temporarily in the parent, |
| 1742 | * while we create the listener thread. |
| 1743 | */ |
| 1744 | sigfillset(&sig_all_blocked); |
| 1745 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); |
| 1746 | if (ret) { |
| 1747 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 1748 | } |
| 1749 | |
| 1750 | ret = pthread_attr_init(&thread_attr); |
| 1751 | if (ret) { |
| 1752 | ERR("pthread_attr_init: %s", strerror(ret)); |
| 1753 | } |
| 1754 | ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); |
| 1755 | if (ret) { |
| 1756 | ERR("pthread_attr_setdetachstate: %s", strerror(ret)); |
| 1757 | } |
| 1758 | |
| 1759 | pthread_mutex_lock(&ust_exit_mutex); |
| 1760 | ret = pthread_create(&global_apps.ust_listener, &thread_attr, |
| 1761 | ust_listener_thread, &global_apps); |
| 1762 | if (ret) { |
| 1763 | ERR("pthread_create global: %s", strerror(ret)); |
| 1764 | } |
| 1765 | global_apps.thread_active = 1; |
| 1766 | pthread_mutex_unlock(&ust_exit_mutex); |
| 1767 | |
| 1768 | if (local_apps.allowed) { |
| 1769 | pthread_mutex_lock(&ust_exit_mutex); |
| 1770 | ret = pthread_create(&local_apps.ust_listener, &thread_attr, |
| 1771 | ust_listener_thread, &local_apps); |
| 1772 | if (ret) { |
| 1773 | ERR("pthread_create local: %s", strerror(ret)); |
| 1774 | } |
| 1775 | local_apps.thread_active = 1; |
| 1776 | pthread_mutex_unlock(&ust_exit_mutex); |
| 1777 | } else { |
| 1778 | handle_register_done(&local_apps); |
| 1779 | } |
| 1780 | ret = pthread_attr_destroy(&thread_attr); |
| 1781 | if (ret) { |
| 1782 | ERR("pthread_attr_destroy: %s", strerror(ret)); |
| 1783 | } |
| 1784 | |
| 1785 | /* Restore original signal mask in parent */ |
| 1786 | ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); |
| 1787 | if (ret) { |
| 1788 | ERR("pthread_sigmask: %s", strerror(ret)); |
| 1789 | } |
| 1790 | |
| 1791 | switch (timeout_mode) { |
| 1792 | case 1: /* timeout wait */ |
| 1793 | do { |
| 1794 | ret = sem_timedwait(&constructor_wait, |
| 1795 | &constructor_timeout); |
| 1796 | } while (ret < 0 && errno == EINTR); |
| 1797 | if (ret < 0) { |
| 1798 | switch (errno) { |
| 1799 | case ETIMEDOUT: |
| 1800 | ERR("Timed out waiting for lttng-sessiond"); |
| 1801 | break; |
| 1802 | case EINVAL: |
| 1803 | PERROR("sem_timedwait"); |
| 1804 | break; |
| 1805 | default: |
| 1806 | ERR("Unexpected error \"%s\" returned by sem_timedwait", |
| 1807 | strerror(errno)); |
| 1808 | } |
| 1809 | } |
| 1810 | break; |
| 1811 | case -1:/* wait forever */ |
| 1812 | do { |
| 1813 | ret = sem_wait(&constructor_wait); |
| 1814 | } while (ret < 0 && errno == EINTR); |
| 1815 | if (ret < 0) { |
| 1816 | switch (errno) { |
| 1817 | case EINVAL: |
| 1818 | PERROR("sem_wait"); |
| 1819 | break; |
| 1820 | default: |
| 1821 | ERR("Unexpected error \"%s\" returned by sem_wait", |
| 1822 | strerror(errno)); |
| 1823 | } |
| 1824 | } |
| 1825 | break; |
| 1826 | case 0: /* no timeout */ |
| 1827 | break; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | static |
| 1832 | void lttng_ust_cleanup(int exiting) |
| 1833 | { |
| 1834 | cleanup_sock_info(&global_apps, exiting); |
| 1835 | cleanup_sock_info(&local_apps, exiting); |
| 1836 | local_apps.allowed = 0; |
| 1837 | /* |
| 1838 | * The teardown in this function all affect data structures |
| 1839 | * accessed under the UST lock by the listener thread. This |
| 1840 | * lock, along with the lttng_ust_comm_should_quit flag, ensure |
| 1841 | * that none of these threads are accessing this data at this |
| 1842 | * point. |
| 1843 | */ |
| 1844 | lttng_ust_abi_exit(); |
| 1845 | lttng_ust_events_exit(); |
| 1846 | lttng_perf_counter_exit(); |
| 1847 | lttng_ring_buffer_client_discard_rt_exit(); |
| 1848 | lttng_ring_buffer_client_discard_exit(); |
| 1849 | lttng_ring_buffer_client_overwrite_rt_exit(); |
| 1850 | lttng_ring_buffer_client_overwrite_exit(); |
| 1851 | lttng_ring_buffer_metadata_client_exit(); |
| 1852 | lttng_ust_statedump_destroy(); |
| 1853 | exit_tracepoint(); |
| 1854 | if (!exiting) { |
| 1855 | /* Reinitialize values for fork */ |
| 1856 | sem_count = 2; |
| 1857 | lttng_ust_comm_should_quit = 0; |
| 1858 | initialized = 0; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | void __attribute__((destructor)) lttng_ust_exit(void) |
| 1863 | { |
| 1864 | int ret; |
| 1865 | |
| 1866 | /* |
| 1867 | * Using pthread_cancel here because: |
| 1868 | * A) we don't want to hang application teardown. |
| 1869 | * B) the thread is not allocating any resource. |
| 1870 | */ |
| 1871 | |
| 1872 | /* |
| 1873 | * Require the communication thread to quit. Synchronize with |
| 1874 | * mutexes to ensure it is not in a mutex critical section when |
| 1875 | * pthread_cancel is later called. |
| 1876 | */ |
| 1877 | ust_lock_nocheck(); |
| 1878 | lttng_ust_comm_should_quit = 1; |
| 1879 | ust_unlock(); |
| 1880 | |
| 1881 | pthread_mutex_lock(&ust_exit_mutex); |
| 1882 | /* cancel threads */ |
| 1883 | if (global_apps.thread_active) { |
| 1884 | ret = pthread_cancel(global_apps.ust_listener); |
| 1885 | if (ret) { |
| 1886 | ERR("Error cancelling global ust listener thread: %s", |
| 1887 | strerror(ret)); |
| 1888 | } else { |
| 1889 | global_apps.thread_active = 0; |
| 1890 | } |
| 1891 | } |
| 1892 | if (local_apps.thread_active) { |
| 1893 | ret = pthread_cancel(local_apps.ust_listener); |
| 1894 | if (ret) { |
| 1895 | ERR("Error cancelling local ust listener thread: %s", |
| 1896 | strerror(ret)); |
| 1897 | } else { |
| 1898 | local_apps.thread_active = 0; |
| 1899 | } |
| 1900 | } |
| 1901 | pthread_mutex_unlock(&ust_exit_mutex); |
| 1902 | |
| 1903 | /* |
| 1904 | * Do NOT join threads: use of sys_futex makes it impossible to |
| 1905 | * join the threads without using async-cancel, but async-cancel |
| 1906 | * is delivered by a signal, which could hit the target thread |
| 1907 | * anywhere in its code path, including while the ust_lock() is |
| 1908 | * held, causing a deadlock for the other thread. Let the OS |
| 1909 | * cleanup the threads if there are stalled in a syscall. |
| 1910 | */ |
| 1911 | lttng_ust_cleanup(1); |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * We exclude the worker threads across fork and clone (except |
| 1916 | * CLONE_VM), because these system calls only keep the forking thread |
| 1917 | * running in the child. Therefore, we don't want to call fork or clone |
| 1918 | * in the middle of an tracepoint or ust tracing state modification. |
| 1919 | * Holding this mutex protects these structures across fork and clone. |
| 1920 | */ |
| 1921 | void ust_before_fork(sigset_t *save_sigset) |
| 1922 | { |
| 1923 | /* |
| 1924 | * Disable signals. This is to avoid that the child intervenes |
| 1925 | * before it is properly setup for tracing. It is safer to |
| 1926 | * disable all signals, because then we know we are not breaking |
| 1927 | * anything by restoring the original mask. |
| 1928 | */ |
| 1929 | sigset_t all_sigs; |
| 1930 | int ret; |
| 1931 | |
| 1932 | /* Fixup lttng-ust TLS. */ |
| 1933 | lttng_ust_fixup_tls(); |
| 1934 | |
| 1935 | if (URCU_TLS(lttng_ust_nest_count)) |
| 1936 | return; |
| 1937 | /* Disable signals */ |
| 1938 | sigfillset(&all_sigs); |
| 1939 | ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); |
| 1940 | if (ret == -1) { |
| 1941 | PERROR("sigprocmask"); |
| 1942 | } |
| 1943 | |
| 1944 | pthread_mutex_lock(&ust_fork_mutex); |
| 1945 | |
| 1946 | ust_lock_nocheck(); |
| 1947 | rcu_bp_before_fork(); |
| 1948 | } |
| 1949 | |
| 1950 | static void ust_after_fork_common(sigset_t *restore_sigset) |
| 1951 | { |
| 1952 | int ret; |
| 1953 | |
| 1954 | DBG("process %d", getpid()); |
| 1955 | ust_unlock(); |
| 1956 | |
| 1957 | pthread_mutex_unlock(&ust_fork_mutex); |
| 1958 | |
| 1959 | /* Restore signals */ |
| 1960 | ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL); |
| 1961 | if (ret == -1) { |
| 1962 | PERROR("sigprocmask"); |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | void ust_after_fork_parent(sigset_t *restore_sigset) |
| 1967 | { |
| 1968 | if (URCU_TLS(lttng_ust_nest_count)) |
| 1969 | return; |
| 1970 | DBG("process %d", getpid()); |
| 1971 | rcu_bp_after_fork_parent(); |
| 1972 | /* Release mutexes and reenable signals */ |
| 1973 | ust_after_fork_common(restore_sigset); |
| 1974 | } |
| 1975 | |
| 1976 | /* |
| 1977 | * After fork, in the child, we need to cleanup all the leftover state, |
| 1978 | * except the worker thread which already magically disappeared thanks |
| 1979 | * to the weird Linux fork semantics. After tyding up, we call |
| 1980 | * lttng_ust_init() again to start over as a new PID. |
| 1981 | * |
| 1982 | * This is meant for forks() that have tracing in the child between the |
| 1983 | * fork and following exec call (if there is any). |
| 1984 | */ |
| 1985 | void ust_after_fork_child(sigset_t *restore_sigset) |
| 1986 | { |
| 1987 | if (URCU_TLS(lttng_ust_nest_count)) |
| 1988 | return; |
| 1989 | lttng_context_vpid_reset(); |
| 1990 | lttng_context_vtid_reset(); |
| 1991 | DBG("process %d", getpid()); |
| 1992 | /* Release urcu mutexes */ |
| 1993 | rcu_bp_after_fork_child(); |
| 1994 | lttng_ust_cleanup(0); |
| 1995 | /* Release mutexes and reenable signals */ |
| 1996 | ust_after_fork_common(restore_sigset); |
| 1997 | lttng_ust_init(); |
| 1998 | } |
| 1999 | |
| 2000 | void lttng_ust_sockinfo_session_enabled(void *owner) |
| 2001 | { |
| 2002 | struct sock_info *sock_info = owner; |
| 2003 | sock_info->statedump_pending = 1; |
| 2004 | } |