+
+LTTNG_HIDDEN
+int mi_lttng_rotation_schedule(struct mi_writer *writer,
+ const struct lttng_rotation_schedule *schedule)
+{
+ int ret = 0;
+ enum lttng_rotation_status status;
+ uint64_t value;
+ const char *element_name;
+ const char *value_name;
+ bool empty_schedule = false;
+
+ switch (lttng_rotation_schedule_get_type(schedule)) {
+ case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
+ status = lttng_rotation_schedule_periodic_get_period(schedule,
+ &value);
+ element_name = mi_lttng_element_rotation_schedule_periodic;
+ value_name = mi_lttng_element_rotation_schedule_periodic_time_us;
+ break;
+ case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
+ status = lttng_rotation_schedule_size_threshold_get_threshold(
+ schedule, &value);
+ element_name = mi_lttng_element_rotation_schedule_size_threshold;
+ value_name = mi_lttng_element_rotation_schedule_size_threshold_bytes;
+ break;
+ default:
+ ret = -1;
+ goto end;
+ }
+
+ if (status != LTTNG_ROTATION_STATUS_OK) {
+ if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
+ empty_schedule = true;
+ } else {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ ret = mi_lttng_writer_open_element(writer, element_name);
+ if (ret) {
+ goto end;
+ }
+
+ if (!empty_schedule) {
+ ret = mi_lttng_writer_write_element_unsigned_int(writer,
+ value_name, value);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ /* Close schedule descriptor element. */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int mi_lttng_rotation_schedule_result(struct mi_writer *writer,
+ const struct lttng_rotation_schedule *schedule,
+ bool success)
+{
+ int ret = 0;
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation_schedule_result);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation_schedule);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_rotation_schedule(writer, schedule);
+ if (ret) {
+ goto end;
+ }
+
+ /* Close rotation_schedule element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_bool(writer,
+ mi_lttng_element_command_success, success);
+ if (ret) {
+ goto end;
+ }
+
+ /* Close rotation_schedule_result element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+end:
+ return ret;
+}
+
+static
+int mi_lttng_location(struct mi_writer *writer,
+ const struct lttng_trace_archive_location *location)
+{
+ int ret = 0;
+ enum lttng_trace_archive_location_type location_type;
+ enum lttng_trace_archive_location_status status;
+
+ location_type = lttng_trace_archive_location_get_type(location);
+
+ 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;
+ }
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation_location_local);
+ if (ret) {
+ goto end;
+ }
+
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_rotation_location_local_absolute_path,
+ absolute_path);
+ if (ret) {
+ goto end;
+ }
+
+ /* Close local element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+ break;
+ }
+ case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
+ {
+ uint16_t control_port, data_port;
+ const char *host, *relative_path;
+ 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;
+ }
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation_location_relay);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_rotation_location_relay_host,
+ host);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_unsigned_int(writer,
+ mi_lttng_element_rotation_location_relay_control_port,
+ control_port);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_unsigned_int(writer,
+ mi_lttng_element_rotation_location_relay_data_port,
+ data_port);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_rotation_location_relay_protocol,
+ mi_lttng_trace_archive_location_relay_protocol_type_string(protocol));
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_rotation_location_relay_relative_path,
+ relative_path);
+ if (ret) {
+ goto end;
+ }
+
+ /* Close relay element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+ break;
+ }
+ default:
+ abort();
+ }
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int mi_lttng_rotate(struct mi_writer *writer,
+ const char *session_name,
+ enum lttng_rotation_state rotation_state,
+ const struct lttng_trace_archive_location *location)
+{
+ int ret;
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_session_name,
+ session_name);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_writer_write_element_string(writer,
+ mi_lttng_element_rotation_state,
+ mi_lttng_rotation_state_string(rotation_state));
+ if (ret) {
+ goto end;
+ }
+
+ if (!location) {
+ /* Not a serialization error. */
+ goto close_rotation;
+ }
+
+ ret = mi_lttng_writer_open_element(writer,
+ mi_lttng_element_rotation_location);
+ if (ret) {
+ goto end;
+ }
+
+ ret = mi_lttng_location(writer, location);
+ if (ret) {
+ goto close_location;
+ }
+
+close_location:
+ /* Close location element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+
+close_rotation:
+ /* Close rotation element */
+ ret = mi_lttng_writer_close_element(writer);
+ if (ret) {
+ goto end;
+ }
+end:
+ return ret;
+}