Changeset 1448 for TI05-delivery


Ignore:
Timestamp:
23/08/06 15:20:05 (13 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.

Location:
TI05-delivery/trunk
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • TI05-delivery/trunk/examples/deliveryd.py

    r1447 r1448  
    1818import logging 
    1919logging.basicConfig(level=logging.DEBUG, filename='./bbftpd.log') 
     20logger = logging.getLogger('ndg.delivery.server') 
    2021 
    2122import ndg.delivery.server.pybbftp as server 
     
    2627    def authenticate(self): 
    2728        msg = self.recv() 
    28         syslog.syslog(syslog.LOG_DEBUG, 'AuthContext received Auth message: %s' % msg) 
     29        logger.debug('AuthContext received Auth message: %s' % msg) 
    2930                 
    3031        self.send(NDG_HANDSHAKE) 
    3132 
    3233        privatestr = self.recv() 
    33         syslog.syslog(syslog.LOG_DEBUG, "AuthContext received privatestr: %s" % privatestr) 
     34        logger.debug("AuthContext received privatestr: %s" % privatestr) 
    3435         
    3536        return self.makeAuthzHandler(msg, "TestCaseUser") 
     
    4445 
    4546    def authzControl(self, msgcode, transferopt, path): 
    46         syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler.authzControl: msgcode = %s' % hex(msgcode)) 
     47        logger.debug('AuthzHandler.authzControl: msgcode = %s' % hex(msgcode)) 
    4748        return self.authzPath(path) 
    4849 
     
    5960        mode = stat.S_IMODE(os.stat(path)[stat.ST_MODE]) 
    6061        if (mode & 0004): 
    61             syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler OK') 
     62            logger.debug('AuthzHandler OK') 
    6263            return True 
    6364        else: 
    64             syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler FAIL') 
     65            logger.debug('AuthzHandler FAIL') 
    6566            raise server.AuthorisationFailure, "AuthzHandler: no read perms" 
    6667 
    67  
    68 syslog.openlog('test_embedded.py', 0, syslog.LOG_LOCAL0) 
    6968 
    7069print ''' 
     
    7372=================================== 
    7473''' 
     74 
     75def after_fork_hook(): 
     76    logging.root.handlers = [] 
     77    logging.basicConfig(level=logging.DEBUG, filename='./bbftpd.log') 
     78    logger.info('Called after_fork_hook') 
    7579 
    7680logfile = './bbftpd.log' 
  • TI05-delivery/trunk/setup.py

    r1443 r1448  
    4444    'bbftpd_private.c', 
    4545    'bbftpd_private_user.c', 
    46     'bbftpd_private_log.c' 
     46    'bbftpd_private_log.c', 
     47    'bbftpd_private_fork.c' 
    4748    ] 
    4849 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_daemon.c

    r1358 r1448  
    4949#include <stdio.h> 
    5050#include <bbftpd_private_log.h> 
     51#include <bbftpd_private_fork.h> 
    5152#include <sys/socket.h> 
    5253#if TIME_WITH_SYS_TIME 
     
    132133    } 
    133134    /* Fork - so I'm not the owner of the process group any more */ 
    134     retcode = fork(); 
     135    retcode = bbftpd_fork(); 
    135136    if (retcode < 0) { 
    136137        bbftpd_log(BBFTPD_ERR, "Cannot fork %s",strerror(errno)); 
     
    208209        } 
    209210            /* Fork off a handler */ 
    210         pid = fork(); 
     211        pid = bbftpd_fork(); 
    211212        if (pid < 0) { 
    212213            bbftpd_log(BBFTPD_ERR, "failed to fork: %s",strerror(errno)); 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_retr.c

    r1392 r1448  
    4444#include <signal.h> 
    4545#include <bbftpd_private_log.h> 
     46#include <bbftpd_private_fork.h> 
    4647#include <sys/stat.h> 
    4748#include <sys/types.h> 
     
    659660        ** So we are going to fork 
    660661        */ 
    661         if ( (retcode = fork()) == 0 ) { 
     662        if ( (retcode = bbftpd_fork()) == 0 ) { 
    662663                    int     ns ; 
    663664            /* 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_retr_rfio.c

    r1358 r1448  
    4141#include <signal.h> 
    4242#include <bbftpd_private_log.h> 
     43#include <bbftpd_private_fork.h> 
    4344#include <sys/stat.h> 
    4445#if TIME_WITH_SYS_TIME 
     
    715716        ** So we are going to fork 
    716717        */ 
    717         if ( (retcode = fork()) == 0 ) { 
     718        if ( (retcode = bbftpd_fork()) == 0 ) { 
    718719                    int     ns ; 
    719720            /* 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_store.c

    r1392 r1448  
    4343#include <signal.h> 
    4444#include <bbftpd_private_log.h> 
     45#include <bbftpd_private_fork.h> 
    4546#include <sys/stat.h> 
    4647#if TIME_WITH_SYS_TIME 
     
    940941        */ 
    941942        flagsighup = 0 ; 
    942         if ( ( retcode = fork() ) == 0 ) { 
     943        if ( ( retcode = bbftpd_fork() ) == 0 ) { 
    943944            int     ns ; 
    944945            /* 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_store_rfio.c

    r1358 r1448  
    4343#include <signal.h> 
    4444#include <bbftpd_private_log.h> 
     45#include <bbftpd_private_fork.h> 
    4546#include <sys/stat.h> 
    4647#if TIME_WITH_SYS_TIME 
     
    13101311        */ 
    13111312        flagsighup = 0 ; 
    1312         if ( ( retcode = fork() ) == 0 ) { 
     1313        if ( ( retcode = bbftpd_fork() ) == 0 ) { 
    13131314            int     ns ; 
    13141315            /* 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/sendafile.c

    r1395 r1448  
    7373#include <signal.h> 
    7474#include <bbftpd_private_log.h> 
     75#include <bbftpd_private_fork.h> 
    7576#include <sys/stat.h> 
    7677#include <sys/time.h> 
     
    403404        ** So we are going to fork 
    404405        */ 
    405         if ( (retcode = fork()) == 0 ) { 
     406        if ( (retcode = bbftpd_fork()) == 0 ) { 
    406407            /* 
    407408            ** We are in child 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/sendafilerfio.c

    r1431 r1448  
    7070#include <signal.h> 
    7171#include <bbftpd_private_log.h> 
     72#include <bbftpd_private_fork.h> 
    7273#include <sys/stat.h> 
    7374#include <sys/time.h> 
     
    380381        ** So we are going to fork 
    381382        */ 
    382         if ( (retcode = fork()) == 0 ) { 
     383        if ( (retcode = bbftpd_fork()) == 0 ) { 
    383384            /* 
    384385            ** We are in child 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/storeafile.c

    r1395 r1448  
    7474#include <stdio.h> 
    7575#include <bbftpd_private_log.h> 
     76#include <bbftpd_private_fork.h> 
    7677#include <sys/stat.h> 
    7778#include <sys/time.h> 
     
    380381        ** So we are going to fork 
    381382        */ 
    382         if ( (retcode = fork()) == 0 ) { 
     383        if ( (retcode = bbftpd_fork()) == 0 ) { 
    383384            /* 
    384385            ** We are in child 
  • TI05-delivery/trunk/src/bbftp-server-3.2.0/includes/ndg.h

    r1447 r1448  
    3838int ndg_authz_store(char *path, char *logmessage); 
    3939void ndg_log(int priority, const char *format, va_list vargs); 
    40  
     40int ndg_before_fork_hook(char *logmessage); 
     41int ndg_after_fork_hook(char *logmessage); 
    4142 
    4243#endif // NDG_PYTHON_EMBED 
  • 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.