Code

Merged 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"
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                 sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
56                                 "or missing priority");
57                 prio = (uint32_t)SDB_LOG_ERR;
58         }
59         sdb_strbuf_skip(buf, 0, sizeof(prio));
61         sdb_log((int)prio, "%s", sdb_strbuf_string(buf));
62 } /* log_printer */
64 static void
65 data_printer(sdb_strbuf_t *buf)
66 {
67         size_t len = sdb_strbuf_len(buf);
69         if ((! len) || (len == sizeof(uint32_t))) {
70                 /* empty command or empty reply */
71                 return;
72         }
73         else if (len < sizeof(uint32_t)) {
74                 sdb_log(SDB_LOG_ERR, "Received a DATA message with invalid "
75                                 "or missing data-type");
76                 return;
77         }
79         /* At the moment, we don't care about the result type. We simply print the
80          * result without further parsing it. */
81         sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
82         printf("%s\n", sdb_strbuf_string(buf));
83 } /* data_printer */
85 static struct {
86         int status;
87         void (*printer)(sdb_strbuf_t *);
88 } response_printers[] = {
89         { SDB_CONNECTION_LOG,  log_printer },
90         { SDB_CONNECTION_DATA, data_printer },
91 };
93 static void
94 clear_query(sdb_input_t *input)
95 {
96         sdb_strbuf_skip(input->input, 0, input->query_len);
97         input->tokenizer_pos -= input->query_len;
98         input->query_len = 0;
99         input->have_input = 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 = -1;
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                 status = (int)rcode;
131         for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
132                 if (status == response_printers[i].status) {
133                         response_printers[i].printer(recv_buf);
134                         sdb_strbuf_destroy(recv_buf);
135                         return status;
136                 }
137         }
139         result = sdb_strbuf_string(recv_buf);
140         if (result && *result)
141                 sdb_log(SDB_LOG_ERR, "%s", result);
142         else if (rcode == UINT32_MAX) {
143                 char errbuf[1024];
144                 sdb_log(SDB_LOG_ERR, "%s",
145                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
146         }
148         sdb_strbuf_destroy(recv_buf);
149         return status;
150 } /* sdb_command_print_reply */
152 char *
153 sdb_command_exec(sdb_input_t *input)
155         const char *query;
156         uint32_t query_len;
158         char *data = NULL;
160         query = sdb_strbuf_string(input->input);
161         query_len = (uint32_t)input->query_len;
163         assert(input->query_len <= input->tokenizer_pos);
165         /* removing leading and trailing newlines */
166         while (query_len && (*query == '\n')) {
167                 ++query;
168                 --query_len;
169         }
170         while (query_len && (query[query_len - 1]) == '\n')
171                 --query_len;
173         if (query_len) {
174                 data = strndup(query, query_len);
175                 /* ignore errors; we'll only hide the command from the caller */
176         }
178         if (sdb_client_eof(input->client)) {
179                 if (sdb_input_reconnect()) {
180                         clear_query(input);
181                         return data;
182                 }
183         }
184         else if (! query_len)
185                 return NULL;
187         sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
189         /* The server may send back log messages but will eventually reply to the
190          * query, which is either DATA or ERROR. */
191         while (42) {
192                 int status = sdb_command_print_reply(input->client);
193                 if (status < 0) {
194                         sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
195                         break;
196                 }
198                 if ((status == SDB_CONNECTION_DATA)
199                                 || (status == SDB_CONNECTION_ERROR))
200                         break;
201         }
202         clear_query(input);
203         return data;
204 } /* sdb_command_exec */
206 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */