Code

frontend: Free parser-allocated memory after handling a 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);
221                         sdb_llist_destroy(parsetree);
222                 }
224                 case CONNECTION_LIST:
225                         status = sdb_fe_list(conn);
226                         break;
228                 default:
229                 {
230                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
231                                         conn->cmd);
232                         sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
233                         status = -1;
234                         break;
235                 }
236         }
238         if (status)
239                 sdb_connection_send(conn, CONNECTION_ERROR,
240                                 (uint32_t)sdb_strbuf_len(conn->errbuf),
241                                 sdb_strbuf_string(conn->errbuf));
243         /* remove the command from the buffer */
244         if (conn->cmd_len)
245                 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
246         conn->cmd = CONNECTION_IDLE;
247         conn->cmd_len = 0;
248         return status;
249 } /* command_handle */
251 /* initialize the connection state information */
252 static int
253 command_init(sdb_conn_t *conn)
255         size_t len;
257         assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
259         conn->cmd = connection_get_int32(conn, 0);
260         conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
262         len = 2 * sizeof(uint32_t);
263         if (conn->cmd == CONNECTION_IDLE)
264                 len += conn->cmd_len;
265         sdb_strbuf_skip(conn->buf, 0, len);
266         return 0;
267 } /* command_init */
269 /* returns negative value on error, 0 on EOF, number of octets else */
270 static ssize_t
271 connection_read(sdb_conn_t *conn)
273         ssize_t n = 0;
275         while (42) {
276                 ssize_t status;
278                 errno = 0;
279                 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
280                 if (status < 0) {
281                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
282                                 break;
283                         return (int)status;
284                 }
285                 else if (! status) /* EOF */
286                         break;
288                 n += status;
289         }
291         return n;
292 } /* connection_read */
294 /*
295  * public API
296  */
298 sdb_conn_t *
299 sdb_connection_accept(int fd)
301         if (fd < 0)
302                 return NULL;
304         /* the placeholder will be replaced with the accepted file
305          * descriptor when initializing the object */
306         return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
307                                 connection_type, fd));
308 } /* sdb_connection_create */
310 void
311 sdb_connection_close(sdb_conn_t *conn)
313         sdb_object_deref(SDB_OBJ(conn));
314 } /* sdb_connection_close */
316 ssize_t
317 sdb_connection_read(sdb_conn_t *conn)
319         ssize_t n = 0;
321         while (42) {
322                 ssize_t status = connection_read(conn);
324                 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
325                                 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
326                         command_init(conn);
327                 if ((conn->cmd != CONNECTION_IDLE)
328                                 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
329                         command_handle(conn);
331                 if (status <= 0)
332                         break;
334                 n += status;
335         }
336         return n;
337 } /* sdb_connection_read */
339 ssize_t
340 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
341                 uint32_t msg_len, const char *msg)
343         ssize_t status;
345         if ((! conn) || (conn->fd < 0))
346                 return -1;
348         status = sdb_proto_send_msg(conn->fd, code, msg_len, msg);
349         if (status < 0) {
350                 char errbuf[1024];
352                 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
353                                 "(code: %u, len: %u) to client: %s", code, msg_len,
354                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
355         }
356         return status;
357 } /* sdb_connection_send */
359 int
360 sdb_connection_ping(sdb_conn_t *conn)
362         if ((! conn) || (conn->cmd != CONNECTION_PING))
363                 return -1;
365         /* we're alive */
366         sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
367         return 0;
368 } /* sdb_connection_ping */
370 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */