source: ipc.c @ 82ca986

Last change on this file since 82ca986 was 82ca986, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-27T17:45:16Z

Fixed shutdown sequence (could cause 100% CPU usage on SIGTERM).

  • Property mode set to 100644
File size: 24.0 KB
RevLine 
[0431ea1]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[3709301]4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
[0431ea1]5  \********************************************************************/
6
7/* IPC - communication between BitlBee processes                        */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "ipc.h"
29#include "commands.h"
[6dff9d4]30#ifndef _WIN32
[3709301]31#include <sys/uio.h>
[6dff9d4]32#include <sys/un.h>
33#endif
[0431ea1]34
35GSList *child_list = NULL;
[0b09da0]36static int ipc_child_recv_fd = -1;
[74c119d]37
[c5bff81]38static void ipc_master_takeover_fail( struct bitlbee_child *child, gboolean both );
39static gboolean ipc_send_fd( int fd, int send_fd );
40
[f73b969]41static void ipc_master_cmd_client( irc_t *data, char **cmd )
[2face62]42{
[5e713f6]43        /* Normally data points at an irc_t block, but for the IPC master
44           this is different. We think this scary cast is better than
45           creating a new command_t structure, just to make the compiler
46           happy. */
[2face62]47        struct bitlbee_child *child = (void*) data;
48       
[54879ab]49        if( child && cmd[1] )
[bd9b00f]50        {
51                child->host = g_strdup( cmd[1] );
52                child->nick = g_strdup( cmd[2] );
53                child->realname = g_strdup( cmd[3] );
54        }
[2face62]55       
[debe871]56        /* CLIENT == On initial connects, HELLO is after /RESTARTs. */
[54879ab]57        if( g_strcasecmp( cmd[0], "CLIENT" ) == 0 )
58                ipc_to_children_str( "OPERMSG :Client connecting (PID=%d): %s@%s (%s)\r\n",
[52744f8]59                                     (int) ( child ? child->pid : -1 ), cmd[2], cmd[1], cmd[3] );
[2face62]60}
61
[debe871]62static void ipc_master_cmd_nick( irc_t *data, char **cmd )
63{
64        struct bitlbee_child *child = (void*) data;
65       
66        if( child && cmd[1] )
67        {
68                g_free( child->nick );
69                child->nick = g_strdup( cmd[1] );
70        }
71}
72
[f73b969]73static void ipc_master_cmd_die( irc_t *data, char **cmd )
[0431ea1]74{
[74c119d]75        if( global.conf->runmode == RUNMODE_FORKDAEMON )
76                ipc_to_children_str( "DIE\r\n" );
77       
[ba9edaa]78        bitlbee_shutdown( NULL, -1, 0 );
[0431ea1]79}
80
[565a1ea]81static void ipc_master_cmd_deaf( irc_t *data, char **cmd )
82{
83        if( global.conf->runmode == RUNMODE_DAEMON )
84        {
85                b_event_remove( global.listen_watch_source_id );
86                close( global.listen_socket );
87               
88                global.listen_socket = global.listen_watch_source_id = -1;
89       
90                ipc_to_children_str( "OPERMSG :Closed listening socket, waiting "
91                                     "for all users to disconnect." );
92        }
93        else
94        {
95                ipc_to_children_str( "OPERMSG :The DEAF command only works in "
96                                     "normal daemon mode. Try DIE instead." );
97        }
98}
99
[f73b969]100void ipc_master_cmd_rehash( irc_t *data, char **cmd )
[0431ea1]101{
[f4a5940]102        runmode_t oldmode;
103       
104        oldmode = global.conf->runmode;
105       
106        g_free( global.conf );
107        global.conf = conf_load( 0, NULL );
108       
109        if( global.conf->runmode != oldmode )
110        {
111                log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" );
112                global.conf->runmode = oldmode;
113        }
114       
115        if( global.conf->runmode == RUNMODE_FORKDAEMON )
116                ipc_to_children( cmd );
[0431ea1]117}
118
[54879ab]119void ipc_master_cmd_restart( irc_t *data, char **cmd )
120{
121        if( global.conf->runmode != RUNMODE_FORKDAEMON )
122        {
123                /* Tell child that this is unsupported. */
124                return;
125        }
126       
127        global.restart = -1;
[ba9edaa]128        bitlbee_shutdown( NULL, -1, 0 );
[54879ab]129}
130
[6c2404e]131void ipc_master_cmd_identify( irc_t *data, char **cmd )
132{
133        struct bitlbee_child *child = (void*) data, *old = NULL;
[0b09da0]134        char *resp;
[6c2404e]135        GSList *l;
136       
[133cdff]137        if( !child || !child->nick || strcmp( child->nick, cmd[1] ) != 0 )
[6c2404e]138                return;
139       
140        g_free( child->password );
141        child->password = g_strdup( cmd[2] );
142       
143        for( l = child_list; l; l = l->next )
144        {
145                old = l->data;
[133cdff]146                if( child != old &&
147                    old->nick && nick_cmp( old->nick, child->nick ) == 0 &&
[0b09da0]148                    old->password && strcmp( old->password, child->password ) == 0 )
[6c2404e]149                        break;
150        }
151       
[f545372]152        if( l && !child->to_child && !old->to_child )
[0b09da0]153        {
154                resp = "TAKEOVER INIT\r\n";
[f545372]155                child->to_child = old;
156                old->to_child = child;
[0b09da0]157        }
158        else
159        {
160                /* Won't need the fd since we can't send it anywhere. */
[c5bff81]161                closesocket( child->to_fd );
[0b09da0]162                child->to_fd = -1;
163                resp = "TAKEOVER NO\r\n";
164        }
165       
166        if( write( child->ipc_fd, resp, strlen( resp ) ) != strlen( resp ) )
167                ipc_master_free_one( child );
168}
169
170
171void ipc_master_cmd_takeover( irc_t *data, char **cmd )
172{
173        struct bitlbee_child *child = (void*) data;
[f545372]174        char *fwd = NULL;
[0b09da0]175       
[e92c4f4]176        /* Normal daemon mode doesn't keep these and has simplified code for
177           takeovers. */
178        if( child == NULL )
179                return;
180       
[c5bff81]181        if( child->to_child == NULL ||
182            g_slist_find( child_list, child->to_child ) == NULL )
183                return ipc_master_takeover_fail( child, FALSE );
184       
[0b09da0]185        if( strcmp( cmd[1], "AUTH" ) == 0 )
186        {
[c5bff81]187                /* New connection -> Master */
[0b09da0]188                if( child->to_child &&
189                    child->nick && child->to_child->nick && cmd[2] &&
190                    child->password && child->to_child->password && cmd[3] &&
191                    strcmp( child->nick, child->to_child->nick ) == 0 &&
192                    strcmp( child->nick, cmd[2] ) == 0 &&
193                    strcmp( child->password, child->to_child->password ) == 0 &&
194                    strcmp( child->password, cmd[3] ) == 0 )
195                {
196                        ipc_send_fd( child->to_child->ipc_fd, child->to_fd );
197                       
[f545372]198                        fwd = irc_build_line( cmd );
199                        if( write( child->to_child->ipc_fd, fwd, strlen( fwd ) ) != strlen( fwd ) )
[0b09da0]200                                ipc_master_free_one( child );
[f545372]201                        g_free( fwd );
[0b09da0]202                }
[c5bff81]203                else
204                        return ipc_master_takeover_fail( child, TRUE );
[0b09da0]205        }
[f545372]206        else if( strcmp( cmd[1], "DONE" ) == 0 || strcmp( cmd[1], "FAIL" ) == 0 )
207        {
[c5bff81]208                /* Old connection -> Master */
[f545372]209                int fd;
210               
211                /* The copy was successful (or not), we don't need it anymore. */
[c5bff81]212                closesocket( child->to_fd );
[f545372]213                child->to_fd = -1;
214               
215                /* Pass it through to the other party, and flush all state. */
216                fwd = irc_build_line( cmd );
217                fd = child->to_child->ipc_fd;
218                child->to_child->to_child = NULL;
219                child->to_child = NULL;
220                if( write( fd, fwd, strlen( fwd ) ) != strlen( fwd ) )
221                        ipc_master_free_one( child );
222                g_free( fwd );
223        }
[6c2404e]224}
225
[0431ea1]226static const command_t ipc_master_commands[] = {
[2face62]227        { "client",     3, ipc_master_cmd_client,     0 },
[54879ab]228        { "hello",      0, ipc_master_cmd_client,     0 },
[debe871]229        { "nick",       1, ipc_master_cmd_nick,       0 },
[0431ea1]230        { "die",        0, ipc_master_cmd_die,        0 },
[565a1ea]231        { "deaf",       0, ipc_master_cmd_deaf,       0 },
[f4a5940]232        { "wallops",    1, NULL,                      IPC_CMD_TO_CHILDREN },
[dfc8a46]233        { "wall",       1, NULL,                      IPC_CMD_TO_CHILDREN },
[2face62]234        { "opermsg",    1, NULL,                      IPC_CMD_TO_CHILDREN },
[f4a5940]235        { "rehash",     0, ipc_master_cmd_rehash,     0 },
[48721c3]236        { "kill",       2, NULL,                      IPC_CMD_TO_CHILDREN },
[54879ab]237        { "restart",    0, ipc_master_cmd_restart,    0 },
[6c2404e]238        { "identify",   2, ipc_master_cmd_identify,   0 },
[0b09da0]239        { "takeover",   1, ipc_master_cmd_takeover,   0 },
[0431ea1]240        { NULL }
241};
242
[74c119d]243
[f73b969]244static void ipc_child_cmd_die( irc_t *irc, char **cmd )
[74c119d]245{
[87de505]246        irc_abort( irc, 0, "Shutdown requested by operator" );
[74c119d]247}
248
[f73b969]249static void ipc_child_cmd_wallops( irc_t *irc, char **cmd )
[0431ea1]250{
[3af70b0]251        if( !( irc->status & USTATUS_LOGGED_IN ) )
[f73b969]252                return;
[daa9e02]253       
[0431ea1]254        if( strchr( irc->umode, 'w' ) )
[3ddb7477]255                irc_write( irc, ":%s WALLOPS :%s", irc->root->host, cmd[1] );
[e0ca412]256}
257
[dfc8a46]258static void ipc_child_cmd_wall( irc_t *irc, char **cmd )
[e0ca412]259{
[3af70b0]260        if( !( irc->status & USTATUS_LOGGED_IN ) )
[f73b969]261                return;
[daa9e02]262       
[74c119d]263        if( strchr( irc->umode, 's' ) )
[3ddb7477]264                irc_write( irc, ":%s NOTICE %s :%s", irc->root->host, irc->user->nick, cmd[1] );
[0431ea1]265}
266
[f73b969]267static void ipc_child_cmd_opermsg( irc_t *irc, char **cmd )
[2face62]268{
[3af70b0]269        if( !( irc->status & USTATUS_LOGGED_IN ) )
[f73b969]270                return;
[2face62]271       
272        if( strchr( irc->umode, 'o' ) )
[3ddb7477]273                irc_write( irc, ":%s NOTICE %s :*** OperMsg *** %s", irc->root->host, irc->user->nick, cmd[1] );
[2face62]274}
275
[f73b969]276static void ipc_child_cmd_rehash( irc_t *irc, char **cmd )
[f4a5940]277{
278        runmode_t oldmode;
279       
280        oldmode = global.conf->runmode;
281       
282        g_free( global.conf );
283        global.conf = conf_load( 0, NULL );
284       
285        global.conf->runmode = oldmode;
286}
287
[f73b969]288static void ipc_child_cmd_kill( irc_t *irc, char **cmd )
[48721c3]289{
[3af70b0]290        if( !( irc->status & USTATUS_LOGGED_IN ) )
[f73b969]291                return;
[48721c3]292       
[3ddb7477]293        if( nick_cmp( cmd[1], irc->user->nick ) != 0 )
[f73b969]294                return;         /* It's not for us. */
[48721c3]295       
[3ddb7477]296        irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->root->nick, irc->root->nick, irc->root->host, irc->user->nick, cmd[2] );
[f73b969]297        irc_abort( irc, 0, "Killed by operator: %s", cmd[2] );
[48721c3]298}
299
[54879ab]300static void ipc_child_cmd_hello( irc_t *irc, char **cmd )
301{
[3af70b0]302        if( !( irc->status & USTATUS_LOGGED_IN ) )
[54879ab]303                ipc_to_master_str( "HELLO\r\n" );
304        else
[3ddb7477]305                ipc_to_master_str( "HELLO %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname );
[54879ab]306}
307
[f545372]308static void ipc_child_cmd_takeover_yes( void *data );
309static void ipc_child_cmd_takeover_no( void *data );
310
[0b09da0]311static void ipc_child_cmd_takeover( irc_t *irc, char **cmd )
312{
313        if( strcmp( cmd[1], "NO" ) == 0 )
314        {
[f545372]315                /* Master->New connection */
[0b09da0]316                /* No takeover, finish the login. */
317        }
318        else if( strcmp( cmd[1], "INIT" ) == 0 )
319        {
[f545372]320                /* Master->New connection */
[af9f2ca]321                if( !set_getbool( &irc->b->set, "allow_takeover" ) )
322                {
323                        ipc_child_cmd_takeover_no( irc );
324                        return;
325                }
326               
[f545372]327                /* Offer to take over the old session, unless for some reason
328                   we're already logging into IM connections. */
329                if( irc->login_source_id != -1 )
330                        query_add( irc, NULL,
331                                   "You're already connected to this server. "
332                                   "Would you like to take over this session?",
333                                   ipc_child_cmd_takeover_yes,
[1e52e1f]334                                   ipc_child_cmd_takeover_no, NULL, irc );
[0b09da0]335               
[f545372]336                /* This one's going to connect to accounts, avoid that. */
337                b_event_remove( irc->login_source_id );
338                irc->login_source_id = -1;
[0b09da0]339        }
340        else if( strcmp( cmd[1], "AUTH" ) == 0 )
341        {
[f545372]342                /* Master->Old connection */
[0b09da0]343                if( irc->password && cmd[2] && cmd[3] &&
344                    ipc_child_recv_fd != -1 &&
345                    strcmp( irc->user->nick, cmd[2] ) == 0 &&
[af9f2ca]346                    strcmp( irc->password, cmd[3] ) == 0 &&
347                    set_getbool( &irc->b->set, "allow_takeover" ) )
[0b09da0]348                {
[f1c2b21]349                        irc_switch_fd( irc, ipc_child_recv_fd );
350                        irc_sync( irc );
[f545372]351                        irc_usermsg( irc, "You've successfully taken over your old session" );
[f1c2b21]352                        ipc_child_recv_fd = -1;
[f545372]353                       
354                        ipc_to_master_str( "TAKEOVER DONE\r\n" );
[0b09da0]355                }
[f545372]356                else
357                {
358                        ipc_to_master_str( "TAKEOVER FAIL\r\n" );
359                }
360        }
361        else if( strcmp( cmd[1], "DONE" ) == 0 ) 
362        {
363                /* Master->New connection (now taken over by old process) */
364                irc_free( irc );
365        }
366        else if( strcmp( cmd[1], "FAIL" ) == 0 ) 
367        {
368                /* Master->New connection */
369                irc_usermsg( irc, "Could not take over old session" );
[0b09da0]370        }
371}
372
[f545372]373static void ipc_child_cmd_takeover_yes( void *data )
374{
[e92c4f4]375        irc_t *irc = data, *old = NULL;
376        char *to_auth[] = { "TAKEOVER", "AUTH", irc->user->nick, irc->password, NULL };
[f545372]377       
378        /* Master->New connection */
379        ipc_to_master_str( "TAKEOVER AUTH %s :%s\r\n",
380                           irc->user->nick, irc->password );
381       
[e92c4f4]382        if( global.conf->runmode == RUNMODE_DAEMON )
383        {
384                GSList *l;
385               
386                for( l = irc_connection_list; l; l = l->next )
387                {
388                        old = l->data;
389                       
390                        if( irc != old &&
391                            irc->user->nick && old->user->nick &&
392                            irc->password && old->password &&
393                            strcmp( irc->user->nick, old->user->nick ) == 0 &&
394                            strcmp( irc->password, old->password ) == 0 )
395                                break;
396                }
397                if( l == NULL )
398                {
399                        to_auth[1] = "FAIL";
400                        ipc_child_cmd_takeover( irc, to_auth );
401                        return;
402                }
403        }
404       
[f545372]405        /* Drop credentials, we'll shut down soon and shouldn't overwrite
406           any settings. */
407        irc_usermsg( irc, "Trying to take over existing session" );
408       
[e92c4f4]409        irc_desync( irc );
410       
411        if( old )
412        {
413                ipc_child_recv_fd = dup( irc->fd );
414                ipc_child_cmd_takeover( old, to_auth );
415        }
416       
[f545372]417        /* TODO: irc_setpass() should do all of this. */
418        irc_setpass( irc, NULL );
419        irc->status &= ~USTATUS_IDENTIFIED;
420        irc_umode_set( irc, "-R", 1 );
421       
[e92c4f4]422        if( old )
423                irc_abort( irc, FALSE, NULL );
[f545372]424}
425
426static void ipc_child_cmd_takeover_no( void *data )
427{
428        ipc_to_master_str( "TAKEOVER NO\r\n" );
429        cmd_identify_finish( data, 0, 0 );
430}
431
[0431ea1]432static const command_t ipc_child_commands[] = {
[74c119d]433        { "die",        0, ipc_child_cmd_die,         0 },
434        { "wallops",    1, ipc_child_cmd_wallops,     0 },
[dfc8a46]435        { "wall",       1, ipc_child_cmd_wall,        0 },
[2face62]436        { "opermsg",    1, ipc_child_cmd_opermsg,     0 },
[48721c3]437        { "rehash",     0, ipc_child_cmd_rehash,      0 },
438        { "kill",       2, ipc_child_cmd_kill,        0 },
[54879ab]439        { "hello",      0, ipc_child_cmd_hello,       0 },
[0b09da0]440        { "takeover",   1, ipc_child_cmd_takeover,    0 },
[0431ea1]441        { NULL }
442};
443
[6c2404e]444gboolean ipc_child_identify( irc_t *irc )
445{
446        if( global.conf->runmode == RUNMODE_FORKDAEMON )
447        {
448                if( !ipc_send_fd( global.listen_socket, irc->fd ) )
449                        ipc_child_disable();
450       
451                ipc_to_master_str( "IDENTIFY %s :%s\r\n", irc->user->nick, irc->password );
452               
453                return TRUE;
454        }
[e92c4f4]455        else if( global.conf->runmode == RUNMODE_DAEMON )
456        {
457                GSList *l;
458                irc_t *old;
459                char *to_init[] = { "TAKEOVER", "INIT", NULL };
460               
461                for( l = irc_connection_list; l; l = l->next )
462                {
463                        old = l->data;
464                       
465                        if( irc != old &&
466                            irc->user->nick && old->user->nick &&
467                            irc->password && old->password &&
468                            strcmp( irc->user->nick, old->user->nick ) == 0 &&
469                            strcmp( irc->password, old->password ) == 0 )
470                                break;
471                }
[af9f2ca]472                if( l == NULL ||
473                    !set_getbool( &irc->b->set, "allow_takeover" ) ||
474                    !set_getbool( &old->b->set, "allow_takeover" ) )
[e92c4f4]475                        return FALSE;
476               
477                ipc_child_cmd_takeover( irc, to_init );
478               
479                return TRUE;
480        }
[6c2404e]481        else
482                return FALSE;
483}
[74c119d]484
[c5bff81]485static void ipc_master_takeover_fail( struct bitlbee_child *child, gboolean both )
486{
487        if( child == NULL || g_slist_find( child_list, child ) == NULL )
488                return;
489       
490        if( both && child->to_child != NULL )
491                ipc_master_takeover_fail( child->to_child, FALSE );
492       
493        if( child->to_fd > -1 )
494        {
495                /* Send this error only to the new connection, which can be
496                   recognised by to_fd being set. */
497                if( write( child->ipc_fd, "TAKEOVER FAIL\r\n", 15 ) != 15 )
498                {
499                        ipc_master_free_one( child );
500                        return;
501                }
502                close( child->to_fd );
503                child->to_fd = -1;
504        }
505        child->to_child = NULL;
506}
507
[0431ea1]508static void ipc_command_exec( void *data, char **cmd, const command_t *commands )
509{
[f1d38f2]510        int i, j;
[0431ea1]511       
512        if( !cmd[0] )
513                return;
514       
515        for( i = 0; commands[i].command; i ++ )
516                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
517                {
[f1d38f2]518                        /* There is no typo in this line: */
519                        for( j = 1; cmd[j]; j ++ ); j --;
520                       
521                        if( j < commands[i].required_parameters )
522                                break;
523                       
[f4a5940]524                        if( commands[i].flags & IPC_CMD_TO_CHILDREN )
525                                ipc_to_children( cmd );
526                        else
527                                commands[i].execute( data, cmd );
528                       
[f1d38f2]529                        break;
[0431ea1]530                }
531}
532
[da7b484]533/* Return just one line. Returns NULL if something broke, an empty string
534   on temporary "errors" (EAGAIN and friends). */
[6c2404e]535static char *ipc_readline( int fd, int *recv_fd )
[0431ea1]536{
[6c2404e]537        struct msghdr msg;
538        struct iovec iov;
539        char ccmsg[CMSG_SPACE(sizeof(recv_fd))];
540        struct cmsghdr *cmsg;
[da7b484]541        char buf[513], *eol;
[0431ea1]542        int size;
543       
544        /* Because this is internal communication, it should be pretty safe
545           to just peek at the message, find its length (by searching for the
546           end-of-line) and then just read that message. With internal
547           sockets and limites message length, messages should always be
548           complete. Saves us quite a lot of code and buffering. */
[da7b484]549        size = recv( fd, buf, sizeof( buf ) - 1, MSG_PEEK );
[0431ea1]550        if( size == 0 || ( size < 0 && !sockerr_again() ) )
551                return NULL;
[1ea13be]552        else if( size < 0 ) /* && sockerr_again() */
553                return( g_strdup( "" ) );
[0431ea1]554        else
555                buf[size] = 0;
556       
[da7b484]557        if( ( eol = strstr( buf, "\r\n" ) ) == NULL )
[0431ea1]558                return NULL;
559        else
560                size = eol - buf + 2;
561       
[6c2404e]562        iov.iov_base = buf;
563        iov.iov_len = size;
564       
565        memset( &msg, 0, sizeof( msg ) );
566        msg.msg_iov = &iov;
567        msg.msg_iovlen = 1;
568        msg.msg_control = ccmsg;
569        msg.msg_controllen = sizeof( ccmsg );
570       
571        if( recvmsg( fd, &msg, 0 ) != size )
[0431ea1]572                return NULL;
[6c2404e]573       
574        if( recv_fd )
575                for( cmsg = CMSG_FIRSTHDR( &msg ); cmsg; cmsg = CMSG_NXTHDR( &msg, cmsg ) )
576                        if( cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS )
577                        {
578                                /* Getting more than one shouldn't happen but if it does,
579                                   make sure we don't leave them around. */
580                                if( *recv_fd != -1 )
581                                        close( *recv_fd );
582                               
583                                *recv_fd = *(int*) CMSG_DATA( cmsg );
[e92c4f4]584                                /*
[0b09da0]585                                fprintf( stderr, "pid %d received fd %d\n", (int) getpid(), *recv_fd );
[e92c4f4]586                                */
[6c2404e]587                        }
588       
[e92c4f4]589        /*
[0b09da0]590        fprintf( stderr, "pid %d received: %s", (int) getpid(), buf );
[e92c4f4]591        */
[6c2404e]592        return g_strndup( buf, size - 2 );
[0431ea1]593}
594
[ba9edaa]595gboolean ipc_master_read( gpointer data, gint source, b_input_condition cond )
[0431ea1]596{
[6c2404e]597        struct bitlbee_child *child = data;
[0431ea1]598        char *buf, **cmd;
599       
[6c2404e]600        if( ( buf = ipc_readline( source, &child->to_fd ) ) )
[0431ea1]601        {
602                cmd = irc_parse_line( buf );
603                if( cmd )
[edc767b]604                {
[6c2404e]605                        ipc_command_exec( child, cmd, ipc_master_commands );
[edc767b]606                        g_free( cmd );
607                }
608                g_free( buf );
[0431ea1]609        }
610        else
611        {
[171ef85]612                ipc_master_free_fd( source );
[0431ea1]613        }
[ba9edaa]614       
615        return TRUE;
[0431ea1]616}
617
[ba9edaa]618gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond )
[0431ea1]619{
620        char *buf, **cmd;
621       
[0b09da0]622        if( ( buf = ipc_readline( source, &ipc_child_recv_fd ) ) )
[0431ea1]623        {
624                cmd = irc_parse_line( buf );
625                if( cmd )
[edc767b]626                {
[0431ea1]627                        ipc_command_exec( data, cmd, ipc_child_commands );
[edc767b]628                        g_free( cmd );
629                }
630                g_free( buf );
[0431ea1]631        }
632        else
633        {
[171ef85]634                ipc_child_disable();
[0431ea1]635        }
[ba9edaa]636       
637        return TRUE;
[0431ea1]638}
639
640void ipc_to_master( char **cmd )
641{
642        if( global.conf->runmode == RUNMODE_FORKDAEMON )
643        {
[74c119d]644                char *s = irc_build_line( cmd );
[2face62]645                ipc_to_master_str( "%s", s );
[f4a5940]646                g_free( s );
647        }
648        else if( global.conf->runmode == RUNMODE_DAEMON )
649        {
650                ipc_command_exec( NULL, cmd, ipc_master_commands );
651        }
652}
653
[2face62]654void ipc_to_master_str( char *format, ... )
[f4a5940]655{
[2face62]656        char *msg_buf;
657        va_list params;
658
659        va_start( params, format );
660        msg_buf = g_strdup_vprintf( format, params );
661        va_end( params );
662       
663        if( strlen( msg_buf ) > 512 )
664        {
665                /* Don't send it, it's too long... */
666        }
667        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
[f4a5940]668        {
[171ef85]669                if( global.listen_socket >= 0 )
670                        if( write( global.listen_socket, msg_buf, strlen( msg_buf ) ) <= 0 )
671                                ipc_child_disable();
[f4a5940]672        }
673        else if( global.conf->runmode == RUNMODE_DAEMON )
674        {
[bd9b00f]675                char **cmd, *s;
676               
677                if( ( s = strchr( msg_buf, '\r' ) ) )
678                        *s = 0;
[f4a5940]679               
[2face62]680                cmd = irc_parse_line( msg_buf );
[f4a5940]681                ipc_command_exec( NULL, cmd, ipc_master_commands );
682                g_free( cmd );
[74c119d]683        }
[2face62]684       
685        g_free( msg_buf );
[74c119d]686}
687
688void ipc_to_children( char **cmd )
689{
690        if( global.conf->runmode == RUNMODE_FORKDAEMON )
691        {
692                char *msg_buf = irc_build_line( cmd );
[2face62]693                ipc_to_children_str( "%s", msg_buf );
[74c119d]694                g_free( msg_buf );
695        }
[f4a5940]696        else if( global.conf->runmode == RUNMODE_DAEMON )
697        {
698                GSList *l;
699               
700                for( l = irc_connection_list; l; l = l->next )
701                        ipc_command_exec( l->data, cmd, ipc_child_commands );
702        }
[74c119d]703}
704
[2face62]705void ipc_to_children_str( char *format, ... )
[74c119d]706{
[2face62]707        char *msg_buf;
708        va_list params;
709
710        va_start( params, format );
711        msg_buf = g_strdup_vprintf( format, params );
712        va_end( params );
713       
714        if( strlen( msg_buf ) > 512 )
715        {
716                /* Don't send it, it's too long... */
717        }
718        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
[74c119d]719        {
720                int msg_len = strlen( msg_buf );
[171ef85]721                GSList *l, *next;
[0431ea1]722               
[171ef85]723                for( l = child_list; l; l = next )
[0431ea1]724                {
[74c119d]725                        struct bitlbee_child *c = l->data;
[171ef85]726                       
727                        next = l->next;
728                        if( write( c->ipc_fd, msg_buf, msg_len ) <= 0 )
729                                ipc_master_free_one( c );
[0431ea1]730                }
731        }
[f4a5940]732        else if( global.conf->runmode == RUNMODE_DAEMON )
733        {
[bd9b00f]734                char **cmd, *s;
735               
736                if( ( s = strchr( msg_buf, '\r' ) ) )
737                        *s = 0;
[f4a5940]738               
[2face62]739                cmd = irc_parse_line( msg_buf );
[f4a5940]740                ipc_to_children( cmd );
741                g_free( cmd );
742        }
[2face62]743       
744        g_free( msg_buf );
745}
746
[6c2404e]747static gboolean ipc_send_fd( int fd, int send_fd )
748{
749        struct msghdr msg;
750        struct iovec iov;
751        char ccmsg[CMSG_SPACE(sizeof(fd))];
752        struct cmsghdr *cmsg;
753       
754        memset( &msg, 0, sizeof( msg ) );
[c5bff81]755        iov.iov_base = "0x90\r\n";         /* Ja, noppes */
[6c2404e]756        iov.iov_len = 6;
757        msg.msg_iov = &iov;
758        msg.msg_iovlen = 1;
759       
760        msg.msg_control = ccmsg;
761        msg.msg_controllen = sizeof( ccmsg );
762        cmsg = CMSG_FIRSTHDR( &msg );
763        cmsg->cmsg_level = SOL_SOCKET;
764        cmsg->cmsg_type = SCM_RIGHTS;
765        cmsg->cmsg_len = CMSG_LEN( sizeof( send_fd ) );
766        *(int*)CMSG_DATA( cmsg ) = send_fd;
767        msg.msg_controllen = cmsg->cmsg_len;
768       
769        return sendmsg( fd, &msg, 0 ) == 6;
770}
771
[2face62]772void ipc_master_free_one( struct bitlbee_child *c )
773{
[c5bff81]774        GSList *l;
775       
[ba9edaa]776        b_event_remove( c->ipc_inpa );
[2face62]777        closesocket( c->ipc_fd );
778       
[6c2404e]779        if( c->to_fd != -1 )
780                close( c->to_fd );
781       
[2face62]782        g_free( c->host );
783        g_free( c->nick );
784        g_free( c->realname );
[6c2404e]785        g_free( c->password );
[2face62]786        g_free( c );
[c5bff81]787       
788        child_list = g_slist_remove( child_list, c );
789       
790        /* Also, if any child has a reference to this one, remove it. */
791        for( l = child_list; l; l = l->next )
792        {
793                struct bitlbee_child *oc = l->data;
794               
795                if( oc->to_child == c )
796                        ipc_master_takeover_fail( oc, FALSE );
797        }
[2face62]798}
799
[171ef85]800void ipc_master_free_fd( int fd )
801{
802        GSList *l;
803        struct bitlbee_child *c;
804       
805        for( l = child_list; l; l = l->next )
806        {
807                c = l->data;
808                if( c->ipc_fd == fd )
809                {
810                        ipc_master_free_one( c );
811                        break;
812                }
813        }
814}
815
[2face62]816void ipc_master_free_all()
817{
[c5bff81]818        while( child_list )
819                ipc_master_free_one( child_list->data );
[0431ea1]820}
[54879ab]821
[171ef85]822void ipc_child_disable()
823{
824        b_event_remove( global.listen_watch_source_id );
825        close( global.listen_socket );
826       
827        global.listen_socket = -1;
828}
829
[80c1e4d]830#ifndef _WIN32
[54879ab]831char *ipc_master_save_state()
832{
833        char *fn = g_strdup( "/tmp/bee-restart.XXXXXX" );
834        int fd = mkstemp( fn );
835        GSList *l;
836        FILE *fp;
837        int i;
838       
839        if( fd == -1 )
840        {
841                log_message( LOGLVL_ERROR, "Could not create temporary file: %s", strerror( errno ) );
842                g_free( fn );
843                return NULL;
844        }
845       
846        /* This is more convenient now. */
847        fp = fdopen( fd, "w" );
848       
849        for( l = child_list, i = 0; l; l = l->next )
850                i ++;
851       
852        /* Number of client processes. */
853        fprintf( fp, "%d\n", i );
854       
855        for( l = child_list; l; l = l->next )
[52744f8]856                fprintf( fp, "%d %d\n", (int) ((struct bitlbee_child*)l->data)->pid,
[54879ab]857                                        ((struct bitlbee_child*)l->data)->ipc_fd );
858       
859        if( fclose( fp ) == 0 )
860        {
861                return fn;
862        }
863        else
864        {
865                unlink( fn );
866                g_free( fn );
867                return NULL;
868        }
869}
870
[6dff9d4]871
[ba9edaa]872static gboolean new_ipc_client( gpointer data, gint serversock, b_input_condition cond )
[6dff9d4]873{
874        struct bitlbee_child *child = g_new0( struct bitlbee_child, 1 );
[a0d04d6]875       
[6c2404e]876        child->to_fd = -1;
[a0d04d6]877        child->ipc_fd = accept( serversock, NULL, 0 );
878        if( child->ipc_fd == -1 )
879        {
[8a56e52]880                log_message( LOGLVL_WARNING, "Unable to accept connection on UNIX domain socket: %s", strerror(errno) );
881                return TRUE;
882        }
[6dff9d4]883               
[e046390]884        child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child );
[a0d04d6]885       
[6c2404e]886        child_list = g_slist_prepend( child_list, child );
[ba9edaa]887       
[6dff9d4]888        return TRUE;
889}
890
891int ipc_master_listen_socket()
892{
893        struct sockaddr_un un_addr;
894        int serversock;
895
896        /* Clean up old socket files that were hanging around.. */
[8a56e52]897        if (unlink(IPCSOCKET) == -1 && errno != ENOENT) {
898                log_message( LOGLVL_ERROR, "Could not remove old IPC socket at %s: %s", IPCSOCKET, strerror(errno) );
899                return 0;
900        }
[6dff9d4]901
902        un_addr.sun_family = AF_UNIX;
903        strcpy(un_addr.sun_path, IPCSOCKET);
904
905        serversock = socket(AF_UNIX, SOCK_STREAM, PF_UNIX);
906
[8a56e52]907        if (serversock == -1) {
908                log_message( LOGLVL_WARNING, "Unable to create UNIX socket: %s", strerror(errno) );
909                return 0;
910        }
[6dff9d4]911
[5d6c178]912        if (bind(serversock, (struct sockaddr *)&un_addr, sizeof(un_addr)) == -1) {
[8a56e52]913                log_message( LOGLVL_WARNING, "Unable to bind UNIX socket to %s: %s", IPCSOCKET, strerror(errno) );
914                return 0;
915        }
916
917        if (listen(serversock, 5) == -1) {
918                log_message( LOGLVL_WARNING, "Unable to listen on UNIX socket: %s", strerror(errno) );
919                return 0;
920        }
[6dff9d4]921       
[e046390]922        b_input_add( serversock, B_EV_IO_READ, new_ipc_client, NULL );
[8a56e52]923       
[6dff9d4]924        return 1;
925}
926#else
[1cda4f3]927int ipc_master_listen_socket()
928{
[6dff9d4]929        /* FIXME: Open named pipe \\.\BITLBEE */
[1cda4f3]930        return 0;
931}
[6dff9d4]932#endif
933
[cd63d58]934int ipc_master_load_state( char *statefile )
[54879ab]935{
936        struct bitlbee_child *child;
937        FILE *fp;
938        int i, n;
939       
940        if( statefile == NULL )
941                return 0;
[cd63d58]942       
[54879ab]943        fp = fopen( statefile, "r" );
944        unlink( statefile );    /* Why do it later? :-) */
945        if( fp == NULL )
946                return 0;
947       
948        if( fscanf( fp, "%d", &n ) != 1 )
949        {
950                log_message( LOGLVL_WARNING, "Could not import state information for child processes." );
951                fclose( fp );
952                return 0;
953        }
954       
955        log_message( LOGLVL_INFO, "Importing information for %d child processes.", n );
956        for( i = 0; i < n; i ++ )
957        {
958                child = g_new0( struct bitlbee_child, 1 );
959               
[52744f8]960                if( fscanf( fp, "%d %d", (int *) &child->pid, &child->ipc_fd ) != 2 )
[54879ab]961                {
962                        log_message( LOGLVL_WARNING, "Unexpected end of file: Only processed %d clients.", i );
963                        g_free( child );
964                        fclose( fp );
965                        return 0;
966                }
[e046390]967                child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child );
[6c2404e]968                child->to_fd = -1;
[54879ab]969               
[6c2404e]970                child_list = g_slist_prepend( child_list, child );
[54879ab]971        }
972       
973        ipc_to_children_str( "HELLO\r\n" );
974        ipc_to_children_str( "OPERMSG :New BitlBee master process started (version " BITLBEE_VERSION ")\r\n" );
975       
[dd89a55]976        fclose( fp );
[54879ab]977        return 1;
978}
Note: See TracBrowser for help on using the repository browser.