X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=liblttng-ust%2Flttng-ust-comm.c;h=78201835719d36d82d2bc20fc9d0a07bf7f3957b;hb=7bc53e94a229963972aa78880b361b1510fdd268;hp=1815801a8fe08a06df22b6d1614efc969ca75b4d;hpb=e3feda7db3de3530e7c9c511536c36cccf317013;p=lttng-ust.git diff --git a/liblttng-ust/lttng-ust-comm.c b/liblttng-ust/lttng-ust-comm.c index 1815801a..78201835 100644 --- a/liblttng-ust/lttng-ust-comm.c +++ b/liblttng-ust/lttng-ust-comm.c @@ -41,8 +41,10 @@ #include #include #include +#include #include #include +#include #include "tracepoint-internal.h" #include "ltt-tracer-core.h" #include "compat.h" @@ -78,6 +80,12 @@ static sem_t constructor_wait; */ static int sem_count = { 2 }; +/* + * Counting nesting within lttng-ust. Used to ensure that calling fork() + * from liblttng-ust does not execute the pre/post fork handlers. + */ +static int __thread lttng_ust_nest_count; + /* * Info about socket and associated listener thread. */ @@ -130,6 +138,15 @@ extern void ltt_ring_buffer_client_overwrite_exit(void); extern void ltt_ring_buffer_client_discard_exit(void); extern void ltt_ring_buffer_metadata_client_exit(void); +/* + * Force a read (imply TLS fixup for dlopen) of TLS variables. + */ +static +void lttng_fixup_nest_count_tls(void) +{ + asm volatile ("" : : "m" (lttng_ust_nest_count)); +} + static int setup_local_apps(void) { @@ -141,14 +158,16 @@ int setup_local_apps(void) * Disallow per-user tracing for setuid binaries. */ if (uid != geteuid()) { - local_apps.allowed = 0; + assert(local_apps.allowed == 0); return 0; - } else { - local_apps.allowed = 1; } home_dir = (const char *) getenv("HOME"); - if (!home_dir) + if (!home_dir) { + WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing."); + assert(local_apps.allowed == 0); return -ENOENT; + } + local_apps.allowed = 1; snprintf(local_apps.sock_path, PATH_MAX, DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); snprintf(local_apps.wait_shm_path, PATH_MAX, @@ -196,15 +215,15 @@ int send_reply(int sock, struct ustcomm_ust_reply *lur) case sizeof(*lur): DBG("message successfully sent"); return 0; - case -1: - if (errno == ECONNRESET) { - printf("remote end closed connection\n"); + default: + if (len == -ECONNRESET) { + DBG("remote end closed connection"); return 0; } - return -1; - default: - printf("incorrect message size: %zd\n", len); - return -1; + if (len < 0) + return len; + DBG("incorrect message size: %zd", len); + return -EINVAL; } } @@ -236,6 +255,7 @@ int handle_message(struct sock_info *sock_info, struct ustcomm_ust_reply lur; int shm_fd, wait_fd; union ust_args args; + ssize_t len; ust_lock(); @@ -265,6 +285,74 @@ int handle_message(struct sock_info *sock_info, else ret = lttng_ust_objd_unref(lum->handle); break; + case LTTNG_UST_FILTER: + { + /* Receive filter data */ + struct lttng_ust_filter_bytecode *bytecode; + + if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) { + ERR("Filter data size is too large: %u bytes", + lum->u.filter.data_size); + ret = -EINVAL; + goto error; + } + + if (lum->u.filter.reloc_offset > lum->u.filter.data_size) { + ERR("Filter reloc offset %u is not within data", + lum->u.filter.reloc_offset); + ret = -EINVAL; + goto error; + } + + bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size); + if (!bytecode) { + ret = -ENOMEM; + goto error; + } + len = ustcomm_recv_unix_sock(sock, bytecode->data, + lum->u.filter.data_size); + switch (len) { + case 0: /* orderly shutdown */ + ret = 0; + free(bytecode); + goto error; + default: + if (len == lum->u.filter.data_size) { + DBG("filter data received"); + break; + } else if (len < 0) { + DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); + if (len == -ECONNRESET) { + ERR("%s remote end closed connection", sock_info->name); + ret = len; + free(bytecode); + goto error; + } + ret = len; + goto end; + } else { + DBG("incorrect filter data message size: %zd", len); + ret = -EINVAL; + free(bytecode); + goto end; + } + } + bytecode->len = lum->u.filter.data_size; + bytecode->reloc_offset = lum->u.filter.reloc_offset; + if (ops->cmd) { + ret = ops->cmd(lum->handle, lum->cmd, + (unsigned long) bytecode, + &args); + if (ret) { + free(bytecode); + } + /* don't free bytecode if everything went fine. */ + } else { + ret = -ENOSYS; + free(bytecode); + } + break; + } default: if (ops->cmd) ret = ops->cmd(lum->handle, lum->cmd, @@ -280,10 +368,18 @@ end: lur.cmd = lum->cmd; lur.ret_val = ret; if (ret >= 0) { - lur.ret_code = USTCOMM_OK; + lur.ret_code = LTTNG_UST_OK; } else { - //lur.ret_code = USTCOMM_SESSION_FAIL; - lur.ret_code = ret; + /* + * Use -LTTNG_UST_ERR as wildcard for UST internal + * error that are not caused by the transport, except if + * we already have a more precise error message to + * report. + */ + if (ret > -LTTNG_UST_ERR) + lur.ret_code = -LTTNG_UST_ERR; + else + lur.ret_code = ret; } if (ret >= 0) { switch (lum->cmd) { @@ -312,22 +408,28 @@ end: } ret = send_reply(sock, &lur); if (ret < 0) { - perror("error sending reply"); + DBG("error sending reply"); goto error; } if ((lum->cmd == LTTNG_UST_STREAM || lum->cmd == LTTNG_UST_CHANNEL || lum->cmd == LTTNG_UST_METADATA) - && lur.ret_code == USTCOMM_OK) { + && lur.ret_code == LTTNG_UST_OK) { + int sendret = 0; + /* we also need to send the file descriptors. */ ret = ustcomm_send_fds_unix_sock(sock, &shm_fd, &shm_fd, 1, sizeof(int)); if (ret < 0) { - perror("send shm_fd"); - goto error; + ERR("send shm_fd"); + sendret = ret; } + /* + * The sessiond expects 2 file descriptors, even upon + * error. + */ ret = ustcomm_send_fds_unix_sock(sock, &wait_fd, &wait_fd, 1, sizeof(int)); @@ -335,6 +437,30 @@ end: perror("send wait_fd"); goto error; } + if (sendret) { + ret = sendret; + goto error; + } + } + /* + * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field + * after the reply. + */ + if (lur.ret_code == LTTNG_UST_OK) { + switch (lum->cmd) { + case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: + len = ustcomm_send_unix_sock(sock, + &args.field_list.entry, + sizeof(args.field_list.entry)); + if (len < 0) { + ret = len; + goto error; + } + if (len != sizeof(args.field_list.entry)) { + ret = -EINVAL; + goto error; + } + } } /* * We still have the memory map reference, and the fds have been @@ -342,7 +468,7 @@ end: * that we keep the write side of the wait_fd open, but close * the read side. */ - if (lur.ret_code == USTCOMM_OK) { + if (lur.ret_code == LTTNG_UST_OK) { switch (lum->cmd) { case LTTNG_UST_STREAM: if (shm_fd >= 0) { @@ -391,7 +517,7 @@ void cleanup_sock_info(struct sock_info *sock_info, int exiting) int ret; if (sock_info->socket != -1) { - ret = close(sock_info->socket); + ret = ustcomm_close_unix_sock(sock_info->socket); if (ret) { ERR("Error closing apps socket"); } @@ -454,7 +580,9 @@ int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) * If the open failed because the file did not exist, try * creating it ourself. */ + lttng_ust_nest_count++; pid = fork(); + lttng_ust_nest_count--; if (pid > 0) { int status; @@ -503,9 +631,9 @@ int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) ret = ftruncate(wait_shm_fd, mmap_size); if (ret) { PERROR("ftruncate"); - exit(EXIT_FAILURE); + _exit(EXIT_FAILURE); } - exit(EXIT_SUCCESS); + _exit(EXIT_SUCCESS); } /* * For local shm, we need to have rw access to accept @@ -517,13 +645,13 @@ int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) */ if (!sock_info->global && errno != EACCES) { ERR("Error opening shm %s", sock_info->wait_shm_path); - exit(EXIT_FAILURE); + _exit(EXIT_FAILURE); } /* * The shm exists, but we cannot open it RW. Report * success. */ - exit(EXIT_SUCCESS); + _exit(EXIT_SUCCESS); } else { return -1; } @@ -637,7 +765,7 @@ error: * This thread does not allocate any resource, except within * handle_message, within mutex protection. This mutex protects against * fork and exit. - * The other moment it allocates resources is at socket connexion, which + * The other moment it allocates resources is at socket connection, which * is also protected by the mutex. */ static @@ -671,7 +799,7 @@ restart: } if (sock_info->socket != -1) { - ret = close(sock_info->socket); + ret = ustcomm_close_unix_sock(sock_info->socket); if (ret) { ERR("Error closing %s apps socket", sock_info->name); } @@ -731,7 +859,7 @@ restart: len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); switch (len) { case 0: /* orderly shutdown */ - DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); + DBG("%s ltt-sessiond has performed an orderly shutdown", sock_info->name); ust_lock(); /* * Either sessiond has shutdown or refused us by closing the socket. @@ -748,22 +876,23 @@ restart: ust_unlock(); goto end; case sizeof(lum): - DBG("message received\n"); + DBG("message received"); ret = handle_message(sock_info, sock, &lum); - if (ret < 0) { + if (ret) { ERR("Error handling message for %s socket", sock_info->name); } continue; - case -1: - DBG("Receive failed from lttng-sessiond with errno %d", errno); - if (errno == ECONNRESET) { - ERR("%s remote end closed connection\n", sock_info->name); + default: + if (len < 0) { + DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); + } else { + DBG("incorrect message size (%s socket): %zd", sock_info->name, len); + } + if (len == -ECONNRESET) { + DBG("%s remote end closed connection", sock_info->name); goto end; } goto end; - default: - ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); - continue; } } @@ -817,11 +946,11 @@ int get_timeout(struct timespec *constructor_timeout) * sessiond monitoring thread: monitor presence of global and per-user * sessiond by polling the application common named pipe. */ -/* TODO */ - void __attribute__((constructor)) lttng_ust_init(void) { struct timespec constructor_timeout; + sigset_t sig_all_blocked, orig_parent_mask; + pthread_attr_t thread_attr; int timeout_mode; int ret; @@ -836,6 +965,8 @@ void __attribute__((constructor)) lttng_ust_init(void) lttng_fixup_event_tls(); lttng_fixup_ringbuffer_tls(); lttng_fixup_vtid_tls(); + lttng_fixup_nest_count_tls(); + lttng_fixup_procname_tls(); /* * We want precise control over the order in which we construct @@ -856,17 +987,53 @@ void __attribute__((constructor)) lttng_ust_init(void) ret = setup_local_apps(); if (ret) { - ERR("Error setting up to local apps"); + DBG("local apps setup returned %d", ret); } - ret = pthread_create(&local_apps.ust_listener, NULL, - ust_listener_thread, &local_apps); + /* A new thread created by pthread_create inherits the signal mask + * from the parent. To avoid any signal being received by the + * listener thread, we block all signals temporarily in the parent, + * while we create the listener thread. + */ + sigfillset(&sig_all_blocked); + ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); + if (ret) { + ERR("pthread_sigmask: %s", strerror(ret)); + } + + ret = pthread_attr_init(&thread_attr); + if (ret) { + ERR("pthread_attr_init: %s", strerror(ret)); + } + ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + if (ret) { + ERR("pthread_attr_setdetachstate: %s", strerror(ret)); + } + + ret = pthread_create(&global_apps.ust_listener, &thread_attr, + ust_listener_thread, &global_apps); + if (ret) { + ERR("pthread_create global: %s", strerror(ret)); + } if (local_apps.allowed) { - ret = pthread_create(&global_apps.ust_listener, NULL, - ust_listener_thread, &global_apps); + ret = pthread_create(&local_apps.ust_listener, &thread_attr, + ust_listener_thread, &local_apps); + if (ret) { + ERR("pthread_create local: %s", strerror(ret)); + } } else { handle_register_done(&local_apps); } + ret = pthread_attr_destroy(&thread_attr); + if (ret) { + ERR("pthread_attr_destroy: %s", strerror(ret)); + } + + /* Restore original signal mask in parent */ + ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); + if (ret) { + ERR("pthread_sigmask: %s", strerror(ret)); + } switch (timeout_mode) { case 1: /* timeout wait */ @@ -941,12 +1108,14 @@ void __attribute__((destructor)) lttng_ust_exit(void) /* cancel threads */ ret = pthread_cancel(global_apps.ust_listener); if (ret) { - ERR("Error cancelling global ust listener thread"); + ERR("Error cancelling global ust listener thread: %s", + strerror(ret)); } if (local_apps.allowed) { ret = pthread_cancel(local_apps.ust_listener); if (ret) { - ERR("Error cancelling local ust listener thread"); + ERR("Error cancelling local ust listener thread: %s", + strerror(ret)); } } /* @@ -978,6 +1147,8 @@ void ust_before_fork(sigset_t *save_sigset) sigset_t all_sigs; int ret; + if (lttng_ust_nest_count) + return; /* Disable signals */ sigfillset(&all_sigs); ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); @@ -1003,6 +1174,8 @@ static void ust_after_fork_common(sigset_t *restore_sigset) void ust_after_fork_parent(sigset_t *restore_sigset) { + if (lttng_ust_nest_count) + return; DBG("process %d", getpid()); rcu_bp_after_fork_parent(); /* Release mutexes and reenable signals */ @@ -1020,6 +1193,8 @@ void ust_after_fork_parent(sigset_t *restore_sigset) */ void ust_after_fork_child(sigset_t *restore_sigset) { + if (lttng_ust_nest_count) + return; DBG("process %d", getpid()); /* Release urcu mutexes */ rcu_bp_after_fork_child();