Fix: cleanup local_apps.allowed flag on lib cleanup
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index 0a3de32263c068fbffa7c2968218df8dcc14c8a9..a5a2cc2b689ab9331da4ff2de3575bf8edd901bc 100644 (file)
@@ -106,6 +106,14 @@ static pthread_mutex_t ust_fork_mutex = PTHREAD_MUTEX_INITIALIZER;
 /* Should the ust comm thread quit ? */
 static int lttng_ust_comm_should_quit;
 
+/*
+ * This variable can be tested by applications to check whether
+ * lttng-ust is loaded. They simply have to define their own
+ * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
+ * library constructor.
+ */
+int lttng_ust_loaded __attribute__((weak));
+
 /*
  * Return 0 on success, -1 if should quit.
  * The lock is taken in both cases.
@@ -329,6 +337,24 @@ extern void lttng_ring_buffer_client_discard_exit(void);
 extern void lttng_ring_buffer_client_discard_rt_exit(void);
 extern void lttng_ring_buffer_metadata_client_exit(void);
 
+ssize_t lttng_ust_read(int fd, void *buf, size_t len)
+{
+       ssize_t ret;
+       size_t copied = 0, to_copy = len;
+
+       do {
+               ret = read(fd, buf + copied, to_copy);
+               if (ret > 0) {
+                       copied += ret;
+                       to_copy -= ret;
+               }
+       } while ((ret > 0 && to_copy > 0)
+               || (ret < 0 && errno == EINTR));
+       if (ret > 0) {
+               ret = copied;
+       }
+       return ret;
+}
 /*
  * Returns the HOME directory path. Caller MUST NOT free(3) the returned
  * pointer.
@@ -437,6 +463,9 @@ long get_timeout(void)
        }
        if (str_timeout)
                constructor_delay_ms = strtol(str_timeout, NULL, 10);
+       /* All negative values are considered as "-1". */
+       if (constructor_delay_ms < -1)
+               constructor_delay_ms = -1;
        return constructor_delay_ms;
 }
 
