Code

sysdb: When using -c, wait for the server's final response.
authorSebastian Harl <sh@tokkee.org>
Thu, 1 May 2014 13:56:23 +0000 (15:56 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 1 May 2014 13:56:23 +0000 (15:56 +0200)
There might be some intermediate log messages, which should be printed but
then we need to wait for further replies.

src/tools/sysdb/command.c
src/tools/sysdb/main.c

index 77f3c8346498c430be10fcbafcf1ab9a06ee5cc2..7cbde41a7e05e1bff9f6c3f0dfd5d966dda62e8f 100644 (file)
@@ -67,7 +67,7 @@ sdb_command_print_reply(sdb_client_t *client)
                return -1;
 
        if (rcode == CONNECTION_ERROR)
-               status = 1;
+               status = CONNECTION_ERROR;
 
        if (rcode == UINT32_MAX)
                printf("ERROR: ");
@@ -109,6 +109,12 @@ sdb_command_exec(sdb_input_t *input)
                /* ignore errors; we'll only hide the command from the caller */
 
                sdb_client_send(input->client, CONNECTION_QUERY, query_len, query);
+
+               /* The server will send back *something*, either error/log messages
+                * and/or the reply to the query. Here, we don't care about what it
+                * sends back. We'll wait for the first reply and then return to the
+                * main loop which will handle any subsequent replies, including
+                * eventually the reply to the query (if it's not the first reply). */
                if (sdb_command_print_reply(input->client) < 0)
                        return NULL;
        }
index 8455a6c7000e4856483b8650ce02a932c0fa98a2..1174fa9fa0462ddb13603456b7de2bba19a4d33e 100644 (file)
@@ -184,7 +184,6 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands)
 
        while (sdb_llist_iter_has_next(iter)) {
                sdb_object_t *obj = sdb_llist_iter_get_next(iter);
-               int err;
 
                if (sdb_client_send(client, CONNECTION_QUERY,
                                        (uint32_t)strlen(obj->name), obj->name) <= 0) {
@@ -193,13 +192,23 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands)
                        status = 1;
                        break;
                }
-               err = sdb_command_print_reply(client);
-               if (err) {
-                       if (err < 0)
+
+               /* Wait for server replies. We might get any number of log messages
+                * but eventually see the reply to the query, which is either OK or
+                * ERROR. */
+               while (42) {
+                       status = sdb_command_print_reply(client);
+                       if (status < 0) {
                                sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
-                       status = 1;
-                       break;
+                               break;
+                       }
+
+                       if ((status == CONNECTION_OK) || (status == CONNECTION_ERROR))
+                               break;
                }
+
+               if (status)
+                       break;
        }
 
        sdb_llist_iter_destroy(iter);