[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 | |
---|
| 34 | GSList *child_list = NULL; |
---|
[54879ab] | 35 | static char *statefile = NULL; |
---|
[74c119d] | 36 | |
---|
[f73b969] | 37 | static void ipc_master_cmd_client( irc_t *data, char **cmd ) |
---|
[2face62] | 38 | { |
---|
| 39 | struct bitlbee_child *child = (void*) data; |
---|
| 40 | |
---|
[54879ab] | 41 | if( child && cmd[1] ) |
---|
[bd9b00f] | 42 | { |
---|
| 43 | child->host = g_strdup( cmd[1] ); |
---|
| 44 | child->nick = g_strdup( cmd[2] ); |
---|
| 45 | child->realname = g_strdup( cmd[3] ); |
---|
| 46 | } |
---|
[2face62] | 47 | |
---|
[54879ab] | 48 | if( g_strcasecmp( cmd[0], "CLIENT" ) == 0 ) |
---|
| 49 | ipc_to_children_str( "OPERMSG :Client connecting (PID=%d): %s@%s (%s)\r\n", |
---|
| 50 | child ? child->pid : -1, cmd[2], cmd[1], cmd[3] ); |
---|
[2face62] | 51 | } |
---|
| 52 | |
---|
[f73b969] | 53 | static void ipc_master_cmd_die( irc_t *data, char **cmd ) |
---|
[0431ea1] | 54 | { |
---|
[74c119d] | 55 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 56 | ipc_to_children_str( "DIE\r\n" ); |
---|
| 57 | |
---|
[13caf0a] | 58 | bitlbee_shutdown( NULL ); |
---|
[0431ea1] | 59 | } |
---|
| 60 | |
---|
[f73b969] | 61 | void ipc_master_cmd_rehash( irc_t *data, char **cmd ) |
---|
[0431ea1] | 62 | { |
---|
[f4a5940] | 63 | runmode_t oldmode; |
---|
| 64 | |
---|
| 65 | oldmode = global.conf->runmode; |
---|
| 66 | |
---|
| 67 | g_free( global.conf ); |
---|
| 68 | global.conf = conf_load( 0, NULL ); |
---|
| 69 | |
---|
| 70 | if( global.conf->runmode != oldmode ) |
---|
| 71 | { |
---|
| 72 | log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" ); |
---|
| 73 | global.conf->runmode = oldmode; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 77 | ipc_to_children( cmd ); |
---|
[0431ea1] | 78 | } |
---|
| 79 | |
---|
[54879ab] | 80 | void ipc_master_cmd_restart( irc_t *data, char **cmd ) |
---|
| 81 | { |
---|
| 82 | struct bitlbee_child *child = (void*) data; |
---|
| 83 | |
---|
| 84 | if( global.conf->runmode != RUNMODE_FORKDAEMON ) |
---|
| 85 | { |
---|
| 86 | /* Tell child that this is unsupported. */ |
---|
| 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | global.restart = -1; |
---|
| 91 | bitlbee_shutdown( NULL ); |
---|
| 92 | } |
---|
| 93 | |
---|
[0431ea1] | 94 | static const command_t ipc_master_commands[] = { |
---|
[2face62] | 95 | { "client", 3, ipc_master_cmd_client, 0 }, |
---|
[54879ab] | 96 | { "hello", 0, ipc_master_cmd_client, 0 }, |
---|
[0431ea1] | 97 | { "die", 0, ipc_master_cmd_die, 0 }, |
---|
[f4a5940] | 98 | { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
| 99 | { "lilo", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
[2face62] | 100 | { "opermsg", 1, NULL, IPC_CMD_TO_CHILDREN }, |
---|
[f4a5940] | 101 | { "rehash", 0, ipc_master_cmd_rehash, 0 }, |
---|
[48721c3] | 102 | { "kill", 2, NULL, IPC_CMD_TO_CHILDREN }, |
---|
[54879ab] | 103 | { "restart", 0, ipc_master_cmd_restart, 0 }, |
---|
[0431ea1] | 104 | { NULL } |
---|
| 105 | }; |
---|
| 106 | |
---|
[74c119d] | 107 | |
---|
[f73b969] | 108 | static void ipc_child_cmd_die( irc_t *irc, char **cmd ) |
---|
[74c119d] | 109 | { |
---|
[87de505] | 110 | irc_abort( irc, 0, "Shutdown requested by operator" ); |
---|
[74c119d] | 111 | } |
---|
| 112 | |
---|
[f73b969] | 113 | static void ipc_child_cmd_wallops( irc_t *irc, char **cmd ) |
---|
[0431ea1] | 114 | { |
---|
[daa9e02] | 115 | if( irc->status < USTATUS_LOGGED_IN ) |
---|
[f73b969] | 116 | return; |
---|
[daa9e02] | 117 | |
---|
[0431ea1] | 118 | if( strchr( irc->umode, 'w' ) ) |
---|
[e0ca412] | 119 | irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] ); |
---|
| 120 | } |
---|
| 121 | |
---|
[f73b969] | 122 | static void ipc_child_cmd_lilo( irc_t *irc, char **cmd ) |
---|
[e0ca412] | 123 | { |
---|
[daa9e02] | 124 | if( irc->status < USTATUS_LOGGED_IN ) |
---|
[f73b969] | 125 | return; |
---|
[daa9e02] | 126 | |
---|
[74c119d] | 127 | if( strchr( irc->umode, 's' ) ) |
---|
| 128 | irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] ); |
---|
[0431ea1] | 129 | } |
---|
| 130 | |
---|
[f73b969] | 131 | static void ipc_child_cmd_opermsg( irc_t *irc, char **cmd ) |
---|
[2face62] | 132 | { |
---|
| 133 | if( irc->status < USTATUS_LOGGED_IN ) |
---|
[f73b969] | 134 | return; |
---|
[2face62] | 135 | |
---|
| 136 | if( strchr( irc->umode, 'o' ) ) |
---|
| 137 | irc_write( irc, ":%s NOTICE %s :*** OperMsg *** %s", irc->myhost, irc->nick, cmd[1] ); |
---|
| 138 | } |
---|
| 139 | |
---|
[f73b969] | 140 | static void ipc_child_cmd_rehash( irc_t *irc, char **cmd ) |
---|
[f4a5940] | 141 | { |
---|
| 142 | runmode_t oldmode; |
---|
| 143 | |
---|
| 144 | oldmode = global.conf->runmode; |
---|
| 145 | |
---|
| 146 | g_free( global.conf ); |
---|
| 147 | global.conf = conf_load( 0, NULL ); |
---|
| 148 | |
---|
| 149 | global.conf->runmode = oldmode; |
---|
| 150 | } |
---|
| 151 | |
---|
[f73b969] | 152 | static void ipc_child_cmd_kill( irc_t *irc, char **cmd ) |
---|
[48721c3] | 153 | { |
---|
| 154 | if( irc->status < USTATUS_LOGGED_IN ) |
---|
[f73b969] | 155 | return; |
---|
[48721c3] | 156 | |
---|
| 157 | if( nick_cmp( cmd[1], irc->nick ) != 0 ) |
---|
[f73b969] | 158 | return; /* It's not for us. */ |
---|
[48721c3] | 159 | |
---|
| 160 | irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->mynick, irc->mynick, irc->myhost, irc->nick, cmd[2] ); |
---|
[f73b969] | 161 | irc_abort( irc, 0, "Killed by operator: %s", cmd[2] ); |
---|
[48721c3] | 162 | } |
---|
| 163 | |
---|
[54879ab] | 164 | static void ipc_child_cmd_hello( irc_t *irc, char **cmd ) |
---|
| 165 | { |
---|
| 166 | if( irc->status < USTATUS_LOGGED_IN ) |
---|
| 167 | ipc_to_master_str( "HELLO\r\n" ); |
---|
| 168 | else |
---|
| 169 | ipc_to_master_str( "HELLO %s %s :%s\r\n", irc->host, irc->nick, irc->realname ); |
---|
| 170 | } |
---|
| 171 | |
---|
[0431ea1] | 172 | static const command_t ipc_child_commands[] = { |
---|
[74c119d] | 173 | { "die", 0, ipc_child_cmd_die, 0 }, |
---|
| 174 | { "wallops", 1, ipc_child_cmd_wallops, 0 }, |
---|
| 175 | { "lilo", 1, ipc_child_cmd_lilo, 0 }, |
---|
[2face62] | 176 | { "opermsg", 1, ipc_child_cmd_opermsg, 0 }, |
---|
[48721c3] | 177 | { "rehash", 0, ipc_child_cmd_rehash, 0 }, |
---|
| 178 | { "kill", 2, ipc_child_cmd_kill, 0 }, |
---|
[54879ab] | 179 | { "hello", 0, ipc_child_cmd_hello, 0 }, |
---|
[0431ea1] | 180 | { NULL } |
---|
| 181 | }; |
---|
| 182 | |
---|
[74c119d] | 183 | |
---|
[0431ea1] | 184 | static void ipc_command_exec( void *data, char **cmd, const command_t *commands ) |
---|
| 185 | { |
---|
[f1d38f2] | 186 | int i, j; |
---|
[0431ea1] | 187 | |
---|
| 188 | if( !cmd[0] ) |
---|
| 189 | return; |
---|
| 190 | |
---|
| 191 | for( i = 0; commands[i].command; i ++ ) |
---|
| 192 | if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) |
---|
| 193 | { |
---|
[f1d38f2] | 194 | /* There is no typo in this line: */ |
---|
| 195 | for( j = 1; cmd[j]; j ++ ); j --; |
---|
| 196 | |
---|
| 197 | if( j < commands[i].required_parameters ) |
---|
| 198 | break; |
---|
| 199 | |
---|
[f4a5940] | 200 | if( commands[i].flags & IPC_CMD_TO_CHILDREN ) |
---|
| 201 | ipc_to_children( cmd ); |
---|
| 202 | else |
---|
| 203 | commands[i].execute( data, cmd ); |
---|
| 204 | |
---|
[f1d38f2] | 205 | break; |
---|
[0431ea1] | 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | static char *ipc_readline( int fd ) |
---|
| 210 | { |
---|
| 211 | char *buf, *eol; |
---|
| 212 | int size; |
---|
| 213 | |
---|
| 214 | buf = g_new0( char, 513 ); |
---|
| 215 | |
---|
| 216 | /* Because this is internal communication, it should be pretty safe |
---|
| 217 | to just peek at the message, find its length (by searching for the |
---|
| 218 | end-of-line) and then just read that message. With internal |
---|
| 219 | sockets and limites message length, messages should always be |
---|
| 220 | complete. Saves us quite a lot of code and buffering. */ |
---|
| 221 | size = recv( fd, buf, 512, MSG_PEEK ); |
---|
| 222 | if( size == 0 || ( size < 0 && !sockerr_again() ) ) |
---|
| 223 | return NULL; |
---|
[1ea13be] | 224 | else if( size < 0 ) /* && sockerr_again() */ |
---|
| 225 | return( g_strdup( "" ) ); |
---|
[0431ea1] | 226 | else |
---|
| 227 | buf[size] = 0; |
---|
| 228 | |
---|
| 229 | eol = strstr( buf, "\r\n" ); |
---|
| 230 | if( eol == NULL ) |
---|
| 231 | return NULL; |
---|
| 232 | else |
---|
| 233 | size = eol - buf + 2; |
---|
| 234 | |
---|
| 235 | g_free( buf ); |
---|
| 236 | buf = g_new0( char, size + 1 ); |
---|
| 237 | |
---|
| 238 | if( recv( fd, buf, size, 0 ) != size ) |
---|
| 239 | return NULL; |
---|
| 240 | else |
---|
| 241 | buf[size-2] = 0; |
---|
| 242 | |
---|
| 243 | return buf; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ) |
---|
| 247 | { |
---|
| 248 | char *buf, **cmd; |
---|
| 249 | |
---|
| 250 | if( ( buf = ipc_readline( source ) ) ) |
---|
| 251 | { |
---|
| 252 | cmd = irc_parse_line( buf ); |
---|
| 253 | if( cmd ) |
---|
| 254 | ipc_command_exec( data, cmd, ipc_master_commands ); |
---|
| 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
| 258 | GSList *l; |
---|
| 259 | struct bitlbee_child *c; |
---|
| 260 | |
---|
| 261 | for( l = child_list; l; l = l->next ) |
---|
| 262 | { |
---|
| 263 | c = l->data; |
---|
| 264 | if( c->ipc_fd == source ) |
---|
| 265 | { |
---|
[2face62] | 266 | ipc_master_free_one( c ); |
---|
| 267 | child_list = g_slist_remove( child_list, c ); |
---|
[0431ea1] | 268 | break; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ) |
---|
| 275 | { |
---|
| 276 | char *buf, **cmd; |
---|
| 277 | |
---|
| 278 | if( ( buf = ipc_readline( source ) ) ) |
---|
| 279 | { |
---|
| 280 | cmd = irc_parse_line( buf ); |
---|
| 281 | if( cmd ) |
---|
| 282 | ipc_command_exec( data, cmd, ipc_child_commands ); |
---|
| 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | gaim_input_remove( global.listen_watch_source_id ); |
---|
| 287 | close( global.listen_socket ); |
---|
| 288 | |
---|
| 289 | global.listen_socket = -1; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | void ipc_to_master( char **cmd ) |
---|
| 294 | { |
---|
| 295 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 296 | { |
---|
[74c119d] | 297 | char *s = irc_build_line( cmd ); |
---|
[2face62] | 298 | ipc_to_master_str( "%s", s ); |
---|
[f4a5940] | 299 | g_free( s ); |
---|
| 300 | } |
---|
| 301 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 302 | { |
---|
| 303 | ipc_command_exec( NULL, cmd, ipc_master_commands ); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
[2face62] | 307 | void ipc_to_master_str( char *format, ... ) |
---|
[f4a5940] | 308 | { |
---|
[2face62] | 309 | char *msg_buf; |
---|
| 310 | va_list params; |
---|
| 311 | |
---|
| 312 | va_start( params, format ); |
---|
| 313 | msg_buf = g_strdup_vprintf( format, params ); |
---|
| 314 | va_end( params ); |
---|
| 315 | |
---|
| 316 | if( strlen( msg_buf ) > 512 ) |
---|
| 317 | { |
---|
| 318 | /* Don't send it, it's too long... */ |
---|
| 319 | } |
---|
| 320 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
[f4a5940] | 321 | { |
---|
| 322 | write( global.listen_socket, msg_buf, strlen( msg_buf ) ); |
---|
| 323 | } |
---|
| 324 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 325 | { |
---|
[bd9b00f] | 326 | char **cmd, *s; |
---|
| 327 | |
---|
| 328 | if( ( s = strchr( msg_buf, '\r' ) ) ) |
---|
| 329 | *s = 0; |
---|
[f4a5940] | 330 | |
---|
[2face62] | 331 | cmd = irc_parse_line( msg_buf ); |
---|
[f4a5940] | 332 | ipc_command_exec( NULL, cmd, ipc_master_commands ); |
---|
| 333 | g_free( cmd ); |
---|
[74c119d] | 334 | } |
---|
[2face62] | 335 | |
---|
| 336 | g_free( msg_buf ); |
---|
[74c119d] | 337 | } |
---|
| 338 | |
---|
| 339 | void ipc_to_children( char **cmd ) |
---|
| 340 | { |
---|
| 341 | if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 342 | { |
---|
| 343 | char *msg_buf = irc_build_line( cmd ); |
---|
[2face62] | 344 | ipc_to_children_str( "%s", msg_buf ); |
---|
[74c119d] | 345 | g_free( msg_buf ); |
---|
| 346 | } |
---|
[f4a5940] | 347 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 348 | { |
---|
| 349 | GSList *l; |
---|
| 350 | |
---|
| 351 | for( l = irc_connection_list; l; l = l->next ) |
---|
| 352 | ipc_command_exec( l->data, cmd, ipc_child_commands ); |
---|
| 353 | } |
---|
[74c119d] | 354 | } |
---|
| 355 | |
---|
[2face62] | 356 | void ipc_to_children_str( char *format, ... ) |
---|
[74c119d] | 357 | { |
---|
[2face62] | 358 | char *msg_buf; |
---|
| 359 | va_list params; |
---|
| 360 | |
---|
| 361 | va_start( params, format ); |
---|
| 362 | msg_buf = g_strdup_vprintf( format, params ); |
---|
| 363 | va_end( params ); |
---|
| 364 | |
---|
| 365 | if( strlen( msg_buf ) > 512 ) |
---|
| 366 | { |
---|
| 367 | /* Don't send it, it's too long... */ |
---|
| 368 | } |
---|
| 369 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
[74c119d] | 370 | { |
---|
| 371 | int msg_len = strlen( msg_buf ); |
---|
| 372 | GSList *l; |
---|
[0431ea1] | 373 | |
---|
[74c119d] | 374 | for( l = child_list; l; l = l->next ) |
---|
[0431ea1] | 375 | { |
---|
[74c119d] | 376 | struct bitlbee_child *c = l->data; |
---|
| 377 | write( c->ipc_fd, msg_buf, msg_len ); |
---|
[0431ea1] | 378 | } |
---|
| 379 | } |
---|
[f4a5940] | 380 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 381 | { |
---|
[bd9b00f] | 382 | char **cmd, *s; |
---|
| 383 | |
---|
| 384 | if( ( s = strchr( msg_buf, '\r' ) ) ) |
---|
| 385 | *s = 0; |
---|
[f4a5940] | 386 | |
---|
[2face62] | 387 | cmd = irc_parse_line( msg_buf ); |
---|
[f4a5940] | 388 | ipc_to_children( cmd ); |
---|
| 389 | g_free( cmd ); |
---|
| 390 | } |
---|
[2face62] | 391 | |
---|
| 392 | g_free( msg_buf ); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | void ipc_master_free_one( struct bitlbee_child *c ) |
---|
| 396 | { |
---|
| 397 | gaim_input_remove( c->ipc_inpa ); |
---|
| 398 | closesocket( c->ipc_fd ); |
---|
| 399 | |
---|
| 400 | g_free( c->host ); |
---|
| 401 | g_free( c->nick ); |
---|
| 402 | g_free( c->realname ); |
---|
| 403 | g_free( c ); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | void ipc_master_free_all() |
---|
| 407 | { |
---|
| 408 | GSList *l; |
---|
| 409 | |
---|
| 410 | for( l = child_list; l; l = l->next ) |
---|
| 411 | ipc_master_free_one( l->data ); |
---|
| 412 | |
---|
| 413 | g_slist_free( child_list ); |
---|
| 414 | child_list = NULL; |
---|
[0431ea1] | 415 | } |
---|
[54879ab] | 416 | |
---|
| 417 | char *ipc_master_save_state() |
---|
| 418 | { |
---|
| 419 | char *fn = g_strdup( "/tmp/bee-restart.XXXXXX" ); |
---|
| 420 | int fd = mkstemp( fn ); |
---|
| 421 | GSList *l; |
---|
| 422 | FILE *fp; |
---|
| 423 | int i; |
---|
| 424 | |
---|
| 425 | if( fd == -1 ) |
---|
| 426 | { |
---|
| 427 | log_message( LOGLVL_ERROR, "Could not create temporary file: %s", strerror( errno ) ); |
---|
| 428 | g_free( fn ); |
---|
| 429 | return NULL; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /* This is more convenient now. */ |
---|
| 433 | fp = fdopen( fd, "w" ); |
---|
| 434 | |
---|
| 435 | for( l = child_list, i = 0; l; l = l->next ) |
---|
| 436 | i ++; |
---|
| 437 | |
---|
| 438 | /* Number of client processes. */ |
---|
| 439 | fprintf( fp, "%d\n", i ); |
---|
| 440 | |
---|
| 441 | for( l = child_list; l; l = l->next ) |
---|
| 442 | fprintf( fp, "%d %d\n", ((struct bitlbee_child*)l->data)->pid, |
---|
| 443 | ((struct bitlbee_child*)l->data)->ipc_fd ); |
---|
| 444 | |
---|
| 445 | if( fclose( fp ) == 0 ) |
---|
| 446 | { |
---|
| 447 | return fn; |
---|
| 448 | } |
---|
| 449 | else |
---|
| 450 | { |
---|
| 451 | unlink( fn ); |
---|
| 452 | g_free( fn ); |
---|
| 453 | return NULL; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | void ipc_master_set_statefile( char *fn ) |
---|
| 458 | { |
---|
| 459 | statefile = g_strdup( fn ); |
---|
| 460 | } |
---|
| 461 | |
---|
[6dff9d4] | 462 | |
---|
| 463 | static gboolean new_ipc_client (GIOChannel *gio, GIOCondition cond, gpointer data) |
---|
| 464 | { |
---|
| 465 | struct bitlbee_child *child = g_new0( struct bitlbee_child, 1 ); |
---|
| 466 | int serversock; |
---|
| 467 | |
---|
| 468 | serversock = g_io_channel_unix_get_fd(gio); |
---|
| 469 | |
---|
| 470 | child->ipc_fd = accept(serversock, NULL, 0); |
---|
[8a56e52] | 471 | |
---|
| 472 | if (child->ipc_fd == -1) { |
---|
| 473 | log_message( LOGLVL_WARNING, "Unable to accept connection on UNIX domain socket: %s", strerror(errno) ); |
---|
| 474 | return TRUE; |
---|
| 475 | } |
---|
[6dff9d4] | 476 | |
---|
| 477 | child->ipc_inpa = gaim_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child ); |
---|
| 478 | |
---|
| 479 | child_list = g_slist_append( child_list, child ); |
---|
| 480 | |
---|
| 481 | return TRUE; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | #ifndef _WIN32 |
---|
| 485 | int ipc_master_listen_socket() |
---|
| 486 | { |
---|
| 487 | struct sockaddr_un un_addr; |
---|
| 488 | int serversock; |
---|
| 489 | GIOChannel *gio; |
---|
| 490 | |
---|
| 491 | /* Clean up old socket files that were hanging around.. */ |
---|
[8a56e52] | 492 | if (unlink(IPCSOCKET) == -1 && errno != ENOENT) { |
---|
| 493 | log_message( LOGLVL_ERROR, "Could not remove old IPC socket at %s: %s", IPCSOCKET, strerror(errno) ); |
---|
| 494 | return 0; |
---|
| 495 | } |
---|
[6dff9d4] | 496 | |
---|
| 497 | un_addr.sun_family = AF_UNIX; |
---|
| 498 | strcpy(un_addr.sun_path, IPCSOCKET); |
---|
| 499 | |
---|
| 500 | serversock = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); |
---|
| 501 | |
---|
[8a56e52] | 502 | if (serversock == -1) { |
---|
| 503 | log_message( LOGLVL_WARNING, "Unable to create UNIX socket: %s", strerror(errno) ); |
---|
| 504 | return 0; |
---|
| 505 | } |
---|
[6dff9d4] | 506 | |
---|
[8a56e52] | 507 | if (bind(serversock, &un_addr, sizeof(un_addr)) == -1) { |
---|
| 508 | log_message( LOGLVL_WARNING, "Unable to bind UNIX socket to %s: %s", IPCSOCKET, strerror(errno) ); |
---|
| 509 | return 0; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | if (listen(serversock, 5) == -1) { |
---|
| 513 | log_message( LOGLVL_WARNING, "Unable to listen on UNIX socket: %s", strerror(errno) ); |
---|
| 514 | return 0; |
---|
| 515 | } |
---|
[6dff9d4] | 516 | |
---|
| 517 | gio = g_io_channel_unix_new(serversock); |
---|
[8a56e52] | 518 | |
---|
| 519 | if (gio == NULL) { |
---|
| 520 | log_message( LOGLVL_WARNING, "Unable to create IO channel for unix socket" ); |
---|
| 521 | return 0; |
---|
| 522 | } |
---|
[6dff9d4] | 523 | |
---|
| 524 | g_io_add_watch(gio, G_IO_IN, new_ipc_client, NULL); |
---|
| 525 | return 1; |
---|
| 526 | } |
---|
| 527 | #else |
---|
| 528 | /* FIXME: Open named pipe \\.\BITLBEE */ |
---|
| 529 | #endif |
---|
| 530 | |
---|
[54879ab] | 531 | int ipc_master_load_state() |
---|
| 532 | { |
---|
| 533 | struct bitlbee_child *child; |
---|
| 534 | FILE *fp; |
---|
| 535 | int i, n; |
---|
| 536 | |
---|
| 537 | if( statefile == NULL ) |
---|
| 538 | return 0; |
---|
| 539 | fp = fopen( statefile, "r" ); |
---|
| 540 | unlink( statefile ); /* Why do it later? :-) */ |
---|
| 541 | if( fp == NULL ) |
---|
| 542 | return 0; |
---|
| 543 | |
---|
| 544 | if( fscanf( fp, "%d", &n ) != 1 ) |
---|
| 545 | { |
---|
| 546 | log_message( LOGLVL_WARNING, "Could not import state information for child processes." ); |
---|
| 547 | fclose( fp ); |
---|
| 548 | return 0; |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | log_message( LOGLVL_INFO, "Importing information for %d child processes.", n ); |
---|
| 552 | for( i = 0; i < n; i ++ ) |
---|
| 553 | { |
---|
| 554 | child = g_new0( struct bitlbee_child, 1 ); |
---|
| 555 | |
---|
| 556 | if( fscanf( fp, "%d %d", &child->pid, &child->ipc_fd ) != 2 ) |
---|
| 557 | { |
---|
| 558 | log_message( LOGLVL_WARNING, "Unexpected end of file: Only processed %d clients.", i ); |
---|
| 559 | g_free( child ); |
---|
| 560 | fclose( fp ); |
---|
| 561 | return 0; |
---|
| 562 | } |
---|
| 563 | child->ipc_inpa = gaim_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child ); |
---|
| 564 | |
---|
| 565 | child_list = g_slist_append( child_list, child ); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | ipc_to_children_str( "HELLO\r\n" ); |
---|
| 569 | ipc_to_children_str( "OPERMSG :New BitlBee master process started (version " BITLBEE_VERSION ")\r\n" ); |
---|
| 570 | |
---|
| 571 | return 1; |
---|
| 572 | } |
---|