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 ok_printer(sdb_strbuf_t *buf)
51 {
52 const char *msg = sdb_strbuf_string(buf);
53 if (msg && *msg)
54 printf("%s\n", msg);
55 else
56 printf("OK\n");
57 } /* ok_printer */
59 static void
60 log_printer(sdb_strbuf_t *buf)
61 {
62 uint32_t prio = 0;
64 if (sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &prio) < 0) {
65 sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
66 "or missing priority");
67 prio = (uint32_t)SDB_LOG_ERR;
68 }
69 sdb_strbuf_skip(buf, 0, sizeof(prio));
71 sdb_log((int)prio, "%s", sdb_strbuf_string(buf));
72 } /* log_printer */
74 static void
75 data_printer(sdb_strbuf_t *buf)
76 {
77 size_t len = sdb_strbuf_len(buf);
79 if ((! len) || (len == sizeof(uint32_t))) {
80 /* empty command or empty reply */
81 return;
82 }
83 else if (len < sizeof(uint32_t)) {
84 sdb_log(SDB_LOG_ERR, "Received a DATA message with invalid "
85 "or missing data-type");
86 return;
87 }
89 /* At the moment, we don't care about the result type. We simply print the
90 * result without further parsing it. */
91 sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
92 printf("%s\n", sdb_strbuf_string(buf));
93 } /* data_printer */
95 static struct {
96 int status;
97 void (*printer)(sdb_strbuf_t *);
98 } response_printers[] = {
99 { SDB_CONNECTION_OK, ok_printer },
100 { SDB_CONNECTION_LOG, log_printer },
101 { SDB_CONNECTION_DATA, data_printer },
102 };
104 static void
105 clear_query(sdb_input_t *input)
106 {
107 sdb_strbuf_skip(input->input, 0, input->query_len);
108 input->tokenizer_pos -= input->query_len;
109 input->query_len = 0;
110 input->have_input = 0;
111 } /* clear_query */
113 /*
114 * public API
115 */
117 int
118 sdb_command_print_reply(sdb_client_t *client)
119 {
120 sdb_strbuf_t *recv_buf;
121 const char *result;
122 uint32_t rcode = 0;
124 int status = -1;
125 size_t i;
127 recv_buf = sdb_strbuf_create(1024);
128 if (! recv_buf)
129 return -1;
131 if (sdb_client_recv(client, &rcode, recv_buf) < 0)
132 rcode = UINT32_MAX;
134 if (sdb_client_eof(client)) {
135 sdb_strbuf_destroy(recv_buf);
136 return -1;
137 }
139 if (rcode != UINT32_MAX)
140 status = (int)rcode;
142 for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
143 if (status == response_printers[i].status) {
144 response_printers[i].printer(recv_buf);
145 sdb_strbuf_destroy(recv_buf);
146 return status;
147 }
148 }
150 result = sdb_strbuf_string(recv_buf);
151 if (result && *result)
152 sdb_log(SDB_LOG_ERR, "%s", result);
153 else if (rcode == UINT32_MAX) {
154 char errbuf[1024];
155 sdb_log(SDB_LOG_ERR, "%s",
156 sdb_strerror(errno, errbuf, sizeof(errbuf)));
157 }
159 sdb_strbuf_destroy(recv_buf);
160 return status;
161 } /* sdb_command_print_reply */
163 char *
164 sdb_command_exec(sdb_input_t *input)
165 {
166 const char *query;
167 uint32_t query_len;
169 char *data = NULL;
171 query = sdb_strbuf_string(input->input);
172 query_len = (uint32_t)input->query_len;
174 assert(input->query_len <= input->tokenizer_pos);
176 /* removing leading and trailing newlines */
177 while (query_len && (*query == '\n')) {
178 ++query;
179 --query_len;
180 }
181 while (query_len && (query[query_len - 1]) == '\n')
182 --query_len;
184 if (query_len) {
185 data = strndup(query, query_len);
186 /* ignore errors; we'll only hide the command from the caller */
187 }
189 if (sdb_client_eof(input->client)) {
190 if (sdb_input_reconnect()) {
191 clear_query(input);
192 return data;
193 }
194 }
195 else if (! query_len)
196 return NULL;
198 sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
200 /* The server may send back log messages but will eventually reply to the
201 * query, which is either DATA or ERROR. */
202 while (42) {
203 int status = sdb_command_print_reply(input->client);
204 if (status < 0) {
205 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
206 break;
207 }
209 if ((status == SDB_CONNECTION_OK)
210 || (status == SDB_CONNECTION_DATA)
211 || (status == SDB_CONNECTION_ERROR))
212 break;
213 }
214 clear_query(input);
215 return data;
216 } /* sdb_command_exec */
218 void
219 sdb_command_print_server_version(sdb_input_t *input)
220 {
221 sdb_strbuf_t *buf = sdb_strbuf_create(32);
222 uint32_t code = 0, version = 0;
223 const char *extra;
225 if ((sdb_client_rpc(input->client, SDB_CONNECTION_SERVER_VERSION,
226 0, NULL, &code, buf) < 0) || (code != SDB_CONNECTION_OK))
227 return;
228 if (sdb_strbuf_len(buf) < sizeof(version))
229 return;
231 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &version);
232 extra = sdb_strbuf_string(buf) + sizeof(version);
233 sdb_log(SDB_LOG_INFO, "SysDB server %d.%d.%d%s",
234 SDB_VERSION_DECODE((int)version), extra);
235 sdb_strbuf_destroy(buf);
236 } /* sdb_command_print_server_version */
238 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */