@@ -107,7 +107,8 @@ control_connection_message_log(ControlConnection *cc, GString *command, gpointer
107
107
}
108
108
109
109
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 )
111
112
{
112
113
while (max_seconds != 0 && !(* cancelled ))
113
114
{
@@ -116,29 +117,27 @@ _wait_until_peer_disappears(ControlConnection *cc, gint max_seconds, gboolean *c
116
117
max_seconds -- ;
117
118
control_connection_send_batched_reply (cc , g_string_new ("ALIVE\n" ));
118
119
}
119
- console_release (TRUE);
120
+ if (FALSE == dont_release_console )
121
+ console_release (restore_initial_console );
120
122
}
121
123
122
- static void
123
- control_connection_attach (ControlConnection * cc , GString * command , gpointer user_data , gboolean * cancelled )
124
+ typedef struct _AttachCommandArgs
124
125
{
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 ;
136
133
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 );
140
139
141
- if (! cmds [1 ])
140
+ if (cmds [1 ] == NULL )
142
141
{
143
142
g_string_assign (result , "FAIL Invalid arguments received" );
144
143
goto exit ;
@@ -150,18 +149,18 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user
150
149
}
151
150
else if (g_str_equal (cmds [1 ], "LOGS" ))
152
151
{
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 )
157
156
{
158
157
g_string_assign (result , "FAIL Invalid log level" );
159
158
goto exit ;
160
159
}
161
160
}
162
161
else if (g_str_equal (cmds [1 ], "DEBUGGER" ))
163
162
{
164
- start_debugger = TRUE;
163
+ args -> start_debugger = TRUE;
165
164
}
166
165
else
167
166
{
@@ -170,49 +169,86 @@ control_connection_attach(ControlConnection *cc, GString *command, gpointer user
170
169
}
171
170
172
171
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 ;
174
213
175
214
gint fds [3 ];
176
215
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 )
178
217
{
179
218
g_string_assign (result ,
180
219
"FAIL The underlying transport for syslog-ng-ctl does not support fd passing or incorrect number of fds received" );
181
220
goto exit ;
182
221
}
183
222
184
- if (! console_acquire_from_fds (fds , TRUE))
223
+ if (FALSE == console_acquire_from_fds (fds , TRUE))
185
224
{
186
- g_string_assign (result ,
187
- "FAIL Error acquiring console" );
225
+ g_string_assign (result , "FAIL Error acquiring console" );
188
226
goto exit ;
189
227
}
190
228
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 );
193
231
194
- if (start_debugger && ! debugger_is_running ())
232
+ if (cmd_args . start_debugger && FALSE == debugger_is_running ())
195
233
{
196
234
//cfg_load_module(self->current_configuration, "mod-python");
197
235
debugger_start (main_loop , main_loop_get_current_config (main_loop ));
198
236
}
199
237
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 );
201
240
202
- if (start_debugger && debugger_is_running ())
203
- {
204
- debugger_stop ();
205
- }
241
+ if (cmd_args .start_debugger && debugger_is_running ())
242
+ debugger_stop ();
206
243
207
244
log_stderr = old_values .log_stderr ;
208
245
msg_set_log_level (old_values .log_level );
209
246
210
247
g_string_assign (result , "OK [console output ends here]" );
211
- exit :
212
248
249
+ exit :
213
250
control_connection_send_batched_reply (cc , result );
214
251
control_connection_send_close_batch (cc );
215
- g_strfreev (cmds );
216
252
}
217
253
218
254
static void
0 commit comments