Signed-off-by: Federico Simoncelli fsimonce@redhat.com --- .gitignore | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore index 74a57d8..05f923a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ *.co .vimdir cscope.* +wdmd/libwdmd.so* +wdmd/wdmd +wdmd/wdmd_client src/libsanlock.so* src/libsanlock_client.so* src/sanlock
* mark __set_exception and __parse_resource for cpychecker (as functions that set exceptions) * fix const char* warnings * fix PyArg_ParseTupleAndKeywords format issues * fix a refcount of ls_entry in py_write_lockspace * fix refcounts in initexception
Signed-off-by: Federico Simoncelli fsimonce@redhat.com --- python/sanlock.c | 74 ++++++++++++++++++++++++++++++----------------- src/client.c | 2 +- src/sanlock_resource.h | 2 +- 3 files changed, 49 insertions(+), 29 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c index 92e1396..c0c0276 100644 --- a/python/sanlock.c +++ b/python/sanlock.c @@ -16,13 +16,30 @@ #define __unused __attribute__ ((unused)) #endif
+#ifdef WITH_CPYCHECKER_SETS_EXCEPTION_ATTRIBUTE +#define __sets_exception \ + __attribute__((cpychecker_sets_exception)) +#else +#define __sets_exception +#endif + +#ifdef WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE +#define __neg_sets_exception \ + __attribute__((cpychecker_negative_result_sets_exception)) +#else +#define __neg_sets_exception +#endif + +/* Functions prototypes */ +static void __set_exception(int en, char *msg) __sets_exception; +static int __parse_resource(PyObject *obj, struct sanlk_resource **res_ret) __neg_sets_exception; + /* Sanlock module */ PyDoc_STRVAR(pydoc_sanlock, "\ Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved.\n\ This copyrighted material is made available to anyone wishing to use,\n\ modify, copy, or redistribute it subject to the terms and conditions\n\ of the GNU General Public License v2 or (at your option) any later version."); -PyObject *py_module;
/* Sanlock exception */ static PyObject *py_exception; @@ -30,7 +47,7 @@ static PyObject *py_exception; static void __set_exception(int en, char *msg) { - char *err_name; + const char *err_name; PyObject *exc_tuple;
if (en < 0 && en > -200) { @@ -241,7 +258,7 @@ py_init_resource(PyObject *self __unused, PyObject *args, PyObject *keywds) }
/* parse and check sanlock resource */ - if (__parse_resource(disks, &res) != 0) { + if (__parse_resource(disks, &res) < 0) { return NULL; }
@@ -287,7 +304,7 @@ py_write_lockspace(PyObject *self __unused, PyObject *args, PyObject *keywds) memset(&ls, 0, sizeof(struct sanlk_lockspace));
/* parse python tuple */ - if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|kiiI", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|kiI", kwlist, &lockspace, &path, &ls.host_id_disk.offset, &max_hosts, &io_timeout)) { return NULL; @@ -363,8 +380,8 @@ py_read_lockspace(PyObject *self __unused, PyObject *args, PyObject *keywds) /* fill the dictionary information: iotimeout */ if ((ls_entry = PyInt_FromLong(io_timeout)) == NULL) goto exit_fail; - Py_DECREF(ls_entry); rv = PyDict_SetItemString(ls_info, "iotimeout", ls_entry); + Py_DECREF(ls_entry); if (rv != 0) goto exit_fail;
@@ -473,14 +490,14 @@ py_write_resource(PyObject *self __unused, PyObject *args, PyObject *keywds) "num_hosts", NULL};
/* parse python tuple */ - if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|iii", + if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|ii", kwlist, &lockspace, &resource, &PyList_Type, &disks, &max_hosts, &num_hosts)) { return NULL; }
/* parse and check sanlock resource */ - if (__parse_resource(disks, &rs) != 0) { + if (__parse_resource(disks, &rs) < 0) { return NULL; }
@@ -701,7 +718,7 @@ py_acquire(PyObject *self __unused, PyObject *args, PyObject *keywds) }
/* parse and check sanlock resource */ - if (__parse_resource(disks, &res) != 0) { + if (__parse_resource(disks, &res) < 0) { return NULL; }
@@ -756,7 +773,7 @@ py_release(PyObject *self __unused, PyObject *args, PyObject *keywds) }
/* parse and check sanlock resource */ - if (__parse_resource(disks, &res) != 0) { + if (__parse_resource(disks, &res) < 0) { return NULL; }
@@ -794,7 +811,8 @@ static PyObject * py_killpath(PyObject *self __unused, PyObject *args, PyObject *keywds) { int rv, i, j, n, num_args, sanlockfd = -1; - char *p, *path, kpargs[SANLK_HELPER_ARGS_LEN]; + char kpargs[SANLK_HELPER_ARGS_LEN]; + const char *p, *path; PyObject *argslist, *item;
static char *kwlist[] = {"path", "args", "slkfd", NULL}; @@ -917,51 +935,53 @@ sanlock_exception = { "errno", (PyCFunction) py_exception_errno, METH_O, pydoc_errno };
-static void +static PyObject * initexception(void) { int rv; - PyObject *dict, *func, *meth; + PyObject *dict, *func, *meth, *excp = NULL;
- dict = PyDict_New(); + if ((dict = PyDict_New()) == NULL) + goto exit_fail;
- if (dict == NULL) - return; + if ((func = PyCFunction_New(&sanlock_exception, NULL)) == NULL) + goto exit_fail;
- func = PyCFunction_New(&sanlock_exception, NULL); meth = PyObject_CallFunction((PyObject *) &PyProperty_Type, "O", func); Py_DECREF(func); - if (meth == NULL) - return; + goto exit_fail;
rv = PyDict_SetItemString(dict, sanlock_exception.ml_name, meth); Py_DECREF(meth); - if (rv < 0) - return; + goto exit_fail; + + excp = PyErr_NewException("sanlock.SanlockException", NULL, dict);
- py_exception = PyErr_NewException("sanlock.SanlockException", NULL, dict); - Py_DECREF(dict); +exit_fail: + Py_XDECREF(dict); + + return excp; }
PyMODINIT_FUNC initsanlock(void) { + PyObject *py_module;
py_module = Py_InitModule4("sanlock", sanlock_methods, pydoc_sanlock, NULL, PYTHON_API_VERSION);
- /* Python's module loader doesn't support clean recovery from errors */ if (py_module == NULL) return;
- /* Initializing sanlock exception */ - initexception(); + py_exception = initexception();
if (py_exception == NULL) return;
- Py_INCREF(py_exception); - PyModule_AddObject(py_module, "SanlockException", py_exception); + if (PyModule_AddObject(py_module, "SanlockException", py_exception) == 0) { + Py_INCREF(py_exception); + } } diff --git a/src/client.c b/src/client.c index 5b4d81b..723f491 100644 --- a/src/client.c +++ b/src/client.c @@ -543,7 +543,7 @@ int sanlock_restrict(int sock, uint32_t flags) return rv; }
-int sanlock_killpath(int sock, uint32_t flags, char *path, char *args) +int sanlock_killpath(int sock, uint32_t flags, const char *path, char *args) { char path_max[SANLK_HELPER_PATH_LEN]; char args_max[SANLK_HELPER_ARGS_LEN]; diff --git a/src/sanlock_resource.h b/src/sanlock_resource.h index 5215797..cf2a798 100644 --- a/src/sanlock_resource.h +++ b/src/sanlock_resource.h @@ -45,7 +45,7 @@ int sanlock_register(void);
int sanlock_restrict(int sock, uint32_t flags);
-int sanlock_killpath(int sock, uint32_t flags, char *path, char *args); +int sanlock_killpath(int sock, uint32_t flags, const char *path, char *args);
int sanlock_acquire(int sock, int pid, uint32_t flags, int res_count, struct sanlk_resource *res_args[],
Signed-off-by: Federico Simoncelli fsimonce@redhat.com --- python/sanlock.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 113 insertions(+), 1 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c index c0c0276..de91ff4 100644 --- a/python/sanlock.c +++ b/python/sanlock.c @@ -684,6 +684,104 @@ py_rem_lockspace(PyObject *self __unused, PyObject *args, PyObject *keywds) Py_RETURN_NONE; }
+/* get_lockspaces */ +PyDoc_STRVAR(pydoc_get_lockspaces, "\ +get_lockspaces() => dict\n\ +Return the list of lockspaces on which the host is currently renewing the\ +"); + +static PyObject * +py_get_lockspaces(PyObject *self __unused, PyObject *args, PyObject *keywds) +{ + int rv, i, lss_size = 0, lss_count; + struct sanlk_lockspace *lss = NULL; + PyObject *ls_list = NULL, *ls_entry = NULL, *ls_value = NULL; + + while (1) { + /* get all the lockspaces (gil disabled) */ + Py_BEGIN_ALLOW_THREADS + rv = sanlock_get_lockspaces(lss, lss_size, &lss_count, 0); + Py_END_ALLOW_THREADS + + if (rv == 0) + break; + + if (rv != -ENOBUFS) { + __set_exception(rv, "Sanlock lockspace remove failure"); + goto exit_fail; + } + + lss_size = (lss_count * sizeof(struct sanlk_lockspace)); + lss = realloc(lss, lss_size); + } + + /* prepare the dictionary holding the information */ + if ((ls_list = PyList_New(0)) == NULL) + goto exit_fail; + + for (i = 0; i < lss_count; i++) { + if ((ls_entry = PyDict_New()) == NULL) + goto exit_fail; + + /* fill the dictionary information: lockspace */ + if ((ls_value = PyString_FromString(lss[i].name)) == NULL) + goto exit_fail; + rv = PyDict_SetItemString(ls_entry, "lockspace", ls_value); + Py_DECREF(ls_value); + if (rv != 0) + goto exit_fail; + + /* fill the dictionary information: host_id */ + if ((ls_value = PyInt_FromLong(lss[i].host_id)) == NULL) + goto exit_fail; + rv = PyDict_SetItemString(ls_entry, "host_id", ls_value); + Py_DECREF(ls_value); + if (rv != 0) + goto exit_fail; + + /* fill the dictionary information: path */ + if ((ls_value = PyString_FromString(lss[i].host_id_disk.path)) == NULL) + goto exit_fail; + rv = PyDict_SetItemString(ls_entry, "path", ls_value); + Py_DECREF(ls_value); + if (rv != 0) + goto exit_fail; + + /* fill the dictionary information: offset */ + if ((ls_value = PyInt_FromLong(lss[i].host_id_disk.offset)) == NULL) + goto exit_fail; + rv = PyDict_SetItemString(ls_entry, "offset", ls_value); + Py_DECREF(ls_value); + if (rv != 0) + goto exit_fail; + + /* fill the dictionary information: flags */ + if ((ls_value = PyInt_FromLong(lss[i].flags)) == NULL) + goto exit_fail; + rv = PyDict_SetItemString(ls_entry, "flags", ls_value); + Py_DECREF(ls_value); + if (rv != 0) + goto exit_fail; + + if (PyList_Append(ls_list, ls_entry) != 0) + goto exit_fail; + + Py_DECREF(ls_entry); + } + + /* success */ + free(lss); + return ls_list; + + /* failure */ +exit_fail: + if (lss) free(lss); + Py_XDECREF(ls_entry); + Py_XDECREF(ls_list); + return NULL; +} + + /* acquire */ PyDoc_STRVAR(pydoc_acquire, "\ acquire(lockspace, resource, disks [, slkfd=fd, pid=owner, shared=False])\n\ @@ -921,6 +1019,8 @@ sanlock_methods[] = { METH_VARARGS|METH_KEYWORDS, pydoc_inq_lockspace}, {"rem_lockspace", (PyCFunction) py_rem_lockspace, METH_VARARGS|METH_KEYWORDS, pydoc_rem_lockspace}, + {"get_lockspaces", (PyCFunction) py_get_lockspaces, + METH_VARARGS|METH_KEYWORDS, pydoc_get_lockspaces}, {"acquire", (PyCFunction) py_acquire, METH_VARARGS|METH_KEYWORDS, pydoc_acquire}, {"release", (PyCFunction) py_release, @@ -968,7 +1068,7 @@ exit_fail: PyMODINIT_FUNC initsanlock(void) { - PyObject *py_module; + PyObject *py_module, *sk_constant;
py_module = Py_InitModule4("sanlock", sanlock_methods, pydoc_sanlock, NULL, PYTHON_API_VERSION); @@ -984,4 +1084,16 @@ initsanlock(void) if (PyModule_AddObject(py_module, "SanlockException", py_exception) == 0) { Py_INCREF(py_exception); } + + if ((sk_constant = PyInt_FromLong(SANLK_LSF_ADD)) != NULL) { + if (PyModule_AddObject(py_module, "SANLK_LSF_ADD", sk_constant)) { + Py_DECREF(sk_constant); + } + } + + if ((sk_constant = PyInt_FromLong(SANLK_LSF_REM)) != NULL) { + if (PyModule_AddObject(py_module, "SANLK_LSF_REM", sk_constant)) { + Py_DECREF(sk_constant); + } + } }
sanlock-devel@lists.fedorahosted.org