@@ -570,6 +599,7 @@ int handle_message(struct sock_info *sock_info,
        const struct lttng_ust_objd_ops *ops;
        struct ustcomm_ust_reply lur;
        union ust_args args;
+       char ctxstr[LTTNG_UST_SYM_NAME_LEN];    /* App context string. */
        ssize_t len;
 
        memset(&lur, 0, sizeof(lur));
@@ -789,6 +819,64 @@ int handle_message(struct sock_info *sock_info,
                        ret = -ENOSYS;
                break;
        }
+       case LTTNG_UST_CONTEXT:
+               switch (lum->u.context.ctx) {
+               case LTTNG_UST_CONTEXT_APP_CONTEXT:
+               {
+                       char *p;
+                       size_t ctxlen, recvlen;
+
+                       ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1
+                                       + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len;
+                       if (ctxlen >= LTTNG_UST_SYM_NAME_LEN) {
+                               ERR("Application context string length size is too large: %zu bytes",
+                                       ctxlen);
+                               ret = -EINVAL;
+                               goto error;
+                       }
+                       strcpy(ctxstr, "$app.");
+                       p = &ctxstr[strlen("$app.")];
+                       recvlen = ctxlen - strlen("$app.");
+                       len = ustcomm_recv_unix_sock(sock, p, recvlen);
+                       switch (len) {
+                       case 0: /* orderly shutdown */
+                               ret = 0;
+                               goto error;
+                       default:
+                               if (len == recvlen) {
+                                       DBG("app context 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;
+                                               goto error;
+                                       }
+                                       ret = len;
+                                       goto error;
+                               } else {
+                                       DBG("incorrect app context data message size: %zd", len);
+                                       ret = -EINVAL;
+                                       goto error;
+                               }
+                       }
+                       /* Put : between provider and ctxname. */
+                       p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':';
+                       args.app_context.ctxname = ctxstr;
+                       break;
+               }
+               default:
+                       break;
+               }
+               if (ops->cmd) {
+                       ret = ops->cmd(lum->handle, lum->cmd,
+                                       (unsigned long) &lum->u,
+                                       &args, sock_info);
+               } else {
+                       ret = -ENOSYS;
+               }
+               break;
        default:
                if (ops->cmd)
                        ret = ops->cmd(lum->handle, lum->cmd,
@@ -930,7 +1018,12 @@ void cleanup_sock_info(struct sock_info *sock_info, int exiting)
                long page_size;
 
                page_size = sysconf(_SC_PAGE_SIZE);
-               if (page_size > 0) {
+               if (page_size <= 0) {
+                       if (!page_size) {
+                               errno = EINVAL;
+                       }
+                       PERROR("Error in sysconf(_SC_PAGE_SIZE)");
+               } else {
                        ret = munmap(sock_info->wait_shm_mmap, page_size);
                        if (ret) {
                                ERR("Error unmapping wait shm");
@@ -1112,7 +1205,11 @@ char *get_map_shm(struct sock_info *sock_info)
        char *wait_shm_mmap;
 
        page_size = sysconf(_SC_PAGE_SIZE);
-       if (page_size < 0) {
+       if (page_size <= 0) {
+               if (!page_size) {
+                       errno = EINVAL;
+               }
+               PERROR("Error in sysconf(_SC_PAGE_SIZE)");
                goto error;
        }
 
@@ -1206,6 +1303,15 @@ void *ust_listener_thread(void *arg)
        int sock, ret, prev_connect_failed = 0, has_waited = 0;
        long timeout;
 
+       /*
+        * If available, add '-ust' to the end of this thread's
+        * process name
+        */
+       ret = lttng_ust_setustprocname();
+       if (ret) {
+               ERR("Unable to set UST process name");
+       }
+
        /* Restart trying to connect to the session daemon */
 restart:
        if (prev_connect_failed) {
@@ -1219,8 +1325,9 @@ restart:
                         * deals with a killed or broken session daemon.
                         */
                        sleep(5);
+               } else {
+                       has_waited = 1;
                }
-               has_waited = 1;
                prev_connect_failed = 0;
        }
 
@@ -1475,6 +1582,8 @@ void __attribute__((constructor)) lttng_ust_init(void)
        lttng_fixup_procname_tls();
        lttng_fixup_ust_mutex_nest_tls();
 
+       lttng_ust_loaded = 1;
+
        /*
         * We want precise control over the order in which we construct
         * our sub-libraries vs starting to receive commands from
@@ -1492,7 +1601,6 @@ void __attribute__((constructor)) lttng_ust_init(void)
        lttng_ring_buffer_client_discard_init();
        lttng_ring_buffer_client_discard_rt_init();
        lttng_perf_counter_init();
-       lttng_context_init();
        /*
         * Invoke ust malloc wrapper init before starting other threads.
         */
@@ -1501,7 +1609,9 @@ void __attribute__((constructor)) lttng_ust_init(void)
        timeout_mode = get_constructor_timeout(&constructor_timeout);
 
        ret = sem_init(&constructor_wait, 0, 0);
-       assert(!ret);
+       if (ret) {
+               PERROR("sem_init");
+       }
 
        ret = setup_local_apps();
        if (ret) {
@@ -1566,17 +1676,34 @@ void __attribute__((constructor)) lttng_ust_init(void)
                        ret = sem_timedwait(&constructor_wait,
                                        &constructor_timeout);
                } while (ret < 0 && errno == EINTR);
-               if (ret < 0 && errno == ETIMEDOUT) {
-                       ERR("Timed out waiting for lttng-sessiond");
-               } else {
-                       assert(!ret);
+               if (ret < 0) {
+                       switch (errno) {
+                       case ETIMEDOUT:
+                               ERR("Timed out waiting for lttng-sessiond");
+                               break;
+                       case EINVAL:
+                               PERROR("sem_timedwait");
+                               break;
+                       default:
+                               ERR("Unexpected error \"%s\" returned by sem_timedwait",
+                                       strerror(errno));
+                       }
                }
                break;
        case -1:/* wait forever */
                do {
                        ret = sem_wait(&constructor_wait);
                } while (ret < 0 && errno == EINTR);
-               assert(!ret);
+               if (ret < 0) {
+                       switch (errno) {
+                       case EINVAL:
+                               PERROR("sem_wait");
+                               break;
+                       default:
+                               ERR("Unexpected error \"%s\" returned by sem_wait",
+                                       strerror(errno));
+                       }
+               }
                break;
        case 0: /* no timeout */
                break;
@@ -1588,6 +1715,7 @@ void lttng_ust_cleanup(int exiting)
 {
        cleanup_sock_info(&global_apps, exiting);
        cleanup_sock_info(&local_apps, exiting);
+       local_apps.allowed = 0;
        /*
         * The teardown in this function all affect data structures
         * accessed under the UST lock by the listener thread. This
@@ -1597,7 +1725,6 @@ void lttng_ust_cleanup(int exiting)
         */
        lttng_ust_abi_exit();
        lttng_ust_events_exit();
-       lttng_context_exit();
        lttng_perf_counter_exit();
        lttng_ring_buffer_client_discard_rt_exit();
        lttng_ring_buffer_client_discard_exit();
This page took 0.025507 seconds and 4 git commands to generate.