$search
00001 /* 00002 * GPLv2 License. Modified from sigmask.c in qmtest. 00003 */ 00004 00005 /****************************************************************** 00006 * 00007 * File: sigblock.c 00008 * Author: Nathaniel Smith 00009 * Date: 2004-05-01 00010 * 00011 * Contents: 00012 * Simple Python support for saving and restoring the signal mask. 00013 * 00014 * Copyright (c) 2004 by CodeSourcery, LLC. All rights reserved. 00015 * 00016 * For license terms see the file COPYING. 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 /* We take no arguments. */ 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 /* We take no arguments. */ 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 /* We take no arguments. */ 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} /* Sentinel */ 00104 }; 00105 00106 00107 #ifndef PyMODINIT_FUNC /* Declarations for DLL import/export */ 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