Exemplo n.º 1
0
void bot_start(void)
{
    /* Create the server tree */
    ServerTree = BalancedBTreeCreate( BTREE_KEY_INT );

    versionAdd( "botnet", BN_GetVersion() );

    BalancedBTreeLock( ServerTree );

    /* Read the list of servers */
    cursesMenuItemAdd( 2, MENU_SERVERS, "New Server", cursesServerNew, NULL );
    db_load_servers();

    /* Read the list of channels */
    cursesMenuItemAdd( 2, MENU_CHANNELS, "New Channel", cursesChannelNew, 
                       NULL );

    db_load_channels();

    ChannelsLoaded = TRUE;

    serverStartTree( ServerTree->root );

    BalancedBTreeUnlock( ServerTree );
}
Exemplo n.º 2
0
void plugins_initialize( void )
{
    static char        *command = "plugin";
    LinkedList_t       *list;

    extBlock = ProtectedDataCreate();
    if( !extBlock ) {
        LogPrintNoArg( LOG_CRIT, "No memory to create plugin extension "
                                 "protected structure!!");
        exit(1);
    }

    extBlock->data = NULL;

    pluginTree = BalancedBTreeCreate( BTREE_KEY_STRING );
    if( !pluginTree ) {
        return;
    }

    list = pluginFindPlugins( "plugin_", ".so" );
    db_check_plugins( list );
    LinkedListDestroy( list );

    LogPrint( LOG_NOTICE, "Plugin path: %s", PLUGIN_PATH );

    BalancedBTreeLock( pluginTree );

    db_get_plugins( pluginTree );
    pluginInitializeTree( pluginTree->root );

    BalancedBTreeUnlock( pluginTree );

    botCmd_add( (const char **)&command, botCmdPlugin, NULL, NULL );
}
Exemplo n.º 3
0
void thread_register( pthread_t *pthreadId, char *name, 
                      ThreadCallback_t *callbacks )
{
    BalancedBTreeItem_t    *item;
    Thread_t               *thread;

    if( !ThreadTree ) {
        ThreadTree = BalancedBTreeCreate( NULL, BTREE_KEY_PTHREAD );
        if( !ThreadTree ) {
            fprintf( stderr, "Couldn't create thread tree!\n" );
            _exit( 1 );
        }
    }

    thread = CREATE(Thread_t);
    thread->threadId   = pthreadId;
    thread->name       = name;
    if( callbacks ) {
        thread->callbacks  = CREATE(ThreadCallback_t);
        memcpy(thread->callbacks, callbacks, sizeof(ThreadCallback_t));
    }
    threadCount++;
    thread->background = (threadCount - 1) / 15;
    thread->foreground = (threadCount - 1) % 15;
    if( thread->foreground >= thread->background) {
        thread->foreground++;
    }

    item = CREATE(BalancedBTreeItem_t);
    item->item = (void *)thread;
    item->key  = (void *)thread->threadId;

    BalancedBTreeAdd( ThreadTree, item, UNLOCKED, TRUE );
    LogPrint( LOG_INFO, "Added thread as \"%s%s%s%s%s\" (%d/%d)", 
              backgroundColors[thread->background], 
              foregroundColors[thread->foreground], name,
              backgroundColors[0], foregroundColors[1],
              thread->background, thread->foreground );
}
Exemplo n.º 4
0
int main ( int argc, char **argv )
{
    pthread_mutex_t     spinLockMutex;
    pid_t               childPid;
    struct sigaction    sa;
    sigset_t            sigmsk;
    size_t              len;
    ThreadCallback_t    callbacks;

    GlobalAbort = false;

    /* Parse the command line options */
    MainParseArgs( argc, argv );

#ifndef __CYGWIN__
    len = confstr( _CS_GNU_LIBPTHREAD_VERSION, NULL, 0 );
    if( len ) {
        pthreadsVersion = (char *)malloc(len);
        confstr( _CS_GNU_LIBPTHREAD_VERSION, pthreadsVersion, len );
    }

    if( !pthreadsVersion || strstr( pthreadsVersion, "linuxthreads" ) ) {
        fprintf( stderr, "beirdobot requires NPTL to operate correctly.\n\n"
                         "The signal handling in linuxthreads is just too "
                         "broken to use.\n\n" );
        exit( 1 );
    }
#else
    len = 0;
#endif

    /* Do we need to detach? */
    if( Daemon ) {
        childPid = fork();
        if( childPid < 0 ) {
            perror( "Couldn't detach in daemon mode" );
            _exit( 1 );
        }

        if( childPid != 0 ) {
            /* This is still the parent, report the child's pid and exit */
            printf( "[Detached as PID %d]\n", childPid );
            /* And exit the parent */
            _exit( 0 );
        }

        /* After this is in the detached child */

        /* Close stdin, stdout, stderr to release the tty */
        close(0);
        close(1);
        close(2);
    }

    mainThreadId = pthread_self();

    /* 
     * Setup the sigmasks for this thread (which is the parent to all others).
     * This will propogate to all children.
     */
    sigfillset( &sigmsk );
    sigdelset( &sigmsk, SIGUSR1 );
    sigdelset( &sigmsk, SIGUSR2 );
    sigdelset( &sigmsk, SIGHUP );
    sigdelset( &sigmsk, SIGWINCH );
    sigdelset( &sigmsk, SIGINT );
    sigdelset( &sigmsk, SIGSEGV );
    sigdelset( &sigmsk, SIGILL );
    sigdelset( &sigmsk, SIGFPE );
    pthread_sigmask( SIG_SETMASK, &sigmsk, NULL );

    /* Initialize the non-threadsafe CURL library functionality */
    curl_global_init( CURL_GLOBAL_ALL );

    /* Start up the Logging thread */
    logging_initialize(TRUE);

    memset( &callbacks, 0, sizeof(ThreadCallback_t) );
    callbacks.sighupFunc = mainSighup;
    thread_register( &mainThreadId, "thread_main", &callbacks );

    /* Setup signal handler for SIGUSR1 (toggles Debug) */
    sa.sa_sigaction = (sigAction_t)logging_toggle_debug;
    sigemptyset( &sa.sa_mask );
    sa.sa_flags = SA_RESTART;
    sigaction( SIGUSR1, &sa, NULL );

    /* Setup the exit handler */
    atexit( MainDelayExit );

    /* Setup signal handler for SIGINT (shut down cleanly) */
    sa.sa_sigaction = signal_interrupt;
    sigemptyset( &sa.sa_mask );
    sa.sa_flags = SA_RESTART;
    sigaction( SIGINT, &sa, NULL );
    
    /* Setup signal handlers that are to be propogated to all threads */
    sa.sa_sigaction = signal_everyone;
    sigemptyset( &sa.sa_mask );
    sa.sa_flags = SA_RESTART | SA_SIGINFO;
    sigaction( SIGUSR2, &sa, NULL );
    sigaction( SIGHUP, &sa, NULL );
    sigaction( SIGWINCH, &sa, NULL );

    /* Setup signal handlers for SEGV, ILL, FPE */
    sa.sa_sigaction = signal_death;
    sigemptyset( &sa.sa_mask );
    sa.sa_flags = SA_RESTART | SA_SIGINFO;
    sigaction( SIGSEGV, &sa, NULL );
    sigaction( SIGILL, &sa, NULL );
    sigaction( SIGFPE, &sa, NULL );

    versionTree = BalancedBTreeCreate( BTREE_KEY_STRING );

#ifndef __CYGWIN__
    versionAdd( "pthreads", pthreadsVersion );
#endif

    curses_start();
    cursesMenuItemAdd( 2, MENU_SYSTEM, "About", mainAbout, NULL );
    cursesMenuItemAdd( 2, MENU_SYSTEM, "Licensing", mainLicensing, NULL );
    cursesMenuItemAdd( 2, MENU_SYSTEM, "Versions", mainVersions, NULL );
    cursesMenuItemAdd( 2, MENU_SYSTEM, "Reload All", mainReloadAll, NULL );

    /* Add the terminal setting as a version */
    versionAdd( "TERM", getenv("TERM") );

    /* Print the startup log messages */
    LogBanner();

    LogPrint( LOG_INFO, "CFLAGS: %s", CFLAGS );
    LogPrint( LOG_INFO, "LDFLAGS: %s", LDFLAGS );

    /* Setup the CLucene indexer */
    clucene_init(0);

    /* Setup the MySQL connection */
    db_setup();
    db_check_schema_main();

    /* Setup the bot commands */
    botCmd_initialize();

    /* Setup the regexp support */
    regexp_initialize();

    /* Setup the plugins */
    plugins_initialize();

    /* Start the notifier thread */
    notify_start();

    /* Start the authenticate thread */
    authenticate_start();

    /* Start the bot */
    bot_start();

    /* Sit on this and rotate - this causes an intentional deadlock, this
     * thread should stop dead in its tracks
     */
    pthread_mutex_init( &spinLockMutex, NULL );
    pthread_mutex_lock( &spinLockMutex );
    pthread_mutex_lock( &spinLockMutex );

    return(0);
}