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