.gitignore: ignore local vscode workspace settings file
[lttng-tools.git] / src / bin / lttng-sessiond / main.cpp
index 0037d9e71bc3afc1554530835130f7a934856680..bfd53e7e47041818996b55d9bb15266901688ebf 100644 (file)
@@ -52,6 +52,7 @@
 #include <common/futex.hpp>
 #include <common/ini-config/ini-config.hpp>
 #include <common/kernel-consumer/kernel-consumer.hpp>
+#include <common/lockfile.hpp>
 #include <common/logging-utils.hpp>
 #include <common/path.hpp>
 #include <common/relayd/relayd.hpp>
@@ -84,7 +85,7 @@ static const char *help_msg =
 #ifdef LTTNG_EMBED_HELP
 #include <lttng-sessiond.8.h>
 #else
-       NULL
+       nullptr
 #endif
        ;
 
@@ -104,37 +105,37 @@ static int recv_child_signal;
 
 /* Command line options */
 static const struct option long_options[] = {
-       { "client-sock", required_argument, 0, 'c' },
-       { "apps-sock", required_argument, 0, 'a' },
-       { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
-       { "kconsumerd-err-sock", required_argument, 0, '\0' },
-       { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
-       { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
-       { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
-       { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
-       { "consumerd32-path", required_argument, 0, '\0' },
-       { "consumerd32-libdir", required_argument, 0, '\0' },
-       { "consumerd64-path", required_argument, 0, '\0' },
-       { "consumerd64-libdir", required_argument, 0, '\0' },
-       { "daemonize", no_argument, 0, 'd' },
-       { "background", no_argument, 0, 'b' },
-       { "sig-parent", no_argument, 0, 'S' },
-       { "help", no_argument, 0, 'h' },
-       { "group", required_argument, 0, 'g' },
-       { "version", no_argument, 0, 'V' },
-       { "quiet", no_argument, 0, 'q' },
-       { "verbose", no_argument, 0, 'v' },
-       { "verbose-consumer", no_argument, 0, '\0' },
-       { "no-kernel", no_argument, 0, '\0' },
-       { "pidfile", required_argument, 0, 'p' },
-       { "agent-tcp-port", required_argument, 0, '\0' },
-       { "config", required_argument, 0, 'f' },
-       { "load", required_argument, 0, 'l' },
-       { "kmod-probes", required_argument, 0, '\0' },
-       { "extra-kmod-probes", required_argument, 0, '\0' },
-       { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, required_argument, 0, '\0' },
-       { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, required_argument, 0, '\0' },
-       { NULL, 0, 0, 0 }
+       { "client-sock", required_argument, nullptr, 'c' },
+       { "apps-sock", required_argument, nullptr, 'a' },
+       { "kconsumerd-cmd-sock", required_argument, nullptr, '\0' },
+       { "kconsumerd-err-sock", required_argument, nullptr, '\0' },
+       { "ustconsumerd32-cmd-sock", required_argument, nullptr, '\0' },
+       { "ustconsumerd32-err-sock", required_argument, nullptr, '\0' },
+       { "ustconsumerd64-cmd-sock", required_argument, nullptr, '\0' },
+       { "ustconsumerd64-err-sock", required_argument, nullptr, '\0' },
+       { "consumerd32-path", required_argument, nullptr, '\0' },
+       { "consumerd32-libdir", required_argument, nullptr, '\0' },
+       { "consumerd64-path", required_argument, nullptr, '\0' },
+       { "consumerd64-libdir", required_argument, nullptr, '\0' },
+       { "daemonize", no_argument, nullptr, 'd' },
+       { "background", no_argument, nullptr, 'b' },
+       { "sig-parent", no_argument, nullptr, 'S' },
+       { "help", no_argument, nullptr, 'h' },
+       { "group", required_argument, nullptr, 'g' },
+       { "version", no_argument, nullptr, 'V' },
+       { "quiet", no_argument, nullptr, 'q' },
+       { "verbose", no_argument, nullptr, 'v' },
+       { "verbose-consumer", no_argument, nullptr, '\0' },
+       { "no-kernel", no_argument, nullptr, '\0' },
+       { "pidfile", required_argument, nullptr, 'p' },
+       { "agent-tcp-port", required_argument, nullptr, '\0' },
+       { "config", required_argument, nullptr, 'f' },
+       { "load", required_argument, nullptr, 'l' },
+       { "kmod-probes", required_argument, nullptr, '\0' },
+       { "extra-kmod-probes", required_argument, nullptr, '\0' },
+       { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, required_argument, nullptr, '\0' },
+       { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, required_argument, nullptr, '\0' },
+       { nullptr, 0, nullptr, 0 }
 };
 
 /* Command line options to ignore from configuration file */
@@ -170,7 +171,7 @@ static int is_root;
  * Notify the main thread to initiate the teardown of the worker threads by
  * writing to the main quit pipe.
  */
-static void notify_main_quit_pipe(void)
+static void notify_main_quit_pipe()
 {
        int ret;
 
@@ -185,7 +186,7 @@ static void notify_main_quit_pipe(void)
 /*
  * Close every consumer sockets.
  */
-static void close_consumer_sockets(void)
+static void close_consumer_sockets()
 {
        int ret;
 
@@ -273,7 +274,7 @@ static void wait_consumer(struct consumer_data *consumer_data)
 /*
  * Cleanup the session daemon's data structures.
  */
-static void sessiond_cleanup(void)
+static void sessiond_cleanup()
 {
        int ret;
        struct ltt_session_list *session_list = session_get_list();
@@ -357,7 +358,7 @@ static void sessiond_cleanup(void)
 /*
  * Cleanup the daemon's option data structures.
  */
-static void sessiond_cleanup_options(void)
+static void sessiond_cleanup_options()
 {
        DBG("Cleaning up options");
 
@@ -641,7 +642,7 @@ static int set_option(int opt, const char *arg, const char *optname)
                        unsigned long v;
 
                        errno = 0;
-                       v = strtoul(arg, NULL, 0);
+                       v = strtoul(arg, nullptr, 0);
                        if (errno != 0 || !isdigit(arg[0])) {
                                ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
                                return -1;
@@ -702,7 +703,7 @@ static int set_option(int opt, const char *arg, const char *optname)
                unsigned long v;
 
                errno = 0;
-               v = strtoul(arg, NULL, 0);
+               v = strtoul(arg, nullptr, 0);
                if (errno != 0 || !isdigit(arg[0])) {
                        ERR("Wrong value in --%s parameter: %s",
                            EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR,
@@ -723,7 +724,7 @@ static int set_option(int opt, const char *arg, const char *optname)
                unsigned long v;
 
                errno = 0;
-               v = strtoul(arg, NULL, 0);
+               v = strtoul(arg, nullptr, 0);
                if (errno != 0 || !isdigit(arg[0])) {
                        ERR("Wrong value in --%s parameter: %s",
                            EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR,
@@ -791,7 +792,7 @@ static int config_entry_handler(const struct config_entry *entry,
 
        for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
                /* Ignore if not fully matched. */
-               if (strcmp(entry->name, long_options[i].name)) {
+               if (strcmp(entry->name, long_options[i].name) != 0) {
                        continue;
                }
 
@@ -823,7 +824,7 @@ end:
        return ret;
 }
 
-static void print_version(void)
+static void print_version()
 {
        fprintf(stdout, "%s\n", VERSION);
 }
@@ -836,7 +837,7 @@ static int set_options(int argc, char **argv)
        int ret = 0, c = 0, option_index = 0;
        int orig_optopt = optopt, orig_optind = optind;
        char *optstring;
-       char *config_path = NULL;
+       char *config_path = nullptr;
 
        optstring = utils_generate_optstring(long_options,
                                             sizeof(long_options) / sizeof(struct option));
@@ -868,7 +869,7 @@ static int set_options(int argc, char **argv)
        }
 
        ret = config_get_section_entries(
-               config_path, config_section_name, config_entry_handler, NULL);
+               config_path, config_section_name, config_entry_handler, nullptr);
        if (ret) {
                if (ret > 0) {
                        ERR("Invalid configuration option at line %i", ret);
@@ -880,7 +881,7 @@ static int set_options(int argc, char **argv)
        /* Reset getopt's global state */
        optopt = orig_optopt;
        optind = orig_optind;
-       while (1) {
+       while (true) {
                option_index = -1;
                /*
                 * getopt_long() will not set option_index if it encounters a
@@ -896,7 +897,7 @@ static int set_options(int argc, char **argv)
                 * unset.
                 */
                ret = set_option(
-                       c, optarg, option_index < 0 ? NULL : long_options[option_index].name);
+                       c, optarg, option_index < 0 ? nullptr : long_options[option_index].name);
                if (ret < 0) {
                        break;
                }
@@ -911,7 +912,7 @@ end:
 /*
  * Create lockfile using the rundir and return its fd.
  */
-static int create_lockfile(void)
+static int create_lockfile()
 {
        return utils_create_lock_file(the_config.lock_file_path.value);
 }
@@ -922,7 +923,7 @@ static int create_lockfile(void)
  *
  * Also attempts to create and hold the lock file.
  */
-static int check_existing_daemon(void)
+static int check_existing_daemon()
 {
        int ret = 0;
 
@@ -941,7 +942,7 @@ end:
        return ret;
 }
 
-static void sessiond_cleanup_lock_file(void)
+static void sessiond_cleanup_lock_file()
 {
        int ret;
 
@@ -1032,7 +1033,7 @@ static int set_permissions(char *rundir)
 /*
  * Create the lttng run directory needed for all global sockets and pipe.
  */
-static int create_lttng_rundir(void)
+static int create_lttng_rundir()
 {
        int ret;
 
@@ -1059,7 +1060,7 @@ error:
 static int set_consumer_sockets(struct consumer_data *consumer_data)
 {
        int ret;
-       char *path = NULL;
+       char *path = nullptr;
 
        switch (consumer_data->type) {
        case LTTNG_CONSUMER_KERNEL:
@@ -1179,7 +1180,7 @@ static void sighandler(int sig, siginfo_t *siginfo, void *arg __attribute__((unu
  * Setup signal handler for :
  *             SIGINT, SIGTERM, SIGPIPE
  */
-static int set_signal_handler(void)
+static int set_signal_handler()
 {
        int ret = 0;
        struct sigaction sa;
@@ -1194,29 +1195,29 @@ static int set_signal_handler(void)
        sa.sa_flags = SA_SIGINFO;
 
        sa.sa_sigaction = sighandler;
-       if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
+       if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) {
                PERROR("sigaction");
                return ret;
        }
 
-       if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
+       if ((ret = sigaction(SIGINT, &sa, nullptr)) < 0) {
                PERROR("sigaction");
                return ret;
        }
 
-       if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
+       if ((ret = sigaction(SIGUSR1, &sa, nullptr)) < 0) {
                PERROR("sigaction");
                return ret;
        }
 
-       if ((ret = sigaction(SIGBUS, &sa, NULL)) < 0) {
+       if ((ret = sigaction(SIGBUS, &sa, nullptr)) < 0) {
                PERROR("sigaction");
                return ret;
        }
 
        sa.sa_flags = 0;
        sa.sa_handler = SIG_IGN;
-       if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
+       if ((ret = sigaction(SIGPIPE, &sa, nullptr)) < 0) {
                PERROR("sigaction");
                return ret;
        }
@@ -1230,7 +1231,7 @@ static int set_signal_handler(void)
  * Set open files limit to unlimited. This daemon can open a large number of
  * file descriptors in order to consume multiple kernel traces.
  */
-static void set_ulimit(void)
+static void set_ulimit()
 {
        int ret;
        struct rlimit lim;
@@ -1245,15 +1246,15 @@ static void set_ulimit(void)
        }
 }
 
-static int write_pidfile(void)
+static int write_pidfile()
 {
        return utils_create_pid_file(getpid(), the_config.pid_file_path.value);
 }
 
-static int set_clock_plugin_env(void)
+static int set_clock_plugin_env()
 {
        int ret = 0;
-       char *env_value = NULL;
+       char *env_value = nullptr;
 
        if (!the_config.lttng_ust_clock_plugin.value) {
                goto end;
@@ -1279,7 +1280,7 @@ end:
        return ret;
 }
 
-static void destroy_all_sessions_and_wait(void)
+static void destroy_all_sessions_and_wait()
 {
        struct ltt_session *session, *tmp;
        struct ltt_session_list *session_list;
@@ -1303,7 +1304,7 @@ static void destroy_all_sessions_and_wait(void)
                        goto unlock_session;
                }
                (void) cmd_stop_trace(session);
-               (void) cmd_destroy_session(session, the_notification_thread_handle, NULL);
+               (void) cmd_destroy_session(session, nullptr);
        unlock_session:
                session_unlock(session);
                session_put(session);
@@ -1316,11 +1317,11 @@ static void destroy_all_sessions_and_wait(void)
        DBG("Destruction of all sessions completed");
 }
 
-static void unregister_all_triggers(void)
+static void unregister_all_triggers()
 {
        enum lttng_error_code ret_code;
        enum lttng_trigger_status trigger_status;
-       struct lttng_triggers *triggers = NULL;
+       struct lttng_triggers *triggers = nullptr;
        unsigned int trigger_count, i;
        const struct lttng_credentials creds = {
                .uid = LTTNG_OPTIONAL_INIT_VALUE(0),
@@ -1392,7 +1393,7 @@ static int launch_run_as_worker(const char *procname)
        return run_as_create_worker(procname, run_as_worker_post_fork_cleanup, &the_config);
 }
 
-static void sessiond_uuid_log(void)
+static void sessiond_uuid_log()
 {
        char uuid_str[LTTNG_UUID_STR_LEN];
 
@@ -1403,20 +1404,19 @@ static void sessiond_uuid_log(void)
 /*
  * main
  */
-int main(int argc, char **argv)
+static int _main(int argc, char **argv)
 {
        int ret = 0, retval = 0;
        const char *env_app_timeout;
-       struct lttng_pipe *ust32_channel_monitor_pipe = NULL, *ust64_channel_monitor_pipe = NULL,
-                         *kernel_channel_monitor_pipe = NULL;
+       struct lttng_pipe *ust32_channel_monitor_pipe = nullptr,
+                         *ust64_channel_monitor_pipe = nullptr,
+                         *kernel_channel_monitor_pipe = nullptr;
        struct timer_thread_parameters timer_thread_parameters;
-       /* Rotation thread handle. */
-       struct rotation_thread_handle *rotation_thread_handle = NULL;
        /* Queue of rotation jobs populated by the sessiond-timer. */
-       struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
-       struct lttng_thread *client_thread = NULL;
-       struct lttng_thread *notification_thread = NULL;
-       struct lttng_thread *register_apps_thread = NULL;
+       lttng::sessiond::rotation_thread_timer_queue *rotation_timer_queue = nullptr;
+       struct lttng_thread *client_thread = nullptr;
+       struct lttng_thread *notification_thread = nullptr;
+       struct lttng_thread *register_apps_thread = nullptr;
        enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
 
        logger_set_thread_name("Main", false);
@@ -1599,7 +1599,7 @@ int main(int argc, char **argv)
         * sessiond timer thread and the rotation thread. The main thread keeps
         * its ownership and destroys it when both threads have been joined.
         */
-       rotation_timer_queue = rotation_thread_timer_queue_create();
+       rotation_timer_queue = lttng::sessiond::rotation_thread_timer_queue_create();
        if (!rotation_timer_queue) {
                retval = -1;
                goto stop_threads;
@@ -1770,18 +1770,20 @@ int main(int argc, char **argv)
                goto stop_threads;
        }
 
-       /* rotation_thread_data acquires the pipes' read side. */
-       rotation_thread_handle =
-               rotation_thread_handle_create(rotation_timer_queue, the_notification_thread_handle);
-       if (!rotation_thread_handle) {
+       try {
+               the_rotation_thread_handle = lttng::make_unique<lttng::sessiond::rotation_thread>(
+                       *rotation_timer_queue, *the_notification_thread_handle);
+       } catch (const std::exception& e) {
                retval = -1;
-               ERR("Failed to create rotation thread shared data");
+               ERR("Failed to create rotation thread: %s", e.what());
                goto stop_threads;
        }
 
-       /* Create rotation thread. */
-       if (!launch_rotation_thread(rotation_thread_handle)) {
+       try {
+               the_rotation_thread_handle->launch_thread();
+       } catch (const std::exception& e) {
                retval = -1;
+               ERR("Failed to launch rotation thread: %s", e.what());
                goto stop_threads;
        }
 
@@ -1850,7 +1852,7 @@ int main(int argc, char **argv)
        }
 
        /* Load sessions. */
-       ret = config_load_session(the_config.load_session_path.value, NULL, 1, 1, NULL);
+       ret = config_load_session(the_config.load_session_path.value, nullptr, 1, 1, nullptr);
        if (ret) {
                ERR("Session load failed: %s", error_get_str(ret));
                retval = -1;
@@ -1942,10 +1944,6 @@ stop_threads:
        rcu_thread_offline();
        rcu_unregister_thread();
 
-       if (rotation_thread_handle) {
-               rotation_thread_handle_destroy(rotation_thread_handle);
-       }
-
        /*
         * After the rotation and timer thread have quit, we can safely destroy
         * the rotation_timer_queue.
@@ -1978,3 +1976,13 @@ exit_set_signal_handler:
                exit(EXIT_FAILURE);
        }
 }
+
+int main(int argc, char **argv)
+{
+       try {
+               return _main(argc, argv);
+       } catch (const std::exception& e) {
+               ERR_FMT("Unhandled exception caught by main thread: {}", e.what());
+               abort();
+       }
+}
This page took 0.033062 seconds and 4 git commands to generate.