00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <Python.h>
00021 #include <signal.h>
00022
00023 static PyObject* SigblockError;
00024
00025 static int the_mask_is_set;
00026 static sigset_t the_mask;
00027
00028 static PyObject *
00029 save_mask(PyObject* self, PyObject* args)
00030 {
00031
00032 if (!PyArg_ParseTuple(args, "")) return NULL;
00033
00034 if (sigprocmask(SIG_BLOCK, NULL, &the_mask) == -1)
00035 {
00036 PyErr_SetString(SigblockError, "Error fetching mask");
00037 return NULL;
00038 }
00039
00040 the_mask_is_set = 1;
00041
00042 Py_INCREF(Py_None);
00043 return Py_None;
00044 }
00045
00046
00047 static PyObject *
00048 restore_mask(PyObject* self, PyObject* args)
00049 {
00050
00051 if (!PyArg_ParseTuple(args, "")) return NULL;
00052
00053 if (!the_mask_is_set)
00054 {
00055 PyErr_SetString(SigblockError,
00056 "Must call save_mask before restore_mask");
00057 return NULL;
00058 }
00059
00060 if (sigprocmask(SIG_SETMASK, &the_mask, NULL) == -1)
00061 {
00062 PyErr_SetString(SigblockError, "Error setting mask");
00063 return NULL;
00064 }
00065
00066 Py_INCREF(Py_None);
00067 return Py_None;
00068 }
00069
00070
00071 static PyObject *
00072 block_signal(PyObject* self, PyObject* args)
00073 {
00074 int sig;
00075
00076
00077 if (!PyArg_ParseTuple(args, "i", &sig))
00078 return NULL;
00079
00080 sigset_t mask;
00081 sigemptyset (&mask);
00082 sigaddset(&mask, sig);
00083
00084 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
00085 {
00086 PyErr_SetString(SigblockError, "Error setting mask");
00087 return NULL;
00088 }
00089
00090 Py_INCREF(Py_None);
00091 return Py_None;
00092 }
00093
00094
00095 static PyMethodDef module_methods[] =
00096 {
00097 {"save_mask", save_mask, METH_VARARGS,
00098 "Saves the current signal mask internally."},
00099 {"restore_mask", restore_mask, METH_VARARGS,
00100 "Sets the current signal mask to match that of the last call to save_mask."},
00101 {"block_signal", block_signal, METH_VARARGS,
00102 "Remove specified signal from the signal mask."},
00103 {NULL, NULL, 0, NULL}
00104 };
00105
00106
00107 #ifndef PyMODINIT_FUNC
00108 #define PyMODINIT_FUNC void
00109 #endif
00110 PyMODINIT_FUNC
00111 initsigblock(void)
00112 {
00113 PyObject* m;
00114
00115 m = Py_InitModule3("sigblock", module_methods,
00116 "Module to save/restore signal mask.");
00117
00118 the_mask_is_set = 0;
00119
00120 SigblockError = PyErr_NewException("sigblock.SigblockError", NULL, NULL);
00121 PyModule_AddObject(m, "SigblockError", SigblockError);
00122 }
00123