Fix health check: signal parent before exit testpoint
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index bbbc75588c6f044a159f05a9588579f639037ec4..c05be0a91533797a088670af6b6ec52c00637154 100644 (file)
@@ -801,13 +801,13 @@ static void *thread_manage_kernel(void *data)
         */
        lttng_poll_init(&events);
 
-       if (testpoint(thread_manage_kernel)) {
+       if (testpoint(sessiond_thread_manage_kernel)) {
                goto error_testpoint;
        }
 
        health_code_update();
 
-       if (testpoint(thread_manage_kernel_before_loop)) {
+       if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
                goto error_testpoint;
        }
 
@@ -987,7 +987,7 @@ static void *thread_manage_consumer(void *data)
 restart:
        health_poll_entry();
 
-       if (testpoint(thread_manage_consumer)) {
+       if (testpoint(sessiond_thread_manage_consumer)) {
                goto error;
        }
 
@@ -1260,7 +1260,7 @@ static void *thread_manage_apps(void *data)
 
        health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
 
-       if (testpoint(thread_manage_apps)) {
+       if (testpoint(sessiond_thread_manage_apps)) {
                goto error_testpoint;
        }
 
@@ -1276,7 +1276,7 @@ static void *thread_manage_apps(void *data)
                goto error;
        }
 
-       if (testpoint(thread_manage_apps_before_loop)) {
+       if (testpoint(sessiond_thread_manage_apps_before_loop)) {
                goto error;
        }
 
@@ -1345,10 +1345,6 @@ static void *thread_manage_apps(void *data)
                                        }
 
                                        DBG("Apps with sock %d added to poll set", sock);
-
-                                       health_code_update();
-
-                                       break;
                                }
                        } else {
                                /*
@@ -1364,7 +1360,6 @@ static void *thread_manage_apps(void *data)
 
                                        /* Socket closed on remote end. */
                                        ust_app_unregister(pollfd);
-                                       break;
                                }
                        }
 
@@ -1767,7 +1762,7 @@ static void *thread_registration_apps(void *data)
 
        health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
 
-       if (testpoint(thread_registration_apps)) {
+       if (testpoint(sessiond_thread_registration_apps)) {
                goto error_testpoint;
        }
 
@@ -2936,74 +2931,75 @@ skip_domain:
                break;
        }
        case LTTNG_ENABLE_EVENT:
-       case LTTNG_ENABLE_EVENT_WITH_EXCLUSION:
-       case LTTNG_ENABLE_EVENT_WITH_FILTER:
        {
                struct lttng_event_exclusion *exclusion = NULL;
                struct lttng_filter_bytecode *bytecode = NULL;
 
-               if (cmd_ctx->lsm->cmd_type == LTTNG_ENABLE_EVENT ||
-                               (cmd_ctx->lsm->u.enable.exclusion_count == 0 && cmd_ctx->lsm->u.enable.bytecode_len == 0)) {
-                       ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
-                                       cmd_ctx->lsm->u.enable.channel_name,
-                                       &cmd_ctx->lsm->u.enable.event, NULL, NULL, kernel_poll_pipe[1]);
-               } else {
-                       if (cmd_ctx->lsm->u.enable.exclusion_count != 0) {
-                               exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
-                                               cmd_ctx->lsm->u.enable.exclusion_count * LTTNG_SYMBOL_NAME_LEN);
-                               if (!exclusion) {
-                                       ret = LTTNG_ERR_EXCLUSION_NOMEM;
-                                       goto error;
-                               }
-                               DBG("Receiving var len data from client ...");
-                               exclusion->count = cmd_ctx->lsm->u.enable.exclusion_count;
-                               ret = lttcomm_recv_unix_sock(sock, exclusion->names,
-                                               cmd_ctx->lsm->u.enable.exclusion_count * LTTNG_SYMBOL_NAME_LEN);
-                               if (ret <= 0) {
-                                       DBG("Nothing recv() from client var len data... continuing");
-                                       *sock_error = 1;
-                                       ret = LTTNG_ERR_EXCLUSION_INVAL;
-                                       goto error;
-                               }
+               /* Handle exclusion events and receive it from the client. */
+               if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
+                       size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
+
+                       exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
+                                       (count * LTTNG_SYMBOL_NAME_LEN));
+                       if (!exclusion) {
+                               ret = LTTNG_ERR_EXCLUSION_NOMEM;
+                               goto error;
                        }
