6ae4c89a981eff70cdf9fb8413f8211f6e90e9dc
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 input->have_input = 0;
101 } /* clear_query */
103 /*
104 * public API
105 */
107 int
108 sdb_command_print_reply(sdb_client_t *client)
109 {
110 sdb_strbuf_t *recv_buf;
111 const char *result;
112 uint32_t rcode = 0;
114 int status = 0;
115 size_t i;
117 recv_buf = sdb_strbuf_create(1024);
118 if (! recv_buf)
119 return -1;
121 if (sdb_client_recv(client, &rcode, recv_buf) < 0)
122 rcode = UINT32_MAX;
124 if (sdb_client_eof(client)) {
125 sdb_strbuf_destroy(recv_buf);
126 return -1;
127 }
129 if (rcode == UINT32_MAX) {
130 printf("ERROR: ");
131 status = -1;
132 }
133 else
134 status = (int)rcode;
136 for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
137 if (status == response_printers[i].status) {
138 response_printers[i].printer(recv_buf);
139 sdb_strbuf_destroy(recv_buf);
140 return status;
141 }
142 }
144 result = sdb_strbuf_string(recv_buf);
145 if (result && *result)
146 printf("%s\n", result);
147 else if (rcode == UINT32_MAX) {
148 char errbuf[1024];
149 printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
150 }
152 sdb_strbuf_destroy(recv_buf);
153 return status;
154 } /* sdb_command_print_reply */
156 char *
157 sdb_command_exec(sdb_input_t *input)
158 {
159 const char *query;
160 uint32_t query_len;
162 char *data = NULL;
164 query = sdb_strbuf_string(input->input);
165 query_len = (uint32_t)input->query_len;
167 assert(input->query_len <= input->tokenizer_pos);
169 /* removing leading and trailing newlines */
170 while (query_len && (*query == '\n')) {
171 ++query;
172 --query_len;
173 }
174 while (query_len && (query[query_len - 1]) == '\n')
175 --query_len;
177 if (query_len) {
178 data = strndup(query, query_len);
179 /* ignore errors; we'll only hide the command from the caller */
180 }
182 if (sdb_client_eof(input->client)) {
183 if (sdb_input_reconnect()) {
184 clear_query(input);
185 return data;
186 }
187 }
188 else if (! query_len)
189 return NULL;
191 sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
193 /* The server may send back log messages but will eventually reply to the
194 * query, which is either DATA or ERROR. */
195 while (42) {
196 int status = sdb_command_print_reply(input->client);
197 if (status < 0) {
198 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
199 break;
200 }
202 if ((status == SDB_CONNECTION_DATA)
203 || (status == SDB_CONNECTION_ERROR))
204 break;
205 }
206 clear_query(input);
207 return data;
208 } /* sdb_command_exec */
210 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */