Code

Merge branch 'master' of git://git.tokkee.org/sysdb
[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"
36 #include "tools/sysdb/json.h"
38 #include "frontend/proto.h"
39 #include "utils/error.h"
40 #include "utils/proto.h"
41 #include "utils/strbuf.h"
43 #include <errno.h>
45 #include <assert.h>
46 #include <ctype.h>
47 #include <stdlib.h>
48 #include <string.h>
50 static void
51 ok_printer(sdb_strbuf_t *buf)
52 {
53         const char *msg = sdb_strbuf_string(buf);
54         if (msg && *msg)
55                 printf("%s\n", msg);
56         else
57                 printf("OK\n");
58 } /* ok_printer */
60 static void
61 log_printer(sdb_strbuf_t *buf)
62 {
63         uint32_t prio = 0;
65         if (sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &prio) < 0) {
66                 sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
67                                 "or missing priority");
68                 prio = (uint32_t)SDB_LOG_ERR;
69         }
70         sdb_strbuf_skip(buf, 0, sizeof(prio));
72         sdb_log((int)prio, "%s", sdb_strbuf_string(buf));
73 } /* log_printer */
75 static void
76 data_printer(sdb_strbuf_t *buf)
77 {
78         size_t len = sdb_strbuf_len(buf);
80         if ((! len) || (len == sizeof(uint32_t))) {
81                 /* empty command or empty reply */
82                 return;
83         }
84         else if (len < sizeof(uint32_t)) {
85                 sdb_log(SDB_LOG_ERR, "Received a DATA message with invalid "
86                                 "or missing data-type");
87                 return;
88         }
90         /* At the moment, we don't care about the result type. We simply print the
91          * result without further parsing it. */
92         sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
93         if (sdb_json_print(buf))
94                 sdb_log(SDB_LOG_ERR, "Failed to print result");
95         printf("\n");
96 } /* data_printer */
98 static struct {
99         int status;
100         void (*printer)(sdb_strbuf_t *);
101 } response_printers[] = {
102         { SDB_CONNECTION_OK,   ok_printer },
103         { SDB_CONNECTION_LOG,  log_printer },
104         { SDB_CONNECTION_DATA, data_printer },
105 };
107 static void
108 clear_query(sdb_input_t *input)
110         sdb_strbuf_skip(input->input, 0, input->query_len);
111         input->tokenizer_pos -= input->query_len;
112         input->query_len = 0;
113         input->have_input = 0;
114 } /* clear_query */
116 /*
117  * public API
118  */
120 int
121 sdb_command_print_reply(sdb_client_t *client)
123         sdb_strbuf_t *recv_buf;
124         const char *result;
125         uint32_t rcode = 0;
127         int status = -1;
128         size_t i;
130         recv_buf = sdb_strbuf_create(1024);
131         if (! recv_buf)
132                 return -1;
134         if (sdb_client_recv(client, &rcode, recv_buf) < 0)
135                 rcode = UINT32_MAX;
137         if (sdb_client_eof(client)) {
138                 sdb_strbuf_destroy(recv_buf);
139                 return -1;
140         }
142         if (rcode != UINT32_MAX)
143                 status = (int)rcode;
145         for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
146                 if (status == response_printers[i].status) {
147                         response_printers[i].printer(recv_buf);
148                         sdb_strbuf_destroy(recv_buf);
149                         return status;
150                 }
151         }
153         result = sdb_strbuf_string(recv_buf);
154         if (result && *result)
155                 sdb_log(SDB_LOG_ERR, "%s", result);
156         else if (rcode == UINT32_MAX) {
157                 char errbuf[1024];
158                 sdb_log(SDB_LOG_ERR, "%s",
159                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
160         }
162         sdb_strbuf_destroy(recv_buf);
163         return status;
164 } /* sdb_command_print_reply */
166 char *
167 sdb_command_exec(sdb_input_t *input)
169         const char *query;
170         uint32_t query_len;
172         char *data = NULL;
174         query = sdb_strbuf_string(input->input);
175         query_len = (uint32_t)input->query_len;
177         assert(input->query_len <= input->tokenizer_pos);
179         /* removing leading and trailing newlines */
180         while (query_len && (*query == '\n')) {
181                 ++query;
182                 --query_len;
183         }
184         while (query_len && (query[query_len - 1]) == '\n')
185                 --query_len;
187         if (query_len) {
188                 data = strndup(query, query_len);
189                 /* ignore errors; we'll only hide the command from the caller */
190         }
192         if (sdb_client_eof(input->client)) {
193                 if (sdb_input_reconnect()) {
194                         clear_query(input);
195                         return data;
196                 }
197         }
198         else if (! query_len)
199                 return NULL;
201         sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
203         /* The server may send back log messages but will eventually reply to the
204          * query, which is either DATA or ERROR. */
205         while (42) {
206                 int status = sdb_command_print_reply(input->client);
207                 if (status < 0) {
208                         sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
209                         break;
210                 }
212                 if ((status == SDB_CONNECTION_OK)
213                                 || (status == SDB_CONNECTION_DATA)
214                                 || (status == SDB_CONNECTION_ERROR))
215                         break;
216         }
217         clear_query(input);
218         return data;
219 } /* sdb_command_exec */
221 void
222 sdb_command_print_server_version(sdb_input_t *input)
224         sdb_strbuf_t *buf = sdb_strbuf_create(32);
225         uint32_t code = 0, version = 0;
226         const char *extra;
228         if ((sdb_client_rpc(input->client, SDB_CONNECTION_SERVER_VERSION,
229                                         0, NULL, &code, buf) < 0) || (code != SDB_CONNECTION_OK))
230                 return;
231         if (sdb_strbuf_len(buf) < sizeof(version))
232                 return;
234         sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &version);
235         extra = sdb_strbuf_string(buf) + sizeof(version);
236         sdb_log(SDB_LOG_INFO, "SysDB server %d.%d.%d%s",
237                         SDB_VERSION_DECODE((int)version), extra);
238         sdb_strbuf_destroy(buf);
239 } /* sdb_command_print_server_version */
241 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */