Changeset 6738a67 for ipc.c


Ignore:
Timestamp:
2008-07-16T23:22:52Z (16 years ago)
Author:
Sven Moritz Hallberg <pesco@…>
Branches:
master
Children:
9b55485
Parents:
9730d72 (diff), 6a78c0e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge in latest trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ipc.c

    r9730d72 r6738a67  
    3333
    3434GSList *child_list = NULL;
    35 static char *statefile = NULL;
    3635
    3736static void ipc_master_cmd_client( irc_t *data, char **cmd )
     
    6362}
    6463
     64static void ipc_master_cmd_deaf( irc_t *data, char **cmd )
     65{
     66        if( global.conf->runmode == RUNMODE_DAEMON )
     67        {
     68                b_event_remove( global.listen_watch_source_id );
     69                close( global.listen_socket );
     70               
     71                global.listen_socket = global.listen_watch_source_id = -1;
     72       
     73                ipc_to_children_str( "OPERMSG :Closed listening socket, waiting "
     74                                     "for all users to disconnect." );
     75        }
     76        else
     77        {
     78                ipc_to_children_str( "OPERMSG :The DEAF command only works in "
     79                                     "normal daemon mode. Try DIE instead." );
     80        }
     81}
     82
    6583void ipc_master_cmd_rehash( irc_t *data, char **cmd )
    6684{
     
    98116        { "hello",      0, ipc_master_cmd_client,     0 },
    99117        { "die",        0, ipc_master_cmd_die,        0 },
     118        { "deaf",       0, ipc_master_cmd_deaf,       0 },
    100119        { "wallops",    1, NULL,                      IPC_CMD_TO_CHILDREN },
    101120        { "wall",       1, NULL,                      IPC_CMD_TO_CHILDREN },
     
    209228}
    210229
     230/* Return just one line. Returns NULL if something broke, an empty string
     231   on temporary "errors" (EAGAIN and friends). */
    211232static char *ipc_readline( int fd )
    212233{
    213         char *buf, *eol;
     234        char buf[513], *eol;
    214235        int size;
    215        
    216         buf = g_new0( char, 513 );
    217236       
    218237        /* Because this is internal communication, it should be pretty safe
     
    221240           sockets and limites message length, messages should always be
    222241           complete. Saves us quite a lot of code and buffering. */
    223         size = recv( fd, buf, 512, MSG_PEEK );
     242        size = recv( fd, buf, sizeof( buf ) - 1, MSG_PEEK );
    224243        if( size == 0 || ( size < 0 && !sockerr_again() ) )
    225244                return NULL;
     
    229248                buf[size] = 0;
    230249       
    231         eol = strstr( buf, "\r\n" );
    232         if( eol == NULL )
     250        if( ( eol = strstr( buf, "\r\n" ) ) == NULL )
    233251                return NULL;
    234252        else
    235253                size = eol - buf + 2;
    236        
    237         g_free( buf );
    238         buf = g_new0( char, size + 1 );
    239254       
    240255        if( recv( fd, buf, size, 0 ) != size )
    241256                return NULL;
    242257        else
    243                 buf[size-2] = 0;
    244        
    245         return buf;
     258                return g_strndup( buf, size - 2 );
    246259}
    247260
     
    254267                cmd = irc_parse_line( buf );
    255268                if( cmd )
     269                {
    256270                        ipc_command_exec( data, cmd, ipc_master_commands );
    257         }
    258         else
     271                        g_free( cmd );
     272                }
     273                g_free( buf );
     274        }
     275        else
     276        {
     277                ipc_master_free_fd( source );
     278        }
     279       
     280        return TRUE;
     281}
     282
     283gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond )
     284{
     285        char *buf, **cmd;
     286       
     287        if( ( buf = ipc_readline( source ) ) )
     288        {
     289                cmd = irc_parse_line( buf );
     290                if( cmd )
     291                {
     292                        ipc_command_exec( data, cmd, ipc_child_commands );
     293                        g_free( cmd );
     294                }
     295                g_free( buf );
     296        }
     297        else
     298        {
     299                ipc_child_disable();
     300        }
     301       
     302        return TRUE;
     303}
     304
     305void ipc_to_master( char **cmd )
     306{
     307        if( global.conf->runmode == RUNMODE_FORKDAEMON )
     308        {
     309                char *s = irc_build_line( cmd );
     310                ipc_to_master_str( "%s", s );
     311                g_free( s );
     312        }
     313        else if( global.conf->runmode == RUNMODE_DAEMON )
     314        {
     315                ipc_command_exec( NULL, cmd, ipc_master_commands );
     316        }
     317}
     318
     319void ipc_to_master_str( char *format, ... )
     320{
     321        char *msg_buf;
     322        va_list params;
     323
     324        va_start( params, format );
     325        msg_buf = g_strdup_vprintf( format, params );
     326        va_end( params );
     327       
     328        if( strlen( msg_buf ) > 512 )
     329        {
     330                /* Don't send it, it's too long... */
     331        }
     332        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
     333        {
     334                if( global.listen_socket >= 0 )
     335                        if( write( global.listen_socket, msg_buf, strlen( msg_buf ) ) <= 0 )
     336                                ipc_child_disable();
     337        }
     338        else if( global.conf->runmode == RUNMODE_DAEMON )
     339        {
     340                char **cmd, *s;
     341               
     342                if( ( s = strchr( msg_buf, '\r' ) ) )
     343                        *s = 0;
     344               
     345                cmd = irc_parse_line( msg_buf );
     346                ipc_command_exec( NULL, cmd, ipc_master_commands );
     347                g_free( cmd );
     348        }
     349       
     350        g_free( msg_buf );
     351}
     352
     353void ipc_to_children( char **cmd )
     354{
     355        if( global.conf->runmode == RUNMODE_FORKDAEMON )
     356        {
     357                char *msg_buf = irc_build_line( cmd );
     358                ipc_to_children_str( "%s", msg_buf );
     359                g_free( msg_buf );
     360        }
     361        else if( global.conf->runmode == RUNMODE_DAEMON )
    259362        {
    260363                GSList *l;
    261                 struct bitlbee_child *c;
    262                
    263                 for( l = child_list; l; l = l->next )
     364               
     365                for( l = irc_connection_list; l; l = l->next )
     366                        ipc_command_exec( l->data, cmd, ipc_child_commands );
     367        }
     368}
     369
     370void ipc_to_children_str( char *format, ... )
     371{
     372        char *msg_buf;
     373        va_list params;
     374
     375        va_start( params, format );
     376        msg_buf = g_strdup_vprintf( format, params );
     377        va_end( params );
     378       
     379        if( strlen( msg_buf ) > 512 )
     380        {
     381                /* Don't send it, it's too long... */
     382        }
     383        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
     384        {
     385                int msg_len = strlen( msg_buf );
     386                GSList *l, *next;
     387               
     388                for( l = child_list; l; l = next )
    264389                {
    265                         c = l->data;
    266                         if( c->ipc_fd == source )
     390                        struct bitlbee_child *c = l->data;
     391                       
     392                        next = l->next;
     393                        if( write( c->ipc_fd, msg_buf, msg_len ) <= 0 )
    267394                        {
    268395                                ipc_master_free_one( c );
    269396                                child_list = g_slist_remove( child_list, c );
    270                                 break;
    271397                        }
    272                 }
    273         }
    274        
    275         return TRUE;
    276 }
    277 
    278 gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond )
    279 {
    280         char *buf, **cmd;
    281        
    282         if( ( buf = ipc_readline( source ) ) )
    283         {
    284                 cmd = irc_parse_line( buf );
    285                 if( cmd )
    286                         ipc_command_exec( data, cmd, ipc_child_commands );
    287         }
    288         else
    289         {
    290                 b_event_remove( global.listen_watch_source_id );
    291                 close( global.listen_socket );
    292                
    293                 global.listen_socket = -1;
    294         }
    295        
    296         return TRUE;
    297 }
    298 
    299 void ipc_to_master( char **cmd )
    300 {
    301         if( global.conf->runmode == RUNMODE_FORKDAEMON )
    302         {
    303                 char *s = irc_build_line( cmd );
    304                 ipc_to_master_str( "%s", s );
    305                 g_free( s );
    306         }
    307         else if( global.conf->runmode == RUNMODE_DAEMON )
    308         {
    309                 ipc_command_exec( NULL, cmd, ipc_master_commands );
    310         }
    311 }
    312 
    313 void ipc_to_master_str( char *format, ... )
    314 {
    315         char *msg_buf;
    316         va_list params;
    317 
    318         va_start( params, format );
    319         msg_buf = g_strdup_vprintf( format, params );
    320         va_end( params );
    321        
    322         if( strlen( msg_buf ) > 512 )
    323         {
    324                 /* Don't send it, it's too long... */
    325         }
    326         else if( global.conf->runmode == RUNMODE_FORKDAEMON )
    327         {
    328                 write( global.listen_socket, msg_buf, strlen( msg_buf ) );
    329         }
    330         else if( global.conf->runmode == RUNMODE_DAEMON )
    331         {
    332                 char **cmd, *s;
    333                
    334                 if( ( s = strchr( msg_buf, '\r' ) ) )
    335                         *s = 0;
    336                
    337                 cmd = irc_parse_line( msg_buf );
    338                 ipc_command_exec( NULL, cmd, ipc_master_commands );
    339                 g_free( cmd );
    340         }
    341        
    342         g_free( msg_buf );
    343 }
    344 
    345 void ipc_to_children( char **cmd )
    346 {
    347         if( global.conf->runmode == RUNMODE_FORKDAEMON )
    348         {
    349                 char *msg_buf = irc_build_line( cmd );
    350                 ipc_to_children_str( "%s", msg_buf );
    351                 g_free( msg_buf );
    352         }
    353         else if( global.conf->runmode == RUNMODE_DAEMON )
    354         {
    355                 GSList *l;
    356                
    357                 for( l = irc_connection_list; l; l = l->next )
    358                         ipc_command_exec( l->data, cmd, ipc_child_commands );
    359         }
    360 }
    361 
    362 void ipc_to_children_str( char *format, ... )
    363 {
    364         char *msg_buf;
    365         va_list params;
    366 
    367         va_start( params, format );
    368         msg_buf = g_strdup_vprintf( format, params );
    369         va_end( params );
    370        
    371         if( strlen( msg_buf ) > 512 )
    372         {
    373                 /* Don't send it, it's too long... */
    374         }
    375         else if( global.conf->runmode == RUNMODE_FORKDAEMON )
    376         {
    377                 int msg_len = strlen( msg_buf );
    378                 GSList *l;
    379                
    380                 for( l = child_list; l; l = l->next )
    381                 {
    382                         struct bitlbee_child *c = l->data;
    383                         write( c->ipc_fd, msg_buf, msg_len );
    384398                }
    385399        }
     
    410424}
    411425
     426void ipc_master_free_fd( int fd )
     427{
     428        GSList *l;
     429        struct bitlbee_child *c;
     430       
     431        for( l = child_list; l; l = l->next )
     432        {
     433                c = l->data;
     434                if( c->ipc_fd == fd )
     435                {
     436                        ipc_master_free_one( c );
     437                        child_list = g_slist_remove( child_list, c );
     438                        break;
     439                }
     440        }
     441}
     442
    412443void ipc_master_free_all()
    413444{
     
    421452}
    422453
     454void ipc_child_disable()
     455{
     456        b_event_remove( global.listen_watch_source_id );
     457        close( global.listen_socket );
     458       
     459        global.listen_socket = -1;
     460}
     461
     462#ifndef _WIN32
    423463char *ipc_master_save_state()
    424464{
     
    461501}
    462502
    463 void ipc_master_set_statefile( char *fn )
    464 {
    465         statefile = g_strdup( fn );
    466 }
    467 
    468503
    469504static gboolean new_ipc_client( gpointer data, gint serversock, b_input_condition cond )
     
    486521}
    487522
    488 #ifndef _WIN32
    489523int ipc_master_listen_socket()
    490524{
     
    523557}
    524558#else
     559int ipc_master_listen_socket()
     560{
    525561        /* FIXME: Open named pipe \\.\BITLBEE */
     562        return 0;
     563}
    526564#endif
    527565
    528 int ipc_master_load_state()
     566int ipc_master_load_state( char *statefile )
    529567{
    530568        struct bitlbee_child *child;
     
    534572        if( statefile == NULL )
    535573                return 0;
     574       
    536575        fp = fopen( statefile, "r" );
    537576        unlink( statefile );    /* Why do it later? :-) */
Note: See TracChangeset for help on using the changeset viewer.