Ignore:
Timestamp:
23/08/06 15:20:05 (14 years ago)
Author:
spascoe
Message:

Implemented two hooks in the bbftpd source that allow
python code to be executed before and afer a fork(). This is needed
to reset python logging and will probably be usefull for other things
in the future.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI05-delivery/trunk/src/python_ext/bbftpd.c

    r1444 r1448  
    3737static PyObject *authzHandler = NULL; 
    3838static PyObject *serverLogger = NULL; 
     39static PyObject *bbftpdModule = NULL; 
    3940 
    4041 
     
    263264  return; 
    264265} 
     266 
     267/** 
     268 * Take action before a fork if pybbftp.before_fork_hook is callable. 
     269 * 
     270 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE 
     271 *     for storing an error message when returning NULL. 
     272 * @return 0 on success or -1 on failure. 
     273 *  
     274 */ 
     275int ndg_before_fork_hook(char *logmessage) { 
     276  PyObject *hook; 
     277 
     278  if ((hook = PyObject_GetAttrString(bbftpdModule, "before_fork_hook")) == NULL) { 
     279    PyErr_Clear(); 
     280    return 0; 
     281  } 
     282  Py_INCREF(hook); 
     283 
     284  if (!PyCallable_Check(hook)) return 0; 
     285 
     286  if (PyObject_CallObject(hook, NULL) == NULL) { 
     287    ndg_pyerr_to_logmessage(logmessage, "ndg_before_fork_hook"); 
     288    Py_DECREF(hook); 
     289    return -1; 
     290  } 
     291 
     292  Py_DECREF(hook); 
     293  return 0; 
     294} 
     295 
     296/** 
     297 * Take action after a fork if pybbftp.after_fork_hook is callable. 
     298 * 
     299 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE 
     300 *     for storing an error message when returning NULL. 
     301 * @return 0 on success or -1 on failure. 
     302 *  
     303 */ 
     304int ndg_after_fork_hook(char *logmessage) { 
     305  PyObject *hook; 
     306 
     307  if ((hook = PyObject_GetAttrString(bbftpdModule, "after_fork_hook")) == NULL) { 
     308    PyErr_Clear(); 
     309    return 0; 
     310  } 
     311  Py_INCREF(hook); 
     312 
     313  if (!PyCallable_Check(hook)) return 0; 
     314 
     315  if (PyObject_CallObject(hook, NULL) == NULL) { 
     316    ndg_pyerr_to_logmessage(logmessage, "ndg_after_fork_hook"); 
     317    Py_DECREF(hook); 
     318    return -1; 
     319  } 
     320 
     321  Py_DECREF(hook); 
     322  return 0; 
     323} 
     324 
    265325      
    266326/*------------------------------------------------------------------------------------------------- 
     
    456516 
    457517PyMODINIT_FUNC initbbftpd(void) { 
    458   PyObject *mod; 
    459  
    460   mod = Py_InitModule("bbftpd", BbftpdMethods); 
    461  
    462   ADDCONST(mod, MSG_CHDIR_V2); 
    463   ADDCONST(mod, MSG_CHDIR_V2); 
    464   ADDCONST(mod, MSG_LIST_V2); 
    465   ADDCONST(mod, MSG_MKDIR_V2); 
    466   ADDCONST(mod, MSG_RM); 
    467   ADDCONST(mod, MSG_STAT); 
    468   ADDCONST(mod, MSG_DF); 
    469    
    470   ADDCONST(mod, TROPT_TMP); 
    471   ADDCONST(mod, TROPT_ACC); 
    472   ADDCONST(mod, TROPT_MODE); 
    473   ADDCONST(mod, TROPT_DIR); 
    474   ADDCONST(mod, TROPT_GZIP); 
    475   ADDCONST(mod, TROPT_RFIO); 
    476   ADDCONST(mod, TROPT_RFIO_O); 
    477   ADDCONST(mod, TROPT_QBSS); 
    478 } 
    479  
     518  bbftpdModule = Py_InitModule("bbftpd", BbftpdMethods); 
     519  Py_INCREF(bbftpdModule); 
     520 
     521  ADDCONST(bbftpdModule, MSG_CHDIR_V2); 
     522  ADDCONST(bbftpdModule, MSG_CHDIR_V2); 
     523  ADDCONST(bbftpdModule, MSG_LIST_V2); 
     524  ADDCONST(bbftpdModule, MSG_MKDIR_V2); 
     525  ADDCONST(bbftpdModule, MSG_RM); 
     526  ADDCONST(bbftpdModule, MSG_STAT); 
     527  ADDCONST(bbftpdModule, MSG_DF); 
     528   
     529  ADDCONST(bbftpdModule, TROPT_TMP); 
     530  ADDCONST(bbftpdModule, TROPT_ACC); 
     531  ADDCONST(bbftpdModule, TROPT_MODE); 
     532  ADDCONST(bbftpdModule, TROPT_DIR); 
     533  ADDCONST(bbftpdModule, TROPT_GZIP); 
     534  ADDCONST(bbftpdModule, TROPT_RFIO); 
     535  ADDCONST(bbftpdModule, TROPT_RFIO_O); 
     536  ADDCONST(bbftpdModule, TROPT_QBSS); 
     537 
     538  /* after_fork_hook and before_fork_hook allows python to take action before and after bbftpd forks. 
     539     They is called from within the bbftpd source if they are callable.  By default this isn't the case. 
     540  */ 
     541  Py_INCREF(Py_None); /* PyModule_AddObject steels a reference. */ 
     542  PyModule_AddObject(bbftpdModule, "after_fork_hook", Py_None); 
     543  Py_INCREF(Py_None); 
     544  PyModule_AddObject(bbftpdModule, "before_fork_hook", Py_None); 
     545 
     546} 
     547 
Note: See TracChangeset for help on using the changeset viewer.