[PATCHv2] python: add the support for request

Federico Simoncelli fsimonce at redhat.com
Fri May 3 16:03:48 UTC 2013


Signed-off-by: Federico Simoncelli <fsimonce at redhat.com>
---
 python/sanlock.c |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/python/sanlock.c b/python/sanlock.c
index e1e0397..64a95d7 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -907,6 +907,64 @@ exit_fail:
     return NULL;
 }
 
+/* request */
+PyDoc_STRVAR(pydoc_request, "\
+request(lockspace, resource, disks [, action=REQ_RELEASE, version=0])\n\
+Request the owner of a resource to do something specified by action.\n\
+The possible values for action are: REQ_RELEASE to request a graceful\n\
+release of the resource and REQ_SIGKILL to sigkill the owner of the\n\
+resource (forcible release).\n\
+The disks must be in the format: [(path, offset), ... ]");
+static PyObject *
+py_request(PyObject *self __unused, PyObject *args, PyObject *keywds)
+{
+    int rv, action = SANLK_REQ_GRACEFUL, version = 0;
+    const char *lockspace, *resource;
+    struct sanlk_resource *res;
+    PyObject *disks;
+
+    static char *kwlist[] = {"lockspace", "resource", "disks", "action",
+                                "version", NULL};
+
+    /* parse python tuple */
+    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|ii", kwlist,
+        &lockspace, &resource, &PyList_Type, &disks, &action, &version)) {
+        return NULL;
+    }
+
+    /* parse and check sanlock resource */
+    if (__parse_resource(disks, &res) < 0) {
+        return NULL;
+    }
+
+    /* prepare sanlock names */
+    strncpy(res->lockspace_name, lockspace, SANLK_NAME_LEN);
+    strncpy(res->name, resource, SANLK_NAME_LEN);
+
+    /* prepare the resource version */
+    if (version) {
+        res->flags |= SANLK_RES_LVER;
+        res->lver = version;
+    }
+
+    /* request sanlock resource (gil disabled) */
+    Py_BEGIN_ALLOW_THREADS
+    rv = sanlock_request(0, action, res);
+    Py_END_ALLOW_THREADS
+
+    if (rv != 0) {
+        __set_exception(rv, "Sanlock request not submitted");
+        goto exit_fail;
+    }
+
+    free(res);
+    Py_RETURN_NONE;
+
+exit_fail:
+    free(res);
+    return NULL;
+}
+
 /* killpath */
 PyDoc_STRVAR(pydoc_killpath, "\
 killpath(path, args [, slkfd=fd])\n\
@@ -1035,6 +1093,8 @@ sanlock_methods[] = {
                 METH_VARARGS|METH_KEYWORDS, pydoc_acquire},
     {"release", (PyCFunction) py_release,
                 METH_VARARGS|METH_KEYWORDS, pydoc_release},
+    {"request", (PyCFunction) py_request,
+                METH_VARARGS|METH_KEYWORDS, pydoc_request},
     {"killpath", (PyCFunction) py_killpath,
                 METH_VARARGS|METH_KEYWORDS, pydoc_killpath},
     {NULL, NULL, 0, NULL}
@@ -1106,4 +1166,16 @@ initsanlock(void)
             Py_DECREF(sk_constant);
         }
     }
+
+    if ((sk_constant = PyInt_FromLong(SANLK_REQ_FORCE)) != NULL) {
+        if (PyModule_AddObject(py_module, "REQ_FORCE", sk_constant)) {
+            Py_DECREF(sk_constant);
+        }
+    }
+
+    if ((sk_constant = PyInt_FromLong(SANLK_REQ_GRACEFUL)) != NULL) {
+        if (PyModule_AddObject(py_module, "REQ_GRACEFUL", sk_constant)) {
+            Py_DECREF(sk_constant);
+        }
+    }
 }
-- 
1.7.1



More information about the sanlock-devel mailing list