source: ipc.c @ 1ea13be

Last change on this file since 1ea13be was 1ea13be, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-18T18:14:35Z

Fixed a bad mistake in ipc_readline() error handling.

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[0431ea1]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 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
31GSList *child_list = NULL;
32
[74c119d]33
[48721c3]34static int ipc_master_cmd_die( irc_t *irc, char **cmd )
[0431ea1]35{
[74c119d]36        if( global.conf->runmode == RUNMODE_FORKDAEMON )
37                ipc_to_children_str( "DIE\r\n" );
38       
[13caf0a]39        bitlbee_shutdown( NULL );
[74c119d]40       
41        return 1;
[0431ea1]42}
43
[48721c3]44static int ipc_master_cmd_rehash( irc_t *irc, char **cmd )
[0431ea1]45{
[f4a5940]46        runmode_t oldmode;
47       
48        oldmode = global.conf->runmode;
49       
50        g_free( global.conf );
51        global.conf = conf_load( 0, NULL );
52       
53        if( global.conf->runmode != oldmode )
54        {
55                log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" );
56                global.conf->runmode = oldmode;
57        }
58       
59        if( global.conf->runmode == RUNMODE_FORKDAEMON )
60                ipc_to_children( cmd );
[0431ea1]61       
62        return 1;
63}
64
65static const command_t ipc_master_commands[] = {
66        { "die",        0, ipc_master_cmd_die,        0 },
[f4a5940]67        { "wallops",    1, NULL,                      IPC_CMD_TO_CHILDREN },
68        { "lilo",       1, NULL,                      IPC_CMD_TO_CHILDREN },
69        { "rehash",     0, ipc_master_cmd_rehash,     0 },
[48721c3]70        { "kill",       2, NULL,                      IPC_CMD_TO_CHILDREN },
[0431ea1]71        { NULL }
72};
73
[74c119d]74
[48721c3]75static int ipc_child_cmd_die( irc_t *irc, char **cmd )
[74c119d]76{
77        bitlbee_shutdown( NULL );
78       
79        return 1;
80}
81
[48721c3]82static int ipc_child_cmd_wallops( irc_t *irc, char **cmd )
[0431ea1]83{
[daa9e02]84        if( irc->status < USTATUS_LOGGED_IN )
85                return 1;
86       
[0431ea1]87        if( strchr( irc->umode, 'w' ) )
[e0ca412]88                irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] );
89       
90        return 1;
91}
92
[48721c3]93static int ipc_child_cmd_lilo( irc_t *irc, char **cmd )
[e0ca412]94{
[daa9e02]95        if( irc->status < USTATUS_LOGGED_IN )
96                return 1;
97       
[74c119d]98        if( strchr( irc->umode, 's' ) )
99                irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] );
[0431ea1]100       
101        return 1;
102}
103
[48721c3]104static int ipc_child_cmd_rehash( irc_t *irc, char **cmd )
[f4a5940]105{
106        runmode_t oldmode;
107       
108        oldmode = global.conf->runmode;
109       
110        g_free( global.conf );
111        global.conf = conf_load( 0, NULL );
112       
113        global.conf->runmode = oldmode;
114       
115        return 1;
116}
117
[48721c3]118static int ipc_child_cmd_kill( irc_t *irc, char **cmd )
119{
120        if( irc->status < USTATUS_LOGGED_IN )
121                return 1;
122       
123        if( nick_cmp( cmd[1], irc->nick ) != 0 )
124                return 1;       /* It's not for us. */
125       
126        irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->mynick, irc->mynick, irc->myhost, irc->nick, cmd[2] );
127        g_io_channel_close( irc->io_channel );
128       
129        return 0;
130}
131
[0431ea1]132static const command_t ipc_child_commands[] = {
[74c119d]133        { "die",        0, ipc_child_cmd_die,         0 },
134        { "wallops",    1, ipc_child_cmd_wallops,     0 },
135        { "lilo",       1, ipc_child_cmd_lilo,        0 },
[48721c3]136        { "rehash",     0, ipc_child_cmd_rehash,      0 },
137        { "kill",       2, ipc_child_cmd_kill,        0 },
[0431ea1]138        { NULL }
139};
140
[74c119d]141
[0431ea1]142static void ipc_command_exec( void *data, char **cmd, const command_t *commands )
143{
144        int i;
145       
146        if( !cmd[0] )
147                return;
148       
149        for( i = 0; commands[i].command; i ++ )
150                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
151                {
[f4a5940]152                        if( commands[i].flags & IPC_CMD_TO_CHILDREN )
153                                ipc_to_children( cmd );
154                        else
155                                commands[i].execute( data, cmd );
156                       
[0431ea1]157                        return;
158                }
159}
160
161static char *ipc_readline( int fd )
162{
163        char *buf, *eol;
164        int size;
165       
166        buf = g_new0( char, 513 );
167       
168        /* Because this is internal communication, it should be pretty safe
169           to just peek at the message, find its length (by searching for the
170           end-of-line) and then just read that message. With internal
171           sockets and limites message length, messages should always be
172           complete. Saves us quite a lot of code and buffering. */
173        size = recv( fd, buf, 512, MSG_PEEK );
174        if( size == 0 || ( size < 0 && !sockerr_again() ) )
175                return NULL;
[1ea13be]176        else if( size < 0 ) /* && sockerr_again() */
177                return( g_strdup( "" ) );
[0431ea1]178        else
179                buf[size] = 0;
180       
181        eol = strstr( buf, "\r\n" );
182        if( eol == NULL )
183                return NULL;
184        else
185                size = eol - buf + 2;
186       
187        g_free( buf );
188        buf = g_new0( char, size + 1 );
189       
190        if( recv( fd, buf, size, 0 ) != size )
191                return NULL;
192        else
193                buf[size-2] = 0;
194       
195        return buf;
196}
197
198void ipc_master_read( gpointer data, gint source, GaimInputCondition cond )
199{
200        char *buf, **cmd;
201       
202        if( ( buf = ipc_readline( source ) ) )
203        {
204                cmd = irc_parse_line( buf );
205                if( cmd )
206                        ipc_command_exec( data, cmd, ipc_master_commands );
207        }
208        else
209        {
210                GSList *l;
211                struct bitlbee_child *c;
212               
213                for( l = child_list; l; l = l->next )
214                {
215                        c = l->data;
216                        if( c->ipc_fd == source )
217                        {
218                                close( c->ipc_fd );
219                                gaim_input_remove( c->ipc_inpa );
220                                g_free( c );
221                               
222                                child_list = g_slist_remove( child_list, l );
223                               
224                                break;
225                        }
226                }
227        }
228}
229
230void ipc_child_read( gpointer data, gint source, GaimInputCondition cond )
231{
232        char *buf, **cmd;
233       
234        if( ( buf = ipc_readline( source ) ) )
235        {
236                cmd = irc_parse_line( buf );
237                if( cmd )
238                        ipc_command_exec( data, cmd, ipc_child_commands );
239        }
240        else
241        {
242                gaim_input_remove( global.listen_watch_source_id );
243                close( global.listen_socket );
244               
245                global.listen_socket = -1;
246        }
247}
248
249void ipc_to_master( char **cmd )
250{
251        if( global.conf->runmode == RUNMODE_FORKDAEMON )
252        {
[74c119d]253                char *s = irc_build_line( cmd );
[f4a5940]254                ipc_to_master_str( s );
255                g_free( s );
256        }
257        else if( global.conf->runmode == RUNMODE_DAEMON )
258        {
259                ipc_command_exec( NULL, cmd, ipc_master_commands );
260        }
261}
262
263void ipc_to_master_str( char *msg_buf )
264{
265        if( global.conf->runmode == RUNMODE_FORKDAEMON )
266        {
267                write( global.listen_socket, msg_buf, strlen( msg_buf ) );
268        }
269        else if( global.conf->runmode == RUNMODE_DAEMON )
270        {
271                char *s, **cmd;
272               
273                /* irc_parse_line() wants a read-write string, so get it one: */
274                s = g_strdup( msg_buf );
275                cmd = irc_parse_line( s );
276               
277                ipc_command_exec( NULL, cmd, ipc_master_commands );
278               
279                g_free( cmd );
[74c119d]280                g_free( s );
281        }
282}
283
284void ipc_to_children( char **cmd )
285{
286        if( global.conf->runmode == RUNMODE_FORKDAEMON )
287        {
288                char *msg_buf = irc_build_line( cmd );
289                ipc_to_children_str( msg_buf );
290                g_free( msg_buf );
291        }
[f4a5940]292        else if( global.conf->runmode == RUNMODE_DAEMON )
293        {
294                GSList *l;
295               
296                for( l = irc_connection_list; l; l = l->next )
297                        ipc_command_exec( l->data, cmd, ipc_child_commands );
298        }
[74c119d]299}
300
301void ipc_to_children_str( char *msg_buf )
302{
303        if( global.conf->runmode == RUNMODE_FORKDAEMON )
304        {
305                int msg_len = strlen( msg_buf );
306                GSList *l;
[0431ea1]307               
[74c119d]308                for( l = child_list; l; l = l->next )
[0431ea1]309                {
[74c119d]310                        struct bitlbee_child *c = l->data;
311                        write( c->ipc_fd, msg_buf, msg_len );
[0431ea1]312                }
313        }
[f4a5940]314        else if( global.conf->runmode == RUNMODE_DAEMON )
315        {
316                char *s, **cmd;
317               
318                /* irc_parse_line() wants a read-write string, so get it one: */
319                s = g_strdup( msg_buf );
320                cmd = irc_parse_line( s );
321               
322                ipc_to_children( cmd );
323               
324                g_free( cmd );
325                g_free( s );
326        }
[0431ea1]327}
Note: See TracBrowser for help on using the repository browser.