]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-program-client: Make an explicit enum for the exit code.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Fri, 26 Jan 2018 19:37:32 +0000 (20:37 +0100)
committerStephan Bosch <stephan.bosch@dovecot.fi>
Fri, 26 Jan 2018 19:37:32 +0000 (20:37 +0100)
Before, the meaning of the code was confusing, since the actual program returns
a different set of values.

src/lib-program-client/program-client-local.c
src/lib-program-client/program-client-private.h
src/lib-program-client/program-client-remote.c
src/lib-program-client/program-client.c

index 17e83329546c55e7240f9b6ba2d1880f01998818..f1ea7cd7670e24a6c3f786562d12a75be78d9512 100644 (file)
@@ -304,7 +304,7 @@ void program_client_local_exited(struct program_client_local *plclient)
        plclient->exited = TRUE;
        plclient->pid = -1;
        /* Evaluate child exit status */
-       pclient->exit_code = -1;
+       pclient->exit_code = PROGRAM_CLIENT_EXIT_INTERNAL_FAILURE;
 
        if (WIFEXITED(plclient->status)) {
                /* Exited */
@@ -313,9 +313,9 @@ void program_client_local_exited(struct program_client_local *plclient)
                if (exit_code != 0) {
                        i_info("program `%s' terminated with non-zero exit code %d",
                               pclient->path, exit_code);
-                       pclient->exit_code = 0;
+                       pclient->exit_code = PROGRAM_CLIENT_EXIT_FAILURE;
                } else {
-                       pclient->exit_code = 1;
+                       pclient->exit_code = PROGRAM_CLIENT_EXIT_SUCCESS;
                }
        } else if (WIFSIGNALED(plclient->status)) {
                /* Killed with a signal */
@@ -414,7 +414,7 @@ void program_client_local_disconnect(struct program_client *pclient, bool force)
 
        if (pid < 0) {
                /* program never started */
-               pclient->exit_code = 0;
+               pclient->exit_code = PROGRAM_CLIENT_EXIT_FAILURE;
                program_client_local_exited(plclient);
                return;
        }
index 42e0a952bc14368ff78753d0e8721d595d0d8015..f0d429535e58bc5f55ac20107006f6708ce3cb31 100644 (file)
@@ -14,6 +14,12 @@ enum program_client_error {
        PROGRAM_CLIENT_ERROR_OTHER
 };
 
+enum program_client_exit_code {
+       PROGRAM_CLIENT_EXIT_INTERNAL_FAILURE = -1,
+       PROGRAM_CLIENT_EXIT_FAILURE = 0,
+       PROGRAM_CLIENT_EXIT_SUCCESS = 1,
+};
+
 struct program_client_extra_fd {
        struct program_client *pclient;
 
@@ -50,7 +56,7 @@ struct program_client {
 
        bool other_error;
        enum program_client_error error;
-       int exit_code;
+       enum program_client_exit_code exit_code;
 
        int (*connect) (struct program_client * pclient);
        int (*close_output) (struct program_client * pclient);
index 250538a665ab9a1313b8c235df66c60818a1adff..5a6b5cb499e130c2d546c44352f2626e8c0615cb 100644 (file)
@@ -49,6 +49,32 @@ void program_client_istream_destroy(struct iostream_private *stream)
        i_stream_unref(&scstream->istream.parent);
 }
 
+static void
+program_client_istream_parse_result(struct program_client_istream *scstream,
+       size_t pos)
+{
+       struct istream_private *stream = &scstream->istream;
+
+       if (stream->buffer == NULL || pos < 2 ||
+           stream->buffer[pos - 1] != '\n') {
+               scstream->client->exit_code =
+                       PROGRAM_CLIENT_EXIT_INTERNAL_FAILURE;
+               return;
+       }
+
+       switch (stream->buffer[pos - 2]) {
+       case '+':
+               scstream->client->exit_code = PROGRAM_CLIENT_EXIT_SUCCESS;
+               break;
+       case '-':
+               scstream->client->exit_code = PROGRAM_CLIENT_EXIT_FAILURE;
+               break;
+       default:
+               scstream->client->exit_code =
+                       PROGRAM_CLIENT_EXIT_INTERNAL_FAILURE;
+       }
+}
+
 static ssize_t
 program_client_istream_read(struct istream_private *stream)
 {
@@ -90,22 +116,7 @@ program_client_istream_read(struct istream_private *stream)
 
                        if (stream->parent->eof) {
                                /* Check return code at EOF */
-                               if (stream->buffer != NULL && pos >= 2 &&
-                                   stream->buffer[pos - 1] == '\n') {
-                                       switch (stream->buffer[pos - 2]) {
-                                       case '+':
-                                               scstream->client->exit_code = 1;
-                                               break;
-                                       case '-':
-                                               scstream->client->exit_code = 0;
-                                               break;
-                                       default:
-                                               scstream->client->exit_code =
-                                                       -1;
-                                       }
-                               } else {
-                                       scstream->client->exit_code = -1;
-                               }
+                               program_client_istream_parse_result(scstream, pos);
                        }
 
                        if (stream->buffer != NULL && pos >= 1) {
@@ -562,9 +573,9 @@ void program_client_remote_disconnect(struct program_client *pclient, bool force
                   generally unlikely to occur. */
                if (pclient->program_input->stream_errno != 0 ||
                    i_stream_have_bytes_left(pclient->program_input))
-                       pclient->exit_code = -1;
+                       pclient->exit_code = PROGRAM_CLIENT_EXIT_INTERNAL_FAILURE;
        } else {
-               pclient->exit_code = 1;
+               pclient->exit_code = PROGRAM_CLIENT_EXIT_SUCCESS;
        }
 
        program_client_disconnected(pclient);
index dccef79c7863975fe1dffcafc25a3d32b03cdea6..8762a67086c3a07e2394afa941bbc62bb7e5356c 100644 (file)
@@ -144,7 +144,7 @@ void program_client_disconnected(struct program_client *pclient)
        program_client_callback(pclient,
                pclient->error != PROGRAM_CLIENT_ERROR_NONE ?
                        -1 :
-                       pclient->exit_code,
+                       (int)pclient->exit_code,
                pclient->context);
 }
 
@@ -676,7 +676,7 @@ int program_client_run(struct program_client *pclient)
        if (pclient->error != PROGRAM_CLIENT_ERROR_NONE)
                return -1;
 
-       return pclient->exit_code;
+       return (int)pclient->exit_code;
 }
 
 #undef program_client_run_async
@@ -687,7 +687,7 @@ void program_client_run_async(struct program_client *pclient,
        i_assert(callback != NULL);
 
        pclient->disconnected = FALSE;
-       pclient->exit_code = 1;
+       pclient->exit_code = PROGRAM_CLIENT_EXIT_SUCCESS;
        pclient->error = PROGRAM_CLIENT_ERROR_NONE;
 
        pclient->callback = callback;