source: ipc.c @ 0b09da0

Last change on this file since 0b09da0 was 0b09da0, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-06T23:10:17Z

It works! Fragile like hell though, and without any confirmation or whatever.

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