Code

sysdb: Don't exit if the server closed the connection.
[sysdb.git] / src / tools / sysdb / command.c
1 /*
2  * SysDB - src/tools/sysdb/command.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
34 #include "tools/sysdb/command.h"
35 #include "tools/sysdb/input.h"
37 #include "frontend/proto.h"
38 #include "utils/error.h"
39 #include "utils/proto.h"
40 #include "utils/strbuf.h"
42 #include <errno.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
49 static void
50 log_printer(sdb_strbuf_t *buf)
51 {
52         uint32_t prio = sdb_proto_get_int(buf, 0);
54         if (prio == UINT32_MAX) {
55                 printf("ERROR: Received a LOG message with invalid "
56                                 "or missing priority\n");
57                 return;
58         }
59         sdb_strbuf_skip(buf, 0, sizeof(prio));
61         printf("%s: %s\n", SDB_LOG_PRIO_TO_STRING((int)prio),
62                         sdb_strbuf_string(buf));
63 } /* log_printer */
65 static void
66 data_printer(sdb_strbuf_t *buf)
67 {
68         size_t len = sdb_strbuf_len(buf);
70         if ((! len) || (len == sizeof(uint32_t))) {
71                 /* empty command or empty reply */
72                 return;
73         }
74         else if (len < sizeof(uint32_t)) {
75                 printf("ERROR: Received a DATA message with invalid "
76                                 "or missing data-type\n");
77                 return;
78         }
80         /* At the moment, we don't care about the result type. We simply print the
81          * result without further parsing it. */
82         sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
83         printf("%s\n", sdb_strbuf_string(buf));
84 } /* data_printer */
86 static struct {
87         int status;
88         void (*printer)(sdb_strbuf_t *);
89 } response_printers[] = {
90         { SDB_CONNECTION_LOG,  log_printer },
91         { SDB_CONNECTION_DATA, data_printer },
92 };
94 static void
95 clear_query(sdb_input_t *input)
96 {
97         sdb_strbuf_skip(input->input, 0, input->query_len);
98         input->tokenizer_pos -= input->query_len;
99         input->query_len = 0;
100 } /* clear_query */
102 /*
103  * public API
104  */
106 int
107 sdb_command_print_reply(sdb_client_t *client)
109         sdb_strbuf_t *recv_buf;
110         const char *result;
111         uint32_t rcode = 0;
113         int status = 0;
114         size_t i;
116         recv_buf = sdb_strbuf_create(1024);
117         if (! recv_buf)
118                 return -1;
120         if (sdb_client_recv(client, &rcode, recv_buf) < 0)
121                 rcode = UINT32_MAX;
123         if (sdb_client_eof(client)) {
124                 sdb_strbuf_destroy(recv_buf);
125                 return -1;
126         }
128         if (rcode == UINT32_MAX) {
129                 printf("ERROR: ");
130                 status = -1;
131         }
132         else
133                 status = (int)rcode;
135         for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
136                 if (status == response_printers[i].status) {
137                         response_printers[i].printer(recv_buf);
138                         sdb_strbuf_destroy(recv_buf);
139                         return status;
140                 }
141         }
143         result = sdb_strbuf_string(recv_buf);
144         if (result && *result)
145                 printf("%s\n", result);
146         else if (rcode == UINT32_MAX) {
147                 char errbuf[1024];
148                 printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
149         }
151         sdb_strbuf_destroy(recv_buf);
152         return status;
153 } /* sdb_command_print_reply */
155 char *
156 sdb_command_exec(sdb_input_t *input)
158         const char *query;
159         uint32_t query_len;
161         char *data = NULL;
163         query = sdb_strbuf_string(input->input);
164         query_len = (uint32_t)input->query_len;
166         assert(input->query_len <= input->tokenizer_pos);
168         /* removing leading and trailing newlines */
169         while (query_len && (*query == '\n')) {
170                 ++query;
171                 --query_len;
172         }
173         while (query_len && (query[query_len - 1]) == '\n')
174                 --query_len;
176         if (sdb_client_eof(input->client)) {
177                 sdb_client_close(input->client);
178                 if (sdb_client_connect(input->client, input->user)) {
179                         printf("Failed to reconnect to SysDBd.\n");
180                         clear_query(input);
181                         return NULL;
182                 }
183                 printf("Successfully reconnected to SysDBd.\n");
184         }
186         if (query_len) {
187                 data = strndup(query, query_len);
188                 /* ignore errors; we'll only hide the command from the caller */
190                 sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
192                 /* The server will send back *something*, either error/log messages
193                  * and/or the reply to the query. Here, we don't care about what it
194                  * sends back. We'll wait for the first reply and then return to the
195                  * main loop which will handle any subsequent replies, including
196                  * eventually the reply to the query (if it's not the first reply). */
197                 /* TODO: wait for the actual reply instead */
198                 if (sdb_command_print_reply(input->client) < 0) {
199                         if (data)
200                                 free(data);
201                         data = NULL;
202                 }
203         }
205         clear_query(input);
206         return data;
207 } /* sdb_command_exec */
209 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */