Fix: Conditionally disable tests requiring shared libs support
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index 69414ebd34cc338b4f9f10a3ce02f3af89003239..59b9a98f4d74647b3cab1871148eeb451104a926 100644 (file)
@@ -43,6 +43,7 @@
 #include <lttng/ust.h>
 #include <ust-comm.h>
 #include <usterr-signal-safe.h>
+#include <helper.h>
 #include "tracepoint-internal.h"
 #include "ltt-tracer-core.h"
 #include "compat.h"
@@ -156,14 +157,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,
@@ -284,29 +287,40 @@ int handle_message(struct sock_info *sock_info,
        case LTTNG_UST_FILTER:
        {
                /* Receive filter data */
-               struct {
-                       uint16_t len;
-                       uint16_t reloc_offset;
-                       char data[FILTER_BYTECODE_MAX_LEN];
-               } filter_data;
+               struct lttng_ust_filter_bytecode *bytecode;
 
-               if (lum->u.filter.data_size > 65536) {
+               if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
                        ERR("Filter data size is too large: %u bytes\n",
                                lum->u.filter.data_size);
                        ret = -EINVAL;
                        goto error;
                }
-               len = ustcomm_recv_unix_sock(sock, filter_data.data,
+
+               if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
+                       ERR("Filter reloc offset %u is not within data\n",
+                               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;
                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);
                                ret = -EINVAL;
+                               free(bytecode);
                                goto error;
                        }
                        ret = -EINVAL;
@@ -318,17 +332,24 @@ int handle_message(struct sock_info *sock_info,
                        } else {
                                ERR("incorrect filter data message size: %zd\n", len);
                                ret = -EINVAL;
+                               free(bytecode);
                                goto end;
                        }
                }
-               filter_data.len = lum->u.filter.data_size;
-               filter_data.reloc_offset = lum->u.filter.reloc_offset;
-               if (ops->cmd)
+               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) &filter_data,
+                                       (unsigned long) bytecode,
                                        &args);
-               else
+                       if (ret) {
+                               free(bytecode);
+                       }
+                       /* don't free bytecode if everything went fine. */
+               } else {
                        ret = -ENOSYS;
+                       free(bytecode);
+               }
                break;
        }
        default:
@@ -597,9 +618,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
@@ -611,13 +632,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;
        }
@@ -917,6 +938,7 @@ 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;
 
@@ -932,6 +954,7 @@ void __attribute__((constructor)) lttng_ust_init(void)
        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
@@ -952,7 +975,7 @@ 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);
        }
 
        /* A new thread created by pthread_create inherits the signal mask
@@ -966,13 +989,22 @@ void __attribute__((constructor)) lttng_ust_init(void)
                ERR("pthread_sigmask: %s", strerror(ret));
        }
 
-       ret = pthread_create(&global_apps.ust_listener, NULL,
+       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(&local_apps.ust_listener, NULL,
+               ret = pthread_create(&local_apps.ust_listener, &thread_attr,
                                ust_listener_thread, &local_apps);
                if (ret) {
                        ERR("pthread_create local: %s", strerror(ret));
@@ -980,6 +1012,10 @@ void __attribute__((constructor)) lttng_ust_init(void)
        } 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);
This page took 0.028362 seconds and 4 git commands to generate.