Skip to content

Commit

Permalink
several fixes (#723)
Browse files Browse the repository at this point in the history
* sources/hba_reader.c: fix list of keywords

It must contain {0,0,0} at the end.

Signed-off-by: rkhapov <[email protected]>

* context.c: fix warn

This patch fixes warnings about writing function
pointer into data pointer

Signed-off-by: rkhapov <[email protected]>

* sources/macro.h: fix warn

about signed vs unsiged comparison

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: remove unused func

Signed-off-by: rkhapov <[email protected]>

* sources/counter.c: fix warn

About memcpy to volatile variable

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: fix warn

And remove useless if's

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: remove unused var

Signed-off-by: rkhapov <[email protected]>

* sources/console.c: remove unused vars

Signed-off-by: rkhapov <[email protected]>

* sources/frontend.c: remove unsed var

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: fix warns

Signed-off-by: rkhapov <[email protected]>

* sources/backend.c: remove unused count

From functions od_backend_query*, count arg is not used

Signed-off-by: rkhapov <[email protected]>

* sources/odyssey.h: add backend_sync

To fix warn about implicit declaration of od_backend_request_sync_point

Signed-off-by: rkhapov <[email protected]>

* sources/address.c: fix implicit inet_pton

Signed-off-by: rkhapov <[email protected]>

* sources/address.c: fix warn

Signed-off-by: rkhapov <[email protected]>

* sources/clock.c: fix warn

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: fix uninitialized var

Signed-off-by: rkhapov <[email protected]>

* sources/frontend.c: fix warn

Signed-off-by: rkhapov <[email protected]>

* sources/scram.c: return instead of free

goto in this place will lead to free on unitialized ptr

Signed-off-by: rkhapov <[email protected]>

* sources/scram.c: fix warn

Case when size + 1 >= INT_MAX is extremely unlikely

Signed-off-by: rkhapov <[email protected]>

* test/odyssey: use test macro instead of assert

More informative + do not produce warnings

Signed-off-by: rkhapov <[email protected]>

* thread.c: fix warn about implicit func

Signed-off-by: rkhapov <[email protected]>

* docker/bin/ody-stop: SIGTERM wait

This is more useful for tests: will help to find out
if gracefully termination in odyssey doestn't work.

Signed-off-by: rkhapov <[email protected]>

* docker/Dockerfile: update libasan

This will possibly fix infinite DEADLYSIGNAL error

Signed-off-by: rkhapov <[email protected]>

* docker/entrypoint.sh: disable addr randomization

There is some bug with asan we currently using
This bug leads to infinite DEADLYSIGNAL message
on odyssey-asan runs sometimes

This patch adds some simple workaround about it

Signed-off-by: rkhapov <[email protected]>

* soureces/hba.c: fix reloading

It was broken, because it sets new rules list at the stack.

Signed-off-by: rkhapov <[email protected]>

* sources/rules.c: fix router closing

It should be closed after every usage.
This patch moves closing after every logging,
otherwise there will be use-after-free.

Signed-off-by: rkhapov <[email protected]>

---------

Signed-off-by: rkhapov <[email protected]>
Co-authored-by: rkhapov <[email protected]>
  • Loading branch information
rkhapov and rkhapov authored Dec 9, 2024
1 parent e7b1035 commit a7049a2
Show file tree
Hide file tree
Showing 26 changed files with 224 additions and 155 deletions.
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
openssl \
ldap-utils \
libldap-2.4-2 \
libldap-dev
libldap-dev \
libasan6

RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Expand All @@ -48,6 +49,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
libpq-dev \
vim \
python3 \
postgresql-common \
postgresql-server-dev-14 \
git
Expand Down
64 changes: 62 additions & 2 deletions docker/bin/ody-stop
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
#!/bin/bash
pkill odyssey || (sleep 1 && kill -9 $(pgrep odyssey)) || true
#!/usr/bin/env python3

import os
import time
import signal
import subprocess


def get_odyssey_pids():
print(subprocess.check_output('ps aux | grep odyssey', shell=True).decode('utf-8'))

try:
return list(map(int, subprocess.check_output(['pgrep', 'odyssey']).split()))
except subprocess.CalledProcessError:
# non-zero exit code means that there is no odyssey pids
return []

def terminate_gracefully(pid, timeout):
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
print(f'Process {pid} already finished or doesnt ever existed')
return

print(f'Waiting {timeout} seconds to {pid} finish after SIGTERM...', end='')

start = time.time()
finished = False

while time.time() - start < timeout:
try:
if 'odyssey'.encode('utf-8') not in subprocess.check_output(['ps', '-p', str(pid)], ):
finished = True
break
except subprocess.CalledProcessError:
# non-zero code means there is no process with that pid
finished = True
break

print('.', end='')
time.sleep(0.5)

print()

return finished


def main():
timeout = 5

pids = get_odyssey_pids()
print('Found odyssey pids:', ', '.join(list(map(str, pids))))

for pid in pids:
if not terminate_gracefully(pid, timeout):
print(f'Process {pid} didnt finish within {timeout} seconds')
exit(1)

exit(0)

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ then
fi
echo "" > /var/log/odyssey.log

echo 0 > /proc/sys/kernel/randomize_va_space
/usr/bin/odyssey-asan /etc/odyssey/odyssey.conf
sleep 5
ody-stop
echo 2 > /proc/sys/kernel/randomize_va_space

# TODO: rewrite
#/shell-test/test.sh
Expand Down
4 changes: 3 additions & 1 deletion sources/address.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <machinarium.h>
#include <odyssey.h>
#include <regex.h>
#include <arpa/inet.h>

/*
* Odyssey.
Expand Down Expand Up @@ -158,7 +159,8 @@ static bool od_address_check_hostname(struct sockaddr_storage *client_sa,

char client_hostname[NI_MAXHOST];

ret = getnameinfo(client_sa, sizeof(*client_sa), client_hostname,
ret = getnameinfo((const struct sockaddr *)client_sa,
sizeof(*client_sa), client_hostname,
sizeof(client_hostname), NULL, 0, NI_NAMEREQD);

if (ret != 0)
Expand Down
11 changes: 6 additions & 5 deletions sources/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ int od_backend_update_parameter(od_server_t *server, char *context, char *data,
return 0;
}

int od_backend_ready_wait(od_server_t *server, char *context, int count,
uint32_t time_ms, uint32_t ignore_errors)
int od_backend_ready_wait(od_server_t *server, char *context, uint32_t time_ms,
uint32_t ignore_errors)
{
od_instance_t *instance = server->global->instance;
int query_rc;
Expand Down Expand Up @@ -781,13 +781,14 @@ od_retcode_t od_backend_query_send(od_server_t *server, char *context,

od_retcode_t od_backend_query(od_server_t *server, char *context, char *query,
char *param, int len, uint32_t timeout,
uint32_t count, uint32_t ignore_errors)
uint32_t ignore_errors)
{
if (od_backend_query_send(server, context, query, param, len) ==
NOT_OK_RESPONSE) {
return NOT_OK_RESPONSE;
}
od_retcode_t rc = od_backend_ready_wait(server, context, count, timeout,
ignore_errors);

od_retcode_t rc =
od_backend_ready_wait(server, context, timeout, ignore_errors);
return rc;
}
4 changes: 2 additions & 2 deletions sources/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ void od_backend_error(od_server_t *, char *, char *, uint32_t);

int od_backend_update_parameter(od_server_t *, char *, char *, uint32_t, int);
int od_backend_ready(od_server_t *, char *, uint32_t);
int od_backend_ready_wait(od_server_t *, char *, int, uint32_t, uint32_t);
int od_backend_ready_wait(od_server_t *, char *, uint32_t, uint32_t);

od_retcode_t od_backend_query_send(od_server_t *server, char *context,
char *query, char *param, int len);
od_retcode_t od_backend_query(od_server_t *, char *, char *, char *, int,
uint32_t, uint32_t, uint32_t);
uint32_t, uint32_t);

#endif /* ODYSSEY_BACKEND_H */
2 changes: 1 addition & 1 deletion sources/backend_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int od_backend_request_sync_point(od_server_t *server)
/* update server sync state */
od_server_sync_request(server, 1);

return od_backend_ready_wait(server, "sync-point", 1 /*count*/,
return od_backend_ready_wait(server, "sync-point",
1000 /* timeout 1 sec */,
0 /*ignore error?*/);
}
9 changes: 2 additions & 7 deletions sources/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,10 @@ static int od_console_show_err_router_stats_cb(od_error_logger_t *l,
return od_console_show_router_stats_err_add(stream, l);
}

static inline int od_console_show_help(od_client_t *client,
machine_msg_t *stream)
static inline int od_console_show_help(machine_msg_t *stream)
{
assert(stream);

char msg[OD_QRY_MAX_SZ];
int msg_len;
va_list args;

char *message =
"\n"
"Console usage\n"
Expand Down Expand Up @@ -1753,7 +1748,7 @@ static inline int od_console_show(od_client_t *client, machine_msg_t *stream,
case OD_LSTATS:
return od_console_show_stats(client, stream);
case OD_LHELP:
return od_console_show_help(client, stream);
return od_console_show_help(stream);
case OD_LPOOLS:
return od_console_show_pools(client, stream, false);
case OD_LPOOLS_EXTENDED:
Expand Down
5 changes: 3 additions & 2 deletions sources/counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ od_counter_t *od_counter_create(size_t max_value)

counter->size = values_count;

memset(counter->value_to_count, 0,
sizeof(od_atomic_u64_t) * counter->size);
for (size_t i = 0; i < counter->size; ++i) {
od_atomic_u64_set(&counter->value_to_count[i], 0ULL);
}

return counter;
}
Expand Down
10 changes: 3 additions & 7 deletions sources/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,9 @@ od_frontend_rewrite_msg(char *data, int size, int opname_start_offset,
}

static od_frontend_status_t od_frontend_deploy_prepared_stmt(
od_server_t *server, od_relay_t *relay, char *ctx, char *data,
int size /* to adcance or to write? */, od_hash_t body_hash,
char *opname, int opnamelen)
od_server_t *server, __attribute__((unused)) od_relay_t *relay,
char *ctx, char *data, int size /* to adcance or to write? */,
od_hash_t body_hash, char *opname, int opnamelen)
{
od_route_t *route = server->route;
od_instance_t *instance = server->global->instance;
Expand Down Expand Up @@ -1096,7 +1096,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay,
"%s", kiwi_fe_type_to_string(type));

od_frontend_status_t retstatus = OD_OK;
bool forwarded = 0;
switch (type) {
case KIWI_FE_COPY_DONE:
case KIWI_FE_COPY_FAIL:
Expand Down Expand Up @@ -1186,7 +1185,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay,
od_dbg_printf_on_dvl_lvl(
1, "client relay %p advance msg %c\n", relay,
*(char *)machine_msg_data(msg));
forwarded = 1;
}
break;
case KIWI_FE_PARSE:
Expand Down Expand Up @@ -1340,7 +1338,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay,
od_dbg_printf_on_dvl_lvl(
1, "client relay %p advance msg %c\n", relay,
*(char *)machine_msg_data(msg));
forwarded = 1;
}
break;
case KIWI_FE_EXECUTE:
Expand All @@ -1353,7 +1350,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay,
uint32_t name_len;
kiwi_fe_close_type_t type;
int rc;
forwarded = 1;

if (od_frontend_parse_close(data, size, &name,
&name_len,
Expand Down
12 changes: 11 additions & 1 deletion sources/hba.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ void od_hba_reload(od_hba_t *hba, od_hba_rules_t *rules)
{
od_hba_lock(hba);

od_hba_rules_free(&hba->rules);

od_list_init(&hba->rules);
memcpy(&hba->rules, &rules, sizeof(rules));

od_list_t *i, *n;
od_list_foreach_safe(rules, i, n)
{
od_hba_rule_t *rule;
rule = od_container_of(i, od_hba_rule_t, link);

od_hba_rules_add(&hba->rules, rule);
}

od_hba_unlock(hba);
}
Expand Down
1 change: 1 addition & 0 deletions sources/hba_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static od_keyword_t od_hba_keywords[] = {
od_keyword("trust", OD_LALLOW),
od_keyword("deny", OD_LDENY),
od_keyword("reject", OD_LDENY),
{ 0, 0, 0 },
};

static void od_hba_reader_error(od_config_reader_t *reader, char *msg)
Expand Down
2 changes: 1 addition & 1 deletion sources/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define CONCAT_(A, B) A##B
#define CONCAT(A, B) CONCAT_(A, B)

static const int64_t interval_usec = 1000000ll;
static const uint64_t interval_usec = 1000000ull;

typedef int od_retcode_t;

Expand Down
1 change: 1 addition & 0 deletions sources/odyssey.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@

#include "sources/frontend.h"
#include "sources/backend.h"
#include "sources/backend_sync.h"

#include "sources/mdb_iamproxy.h"
#endif /* ODYSSEY_H */
10 changes: 5 additions & 5 deletions sources/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int od_reset(od_server_t *server)
"not synchronized, wait for %d msec (#%d)",
wait_timeout, wait_try);
wait_try++;
rc = od_backend_ready_wait(server, "reset", 1,
rc = od_backend_ready_wait(server, "reset",
wait_timeout,
1 /*ignore server errors*/);
if (rc == NOT_OK_RESPONSE)
Expand Down Expand Up @@ -137,7 +137,7 @@ int od_reset(od_server_t *server)
char query_rlb[] = "ROLLBACK";
rc = od_backend_query(
server, "reset-rollback", query_rlb, NULL,
sizeof(query_rlb), wait_timeout, 1,
sizeof(query_rlb), wait_timeout,
0 /*do not ignore server error messages*/);
if (rc == NOT_OK_RESPONSE)
goto error;
Expand All @@ -150,7 +150,7 @@ int od_reset(od_server_t *server)
char query_discard[] = "DISCARD ALL";
rc = od_backend_query(
server, "reset-discard", query_discard, NULL,
sizeof(query_discard), wait_timeout, 1,
sizeof(query_discard), wait_timeout,
0 /*do not ignore server error messages*/);
if (rc == NOT_OK_RESPONSE)
goto error;
Expand All @@ -163,7 +163,7 @@ int od_reset(od_server_t *server)
"SET SESSION AUTHORIZATION DEFAULT;RESET ALL;CLOSE ALL;UNLISTEN *;SELECT pg_advisory_unlock_all();DISCARD PLANS;DISCARD SEQUENCES;DISCARD TEMP;";
rc = od_backend_query(
server, "reset-discard-smart", query_discard, NULL,
sizeof(query_discard), wait_timeout, 1,
sizeof(query_discard), wait_timeout,
0 /*do not ignore server error messages*/);
if (rc == NOT_OK_RESPONSE)
goto error;
Expand All @@ -173,7 +173,7 @@ int od_reset(od_server_t *server)
server, "reset-discard-smart-string",
route->rule->pool->discard_query, NULL,
strlen(route->rule->pool->discard_query) + 1,
wait_timeout, 1,
wait_timeout,
0 /*do not ignore server error messages*/);
if (rc == NOT_OK_RESPONSE)
goto error;
Expand Down
Loading

0 comments on commit a7049a2

Please sign in to comment.