Skip to content

Commit f9d4706

Browse files
committed
commands: added --forever and --dont-release to attach command
Signed-off-by: Hofi <[email protected]>
1 parent 5f4f57f commit f9d4706

File tree

2 files changed

+85
-42
lines changed

2 files changed

+85
-42
lines changed

lib/mainloop-control.c

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ control_connection_message_log(ControlConnection *cc, GString *command, gpointer
107107
}
108108

109109
void
110-
_wait_until_peer_disappears(ControlConnection *cc, gint max_seconds, gboolean *cancelled)
110+
_wait_until_peer_disappears(ControlConnection *cc, gint max_seconds, gboolean dont_release_console,
111+
gboolean restore_initial_console, gboolean *cancelled)
111112
{
112113
while (max_seconds != 0 && !(*cancelled))
113114
{
@@ -116,29 +117,27 @@ _wait_until_peer_disappears(ControlConnection *cc, gint max_seconds, gboolean *c
116117
max_seconds--;
117118
control_connection_send_batched_reply(cc, g_string_new("ALIVE\n"));
118119
}
119-
console_release(TRUE);
120+
if (FALSE == dont_release_console)
121+
console_release(restore_initial_console);
120122
}
121123

122-
static void
123-
control_connection_attach(ControlConnection *cc, GString *command, gpointer user_data, gboolean *cancelled)
124+
typedef struct _AttachCommandArgs
124125
{
125-
MainLoop *main_loop = (MainLoop *) user_data;
126-
gchar **cmds = g_strsplit(command->str, " ", 4);
127-
128-
GString *result = g_string_sized_new(128);
129-
gint n_seconds = -1;
130-
gboolean start_debugger = FALSE;
131-
struct
132-
{
133-
gboolean log_stderr;
134-
gint log_level;
135-
} old_values, new_values;
126+
gboolean start_debugger;
127+
gboolean restore_console_on_release;
128+
gboolean dont_release_console;
129+
gint n_seconds;
130+
gboolean log_stderr;
131+
gint log_level;
132+
} AttachCommandArgs;
136133

137-
old_values.log_stderr = log_stderr;
138-
old_values.log_level = msg_get_log_level();
139-
new_values = old_values;
134+
static gboolean
135+
_parse_attach_command_args(GString *command, AttachCommandArgs *args, GString *result)
136+
{
137+
gboolean success = FALSE;
138+
gchar **cmds = g_strsplit(command->str, " ", 6);
140139

141-
if (!cmds[1])
140+
if (cmds[1] == NULL)
142141
{
143142
g_string_assign(result, "FAIL Invalid arguments received");
144143
goto exit;
@@ -150,18 +149,18 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user
150149
}
151150
else if (g_str_equal(cmds[1], "LOGS"))
152151
{
153-
new_values.log_stderr = TRUE;
154-
if (cmds[3])
155-
new_values.log_level = msg_map_string_to_log_level(cmds[3]);
156-
if (new_values.log_level < 0)
152+
args->log_stderr = TRUE;
153+
if (cmds[5])
154+
args->log_level = msg_map_string_to_log_level(cmds[5]);
155+
if (args->log_level < 0)
157156
{
158157
g_string_assign(result, "FAIL Invalid log level");
159158
goto exit;
160159
}
161160
}
162161
else if (g_str_equal(cmds[1], "DEBUGGER"))
163162
{
164-
start_debugger = TRUE;
163+
args->start_debugger = TRUE;
165164
}
166165
else
167166
{
@@ -170,49 +169,86 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user
170169
}
171170

172171
if (cmds[2])
173-
n_seconds = atoi(cmds[2]);
172+
args->n_seconds = atoi(cmds[2]);
173+
174+
if (cmds[3])
175+
args->restore_console_on_release = (FALSE == (gboolean) atoi(cmds[3]));
176+
177+
if (cmds[4])
178+
args->dont_release_console = (gboolean) atoi(cmds[4]);
179+
180+
success = TRUE;
181+
182+
exit:
183+
g_strfreev(cmds);
184+
return success;
185+
}
186+
187+
static void
188+
control_connection_attach(ControlConnection *cc, GString *command, gpointer user_data, gboolean *cancelled)
189+
{
190+
MainLoop *main_loop = (MainLoop *) user_data;
191+
GString *result = g_string_sized_new(128);
192+
struct
193+
{
194+
gboolean log_stderr;
195+
gint log_level;
196+
} old_values =
197+
{
198+
log_stderr,
199+
msg_get_log_level()
200+
};
201+
AttachCommandArgs cmd_args =
202+
{
203+
.start_debugger = FALSE,
204+
.restore_console_on_release = TRUE,
205+
.dont_release_console = FALSE,
206+
.n_seconds = -1,
207+
.log_stderr = old_values.log_stderr,
208+
.log_level = old_values.log_level
209+
};
210+
211+
if (FALSE == _parse_attach_command_args(command, &cmd_args, result))
212+
goto exit;
174213

175214
gint fds[3];
176215
gsize num_fds = G_N_ELEMENTS(fds);
177-
if (!control_connection_get_attached_fds(cc, fds, &num_fds) || num_fds != 3)
216+
if (FALSE == control_connection_get_attached_fds(cc, fds, &num_fds) || num_fds != 3)
178217
{
179218
g_string_assign(result,
180219
"FAIL The underlying transport for syslog-ng-ctl does not support fd passing or incorrect number of fds received");
181220
goto exit;
182221
}
183222

184-
if (!console_acquire_from_fds(fds, TRUE))
223+
if (FALSE == console_acquire_from_fds(fds, TRUE))
185224
{
186-
g_string_assign(result,
187-
"FAIL Error acquiring console");
225+
g_string_assign(result, "FAIL Error acquiring console");
188226
goto exit;
189227
}
190228

191-
log_stderr = new_values.log_stderr;
192-
msg_set_log_level(new_values.log_level);
229+
log_stderr = cmd_args.log_stderr;
230+
msg_set_log_level(cmd_args.log_level);
193231

194-
if (start_debugger && !debugger_is_running())
232+
if (cmd_args.start_debugger && FALSE == debugger_is_running())
195233
{
196234
//cfg_load_module(self->current_configuration, "mod-python");
197235
debugger_start(main_loop, main_loop_get_current_config(main_loop));
198236
}
199237

200-
_wait_until_peer_disappears(cc, n_seconds, cancelled);
238+
_wait_until_peer_disappears(cc, cmd_args.n_seconds, cmd_args.dont_release_console, cmd_args.restore_console_on_release,
239+
cancelled);
201240

202-
if (start_debugger && debugger_is_running())
203-
{
204-
debugger_stop();
205-
}
241+
if (cmd_args.start_debugger && debugger_is_running())
242+
debugger_stop();
206243

207244
log_stderr = old_values.log_stderr;
208245
msg_set_log_level(old_values.log_level);
209246

210247
g_string_assign(result, "OK [console output ends here]");
211-
exit:
212248

249+
exit:
213250
control_connection_send_batched_reply(cc, result);
214251
control_connection_send_close_batch(cc);
215-
g_strfreev(cmds);
216252
}
217253

218254
static void

syslog-ng-ctl/commands/attach.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
#include "ctl-stats.h"
2525
#include "syslog-ng.h"
2626

27-
static gint attach_options_seconds;
27+
static gint attach_options_seconds = -1;
2828
static gchar *attach_options_log_level = NULL;
29+
static gboolean attach_options_forever = FALSE;
30+
static gboolean attach_options_dont_release_console = FALSE;
2931
static gchar **attach_commands = NULL;
3032

3133
static gboolean
@@ -45,8 +47,10 @@ _store_log_level(const gchar *option_name,
4547

4648
GOptionEntry attach_options[] =
4749
{
48-
{ "seconds", 0, 0, G_OPTION_ARG_INT, &attach_options_seconds, "amount of time to attach for", NULL },
50+
{ "seconds", 's', 0, G_OPTION_ARG_INT, &attach_options_seconds, "amount of time to attach for", NULL },
4951
{ "log-level", 0, 0, G_OPTION_ARG_CALLBACK, _store_log_level, "change syslog-ng log level", "<default|verbose|debug|trace>" },
52+
{ "forever", 'f', 0, G_OPTION_ARG_NONE, &attach_options_forever, "do not pass logs/stdio back to syslog-ng after syslog-ng-ctl is exited", NULL },
53+
{ "dont-release", 'd', 0, G_OPTION_ARG_NONE, &attach_options_dont_release_console, "do not pass logs/stdio back to syslog-ng after syslog-ng-ctl is exited and keep logging onto the calling console" },
5054
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &attach_commands, "attach mode: logs, debugger, stdio", NULL },
5155
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
5256
};
@@ -81,9 +85,12 @@ slng_attach(int argc, char *argv[], const gchar *mode, GOptionContext *ctx)
8185
return 1;
8286
}
8387

84-
g_string_append_printf(command, " %d", attach_options_seconds ? : -1);
88+
g_string_append_printf(command, " %d", attach_options_seconds > 0 ? attach_options_seconds : -1);
89+
g_string_append_printf(command, " %d", attach_options_forever);
90+
g_string_append_printf(command, " %d", attach_options_dont_release_console);
8591
if (attach_options_log_level)
8692
g_string_append_printf(command, " %s", attach_options_log_level);
93+
8794
gint result = attach_command(command->str);
8895
g_string_free(command, TRUE);
8996
return result;

0 commit comments

Comments
 (0)