Import CStringView from the Babeltrace tree
[lttng-tools.git] / src / bin / lttng-sessiond / client.cpp
index 123d07f857c39b905e8d6bee338cb9f942d4dc74..43a8b793e9ed366d5be2631feca34cc8f190ddef 100644 (file)
@@ -30,6 +30,7 @@
 #include "utils.hpp"
 
 #include <common/compat/getenv.hpp>
+#include <common/exception.hpp>
 #include <common/tracker.hpp>
 #include <common/unix.hpp>
 #include <common/utils.hpp>
@@ -40,6 +41,7 @@
 #include <lttng/session-internal.hpp>
 #include <lttng/userspace-probe-internal.hpp>
 
+#include <fcntl.h>
 #include <pthread.h>
 #include <signal.h>
 #include <stddef.h>
@@ -1031,6 +1033,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int *sock, int *sock_
        case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION:
        case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS:
        case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY:
+       case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS:
                need_domain = false;
                break;
        default:
@@ -1114,6 +1117,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int *sock, int *sock_
        case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER:
        case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS:
        case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY:
+       case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS:
                need_tracing_session = false;
                break;
        default:
@@ -1784,7 +1788,7 @@ skip_domain:
        }
        case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION:
        {
-               ret = cmd_destroy_session(cmd_ctx->session, the_notification_thread_handle, sock);
+               ret = cmd_destroy_session(cmd_ctx->session, sock);
                break;
        }
        case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS:
@@ -1925,6 +1929,25 @@ skip_domain:
                        cmd_ctx->session, cmd_ctx->lsm.domain.type, cmd_ctx->lsm.u.reg.path, cdata);
                break;
        }
+       case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS:
+       {
+               uint32_t u_status;
+               enum lttng_kernel_tracer_status status;
+
+               ret = cmd_kernel_tracer_status(&status);
+               if (ret != LTTNG_OK) {
+                       goto error;
+               }
+
+               u_status = (uint32_t) status;
+               ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &u_status, 4);
+               if (ret < 0) {
+                       goto error;
+               }
+
+               ret = LTTNG_OK;
+               break;
+       }
        case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING:
        {
                int pending_ret;
@@ -1946,12 +1969,12 @@ skip_domain:
                         * ret will be set to LTTNG_OK at the end of
                         * this function.
                         */
-               } else if (pending_ret < 0) {
+               } else if (pending_ret <= LTTNG_OK || pending_ret >= LTTNG_ERR_NR) {
                        ret = LTTNG_ERR_UNK;
-                       goto setup_error;
+                       goto error;
                } else {
                        ret = pending_ret;
-                       goto setup_error;
+                       goto error;
                }
 
                pending_ret_byte = (uint8_t) pending_ret;
@@ -2214,11 +2237,8 @@ skip_domain:
                                        cmd_ctx->lsm.u.rotation_set_schedule.type;
                value = cmd_ctx->lsm.u.rotation_set_schedule.value;
 
-               ret = cmd_rotation_set_schedule(cmd_ctx->session,
-                                               set_schedule,
-                                               schedule_type,
-                                               value,
-                                               the_notification_thread_handle);
+               ret = cmd_rotation_set_schedule(
+                       cmd_ctx->session, set_schedule, schedule_type, value);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -2585,24 +2605,36 @@ static void *thread_manage_clients(void *data)
                 * informations for the client. The command context struct contains
                 * everything this function may needs.
                 */
-               ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
-               rcu_thread_offline();
-               if (ret < 0) {
-                       if (sock >= 0) {
-                               ret = close(sock);
-                               if (ret) {
-                                       PERROR("close");
+               try {
+                       ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
+                       rcu_thread_offline();
+                       if (ret < 0) {
+                               if (sock >= 0) {
+                                       ret = close(sock);
+                                       if (ret) {
+                                               PERROR("close");
+                                       }
                                }
+                               sock = -1;
+                               /*
+                                * TODO: Inform client somehow of the fatal error. At
+                                * this point, ret < 0 means that a zmalloc failed
+                                * (ENOMEM). Error detected but still accept
+                                * command, unless a socket error has been
+                                * detected.
+                                */
+                               continue;
                        }
-                       sock = -1;
-                       /*
-                        * TODO: Inform client somehow of the fatal error. At
-                        * this point, ret < 0 means that a zmalloc failed
-                        * (ENOMEM). Error detected but still accept
-                        * command, unless a socket error has been
-                        * detected.
-                        */
-                       continue;
+               } catch (const std::bad_alloc& ex) {
+                       WARN_FMT("Failed to allocate memory while handling client request: {}",
+                                ex.what());
+                       ret = LTTNG_ERR_NOMEM;
+               } catch (const lttng::ctl::error& ex) {
+                       WARN_FMT("Client request failed: {}", ex.what());
+                       ret = ex.code();
+               } catch (const std::exception& ex) {
+                       WARN_FMT("Client request failed: {}", ex.what());
+                       ret = LTTNG_ERR_UNK;
                }
 
                if (ret < LTTNG_OK || ret >= LTTNG_ERR_NR) {
This page took 0.025298 seconds and 4 git commands to generate.