Code

frontend/proto: Include the log priority in LOG messages.
[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         if (sdb_strbuf_len(buf) <= sizeof(uint32_t)) {
69                 printf("ERROR: Received a DATA message with invalid "
70                                 "or missing data-type\n");
71                 return;
72         }
74         /* At the moment, we don't care about the result type. We simply print the
75          * result without further parsing it. */
76         sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
77         printf("%s\n", sdb_strbuf_string(buf));
78 } /* data_printer */
80 static struct {
81         int status;
82         void (*printer)(sdb_strbuf_t *);
83 } response_printers[] = {
84         { CONNECTION_LOG,  log_printer },
85         { CONNECTION_DATA, data_printer },
86 };
88 /*
89  * public API
90  */
92 int
93 sdb_command_print_reply(sdb_client_t *client)
94 {
95         sdb_strbuf_t *recv_buf;
96         const char *result;
97         uint32_t rcode = 0;
99         int status = 0;
100         size_t i;
102         recv_buf = sdb_strbuf_create(1024);
103         if (! recv_buf)
104                 return -1;
106         if (sdb_client_recv(client, &rcode, recv_buf) < 0)
107                 rcode = UINT32_MAX;
109         if (sdb_client_eof(client)) {
110                 sdb_strbuf_destroy(recv_buf);
111                 return -1;
112         }
114         if (rcode == UINT32_MAX) {
115                 printf("ERROR: ");
116                 status = -1;
117         }
118         else
119                 status = (int)rcode;
121         for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
122                 if (status == response_printers[i].status) {
123                         response_printers[i].printer(recv_buf);
124                         sdb_strbuf_destroy(recv_buf);
125                         return status;
126                 }
127         }
129         result = sdb_strbuf_string(recv_buf);
130         if (result && *result)
131                 printf("%s\n", result);
132         else if (rcode == UINT32_MAX) {
133                 char errbuf[1024];
134                 printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
135         }
137         sdb_strbuf_destroy(recv_buf);
138         return status;
139 } /* sdb_command_print_reply */
141 char *
142 sdb_command_exec(sdb_input_t *input)
144         const char *query;
145         uint32_t query_len;
147         char *data = NULL;
149         query = sdb_strbuf_string(input->input);
150         query_len = (uint32_t)input->query_len;
152         assert(input->query_len <= input->tokenizer_pos);
154         /* removing leading and trailing newlines */
155         while (query_len && (*query == '\n')) {
156                 ++query;
157                 --query_len;
158         }
159         while (query_len && (query[query_len - 1]) == '\n')
160                 --query_len;
162         if (query_len) {
163                 data = strndup(query, query_len);
164                 /* ignore errors; we'll only hide the command from the caller */
166                 sdb_client_send(input->client, CONNECTION_QUERY, query_len, query);
168                 /* The server will send back *something*, either error/log messages
169                  * and/or the reply to the query. Here, we don't care about what it
170                  * sends back. We'll wait for the first reply and then return to the
171                  * main loop which will handle any subsequent replies, including
172                  * eventually the reply to the query (if it's not the first reply). */
173                 if (sdb_command_print_reply(input->client) < 0) {
174                         if (data)
175                                 free(data);
176                         return NULL;
177                 }
178         }
180         sdb_strbuf_skip(input->input, 0, input->query_len);
181         input->tokenizer_pos -= input->query_len;
182         input->query_len = 0;
183         return data;
184 } /* sdb_command_exec */
186 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */