source: ipc.c @ f1c2b21

Last change on this file since f1c2b21 was f1c2b21, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-10T15:08:02Z

Cleanup. Move some code to a more appropriate location, and show the old
connection a quit message instead of just breaking the connection.

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