source: ipc.c @ 25c4c78

Last change on this file since 25c4c78 was 25c4c78, checked in by dequis <dx@…>, at 2015-01-16T19:50:24Z

Fix compiler warnings on Cygwin and Mac OS X.

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