-                       if (cmd_ctx->lsm->u.enable.bytecode_len != 0) {
-                               bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
-                               if (!bytecode) {
-                                       if (!exclusion)
-                                               free(exclusion);
-                                       ret = LTTNG_ERR_FILTER_NOMEM;
-                                       goto error;
-                               }
-                               /* Receive var. len. data */
-                               DBG("Receiving var len data from client ...");
-                               ret = lttcomm_recv_unix_sock(sock, bytecode,
-                                               cmd_ctx->lsm->u.enable.bytecode_len);
-                               if (ret <= 0) {
-                                       DBG("Nothing recv() from client car len data... continuing");
-                                       *sock_error = 1;
-                                       if (!exclusion)
-                                               free(exclusion);
-                                       ret = LTTNG_ERR_FILTER_INVAL;
-                                       goto error;
-                               }
 
-                               if (bytecode->len + sizeof(*bytecode)
-                                               != cmd_ctx->lsm->u.enable.bytecode_len) {
-                                       free(bytecode);
-                                       if (!exclusion)
-                                               free(exclusion);
-                                       ret = LTTNG_ERR_FILTER_INVAL;
-                                       goto error;
-                               }
+                       DBG("Receiving var len exclusion event list from client ...");
+                       exclusion->count = count;
+                       ret = lttcomm_recv_unix_sock(sock, exclusion->names,
+                                       count * LTTNG_SYMBOL_NAME_LEN);
+                       if (ret <= 0) {
+                               DBG("Nothing recv() from client var len data... continuing");
+                               *sock_error = 1;
+                               free(exclusion);
+                               ret = LTTNG_ERR_EXCLUSION_INVAL;
+                               goto error;
+                       }
+               }
+
+               /* Handle filter and get bytecode from client. */
+               if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
+                       size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
+
+                       if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
+                               ret = LTTNG_ERR_FILTER_INVAL;
+                               free(exclusion);
+                               goto error;
                        }
 
-                       ret = cmd_enable_event(cmd_ctx->session,
-                                       &cmd_ctx->lsm->domain,
-                                       cmd_ctx->lsm->u.enable.channel_name,
-                                       &cmd_ctx->lsm->u.enable.event, bytecode,
-                                       exclusion,
-                                       kernel_poll_pipe[1]);
+                       bytecode = zmalloc(bytecode_len);
+                       if (!bytecode) {
+                               free(exclusion);
+                               ret = LTTNG_ERR_FILTER_NOMEM;
+                               goto error;
+                       }
+
+                       /* Receive var. len. data */
+                       DBG("Receiving var len filter's bytecode from client ...");
+                       ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
+                       if (ret <= 0) {
+                               DBG("Nothing recv() from client car len data... continuing");
+                               *sock_error = 1;
+                               free(bytecode);
+                               free(exclusion);
+                               ret = LTTNG_ERR_FILTER_INVAL;
+                               goto error;
+                       }
+
+                       if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
+                               free(bytecode);
+                               free(exclusion);
+                               ret = LTTNG_ERR_FILTER_INVAL;
+                               goto error;
+                       }
                }
+
+               ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
+                               cmd_ctx->lsm->u.enable.channel_name,
+                               &cmd_ctx->lsm->u.enable.event, bytecode, exclusion,
+                               kernel_poll_pipe[1]);
                break;
        }
        case LTTNG_ENABLE_ALL_EVENT:
@@ -3697,10 +3693,6 @@ static void *thread_manage_clients(void *data)
 
        health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
 
-       if (testpoint(thread_manage_clients)) {
-               goto error_testpoint;
-       }
-
        health_code_update();
 
        ret = lttcomm_listen_unix_sock(client_sock);
@@ -3736,7 +3728,12 @@ static void *thread_manage_clients(void *data)
                kill(child_ppid, SIGUSR1);
        }
 
-       if (testpoint(thread_manage_clients_before_loop)) {
+       /* This testpoint is after we signal readiness to the parent. */
+       if (testpoint(sessiond_thread_manage_clients)) {
+               goto error;
+       }
+
+       if (testpoint(sessiond_thread_manage_clients_before_loop)) {
                goto error;
        }
 
@@ -3911,7 +3908,6 @@ error:
 
 error_listen:
 error_create_poll:
-error_testpoint:
        unlink(client_unix_sock_path);
        if (client_sock >= 0) {
                ret = close(client_sock);
@@ -4579,6 +4575,18 @@ static int daemonize(void)
                 * user.
                 */
                while (!CMM_LOAD_SHARED(recv_child_signal)) {
+                       int status;
+                       pid_t ret;
+
+                       /*
+                        * Check if child exists without blocking. If so, we have to stop
+                        * this parent process and return an error.
+                        */
+                       ret = waitpid(pid, &status, WNOHANG);
+                       if (ret < 0 || (ret != 0 && WIFEXITED(status))) {
+                               /* The child exited somehow or was not valid. */
+                               goto error;
+                       }
                        sleep(1);
                }
 
This page took 0.026635 seconds and 4 git commands to generate.