This is an automated email from Gerrit.

"Evgeniy Naydanov <evgeniy.nayda...@syntacore.com>" just uploaded a new patch 
set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/7953

-- gerrit

commit 3a05831f5288974c744fcbf247978fda81c06dbc
Author: Evgeniy Naydanov <evgeniy.nayda...@syntacore.com>
Date:   Tue Oct 24 12:12:54 2023 +0300

    server: do not override `shutdown error`
    
    Consider a target with an `examine-fail` callback set to
    `shutdown error`:
    ```
    > cat target.cfg
    proc init_targets {} {
            jtag newtap riscv tap -irlen 5
    
            target create riscv.cpu riscv -chain-position riscv.tap
    
            riscv.cpu configure -event examine-fail "shutdown error"
    }
    ```
    The following run will return 0 if the examination fails (prior to the
    patch):
    ```
    > openocd -f adapter.cfg -f target.cfg -c init -c shutdown ; echo $?
    ...
    Info : only one transport option; autoselecting 'jtag'
    init_targets
    ...
    Error: [riscv.cpu] <some error during examination>
    shutdown command invoked
    Error executing event examine-fail on target riscv.cpu:
    
    Warn : target riscv.cpu examination failed
    ...
    shutdown command invoked
    Error: [riscv.cpu] <some more errors during deinitialization>
    ...
    0
    ```
    After the patch `echo $?` prints `1`.
    
    Change-Id: Ica2a0cf9a8bc207791641515a24594a08d745c30
    Signed-off-by: Evgeniy Naydanov <evgeniy.nayda...@syntacore.com>

diff --git a/src/server/server.c b/src/server/server.c
index 2be90451f7..58e6b83429 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -44,6 +44,17 @@ enum shutdown_reason {
 };
 static enum shutdown_reason shutdown_openocd = CONTINUE_MAIN_LOOP;
 
+static void request_shutdown(void)
+{
+       if (shutdown_openocd == CONTINUE_MAIN_LOOP)
+               shutdown_openocd = SHUTDOWN_REQUESTED;
+}
+
+static bool returns_error(void)
+{
+       return shutdown_openocd == SHUTDOWN_WITH_ERROR_CODE;
+}
+
 /* store received signal to exit application by killing ourselves */
 static int last_signal;
 
@@ -568,7 +579,7 @@ int server_loop(struct command_context *command_context)
                                                                        
service->type == CONNECTION_STDINOUT) {
                                                                /* if 
connection uses a pipe then
                                                                 * shutdown 
openocd on error */
-                                                               
shutdown_openocd = SHUTDOWN_REQUESTED;
+                                                               
request_shutdown();
                                                        }
                                                        
remove_connection(service, c);
                                                        LOG_INFO("dropped '%s' 
connection",
@@ -595,7 +606,7 @@ int server_loop(struct command_context *command_context)
        if (shutdown_openocd == SHUTDOWN_WITH_SIGNAL_CODE)
                command_run_line(command_context, "shutdown");
 
-       return shutdown_openocd == SHUTDOWN_WITH_ERROR_CODE ? ERROR_FAIL : 
ERROR_OK;
+       return returns_error() ? ERROR_FAIL : ERROR_OK;
 }
 
 static void sig_handler(int sig)
@@ -757,18 +768,18 @@ COMMAND_HANDLER(handle_shutdown_command)
 {
        LOG_USER("shutdown command invoked");
 
-       shutdown_openocd = SHUTDOWN_REQUESTED;
+       request_shutdown();
 
        command_run_line(CMD_CTX, "_run_pre_shutdown_commands");
 
        if (CMD_ARGC == 1) {
-               if (!strcmp(CMD_ARGV[0], "error")) {
+               if (!strcmp(CMD_ARGV[0], "error"))
                        shutdown_openocd = SHUTDOWN_WITH_ERROR_CODE;
-                       return ERROR_FAIL;
-               }
+               else
+                       LOG_WARNING("Invalid argument %s.", CMD_ARGV[0]);
        }
 
-       return ERROR_COMMAND_CLOSE_CONNECTION;
+       return returns_error() ? ERROR_FAIL : ERROR_COMMAND_CLOSE_CONNECTION;
 }
 
 COMMAND_HANDLER(handle_poll_period_command)

-- 

Reply via email to