2014-03-30 23:54:35 +02:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2014-2021 Joris Vink <joris@coders.se>
|
2014-03-30 23:54:35 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <libpq-fe.h>
|
2014-07-18 15:59:07 +02:00
|
|
|
#include <pg_config.h>
|
2014-03-30 23:54:35 +02:00
|
|
|
|
|
|
|
#include "kore.h"
|
2017-03-24 12:53:07 +01:00
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
2014-03-30 23:54:35 +02:00
|
|
|
#include "http.h"
|
2017-03-24 12:53:07 +01:00
|
|
|
#endif
|
|
|
|
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "pgsql.h"
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2019-09-25 12:25:49 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include "seccomp.h"
|
|
|
|
|
|
|
|
static struct sock_filter filter_pgsql[] = {
|
seccomp improvements.
More BPF helper macros, more helper for granular syscall checking.
Use these throughout kore where it makes sense.
The new helpers:
- KORE_SYSCALL_DENY_ARG(name, arg, value, errno):
Deny the system call with errno if the argument matches value.
- KORE_SYSCALL_DENY_MASK(name, arg, mask, errno):
Deny the system call with errno if the mask argument does not match
the exact mask given.
- KORE_SYSCALL_DENY_WITH_FLAG(name, arg, flag, errno):
Deny the system call with errno if the argument contains the
given flag.
The reverse also exists:
- KORE_SYSCALL_ALLOW_ARG()
- KORE_SYSCALL_ALLOW_MASK()
- KORE_SYSCALL_ALLOW_WITH_FLAG()
2019-09-26 13:51:53 +02:00
|
|
|
/* Allow us to create sockets and call connect. */
|
2019-09-25 12:25:49 +00:00
|
|
|
KORE_SYSCALL_ALLOW(connect),
|
seccomp improvements.
More BPF helper macros, more helper for granular syscall checking.
Use these throughout kore where it makes sense.
The new helpers:
- KORE_SYSCALL_DENY_ARG(name, arg, value, errno):
Deny the system call with errno if the argument matches value.
- KORE_SYSCALL_DENY_MASK(name, arg, mask, errno):
Deny the system call with errno if the mask argument does not match
the exact mask given.
- KORE_SYSCALL_DENY_WITH_FLAG(name, arg, flag, errno):
Deny the system call with errno if the argument contains the
given flag.
The reverse also exists:
- KORE_SYSCALL_ALLOW_ARG()
- KORE_SYSCALL_ALLOW_MASK()
- KORE_SYSCALL_ALLOW_WITH_FLAG()
2019-09-26 13:51:53 +02:00
|
|
|
KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_INET),
|
|
|
|
KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_INET6),
|
|
|
|
KORE_SYSCALL_ALLOW_ARG(socket, 0, AF_UNIX),
|
|
|
|
|
|
|
|
/* Requires these calls. */
|
2019-09-25 12:25:49 +00:00
|
|
|
KORE_SYSCALL_ALLOW(getsockopt),
|
|
|
|
KORE_SYSCALL_ALLOW(getsockname),
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
struct pgsql_wait {
|
2014-08-14 14:34:23 +02:00
|
|
|
struct kore_pgsql *pgsql;
|
2017-03-24 12:53:07 +01:00
|
|
|
TAILQ_ENTRY(pgsql_wait) list;
|
2014-03-30 23:54:35 +02:00
|
|
|
};
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
struct pgsql_job {
|
|
|
|
struct kore_pgsql *pgsql;
|
|
|
|
TAILQ_ENTRY(pgsql_job) list;
|
2014-08-21 16:36:12 +02:00
|
|
|
};
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
#define PGSQL_CONN_MAX 2
|
|
|
|
#define PGSQL_CONN_FREE 0x01
|
2016-01-04 11:12:43 +01:00
|
|
|
#define PGSQL_LIST_INSERTED 0x0100
|
2018-02-13 13:21:27 +01:00
|
|
|
#define PGSQL_QUEUE_LIMIT 1000
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-09-28 21:39:16 +02:00
|
|
|
static void pgsql_queue_wakeup(void);
|
2017-02-06 20:01:16 +01:00
|
|
|
static void pgsql_cancel(struct kore_pgsql *);
|
2016-01-04 11:12:43 +01:00
|
|
|
static void pgsql_set_error(struct kore_pgsql *, const char *);
|
2017-03-24 12:53:07 +01:00
|
|
|
static void pgsql_queue_add(struct kore_pgsql *);
|
|
|
|
static void pgsql_queue_remove(struct kore_pgsql *);
|
2014-08-14 14:46:21 +02:00
|
|
|
static void pgsql_conn_release(struct kore_pgsql *);
|
2014-03-31 01:04:22 +02:00
|
|
|
static void pgsql_conn_cleanup(struct pgsql_conn *);
|
2016-01-04 11:12:43 +01:00
|
|
|
static void pgsql_read_result(struct kore_pgsql *);
|
|
|
|
static void pgsql_schedule(struct kore_pgsql *);
|
|
|
|
|
|
|
|
static struct pgsql_conn *pgsql_conn_create(struct kore_pgsql *,
|
|
|
|
struct pgsql_db *);
|
|
|
|
static struct pgsql_conn *pgsql_conn_next(struct kore_pgsql *,
|
2017-03-24 12:53:07 +01:00
|
|
|
struct pgsql_db *);
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-08-21 16:36:12 +02:00
|
|
|
static struct kore_pool pgsql_job_pool;
|
|
|
|
static struct kore_pool pgsql_wait_pool;
|
2014-03-30 23:54:35 +02:00
|
|
|
static TAILQ_HEAD(, pgsql_conn) pgsql_conn_free;
|
2014-08-21 16:36:12 +02:00
|
|
|
static TAILQ_HEAD(, pgsql_wait) pgsql_wait_queue;
|
2016-01-04 11:12:43 +01:00
|
|
|
static LIST_HEAD(, pgsql_db) pgsql_db_conn_strings;
|
2017-07-11 15:11:13 +02:00
|
|
|
|
2018-02-13 13:21:27 +01:00
|
|
|
u_int32_t pgsql_queue_count = 0;
|
|
|
|
u_int16_t pgsql_conn_max = PGSQL_CONN_MAX;
|
|
|
|
u_int32_t pgsql_queue_limit = PGSQL_QUEUE_LIMIT;
|
2014-03-30 23:54:35 +02:00
|
|
|
|
|
|
|
void
|
2017-03-24 12:53:07 +01:00
|
|
|
kore_pgsql_sys_init(void)
|
2014-03-30 23:54:35 +02:00
|
|
|
{
|
|
|
|
TAILQ_INIT(&pgsql_conn_free);
|
2014-08-21 16:36:12 +02:00
|
|
|
TAILQ_INIT(&pgsql_wait_queue);
|
2016-01-04 11:12:43 +01:00
|
|
|
LIST_INIT(&pgsql_db_conn_strings);
|
2014-08-21 16:36:12 +02:00
|
|
|
|
|
|
|
kore_pool_init(&pgsql_job_pool, "pgsql_job_pool",
|
|
|
|
sizeof(struct pgsql_job), 100);
|
|
|
|
kore_pool_init(&pgsql_wait_pool, "pgsql_wait_pool",
|
2018-02-13 13:21:27 +01:00
|
|
|
sizeof(struct pgsql_wait), pgsql_queue_limit);
|
2019-09-25 12:25:49 +00:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
kore_seccomp_filter("pgsql", filter_pgsql,
|
|
|
|
KORE_FILTER_LEN(filter_pgsql));
|
|
|
|
#endif
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-02-06 11:40:33 +01:00
|
|
|
void
|
|
|
|
kore_pgsql_sys_cleanup(void)
|
|
|
|
{
|
|
|
|
struct pgsql_conn *conn, *next;
|
|
|
|
|
|
|
|
kore_pool_cleanup(&pgsql_job_pool);
|
|
|
|
kore_pool_cleanup(&pgsql_wait_pool);
|
|
|
|
|
2017-02-07 22:44:20 +01:00
|
|
|
for (conn = TAILQ_FIRST(&pgsql_conn_free); conn != NULL; conn = next) {
|
|
|
|
next = TAILQ_NEXT(conn, list);
|
2017-02-06 11:40:33 +01:00
|
|
|
pgsql_conn_cleanup(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
void
|
|
|
|
kore_pgsql_init(struct kore_pgsql *pgsql)
|
2014-03-30 23:54:35 +02:00
|
|
|
{
|
2016-01-04 11:12:43 +01:00
|
|
|
memset(pgsql, 0, sizeof(*pgsql));
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_INIT;
|
2017-03-24 12:53:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kore_pgsql_setup(struct kore_pgsql *pgsql, const char *dbname, int flags)
|
|
|
|
{
|
|
|
|
struct pgsql_db *db;
|
2016-01-04 11:12:43 +01:00
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
if ((flags & KORE_PGSQL_ASYNC) && (flags & KORE_PGSQL_SYNC)) {
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_set_error(pgsql, "invalid query init parameters");
|
2014-09-28 21:39:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 13:00:05 +01:00
|
|
|
if (flags & KORE_PGSQL_ASYNC) {
|
|
|
|
if (pgsql->req == NULL && pgsql->cb == NULL) {
|
|
|
|
pgsql_set_error(pgsql, "nothing was bound");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2017-03-24 12:53:07 +01:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
db = NULL;
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql->flags |= flags;
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
LIST_FOREACH(db, &pgsql_db_conn_strings, rlist) {
|
|
|
|
if (!strcmp(db->name, dbname))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db == NULL) {
|
|
|
|
pgsql_set_error(pgsql, "no database found");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
if ((pgsql->conn = pgsql_conn_next(pgsql, db)) == NULL)
|
2016-01-04 11:12:43 +01:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (pgsql->flags & KORE_PGSQL_ASYNC) {
|
|
|
|
pgsql->conn->job = kore_pool_get(&pgsql_job_pool);
|
|
|
|
pgsql->conn->job->pgsql = pgsql;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
void
|
|
|
|
kore_pgsql_bind_request(struct kore_pgsql *pgsql, struct http_request *req)
|
|
|
|
{
|
|
|
|
if (pgsql->req != NULL || pgsql->cb != NULL)
|
|
|
|
fatal("kore_pgsql_bind_request: already bound");
|
|
|
|
|
|
|
|
pgsql->req = req;
|
|
|
|
pgsql->flags |= PGSQL_LIST_INSERTED;
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&(req->pgsqls), pgsql, rlist);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_pgsql_bind_callback(struct kore_pgsql *pgsql,
|
|
|
|
void (*cb)(struct kore_pgsql *, void *), void *arg)
|
|
|
|
{
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
fatal("kore_pgsql_bind_callback: already bound");
|
|
|
|
|
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
fatal("kore_pgsql_bind_callback: already bound");
|
|
|
|
|
|
|
|
pgsql->cb = cb;
|
|
|
|
pgsql->arg = arg;
|
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
int
|
2019-06-01 23:14:50 +02:00
|
|
|
kore_pgsql_query(struct kore_pgsql *pgsql, const void *query)
|
2016-01-04 11:12:43 +01:00
|
|
|
{
|
|
|
|
if (pgsql->conn == NULL) {
|
|
|
|
pgsql_set_error(pgsql, "no connection was set before query");
|
2014-09-28 21:39:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if (pgsql->flags & KORE_PGSQL_SYNC) {
|
|
|
|
pgsql->result = PQexec(pgsql->conn->db, query);
|
|
|
|
if ((PQresultStatus(pgsql->result) != PGRES_TUPLES_OK) &&
|
|
|
|
(PQresultStatus(pgsql->result) != PGRES_COMMAND_OK)) {
|
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(pgsql->conn->db));
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_DONE;
|
|
|
|
} else {
|
|
|
|
if (!PQsendQuery(pgsql->conn->db, query)) {
|
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(pgsql->conn->db));
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
pgsql_schedule(pgsql);
|
|
|
|
}
|
|
|
|
|
2014-09-28 21:39:16 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2014-08-21 16:36:12 +02:00
|
|
|
|
2014-09-28 21:39:16 +02:00
|
|
|
int
|
2016-06-07 13:12:31 +02:00
|
|
|
kore_pgsql_v_query_params(struct kore_pgsql *pgsql,
|
2019-06-01 23:14:50 +02:00
|
|
|
const void *query, int binary, int count, va_list args)
|
2014-09-28 21:39:16 +02:00
|
|
|
{
|
2019-04-25 23:13:13 +02:00
|
|
|
int i;
|
|
|
|
const char **values;
|
2016-01-04 11:12:43 +01:00
|
|
|
int *lengths, *formats, ret;
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if (pgsql->conn == NULL) {
|
|
|
|
pgsql_set_error(pgsql, "no connection was set before query");
|
2014-09-28 21:39:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-10-12 01:17:35 +02:00
|
|
|
if (count > 0) {
|
|
|
|
lengths = kore_calloc(count, sizeof(int));
|
|
|
|
formats = kore_calloc(count, sizeof(int));
|
|
|
|
values = kore_calloc(count, sizeof(char *));
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
values[i] = va_arg(args, void *);
|
2016-12-27 07:42:42 +01:00
|
|
|
lengths[i] = va_arg(args, int);
|
2014-10-12 01:17:35 +02:00
|
|
|
formats[i] = va_arg(args, int);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lengths = NULL;
|
|
|
|
formats = NULL;
|
|
|
|
values = NULL;
|
2014-09-28 21:39:16 +02:00
|
|
|
}
|
2014-08-14 22:05:34 +02:00
|
|
|
|
2019-04-25 23:13:13 +02:00
|
|
|
ret = kore_pgsql_query_param_fields(pgsql, query, binary, count,
|
|
|
|
values, lengths, formats);
|
|
|
|
|
|
|
|
kore_free(values);
|
|
|
|
kore_free(lengths);
|
|
|
|
kore_free(formats);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-06-01 23:14:50 +02:00
|
|
|
kore_pgsql_query_param_fields(struct kore_pgsql *pgsql, const void *query,
|
2019-04-25 23:13:13 +02:00
|
|
|
int binary, int count, const char **values, int *lengths, int *formats)
|
|
|
|
{
|
|
|
|
if (pgsql->conn == NULL) {
|
|
|
|
pgsql_set_error(pgsql, "no connection was set before query");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2016-01-04 11:12:43 +01:00
|
|
|
|
|
|
|
if (pgsql->flags & KORE_PGSQL_SYNC) {
|
|
|
|
pgsql->result = PQexecParams(pgsql->conn->db, query, count,
|
|
|
|
NULL, (const char * const *)values, lengths, formats,
|
2019-04-25 23:13:13 +02:00
|
|
|
binary);
|
2016-01-04 11:12:43 +01:00
|
|
|
|
|
|
|
if ((PQresultStatus(pgsql->result) != PGRES_TUPLES_OK) &&
|
|
|
|
(PQresultStatus(pgsql->result) != PGRES_COMMAND_OK)) {
|
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(pgsql->conn->db));
|
2019-04-25 23:13:13 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_DONE;
|
|
|
|
} else {
|
|
|
|
if (!PQsendQueryParams(pgsql->conn->db, query, count, NULL,
|
2019-04-25 23:13:13 +02:00
|
|
|
(const char * const *)values, lengths, formats, binary)) {
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(pgsql->conn->db));
|
2019-04-25 23:13:13 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pgsql_schedule(pgsql);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:13:13 +02:00
|
|
|
return (KORE_RESULT_OK);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 13:12:31 +02:00
|
|
|
int
|
|
|
|
kore_pgsql_query_params(struct kore_pgsql *pgsql,
|
2019-06-01 23:14:50 +02:00
|
|
|
const void *query, int binary, int count, ...)
|
2016-06-07 13:12:31 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
2016-09-05 16:22:46 +02:00
|
|
|
va_start(args, count);
|
2019-04-25 23:13:13 +02:00
|
|
|
ret = kore_pgsql_v_query_params(pgsql, query, binary, count, args);
|
2016-09-05 16:22:46 +02:00
|
|
|
va_end(args);
|
2016-06-07 13:12:31 +02:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
int
|
|
|
|
kore_pgsql_register(const char *dbname, const char *connstring)
|
|
|
|
{
|
|
|
|
struct pgsql_db *pgsqldb;
|
|
|
|
|
|
|
|
LIST_FOREACH(pgsqldb, &pgsql_db_conn_strings, rlist) {
|
|
|
|
if (!strcmp(pgsqldb->name, dbname))
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
pgsqldb = kore_malloc(sizeof(*pgsqldb));
|
|
|
|
pgsqldb->name = kore_strdup(dbname);
|
2017-08-21 14:25:09 +02:00
|
|
|
pgsqldb->conn_count = 0;
|
|
|
|
pgsqldb->conn_max = pgsql_conn_max;
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsqldb->conn_string = kore_strdup(connstring);
|
|
|
|
LIST_INSERT_HEAD(&pgsql_db_conn_strings, pgsqldb, rlist);
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_pgsql_handle(void *c, int err)
|
|
|
|
{
|
2014-08-14 14:34:23 +02:00
|
|
|
struct kore_pgsql *pgsql;
|
2014-03-30 23:54:35 +02:00
|
|
|
struct pgsql_conn *conn = (struct pgsql_conn *)c;
|
2014-03-31 00:57:00 +02:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
pgsql_conn_cleanup(conn);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (!(conn->evt.flags & KORE_EVENT_READ))
|
|
|
|
fatal("%s: read event not set", __func__);
|
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql = conn->job->pgsql;
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2019-09-13 23:20:51 +02:00
|
|
|
pgsql_read_result(pgsql);
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
if (pgsql->state == KORE_PGSQL_STATE_WAIT) {
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_sleep(pgsql->req);
|
|
|
|
#endif
|
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
pgsql->cb(pgsql, pgsql->arg);
|
2014-04-17 10:49:48 +02:00
|
|
|
} else {
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_wakeup(pgsql->req);
|
|
|
|
#endif
|
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
pgsql->cb(pgsql, pgsql->arg);
|
2014-04-17 10:49:48 +02:00
|
|
|
}
|
2014-03-31 00:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-24 12:53:07 +01:00
|
|
|
kore_pgsql_continue(struct kore_pgsql *pgsql)
|
2014-03-31 00:57:00 +02:00
|
|
|
{
|
2014-08-14 14:34:23 +02:00
|
|
|
if (pgsql->error) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(pgsql->error);
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql->error = NULL;
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 16:43:40 +02:00
|
|
|
if (pgsql->result) {
|
2014-08-14 14:34:23 +02:00
|
|
|
PQclear(pgsql->result);
|
2014-08-14 16:43:40 +02:00
|
|
|
pgsql->result = NULL;
|
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
switch (pgsql->state) {
|
2014-03-30 23:54:35 +02:00
|
|
|
case KORE_PGSQL_STATE_INIT:
|
|
|
|
case KORE_PGSQL_STATE_WAIT:
|
|
|
|
break;
|
|
|
|
case KORE_PGSQL_STATE_DONE:
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_wakeup(pgsql->req);
|
|
|
|
#endif
|
2014-08-14 14:46:21 +02:00
|
|
|
pgsql_conn_release(pgsql);
|
2014-03-30 23:54:35 +02:00
|
|
|
break;
|
|
|
|
case KORE_PGSQL_STATE_ERROR:
|
|
|
|
case KORE_PGSQL_STATE_RESULT:
|
2018-07-18 11:38:17 +02:00
|
|
|
case KORE_PGSQL_STATE_NOTIFY:
|
2014-08-14 14:46:21 +02:00
|
|
|
kore_pgsql_handle(pgsql->conn, 0);
|
2014-03-30 23:54:35 +02:00
|
|
|
break;
|
|
|
|
default:
|
2014-08-14 14:34:23 +02:00
|
|
|
fatal("unknown pgsql state %d", pgsql->state);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-14 14:34:23 +02:00
|
|
|
kore_pgsql_cleanup(struct kore_pgsql *pgsql)
|
2014-03-30 23:54:35 +02:00
|
|
|
{
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql_queue_remove(pgsql);
|
|
|
|
|
2014-08-14 14:46:21 +02:00
|
|
|
if (pgsql->result != NULL)
|
2014-08-14 14:34:23 +02:00
|
|
|
PQclear(pgsql->result);
|
2014-03-31 00:57:00 +02:00
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
if (pgsql->error != NULL)
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(pgsql->error);
|
2014-03-31 01:09:08 +02:00
|
|
|
|
2014-08-21 16:36:12 +02:00
|
|
|
if (pgsql->conn != NULL)
|
2014-08-14 14:46:21 +02:00
|
|
|
pgsql_conn_release(pgsql);
|
2014-08-14 14:34:23 +02:00
|
|
|
|
|
|
|
pgsql->result = NULL;
|
|
|
|
pgsql->error = NULL;
|
|
|
|
pgsql->conn = NULL;
|
2014-08-14 22:05:34 +02:00
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if (pgsql->flags & PGSQL_LIST_INSERTED) {
|
|
|
|
LIST_REMOVE(pgsql, rlist);
|
|
|
|
pgsql->flags &= ~PGSQL_LIST_INSERTED;
|
|
|
|
}
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 23:01:47 +02:00
|
|
|
void
|
|
|
|
kore_pgsql_logerror(struct kore_pgsql *pgsql)
|
|
|
|
{
|
|
|
|
kore_log(LOG_NOTICE, "pgsql error: %s",
|
|
|
|
(pgsql->error) ? pgsql->error : "unknown");
|
|
|
|
}
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
int
|
2014-04-01 21:43:32 +02:00
|
|
|
kore_pgsql_ntuples(struct kore_pgsql *pgsql)
|
2014-03-30 23:54:35 +02:00
|
|
|
{
|
2014-04-01 21:43:32 +02:00
|
|
|
return (PQntuples(pgsql->result));
|
|
|
|
}
|
|
|
|
|
2017-02-06 11:40:33 +01:00
|
|
|
int
|
|
|
|
kore_pgsql_nfields(struct kore_pgsql *pgsql)
|
|
|
|
{
|
|
|
|
return (PQnfields(pgsql->result));
|
|
|
|
}
|
|
|
|
|
2014-09-28 23:03:49 +02:00
|
|
|
int
|
|
|
|
kore_pgsql_getlength(struct kore_pgsql *pgsql, int row, int col)
|
|
|
|
{
|
|
|
|
return (PQgetlength(pgsql->result, row, col));
|
|
|
|
}
|
|
|
|
|
2017-02-06 11:40:33 +01:00
|
|
|
char *
|
|
|
|
kore_pgsql_fieldname(struct kore_pgsql *pgsql, int field)
|
|
|
|
{
|
|
|
|
return (PQfname(pgsql->result, field));
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:43:32 +02:00
|
|
|
char *
|
|
|
|
kore_pgsql_getvalue(struct kore_pgsql *pgsql, int row, int col)
|
|
|
|
{
|
|
|
|
return (PQgetvalue(pgsql->result, row, col));
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:13:13 +02:00
|
|
|
int
|
|
|
|
kore_pgsql_column_binary(struct kore_pgsql *pgsql, int col)
|
|
|
|
{
|
|
|
|
return (PQfformat(pgsql->result, col));
|
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
static struct pgsql_conn *
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql_conn_next(struct kore_pgsql *pgsql, struct pgsql_db *db)
|
2014-09-28 21:39:16 +02:00
|
|
|
{
|
2017-07-11 15:11:13 +02:00
|
|
|
PGTransactionStatusType state;
|
|
|
|
struct pgsql_conn *conn;
|
2018-07-28 22:28:19 +02:00
|
|
|
struct kore_pgsql rollback;
|
2014-09-28 21:39:16 +02:00
|
|
|
|
2017-07-11 15:11:13 +02:00
|
|
|
rescan:
|
2016-01-04 11:12:43 +01:00
|
|
|
conn = NULL;
|
2014-09-28 21:39:16 +02:00
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
TAILQ_FOREACH(conn, &pgsql_conn_free, list) {
|
|
|
|
if (!(conn->flags & PGSQL_CONN_FREE))
|
|
|
|
fatal("got a pgsql connection that was not free?");
|
|
|
|
if (!strcmp(conn->name, db->name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:11:13 +02:00
|
|
|
if (conn != NULL) {
|
|
|
|
state = PQtransactionStatus(conn->db);
|
|
|
|
if (state == PQTRANS_INERROR) {
|
|
|
|
conn->flags &= ~PGSQL_CONN_FREE;
|
|
|
|
TAILQ_REMOVE(&pgsql_conn_free, conn, list);
|
|
|
|
|
2018-07-28 22:28:19 +02:00
|
|
|
kore_pgsql_init(&rollback);
|
|
|
|
rollback.conn = conn;
|
|
|
|
rollback.flags = KORE_PGSQL_SYNC;
|
2017-07-11 15:11:13 +02:00
|
|
|
|
2018-07-28 22:28:19 +02:00
|
|
|
if (!kore_pgsql_query(&rollback, "ROLLBACK")) {
|
|
|
|
kore_pgsql_logerror(&rollback);
|
|
|
|
kore_pgsql_cleanup(&rollback);
|
2017-07-11 15:11:13 +02:00
|
|
|
pgsql_conn_cleanup(conn);
|
2018-07-28 22:28:19 +02:00
|
|
|
} else {
|
|
|
|
kore_pgsql_cleanup(&rollback);
|
2017-07-11 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto rescan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if (conn == NULL) {
|
2017-08-21 14:25:09 +02:00
|
|
|
if (db->conn_max != 0 &&
|
|
|
|
db->conn_count >= db->conn_max) {
|
2018-02-13 13:21:27 +01:00
|
|
|
if ((pgsql->flags & KORE_PGSQL_ASYNC) &&
|
|
|
|
pgsql_queue_count < pgsql_queue_limit) {
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql_queue_add(pgsql);
|
2016-01-04 11:12:43 +01:00
|
|
|
} else {
|
|
|
|
pgsql_set_error(pgsql,
|
|
|
|
"no available connection");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2014-09-28 21:39:16 +02:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if ((conn = pgsql_conn_create(pgsql, db)) == NULL)
|
|
|
|
return (NULL);
|
2014-09-28 21:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conn->flags &= ~PGSQL_CONN_FREE;
|
|
|
|
TAILQ_REMOVE(&pgsql_conn_free, conn, list);
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
return (conn);
|
|
|
|
}
|
2014-09-28 21:39:16 +02:00
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
static void
|
|
|
|
pgsql_set_error(struct kore_pgsql *pgsql, const char *msg)
|
|
|
|
{
|
|
|
|
if (pgsql->error != NULL)
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(pgsql->error);
|
2016-01-04 11:12:43 +01:00
|
|
|
|
|
|
|
pgsql->error = kore_strdup(msg);
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_ERROR;
|
2014-09-28 21:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_schedule(struct kore_pgsql *pgsql)
|
2014-09-28 21:39:16 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = PQsocket(pgsql->conn->db);
|
|
|
|
if (fd < 0)
|
|
|
|
fatal("PQsocket returned < 0 fd on open connection");
|
|
|
|
|
|
|
|
kore_platform_schedule_read(fd, pgsql->conn);
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_WAIT;
|
2017-01-29 10:34:53 +01:00
|
|
|
pgsql->flags |= KORE_PGSQL_SCHEDULED;
|
2017-03-24 12:53:07 +01:00
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_sleep(pgsql->req);
|
|
|
|
#endif
|
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
pgsql->cb(pgsql, pgsql->arg);
|
2014-09-28 21:39:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-21 16:36:12 +02:00
|
|
|
static void
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql_queue_add(struct kore_pgsql *pgsql)
|
2014-08-21 16:36:12 +02:00
|
|
|
{
|
|
|
|
struct pgsql_wait *pgw;
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_sleep(pgsql->req);
|
|
|
|
#endif
|
2014-08-21 16:36:12 +02:00
|
|
|
|
|
|
|
pgw = kore_pool_get(&pgsql_wait_pool);
|
2017-03-24 12:53:07 +01:00
|
|
|
pgw->pgsql = pgsql;
|
2018-02-13 13:21:27 +01:00
|
|
|
|
|
|
|
pgsql_queue_count++;
|
2014-08-21 16:36:12 +02:00
|
|
|
TAILQ_INSERT_TAIL(&pgsql_wait_queue, pgw, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-24 12:53:07 +01:00
|
|
|
pgsql_queue_remove(struct kore_pgsql *pgsql)
|
2014-08-21 16:36:12 +02:00
|
|
|
{
|
|
|
|
struct pgsql_wait *pgw, *next;
|
|
|
|
|
2017-02-07 22:44:20 +01:00
|
|
|
for (pgw = TAILQ_FIRST(&pgsql_wait_queue); pgw != NULL; pgw = next) {
|
|
|
|
next = TAILQ_NEXT(pgw, list);
|
2017-03-24 12:53:07 +01:00
|
|
|
if (pgw->pgsql != pgsql)
|
2014-08-21 16:36:12 +02:00
|
|
|
continue;
|
|
|
|
|
2018-02-13 13:21:27 +01:00
|
|
|
pgsql_queue_count--;
|
2017-03-24 12:53:07 +01:00
|
|
|
TAILQ_REMOVE(&pgsql_wait_queue, pgw, list);
|
|
|
|
kore_pool_put(&pgsql_wait_pool, pgw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pgsql_queue_wakeup(void)
|
|
|
|
{
|
|
|
|
struct pgsql_wait *pgw, *next;
|
|
|
|
|
|
|
|
for (pgw = TAILQ_FIRST(&pgsql_wait_queue); pgw != NULL; pgw = next) {
|
|
|
|
next = TAILQ_NEXT(pgw, list);
|
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgw->pgsql->req != NULL) {
|
|
|
|
if (pgw->pgsql->req->flags & HTTP_REQUEST_DELETE) {
|
2018-02-14 13:59:23 +01:00
|
|
|
pgsql_queue_count--;
|
2017-03-24 12:53:07 +01:00
|
|
|
TAILQ_REMOVE(&pgsql_wait_queue, pgw, list);
|
|
|
|
kore_pool_put(&pgsql_wait_pool, pgw);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_request_wakeup(pgw->pgsql->req);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (pgw->pgsql->cb != NULL)
|
|
|
|
pgw->pgsql->cb(pgw->pgsql, pgw->pgsql->arg);
|
2014-08-21 16:36:12 +02:00
|
|
|
|
2018-02-14 13:59:23 +01:00
|
|
|
pgsql_queue_count--;
|
2014-08-21 16:36:12 +02:00
|
|
|
TAILQ_REMOVE(&pgsql_wait_queue, pgw, list);
|
|
|
|
kore_pool_put(&pgsql_wait_pool, pgw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
static struct pgsql_conn *
|
|
|
|
pgsql_conn_create(struct kore_pgsql *pgsql, struct pgsql_db *db)
|
2014-03-30 23:54:35 +02:00
|
|
|
{
|
|
|
|
struct pgsql_conn *conn;
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
if (db == NULL || db->conn_string == NULL)
|
2014-04-14 08:45:31 +02:00
|
|
|
fatal("pgsql_conn_create: no connection string");
|
|
|
|
|
2017-08-21 14:25:09 +02:00
|
|
|
db->conn_count++;
|
2017-01-10 15:29:03 +01:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
conn = kore_calloc(1, sizeof(*conn));
|
2017-01-10 15:29:03 +01:00
|
|
|
conn->job = NULL;
|
|
|
|
conn->flags = PGSQL_CONN_FREE;
|
|
|
|
conn->name = kore_strdup(db->name);
|
|
|
|
TAILQ_INSERT_TAIL(&pgsql_conn_free, conn, list);
|
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
conn->evt.type = KORE_TYPE_PGSQL_CONN;
|
|
|
|
conn->evt.handle = kore_pgsql_handle;
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
conn->db = PQconnectdb(db->conn_string);
|
2014-03-30 23:54:35 +02:00
|
|
|
if (conn->db == NULL || (PQstatus(conn->db) != CONNECTION_OK)) {
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(conn->db));
|
2014-03-30 23:54:35 +02:00
|
|
|
pgsql_conn_cleanup(conn);
|
2016-01-04 11:12:43 +01:00
|
|
|
return (NULL);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
return (conn);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 14:46:21 +02:00
|
|
|
static void
|
|
|
|
pgsql_conn_release(struct kore_pgsql *pgsql)
|
|
|
|
{
|
|
|
|
int fd;
|
2017-07-11 15:11:13 +02:00
|
|
|
PGresult *result;
|
2014-08-14 14:46:21 +02:00
|
|
|
|
|
|
|
if (pgsql->conn == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
/* Async query cleanup */
|
|
|
|
if (pgsql->flags & KORE_PGSQL_ASYNC) {
|
2017-02-06 11:40:33 +01:00
|
|
|
if (pgsql->flags & KORE_PGSQL_SCHEDULED) {
|
|
|
|
fd = PQsocket(pgsql->conn->db);
|
|
|
|
kore_platform_disable_read(fd);
|
|
|
|
|
2017-02-06 20:01:16 +01:00
|
|
|
if (pgsql->state != KORE_PGSQL_STATE_DONE)
|
|
|
|
pgsql_cancel(pgsql);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
2017-02-06 11:40:33 +01:00
|
|
|
kore_pool_put(&pgsql_job_pool, pgsql->conn->job);
|
2016-01-04 11:12:43 +01:00
|
|
|
}
|
2014-08-14 14:46:21 +02:00
|
|
|
|
2014-08-14 16:43:40 +02:00
|
|
|
/* Drain just in case. */
|
2017-07-11 15:11:13 +02:00
|
|
|
while ((result = PQgetResult(pgsql->conn->db)) != NULL)
|
|
|
|
PQclear(result);
|
2014-08-14 16:43:40 +02:00
|
|
|
|
2014-08-14 14:46:21 +02:00
|
|
|
pgsql->conn->job = NULL;
|
|
|
|
pgsql->conn->flags |= PGSQL_CONN_FREE;
|
|
|
|
TAILQ_INSERT_TAIL(&pgsql_conn_free, pgsql->conn, list);
|
|
|
|
|
|
|
|
pgsql->conn = NULL;
|
2014-08-21 16:36:12 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_COMPLETE;
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
pgsql->cb(pgsql, pgsql->arg);
|
|
|
|
|
2014-08-21 16:36:12 +02:00
|
|
|
pgsql_queue_wakeup();
|
2014-08-14 14:46:21 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
static void
|
|
|
|
pgsql_conn_cleanup(struct pgsql_conn *conn)
|
|
|
|
{
|
2014-08-14 14:34:23 +02:00
|
|
|
struct kore_pgsql *pgsql;
|
2017-07-11 15:11:13 +02:00
|
|
|
struct pgsql_db *pgsqldb;
|
2014-03-30 23:54:35 +02:00
|
|
|
|
|
|
|
if (conn->flags & PGSQL_CONN_FREE)
|
|
|
|
TAILQ_REMOVE(&pgsql_conn_free, conn, list);
|
|
|
|
|
|
|
|
if (conn->job) {
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql = conn->job->pgsql;
|
2017-03-24 12:53:07 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
if (pgsql->req != NULL)
|
|
|
|
http_request_wakeup(pgsql->req);
|
|
|
|
#endif
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql->conn = NULL;
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_set_error(pgsql, PQerrorMessage(conn->db));
|
2014-03-30 23:54:35 +02:00
|
|
|
|
2014-08-21 16:36:12 +02:00
|
|
|
kore_pool_put(&pgsql_job_pool, conn->job);
|
2014-03-30 23:54:35 +02:00
|
|
|
conn->job = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->db != NULL)
|
|
|
|
PQfinish(conn->db);
|
|
|
|
|
2017-07-11 15:11:13 +02:00
|
|
|
LIST_FOREACH(pgsqldb, &pgsql_db_conn_strings, rlist) {
|
2018-09-19 07:18:46 +02:00
|
|
|
if (!strcmp(pgsqldb->name, conn->name)) {
|
2017-08-21 14:25:09 +02:00
|
|
|
pgsqldb->conn_count--;
|
2017-07-11 15:11:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(conn->name);
|
|
|
|
kore_free(conn);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
2014-03-31 01:04:22 +02:00
|
|
|
|
|
|
|
static void
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_read_result(struct kore_pgsql *pgsql)
|
2014-03-31 01:04:22 +02:00
|
|
|
{
|
2019-09-13 23:20:51 +02:00
|
|
|
struct pgsql_conn *conn;
|
|
|
|
PGnotify *notify;
|
|
|
|
int saved_errno;
|
2018-07-18 11:38:17 +02:00
|
|
|
|
2019-09-13 23:20:51 +02:00
|
|
|
conn = pgsql->conn;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!PQconsumeInput(conn->db)) {
|
2019-09-04 19:19:52 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_ERROR;
|
2019-09-13 23:20:51 +02:00
|
|
|
pgsql->error = kore_strdup(PQerrorMessage(conn->db));
|
2019-09-04 19:19:52 +02:00
|
|
|
return;
|
2019-09-13 23:20:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
saved_errno = errno;
|
|
|
|
|
|
|
|
if (PQisBusy(conn->db)) {
|
|
|
|
if (saved_errno != EAGAIN && saved_errno != EWOULDBLOCK)
|
|
|
|
continue;
|
2019-09-04 19:19:52 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_WAIT;
|
2019-09-13 23:22:38 +02:00
|
|
|
conn->evt.flags &= ~KORE_EVENT_READ;
|
2019-09-04 19:19:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-09-13 23:20:51 +02:00
|
|
|
|
|
|
|
break;
|
2014-03-31 01:04:22 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 23:20:51 +02:00
|
|
|
while ((notify = PQnotifies(conn->db)) != NULL) {
|
2018-07-18 11:38:17 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_NOTIFY;
|
2018-07-18 11:40:59 +02:00
|
|
|
pgsql->notify.extra = notify->extra;
|
|
|
|
pgsql->notify.channel = notify->relname;
|
2018-07-18 11:38:17 +02:00
|
|
|
|
2018-07-18 11:40:59 +02:00
|
|
|
if (pgsql->cb != NULL)
|
|
|
|
pgsql->cb(pgsql, pgsql->arg);
|
2018-07-18 11:38:17 +02:00
|
|
|
|
|
|
|
PQfreemem(notify);
|
|
|
|
}
|
|
|
|
|
2019-09-13 23:20:51 +02:00
|
|
|
pgsql->result = PQgetResult(conn->db);
|
2014-08-14 14:34:23 +02:00
|
|
|
if (pgsql->result == NULL) {
|
|
|
|
pgsql->state = KORE_PGSQL_STATE_DONE;
|
2014-03-31 01:04:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
switch (PQresultStatus(pgsql->result)) {
|
2014-03-31 01:04:22 +02:00
|
|
|
case PGRES_COPY_OUT:
|
|
|
|
case PGRES_COPY_IN:
|
|
|
|
case PGRES_NONFATAL_ERROR:
|
|
|
|
case PGRES_COPY_BOTH:
|
|
|
|
break;
|
|
|
|
case PGRES_COMMAND_OK:
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_DONE;
|
2014-03-31 01:04:22 +02:00
|
|
|
break;
|
|
|
|
case PGRES_TUPLES_OK:
|
2014-07-03 22:58:04 +02:00
|
|
|
#if PG_VERSION_NUM >= 90200
|
2014-03-31 01:04:22 +02:00
|
|
|
case PGRES_SINGLE_TUPLE:
|
2014-07-03 22:58:04 +02:00
|
|
|
#endif
|
2014-08-14 14:34:23 +02:00
|
|
|
pgsql->state = KORE_PGSQL_STATE_RESULT;
|
2014-03-31 01:04:22 +02:00
|
|
|
break;
|
|
|
|
case PGRES_EMPTY_QUERY:
|
|
|
|
case PGRES_BAD_RESPONSE:
|
|
|
|
case PGRES_FATAL_ERROR:
|
2016-01-04 11:12:43 +01:00
|
|
|
pgsql_set_error(pgsql, PQresultErrorMessage(pgsql->result));
|
2014-03-31 01:04:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-06 20:01:16 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
pgsql_cancel(struct kore_pgsql *pgsql)
|
|
|
|
{
|
|
|
|
PGcancel *cancel;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
if ((cancel = PQgetCancel(pgsql->conn->db)) != NULL) {
|
|
|
|
if (!PQcancel(cancel, buf, sizeof(buf)))
|
|
|
|
kore_log(LOG_ERR, "failed to cancel: %s", buf);
|
|
|
|
PQfreeCancel(cancel);
|
|
|
|
}
|
|
|
|
}
|