Code

frontend: Implemented the CONNECTION_QUERY command.
[sysdb.git] / src / frontend / connection.c
1 /*
2  * SysDB - src/frontend/connection.c
3  * Copyright (C) 2013 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 #include "sysdb.h"
29 #include "core/object.h"
30 #include "frontend/connection-private.h"
31 #include "utils/error.h"
32 #include "utils/strbuf.h"
33 #include "utils/proto.h"
35 #include <assert.h>
36 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <fcntl.h>
41 #include <string.h>
43 /*
44  * private data types
45  */
47 /* name of connection objects */
48 #define CONN_FD_PREFIX "conn#"
49 #define CONN_FD_PLACEHOLDER "XXXXXXX"
51 static int
52 connection_init(sdb_object_t *obj, va_list ap)
53 {
54         sdb_conn_t *conn;
55         int sock_fd;
56         int sock_fl;
58         assert(obj);
59         conn = CONN(obj);
61         sock_fd = va_arg(ap, int);
63         conn->buf = sdb_strbuf_create(/* size = */ 128);
64         if (! conn->buf) {
65                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
66                                 "for a new connection");
67                 return -1;
68         }
69         conn->errbuf = sdb_strbuf_create(0);
70         if (! conn->errbuf) {
71                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
72                                 "for a new connection");
73                 return -1;
74         }
76         conn->client_addr_len = sizeof(conn->client_addr);
77         conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
78                         &conn->client_addr_len);
80         if (conn->fd < 0) {
81                 char buf[1024];
82                 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
83                                 "connection: %s", sdb_strerror(errno,
84                                         buf, sizeof(buf)));
85                 return -1;
86         }
88         if (conn->client_addr.ss_family != AF_UNIX) {
89                 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
90                                 "unexpected family type %d", conn->client_addr.ss_family);
91                 return -1;
92         }
94         sock_fl = fcntl(conn->fd, F_GETFL);
95         if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
96                 char buf[1024];
97                 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
98                                 "to non-blocking mode: %s", conn->fd,
99                                 sdb_strerror(errno, buf, sizeof(buf)));
100                 return -1;
101         }
103         sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
104                         conn->fd);
106         conn->cmd = CONNECTION_IDLE;
107         conn->cmd_len = 0;
109         /* update the object name */
110         snprintf(obj->name + strlen(CONN_FD_PREFIX),
111                         strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
112         return 0;
113 } /* connection_init */
115 static void
116 connection_destroy(sdb_object_t *obj)
118         sdb_conn_t *conn;
119         size_t len;
121         assert(obj);
122         conn = CONN(obj);
124         if (conn->buf) {
125                 len = sdb_strbuf_len(conn->buf);
126                 if (len)
127                         sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
128                                         "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
129         }
131         sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i",
132                         conn->fd);
133         close(conn->fd);
134         conn->fd = -1;
136         sdb_strbuf_destroy(conn->buf);
137         conn->buf = NULL;
138         sdb_strbuf_destroy(conn->errbuf);
139         conn->errbuf = NULL;
140 } /* connection_destroy */
142 static sdb_type_t connection_type = {
143         /* size = */ sizeof(sdb_conn_t),
144         /* init = */ connection_init,
145         /* destroy = */ connection_destroy,
146 };
148 /*
149  * connection handler functions
150  */
152 static uint32_t
153 connection_get_int32(sdb_conn_t *conn, size_t offset)
155         const char *data;
156         uint32_t n;
158         assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
160         data = sdb_strbuf_string(conn->buf);
161         memcpy(&n, data + offset, sizeof(n));
162         n = ntohl(n);
163         return n;
164 } /* connection_get_int32 */
166 static int
167 command_handle(sdb_conn_t *conn)
169         int status = -1;
171         assert(conn && (conn->cmd != CONNECTION_IDLE));
173         sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
174                         conn->cmd, conn->cmd_len);
176         /* reset */
177         sdb_strbuf_sprintf(conn->errbuf, "");
179         switch (conn->cmd) {
180                 case CONNECTION_PING:
181                         status = sdb_connection_ping(conn);
182                         break;
183                 case CONNECTION_STARTUP:
184                         status = sdb_fe_session_start(conn);
185                         break;
187                 case CONNECTION_QUERY:
188                 {
189                         sdb_llist_t *parsetree;
190                         sdb_conn_node_t *node = NULL;
192                         parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
193                                         (int)conn->cmd_len);
194                         if (! parsetree) {
195                                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
196                                                 sdb_strbuf_string(conn->buf));
197                                 status = -1;
198                                 break;
199                         }
201                         switch (sdb_llist_len(parsetree)) {
202                                 case 0:
203                                         /* skipping empty command */
204                                         break;
205                                 case 1:
206                                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
207                                         break;
209                                 default:
210                                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s "
211                                                         "in multi-statement query '%s'",
212                                                         sdb_llist_len(parsetree) - 1,
213                                                         sdb_llist_len(parsetree) == 2 ? "" : "s",
214                                                         sdb_strbuf_string(conn->buf));
215                                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
216                         }
218                         if (node)
219                                 status = sdb_fe_exec(conn, node);
220                 }
222                 case CONNECTION_LIST:
223                         status = sdb_fe_list(conn);
224                         break;
226                 default:
227                 {
228                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
229                                         conn->cmd);
230                         sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
231                         status = -1;
232                         break;
233                 }
234         }
236         if (status)
237                 sdb_connection_send(conn, CONNECTION_ERROR,
238                                 (uint32_t)sdb_strbuf_len(conn->errbuf),
239                                 sdb_strbuf_string(conn->errbuf));
241         /* remove the command from the buffer */
242         if (conn->cmd_len)
243                 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
244         conn->cmd = CONNECTION_IDLE;
245         conn->cmd_len = 0;
246         return status;
247 } /* command_handle */
249 /* initialize the connection state information */
250 static int
251 command_init(sdb_conn_t *conn)
253         size_t len;
255         assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
257         conn->cmd = connection_get_int32(conn, 0);
258         conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
260         len = 2 * sizeof(uint32_t);
261         if (conn->cmd == CONNECTION_IDLE)
262                 len += conn->cmd_len;
263         sdb_strbuf_skip(conn->buf, 0, len);
264         return 0;
265 } /* command_init */
267 /* returns negative value on error, 0 on EOF, number of octets else */
268 static ssize_t
269 connection_read(sdb_conn_t *conn)
271         ssize_t n = 0;
273         while (42) {
274                 ssize_t status;
276                 errno = 0;
277                 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
278                 if (status < 0) {
279                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
280                                 break;
281                         return (int)status;
282                 }
283                 else if (! status) /* EOF */
284                         break;
286                 n += status;
287         }
289         return n;
290 } /* connection_read */
292 /*
293  * public API
294  */
296 sdb_conn_t *
297 sdb_connection_accept(int fd)
299         if (fd < 0)
300                 return NULL;
302         /* the placeholder will be replaced with the accepted file
303          * descriptor when initializing the object */
304         return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
305                                 connection_type, fd));
306 } /* sdb_connection_create */
308 void
309 sdb_connection_close(sdb_conn_t *conn)
311         sdb_object_deref(SDB_OBJ(conn));
312 } /* sdb_connection_close */
314 ssize_t
315 sdb_connection_read(sdb_conn_t *conn)
317         ssize_t n = 0;
319         while (42) {
320                 ssize_t status = connection_read(conn);
322                 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
323                                 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
324                         command_init(conn);
325                 if ((conn->cmd != CONNECTION_IDLE)
326                                 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
327                         command_handle(conn);
329                 if (status <= 0)
330                         break;
332                 n += status;
333         }
334         return n;
335 } /* sdb_connection_read */
337 ssize_t
338 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
339                 uint32_t msg_len, const char *msg)
341         ssize_t status;
343         if ((! conn) || (conn->fd < 0))
344                 return -1;
346         status = sdb_proto_send_msg(conn->fd, code, msg_len, msg);
347         if (status < 0) {
348                 char errbuf[1024];
350                 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
351                                 "(code: %u, len: %u) to client: %s", code, msg_len,
352                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
353         }
354         return status;
355 } /* sdb_connection_send */
357 int
358 sdb_connection_ping(sdb_conn_t *conn)
360         if ((! conn) || (conn->cmd != CONNECTION_PING))
361                 return -1;
363         /* we're alive */
364         sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
365         return 0;
366 } /* sdb_connection_ping */
368 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */