[PATCH] BUG/MINOR: server: check return value of fopen() in apply_server_state()

2019-10-17 Thread Vedran Furac
Hello,

this patch fixes the segmentation fault on start when HAProxy is configured to
load global server state file which doesn't exist. This can be considered a
regression from 1.8 which warned but continued without crashing. It needs to be
backported to 2.0.

Thanks,
Vedran
>From df3d9f27e48b29c2bf4885943a16e5a920d5471f Mon Sep 17 00:00:00 2001
From: Vedran Furac 
Date: Wed, 16 Oct 2019 14:49:38 +0200
Subject: [PATCH] BUG/MINOR: server: check return value of fopen() in
 apply_server_state()

fopen() can return NULL when state file is missing. This patch adds a
check of fopen() return value so we can skip processing in such case.
---
 src/server.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/server.c b/src/server.c
index df0dcbfe..6b740240 100644
--- a/src/server.c
+++ b/src/server.c
@@ -3595,6 +3595,8 @@ void apply_server_state(void)
 		f = fopen(globalfilepath, "r");
 		if (errno)
 			ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
+		if (!f)
+			goto out_load_server_state_in_tree;
 
 		global_file_version = srv_state_get_version(f);
 		if (global_file_version == 0)
-- 
2.20.1



Re: [PATCH] MINOR: contrib/spoa_server: Port SPOA server to Python 3

2019-07-23 Thread Vedran Furac
Hey,

On 23. 07. 2019. 15:47, Aleksandar Lazic wrote:
> Hi.
> 
> Am 23.07.2019 um 15:34 schrieb Vedran Furac:
>> Hello,
>>
>> Attached is a simple port of SPOA with embedded Python 2 interpreter to 
>> Python 3
>> following the official Python porting documentation. I tried to keep the 
>> changes
>> at minimum. Tested on my local Debian with Python v3.7 using the provided
>> example script.
> 
> Please can you also adopt the README to reflect this change.

Thanks, updated the README, amended the commit and attached new patch.

Thanks,
Vedran
>From 360387fce1c97d36d7a6c9a7cb38c82f4ab51592 Mon Sep 17 00:00:00 2001
From: Vedran Furac 
Date: Fri, 19 Jul 2019 15:18:56 +0200
Subject: [PATCH] MINOR: contrib/spoa_server: Port SPOA server to Python 3

Python 2 will be EOL in 2020. This patch ports SPOA server with
embedded interpreter to Python 3
---
 contrib/spoa_server/Makefile|   6 +-
 contrib/spoa_server/README  |   2 +-
 contrib/spoa_server/ps_python.c | 123 ++--
 3 files changed, 74 insertions(+), 57 deletions(-)

diff --git a/contrib/spoa_server/Makefile b/contrib/spoa_server/Makefile
index f0752829..70ccf988 100644
--- a/contrib/spoa_server/Makefile
+++ b/contrib/spoa_server/Makefile
@@ -23,12 +23,12 @@ endif
 
 ifneq ($(USE_PYTHON),)
 OBJS += ps_python.o
-CFLAGS += -I/usr/include/python2.7
-LDLIBS += -lpython2.7
+CFLAGS  += $(shell python3-config --cflags) -Wno-sign-compare -fPIC
+LDFLAGS += $(shell python3-config --ldflags)
 endif
 
 spoa: $(OBJS)
-	$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+	$(LD) $(LDFLAGS) -o $@ $^
 
 install: spoa
 	install spoa $(DESTDIR)$(BINDIR)
diff --git a/contrib/spoa_server/README b/contrib/spoa_server/README
index fa039c1a..f8ada376 100644
--- a/contrib/spoa_server/README
+++ b/contrib/spoa_server/README
@@ -16,7 +16,7 @@ distribution repositories or from the source.
 
 CentOS/RHEL: yum install python-devel
 
-The current python version in use is 2.7.
+The current python version in use is 3.x.
 
 
   Compilation
diff --git a/contrib/spoa_server/ps_python.c b/contrib/spoa_server/ps_python.c
index 0a9fbffc..654bebdf 100644
--- a/contrib/spoa_server/ps_python.c
+++ b/contrib/spoa_server/ps_python.c
@@ -36,6 +36,10 @@ static int ps_python_start_worker(struct worker *w);
 static int ps_python_load_file(struct worker *w, const char *file);
 static int ps_python_exec_message(struct worker *w, void *ref, int nargs, struct spoe_kv *args);
 
+struct module_state {
+PyObject *error;
+};
+
 static struct ps ps_python_bindings = {
 	.init_worker = ps_python_start_worker,
 	.load_file = ps_python_load_file,
@@ -171,11 +175,11 @@ static PyObject *ps_python_set_var_ipv4(PyObject *self, PyObject *args)
 	value = PyObject_GetAttrString(ipv4, "packed");
 	if (value == NULL)
 		return NULL;
-	if (PyString_GET_SIZE(value) != sizeof(ip)) {
-		PyErr_Format(spoa_error, "UPv6 manipulation internal error");
+	if (PyBytes_Size(value) != sizeof(ip)) {
+		PyErr_Format(spoa_error, "IPv4 manipulation internal error");
 		return NULL;
 	}
-	memcpy(, PyString_AS_STRING(value), PyString_GET_SIZE(value));
+	memcpy(, PyBytes_AsString(value), PyBytes_Size(value));
 	if (!set_var_ipv4(worker, name, name_len, scope, )) {
 		PyErr_SetString(spoa_error, "No space left available");
 		return NULL;
@@ -202,11 +206,11 @@ static PyObject *ps_python_set_var_ipv6(PyObject *self, PyObject *args)
 	value = PyObject_GetAttrString(ipv6, "packed");
 	if (value == NULL)
 		return NULL;
-	if (PyString_GET_SIZE(value) != sizeof(ip)) {
-		PyErr_Format(spoa_error, "UPv6 manipulation internal error");
+	if (PyBytes_Size(value) != sizeof(ip)) {
+		PyErr_Format(spoa_error, "IPv6 manipulation internal error");
 		return NULL;
 	}
-	memcpy(, PyString_AS_STRING(value), PyString_GET_SIZE(value));
+	memcpy(, PyBytes_AsString(value), PyBytes_Size(value));
 	if (!set_var_ipv6(worker, name, name_len, scope, )) {
 		PyErr_SetString(spoa_error, "No space left available");
 		return NULL;
@@ -272,55 +276,31 @@ static PyMethodDef spoa_methods[] = {
 	 "Set SPOA str variable"},
 	{"set_var_bin", ps_python_set_var_bin, METH_VARARGS,
 	 "Set SPOA bin variable"},
-	{ /* end */ }
+	{NULL, NULL}
 };
 
-static int ps_python_start_worker(struct worker *w)
+static struct PyModuleDef spoa_module = {
+PyModuleDef_HEAD_INIT,
+"spoa",
+NULL,
+sizeof(struct module_state),
+spoa_methods,
+NULL,
+NULL,
+NULL,
+NULL
+};
+
+static PyObject *PyInit_spoa(void)
 {
-	PyObject *m;
-	PyObject *module_name;
-	PyObject *value;
 	int ret;
-
-	Py_SetProgramName("spoa-server");
-	Py_Initialize();
-
-	module_name = PyString_FromString("ipaddress");
-	if (module_name == NULL) {
-		PyErr_Print();
-		return 0;
-	}
-
-	module_ipaddress = PyImport_Import(modul