Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / rotate.c
index 312bbba9ccbf982a9f1a9ff65e592365ea276f3f..7048be32fe932c684bbf0b14789a3ea3fed81b4f 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
+ * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _LGPL_SOURCE
 #include <common/mi-lttng.h>
 
 #include "../command.h"
-#include <lttng/rotation.h>
-#include <lttng/location.h>
+#include <lttng/lttng.h>
 
-static char *opt_session_name;
 static int opt_no_wait;
 static struct mi_writer *writer;
 
@@ -57,101 +45,6 @@ static struct poptOption long_options[] = {
        {0, 0, 0, 0, 0, 0, 0}
 };
 
-static int output_trace_archive_location(
-               const struct lttng_trace_archive_location *location,
-               const char *session_name)
-{
-       int ret = 0;
-       enum lttng_trace_archive_location_type location_type;
-       enum lttng_trace_archive_location_status status;
-       bool printed_location = false;
-
-       location_type = lttng_trace_archive_location_get_type(location);
-
-       _MSG("Trace chunk archive for session %s is now readable",
-                       session_name);
-       switch (location_type) {
-       case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
-       {
-               const char *absolute_path;
-
-               status = lttng_trace_archive_location_local_get_absolute_path(
-                               location, &absolute_path);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-               MSG(" at %s", absolute_path);
-               printed_location = true;
-               break;
-       }
-       case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
-       {
-               uint16_t control_port, data_port;
-               const char *host, *relative_path, *protocol_str;
-               enum lttng_trace_archive_location_relay_protocol_type protocol;
-
-               /* Fetch all relay location parameters. */
-               status = lttng_trace_archive_location_relay_get_protocol_type(
-                               location, &protocol);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-
-               status = lttng_trace_archive_location_relay_get_host(
-                               location, &host);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-
-               status = lttng_trace_archive_location_relay_get_control_port(
-                               location, &control_port);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-
-               status = lttng_trace_archive_location_relay_get_data_port(
-                               location, &data_port);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-
-               status = lttng_trace_archive_location_relay_get_relative_path(
-                               location, &relative_path);
-               if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
-                       ret = -1;
-                       goto end;
-               }
-
-               switch (protocol) {
-               case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
-                       protocol_str = "tcp";
-                       break;
-               default:
-                       protocol_str = "unknown";
-                       break;
-               }
-
-               MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
-                               PRIu16 "]", protocol_str, host,
-                               relative_path, control_port, data_port);
-               printed_location = true;
-               break;
-       }
-       default:
-               break;
-       }
-end:
-       if (!printed_location) {
-               MSG(" at an unknown location");
-       }
-       return ret;
-}
-
 static int rotate_tracing(char *session_name)
 {
        int ret;
@@ -193,21 +86,22 @@ static int rotate_tracing(char *session_name)
                rotation_status = lttng_rotation_handle_get_state(handle,
                                &rotation_state);
                if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
+                       MSG("");
                        ERR("Failed to query the state of the rotation.");
                        goto error;
                }
 
                if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
-                       ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
+                       ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
                        if (ret) {
-                               PERROR("usleep");
+                               PERROR("\nusleep");
                                goto error;
                        }
                        _MSG(".");
 
                        ret = fflush(stdout);
                        if (ret) {
-                               PERROR("fflush");
+                               PERROR("\nfflush");
                                goto error;
                        }
                }
@@ -239,7 +133,7 @@ skip_wait:
        }
 
        if (!lttng_opt_mi && print_location) {
-               ret = output_trace_archive_location(location,
+               ret = print_trace_archive_location(location,
                                session_name);
        } else if (lttng_opt_mi) {
                ret = mi_lttng_rotate(writer, session_name, rotation_state,
@@ -269,14 +163,15 @@ int cmd_rotate(int argc, const char **argv)
        enum cmd_error_code cmd_ret = CMD_SUCCESS;
        int popt_ret;
        static poptContext pc;
+       const char *arg_session_name = NULL;
        char *session_name = NULL;
-       bool free_session_name = false;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        popt_ret = poptReadDefaultConfig(pc, 0);
        if (popt_ret) {
                ERR("poptReadDefaultConfig");
-               goto error;
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
@@ -293,37 +188,43 @@ int cmd_rotate(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
-
-       if (!opt_session_name) {
+       arg_session_name = poptGetArg(pc);
+       if (arg_session_name == NULL) {
                session_name = get_session_name();
-               if (!session_name) {
-                       goto error;
-               }
-               free_session_name = true;
        } else {
-               session_name = opt_session_name;
+               session_name = strdup(arg_session_name);
+               if (session_name == NULL) {
+                       PERROR("Failed to copy session name");
+               }
+       }
+
+       if (session_name == NULL) {
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 
        /* Mi check */
        if (lttng_opt_mi) {
                writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
                if (!writer) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Open rotate command */
                ret = mi_lttng_writer_command_open(writer,
                                mi_lttng_element_command_rotate);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Open output element */
                ret = mi_lttng_writer_open_element(writer,
                                mi_lttng_element_command_output);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
        }
 
@@ -334,35 +235,34 @@ int cmd_rotate(int argc, const char **argv)
                /* Close output element */
                ret = mi_lttng_writer_close_element(writer);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
                /* Success ? */
                ret = mi_lttng_writer_write_element_bool(writer,
                                mi_lttng_element_command_success,
                                cmd_ret == CMD_SUCCESS);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Command element close */
                ret = mi_lttng_writer_command_close(writer);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
        }
 
        /* Mi clean-up */
        if (writer && mi_lttng_writer_destroy(writer)) {
-               goto error;
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 end:
+       free(session_name);
        poptFreeContext(pc);
-       if (free_session_name) {
-               free(session_name);
-       }
 
        return cmd_ret;
-error:
-       cmd_ret = CMD_ERROR;
-       goto end;
 }
This page took 0.029915 seconds and 4 git commands to generate.