#include #include #include #include #include #include #include #include #include #include #include "alarmd_proto.h" #include "pyalarmd_proto.h" static PyMethodDef ExportedMethods[] = { {"connect" , pyalarmd_connect , METH_VARARGS, "Open a connection to the named socket at the given path, returning the fd associated with it."}, {"disconnect" , pyalarmd_disconnect, METH_VARARGS, "Close a socket fd previously returned by connect(path)."}, {"register_alarm" , pyalarmd_register , METH_VARARGS, "Send a request over the given socket to register an alarm with the given name. Returns the UUID of the new alarm as a string"}, {"deregister_alarm", pyalarmd_deregister, METH_VARARGS, "Send a request over the given socket to deregister an alarm with the given UUID."}, {"raise_alarm" , pyalarmd_raise , METH_VARARGS, "Send a request over the given socket to raise the alarm with the given UUID."}, {"clear_alarm" , pyalarmd_clear , METH_VARARGS, "Send a request over the given socket to clear/lower the alarm with the given UUID."}, {NULL , NULL , 0 , NULL } }; static struct PyModuleDef alarm_python = { PyModuleDef_HEAD_INIT, "alarms", /* module name */ NULL, -1, /* storage size -1 since state kept in global variables. */ ExportedMethods }; static PyObject *alarmd_error; PyObject* PyInit_alarms(void) { PyObject *module = NULL; module = PyModule_Create(&alarm_python); alarmd_error = PyErr_NewException("alarms.error", NULL, NULL); PyModule_AddObject(module, "error", alarmd_error); return module; } PyObject* pyalarmd_connect(PyObject *self, PyObject *args) { const char *path = NULL; int sock = 0; struct sockaddr_un server; if (!PyArg_ParseTuple(args, "s", &path)) { return NULL; } if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { PyErr_SetFromErrno(alarmd_error); return NULL; } server.sun_family = AF_UNIX; strcpy(server.sun_path, path); if (connect(sock, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof(server.sun_family)) < 0) { close(sock); PyErr_SetFromErrno(alarmd_error); return NULL; } return PyLong_FromLong(sock); } PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args) { int sock = 0; if (!PyArg_ParseTuple(args, "i", &sock)) { return NULL; } close(sock); Py_RETURN_NONE; } PyObject* pyalarmd_register(PyObject *self, PyObject *args) { int sock = 0; char *desc; char uuid_str[37]; uuid_t uuid; if (!PyArg_ParseTuple(args, "is", &sock, &desc)) { return NULL; } if (alarmd_register(sock, desc, &uuid)) { PyErr_SetFromErrno(alarmd_error); return NULL; } uuid_unparse(uuid, uuid_str); return Py_BuildValue("s", uuid_str); } PyObject* pyalarmd_deregister(PyObject *self, PyObject *args) { int sock = 0; char *uuid_str = NULL; uuid_t uuid; if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { return NULL; } if (uuid_parse(uuid_str, uuid) < 0) { PyErr_SetString(alarmd_error, "Invalid UUID"); return NULL; } if (alarmd_deregister(sock, uuid)) { PyErr_SetFromErrno(alarmd_error); return NULL; } Py_RETURN_NONE; } PyObject* pyalarmd_raise(PyObject *self, PyObject *args) { int sock = 0; char *uuid_str = NULL; uuid_t uuid; if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { return NULL; } if (uuid_parse(uuid_str, uuid) < 0) { PyErr_SetString(alarmd_error, "Invalid UUID"); return NULL; } if (alarmd_raise(sock, uuid)) { PyErr_SetFromErrno(alarmd_error); return NULL; } Py_RETURN_NONE; } PyObject* pyalarmd_clear(PyObject *self, PyObject *args) { int sock = 0; char *uuid_str = NULL; uuid_t uuid; if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { return NULL; } if (uuid_parse(uuid_str, uuid) < 0) { PyErr_SetString(alarmd_error, "Invalid UUID"); return NULL; } if (alarmd_clear(sock, uuid)) { PyErr_SetFromErrno(alarmd_error); return NULL; } Py_RETURN_NONE; }