Code

sysdb: When using -c, exit non-zero if any command failed.
authorSebastian Harl <sh@tokkee.org>
Wed, 30 Apr 2014 21:49:25 +0000 (23:49 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 30 Apr 2014 21:49:25 +0000 (23:49 +0200)
src/tools/sysdb/command.c
src/tools/sysdb/command.h
src/tools/sysdb/main.c

index c79259e401c0786bb80336601208b058180e23e9..77f3c8346498c430be10fcbafcf1ab9a06ee5cc2 100644 (file)
@@ -52,17 +52,23 @@ sdb_command_print_reply(sdb_client_t *client)
        sdb_strbuf_t *recv_buf;
        const char *result;
        uint32_t rcode = 0;
+       int status = 0;
 
        recv_buf = sdb_strbuf_create(1024);
        if (! recv_buf)
                return -1;
 
-       if (sdb_client_recv(client, &rcode, recv_buf) < 0)
+       if (sdb_client_recv(client, &rcode, recv_buf) < 0) {
                rcode = UINT32_MAX;
+               status = -1;
+       }
 
        if (sdb_client_eof(client))
                return -1;
 
+       if (rcode == CONNECTION_ERROR)
+               status = 1;
+
        if (rcode == UINT32_MAX)
                printf("ERROR: ");
        result = sdb_strbuf_string(recv_buf);
@@ -74,7 +80,7 @@ sdb_command_print_reply(sdb_client_t *client)
        }
 
        sdb_strbuf_destroy(recv_buf);
-       return 0;
+       return status;
 } /* sdb_command_print_reply */
 
 char *
@@ -103,7 +109,7 @@ 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);
-               if (sdb_command_print_reply(input->client))
+               if (sdb_command_print_reply(input->client) < 0)
                        return NULL;
        }
 
index febf64f144a3c6f54ecee86d077531d14b414f76..bd9833828167de802c063c28e0878efdf06e3bf1 100644 (file)
@@ -36,7 +36,8 @@
  *
  * Returns:
  *  - 0 on success
- *  - a negative value else
+ *  - a negative value if no reply could be read from the server
+ *  - a positive value if the server returned an error
  */
 int
 sdb_command_print_reply(sdb_client_t *client);
index 991e1ffb6d8e744252309646f4741e67abfe901d..8455a6c7000e4856483b8650ce02a932c0fa98a2 100644 (file)
@@ -184,6 +184,8 @@ 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) {
                        sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
@@ -191,8 +193,10 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands)
                        status = 1;
                        break;
                }
-               if (sdb_command_print_reply(client)) {
-                       sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
+               err = sdb_command_print_reply(client);
+               if (err) {
+                       if (err < 0)
+                               sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
                        status = 1;
                        break;
                }