Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix license cmd #2351

Merged
merged 4 commits into from Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/control/control-commands.c
Expand Up @@ -50,6 +50,16 @@ void
control_register_command(const gchar *command_name, const gchar *description, CommandFunction function,
gpointer user_data)
{
GList *command_it = g_list_find_custom(command_list, command_name, (GCompareFunc)control_command_start_with_command);
if (command_it)
{
ControlCommand *cmd = (ControlCommand *)command_it->data;
if (cmd->func != function)
{
msg_debug("Trying to register an already registered ControlCommand with different CommandFunction.");
}
return;
}
ControlCommand *new_command = g_new0(ControlCommand, 1);
new_command->command_name = command_name;
new_command->description = description;
Expand All @@ -58,6 +68,26 @@ control_register_command(const gchar *command_name, const gchar *description, Co
command_list = g_list_append(command_list, new_command);
}

void
control_replace_command(const gchar *command_name, const gchar *description, CommandFunction function,
gpointer user_data)
{
GList *command_it = g_list_find_custom(command_list, command_name,
(GCompareFunc)control_command_start_with_command);
if (!command_it)
{
msg_debug("Trying to replace a non-existent command. Command will be registered as a new command.",
evt_tag_str("command", command_name));
control_register_command(command_name, description, function, user_data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the warning here?

If we do, I think we need a bit more context, like mentioning that this was a control socket command, but I like the idea that modules can register such stuff.

But all in all I would make this msg_debug().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to debug

return;
}

ControlCommand *command = (ControlCommand *)command_it->data;
command->description = description;
command->func = function;
command->user_data = user_data;
}

static GString *
control_connection_message_log(GString *command, gpointer user_data)
{
Expand Down Expand Up @@ -141,6 +171,12 @@ control_connection_config(GString *command, gpointer user_data)
return result;
}

static GString *
show_ose_license_info(GString *command, gpointer user_data)
{
return g_string_new("You are using the Open Source Edition of syslog-ng.");
}

static GString *
control_connection_reload(GString *command, gpointer user_data)
{
Expand Down Expand Up @@ -252,6 +288,7 @@ ControlCommand default_commands[] =
{ "QUERY", NULL, process_query_command },
{ "PWD", NULL, process_credentials },
{ "CONFIG", NULL, control_connection_config },
{ "LICENSE", NULL, show_ose_license_info },
{ NULL, NULL, NULL },
};

Expand Down
2 changes: 2 additions & 0 deletions lib/control/control-commands.h
Expand Up @@ -30,6 +30,8 @@

void control_register_command(const gchar *command_name, const gchar *description, CommandFunction function,
gpointer user_data);
void control_replace_command(const gchar *command_name, const gchar *description, CommandFunction function,
gpointer user_data);
GList *control_register_default_commands(MainLoop *main_loop);
GList *get_control_command_list(void);
void reset_control_command_list(void);
Expand Down
60 changes: 60 additions & 0 deletions lib/control/tests/test_control_cmds.c
Expand Up @@ -182,3 +182,63 @@ Test(control_cmds, test_reset_stats)
g_string_free(command, TRUE);
}

static GString *
_original_replace(GString *result, gpointer user_data)
{
return NULL;
}

static GString *
_new_replace(GString *result, gpointer user_data)
{
return NULL;
}

static void
_assert_control_command_eq(ControlCommand *cmd, ControlCommand *cmd_other)
{
cr_assert_eq(cmd->func, cmd_other->func);
cr_assert_str_eq(cmd->command_name, cmd_other->command_name);
cr_assert_str_eq(cmd->description, cmd_other->description);
cr_assert_eq(cmd->user_data, cmd_other->user_data);
}

Test(control_cmds, test_replace_existing_command)
{
control_register_command("REPLACE", "original", _original_replace, (gpointer)0xbaadf00d);
ControlCommand *cmd = command_test_get("REPLACE");
ControlCommand expected_original =
{
.func = _original_replace,
.command_name = "REPLACE",
.description = "original",
.user_data = (gpointer)0xbaadf00d
};

_assert_control_command_eq(cmd, &expected_original);

control_replace_command("REPLACE", "new", _new_replace, (gpointer)0xd006f00d);
ControlCommand *new_cmd = command_test_get("REPLACE");
ControlCommand expected_new =
{
.func = _new_replace,
.command_name = "REPLACE",
.description = "new",
.user_data = (gpointer) 0xd006f00d
};
_assert_control_command_eq(new_cmd, &expected_new);
}

Test(control_cmds, test_replace_non_existing_command)
{
control_replace_command("REPLACE", "new", _new_replace, (gpointer)0xd006f00d);
ControlCommand *new_cmd = command_test_get("REPLACE");
ControlCommand expected_new =
{
.func = _new_replace,
.command_name = "REPLACE",
.description = "new",
.user_data = (gpointer) 0xd006f00d
};
_assert_control_command_eq(new_cmd, &expected_new);
}