#include <common/defaults.hpp>
#include <common/error.hpp>
+#include <common/exception.hpp>
+#include <common/make-unique-wrapper.hpp>
#include <common/utils.hpp>
#include <arpa/inet.h>
#include <ctype.h>
+#include <fnmatch.h>
#include <inttypes.h>
#include <limits.h>
#include <netinet/in.h>
static char *_get_session_name(int quiet)
{
const char *path;
- char *session_name = NULL;
+ char *session_name = nullptr;
/* Get path to config file */
path = utils_get_home_dir();
- if (path == NULL) {
+ if (path == nullptr) {
goto error;
}
/* Get session name from config */
session_name = quiet ? config_read_session_name_quiet(path) :
config_read_session_name(path);
- if (session_name == NULL) {
+ if (session_name == nullptr) {
goto error;
}
return session_name;
error:
- return NULL;
+ return nullptr;
}
/*
* Return allocated string with the session name found in the config
* directory.
*/
-char *get_session_name(void)
+char *get_session_name()
{
return _get_session_name(0);
}
* Return allocated string with the session name found in the config
* directory.
*/
-char *get_session_name_quiet(void)
+char *get_session_name_quiet()
{
return _get_session_name(1);
}
void list_commands(struct cmd_struct *commands, FILE *ofp)
{
int i = 0;
- struct cmd_struct *cmd = NULL;
+ struct cmd_struct *cmd = nullptr;
cmd = &commands[i];
- while (cmd->name != NULL) {
+ while (cmd->name != nullptr) {
fprintf(ofp, "%s\n", cmd->name);
i++;
cmd = &commands[i];
void list_cmd_options(FILE *ofp, struct poptOption *options)
{
int i;
- struct poptOption *option = NULL;
+ struct poptOption *option = nullptr;
- for (i = 0; options[i].longName != NULL; i++) {
+ for (i = 0; options[i].longName != nullptr; i++) {
option = &options[i];
fprintf(ofp, "--%s\n", option->longName);
{
int i;
- for (i = 0; options[i].long_name != NULL; i++) {
+ for (i = 0; options[i].long_name != nullptr; i++) {
const struct argpar_opt_descr *option = &options[i];
fprintf(ofp, "--%s\n", option->long_name);
*
* Return 1 if found else 0 if NOT found. Negative value on error.
*/
-int check_relayd(void)
+int check_relayd()
{
int ret, fd;
struct sockaddr_in sin;
int get_session_stats_str(const char *session_name, char **out_str)
{
int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
- struct lttng_domain *domains = NULL;
- struct lttng_channel *channels = NULL;
+ struct lttng_domain *domains = nullptr;
+ struct lttng_channel *channels = nullptr;
uint64_t discarded_events_total = 0, lost_packets_total = 0;
- struct lttng_session *sessions = NULL;
- const struct lttng_session *selected_session = NULL;
- char *stats_str = NULL;
+ struct lttng_session *sessions = nullptr;
+ const struct lttng_session *selected_session = nullptr;
+ char *stats_str = nullptr;
bool print_discarded_events = false, print_lost_packets = false;
count = lttng_list_sessions(&sessions);
}
free(channels);
- channels = NULL;
+ channels = nullptr;
count = lttng_list_channels(handle, &channels);
for (channel_idx = 0; channel_idx < count; channel_idx++) {
uint64_t discarded_events = 0, lost_packets = 0;
}
return ret;
}
+
+namespace {
+template <typename FilterFunctionType>
+session_list get_sessions(const FilterFunctionType& filter, bool return_first_match_only = false)
+{
+ session_list list;
+
+ {
+ int list_ret;
+ struct lttng_session *psessions;
+
+ list_ret = lttng_list_sessions(&psessions);
+
+ if (list_ret < 0) {
+ LTTNG_THROW_CTL("Failed to list sessions",
+ static_cast<lttng_error_code>(list_ret));
+ }
+
+ list = session_list(psessions, list_ret);
+ }
+
+ std::size_t write_to = 0;
+ for (std::size_t read_from = 0; read_from < list.size(); ++read_from) {
+ if (!filter(list[read_from])) {
+ continue;
+ }
+
+ if (read_from != write_to) {
+ list[write_to] = list[read_from];
+ }
+
+ ++write_to;
+
+ if (return_first_match_only) {
+ return session_list(std::move(list), 1);
+ }
+ }
+
+ list.resize(write_to);
+
+ return list;
+}
+} /* namespace */
+
+session_list list_sessions(const struct session_spec& spec)
+{
+ switch (spec.type) {
+ case session_spec::NAME:
+ if (spec.value == nullptr) {
+ const auto configured_name =
+ lttng::make_unique_wrapper<char, lttng::free>(get_session_name());
+
+ if (configured_name) {
+ const struct session_spec new_spec = {
+ .type = session_spec::NAME, .value = configured_name.get()
+ };
+
+ return list_sessions(new_spec);
+ }
+
+ return session_list();
+ }
+
+ return get_sessions(
+ [&spec](const lttng_session& session) {
+ return strcmp(session.name, spec.value) == 0;
+ },
+ true);
+ case session_spec::GLOB_PATTERN:
+ return get_sessions([&spec](const lttng_session& session) {
+ return fnmatch(spec.value, session.name, 0) == 0;
+ });
+ case session_spec::ALL:
+ return get_sessions([](const lttng_session&) { return true; });
+ }
+
+ return session_list();
+}