- Timestamp:
- 2008-07-16T23:22:52Z (16 years ago)
- Branches:
- master
- Children:
- 9b55485
- Parents:
- 9730d72 (diff), 6a78c0e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ipc.c
r9730d72 r6738a67 33 33 34 34 GSList *child_list = NULL; 35 static char *statefile = NULL;36 35 37 36 static void ipc_master_cmd_client( irc_t *data, char **cmd ) … … 63 62 } 64 63 64 static void ipc_master_cmd_deaf( irc_t *data, char **cmd ) 65 { 66 if( global.conf->runmode == RUNMODE_DAEMON ) 67 { 68 b_event_remove( global.listen_watch_source_id ); 69 close( global.listen_socket ); 70 71 global.listen_socket = global.listen_watch_source_id = -1; 72 73 ipc_to_children_str( "OPERMSG :Closed listening socket, waiting " 74 "for all users to disconnect." ); 75 } 76 else 77 { 78 ipc_to_children_str( "OPERMSG :The DEAF command only works in " 79 "normal daemon mode. Try DIE instead." ); 80 } 81 } 82 65 83 void ipc_master_cmd_rehash( irc_t *data, char **cmd ) 66 84 { … … 98 116 { "hello", 0, ipc_master_cmd_client, 0 }, 99 117 { "die", 0, ipc_master_cmd_die, 0 }, 118 { "deaf", 0, ipc_master_cmd_deaf, 0 }, 100 119 { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, 101 120 { "wall", 1, NULL, IPC_CMD_TO_CHILDREN }, … … 209 228 } 210 229 230 /* Return just one line. Returns NULL if something broke, an empty string 231 on temporary "errors" (EAGAIN and friends). */ 211 232 static char *ipc_readline( int fd ) 212 233 { 213 char *buf, *eol;234 char buf[513], *eol; 214 235 int size; 215 216 buf = g_new0( char, 513 );217 236 218 237 /* Because this is internal communication, it should be pretty safe … … 221 240 sockets and limites message length, messages should always be 222 241 complete. Saves us quite a lot of code and buffering. */ 223 size = recv( fd, buf, 512, MSG_PEEK );242 size = recv( fd, buf, sizeof( buf ) - 1, MSG_PEEK ); 224 243 if( size == 0 || ( size < 0 && !sockerr_again() ) ) 225 244 return NULL; … … 229 248 buf[size] = 0; 230 249 231 eol = strstr( buf, "\r\n" ); 232 if( eol == NULL ) 250 if( ( eol = strstr( buf, "\r\n" ) ) == NULL ) 233 251 return NULL; 234 252 else 235 253 size = eol - buf + 2; 236 237 g_free( buf );238 buf = g_new0( char, size + 1 );239 254 240 255 if( recv( fd, buf, size, 0 ) != size ) 241 256 return NULL; 242 257 else 243 buf[size-2] = 0; 244 245 return buf; 258 return g_strndup( buf, size - 2 ); 246 259 } 247 260 … … 254 267 cmd = irc_parse_line( buf ); 255 268 if( cmd ) 269 { 256 270 ipc_command_exec( data, cmd, ipc_master_commands ); 257 } 258 else 271 g_free( cmd ); 272 } 273 g_free( buf ); 274 } 275 else 276 { 277 ipc_master_free_fd( source ); 278 } 279 280 return TRUE; 281 } 282 283 gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond ) 284 { 285 char *buf, **cmd; 286 287 if( ( buf = ipc_readline( source ) ) ) 288 { 289 cmd = irc_parse_line( buf ); 290 if( cmd ) 291 { 292 ipc_command_exec( data, cmd, ipc_child_commands ); 293 g_free( cmd ); 294 } 295 g_free( buf ); 296 } 297 else 298 { 299 ipc_child_disable(); 300 } 301 302 return TRUE; 303 } 304 305 void ipc_to_master( char **cmd ) 306 { 307 if( global.conf->runmode == RUNMODE_FORKDAEMON ) 308 { 309 char *s = irc_build_line( cmd ); 310 ipc_to_master_str( "%s", s ); 311 g_free( s ); 312 } 313 else if( global.conf->runmode == RUNMODE_DAEMON ) 314 { 315 ipc_command_exec( NULL, cmd, ipc_master_commands ); 316 } 317 } 318 319 void ipc_to_master_str( char *format, ... ) 320 { 321 char *msg_buf; 322 va_list params; 323 324 va_start( params, format ); 325 msg_buf = g_strdup_vprintf( format, params ); 326 va_end( params ); 327 328 if( strlen( msg_buf ) > 512 ) 329 { 330 /* Don't send it, it's too long... */ 331 } 332 else if( global.conf->runmode == RUNMODE_FORKDAEMON ) 333 { 334 if( global.listen_socket >= 0 ) 335 if( write( global.listen_socket, msg_buf, strlen( msg_buf ) ) <= 0 ) 336 ipc_child_disable(); 337 } 338 else if( global.conf->runmode == RUNMODE_DAEMON ) 339 { 340 char **cmd, *s; 341 342 if( ( s = strchr( msg_buf, '\r' ) ) ) 343 *s = 0; 344 345 cmd = irc_parse_line( msg_buf ); 346 ipc_command_exec( NULL, cmd, ipc_master_commands ); 347 g_free( cmd ); 348 } 349 350 g_free( msg_buf ); 351 } 352 353 void ipc_to_children( char **cmd ) 354 { 355 if( global.conf->runmode == RUNMODE_FORKDAEMON ) 356 { 357 char *msg_buf = irc_build_line( cmd ); 358 ipc_to_children_str( "%s", msg_buf ); 359 g_free( msg_buf ); 360 } 361 else if( global.conf->runmode == RUNMODE_DAEMON ) 259 362 { 260 363 GSList *l; 261 struct bitlbee_child *c; 262 263 for( l = child_list; l; l = l->next ) 364 365 for( l = irc_connection_list; l; l = l->next ) 366 ipc_command_exec( l->data, cmd, ipc_child_commands ); 367 } 368 } 369 370 void ipc_to_children_str( char *format, ... ) 371 { 372 char *msg_buf; 373 va_list params; 374 375 va_start( params, format ); 376 msg_buf = g_strdup_vprintf( format, params ); 377 va_end( params ); 378 379 if( strlen( msg_buf ) > 512 ) 380 { 381 /* Don't send it, it's too long... */ 382 } 383 else if( global.conf->runmode == RUNMODE_FORKDAEMON ) 384 { 385 int msg_len = strlen( msg_buf ); 386 GSList *l, *next; 387 388 for( l = child_list; l; l = next ) 264 389 { 265 c = l->data; 266 if( c->ipc_fd == source ) 390 struct bitlbee_child *c = l->data; 391 392 next = l->next; 393 if( write( c->ipc_fd, msg_buf, msg_len ) <= 0 ) 267 394 { 268 395 ipc_master_free_one( c ); 269 396 child_list = g_slist_remove( child_list, c ); 270 break;271 397 } 272 }273 }274 275 return TRUE;276 }277 278 gboolean ipc_child_read( gpointer data, gint source, b_input_condition cond )279 {280 char *buf, **cmd;281 282 if( ( buf = ipc_readline( source ) ) )283 {284 cmd = irc_parse_line( buf );285 if( cmd )286 ipc_command_exec( data, cmd, ipc_child_commands );287 }288 else289 {290 b_event_remove( global.listen_watch_source_id );291 close( global.listen_socket );292 293 global.listen_socket = -1;294 }295 296 return TRUE;297 }298 299 void ipc_to_master( char **cmd )300 {301 if( global.conf->runmode == RUNMODE_FORKDAEMON )302 {303 char *s = irc_build_line( cmd );304 ipc_to_master_str( "%s", s );305 g_free( s );306 }307 else if( global.conf->runmode == RUNMODE_DAEMON )308 {309 ipc_command_exec( NULL, cmd, ipc_master_commands );310 }311 }312 313 void ipc_to_master_str( char *format, ... )314 {315 char *msg_buf;316 va_list params;317 318 va_start( params, format );319 msg_buf = g_strdup_vprintf( format, params );320 va_end( params );321 322 if( strlen( msg_buf ) > 512 )323 {324 /* Don't send it, it's too long... */325 }326 else if( global.conf->runmode == RUNMODE_FORKDAEMON )327 {328 write( global.listen_socket, msg_buf, strlen( msg_buf ) );329 }330 else if( global.conf->runmode == RUNMODE_DAEMON )331 {332 char **cmd, *s;333 334 if( ( s = strchr( msg_buf, '\r' ) ) )335 *s = 0;336 337 cmd = irc_parse_line( msg_buf );338 ipc_command_exec( NULL, cmd, ipc_master_commands );339 g_free( cmd );340 }341 342 g_free( msg_buf );343 }344 345 void ipc_to_children( char **cmd )346 {347 if( global.conf->runmode == RUNMODE_FORKDAEMON )348 {349 char *msg_buf = irc_build_line( cmd );350 ipc_to_children_str( "%s", msg_buf );351 g_free( msg_buf );352 }353 else if( global.conf->runmode == RUNMODE_DAEMON )354 {355 GSList *l;356 357 for( l = irc_connection_list; l; l = l->next )358 ipc_command_exec( l->data, cmd, ipc_child_commands );359 }360 }361 362 void ipc_to_children_str( char *format, ... )363 {364 char *msg_buf;365 va_list params;366 367 va_start( params, format );368 msg_buf = g_strdup_vprintf( format, params );369 va_end( params );370 371 if( strlen( msg_buf ) > 512 )372 {373 /* Don't send it, it's too long... */374 }375 else if( global.conf->runmode == RUNMODE_FORKDAEMON )376 {377 int msg_len = strlen( msg_buf );378 GSList *l;379 380 for( l = child_list; l; l = l->next )381 {382 struct bitlbee_child *c = l->data;383 write( c->ipc_fd, msg_buf, msg_len );384 398 } 385 399 } … … 410 424 } 411 425 426 void ipc_master_free_fd( int fd ) 427 { 428 GSList *l; 429 struct bitlbee_child *c; 430 431 for( l = child_list; l; l = l->next ) 432 { 433 c = l->data; 434 if( c->ipc_fd == fd ) 435 { 436 ipc_master_free_one( c ); 437 child_list = g_slist_remove( child_list, c ); 438 break; 439 } 440 } 441 } 442 412 443 void ipc_master_free_all() 413 444 { … … 421 452 } 422 453 454 void ipc_child_disable() 455 { 456 b_event_remove( global.listen_watch_source_id ); 457 close( global.listen_socket ); 458 459 global.listen_socket = -1; 460 } 461 462 #ifndef _WIN32 423 463 char *ipc_master_save_state() 424 464 { … … 461 501 } 462 502 463 void ipc_master_set_statefile( char *fn )464 {465 statefile = g_strdup( fn );466 }467 468 503 469 504 static gboolean new_ipc_client( gpointer data, gint serversock, b_input_condition cond ) … … 486 521 } 487 522 488 #ifndef _WIN32489 523 int ipc_master_listen_socket() 490 524 { … … 523 557 } 524 558 #else 559 int ipc_master_listen_socket() 560 { 525 561 /* FIXME: Open named pipe \\.\BITLBEE */ 562 return 0; 563 } 526 564 #endif 527 565 528 int ipc_master_load_state( )566 int ipc_master_load_state( char *statefile ) 529 567 { 530 568 struct bitlbee_child *child; … … 534 572 if( statefile == NULL ) 535 573 return 0; 574 536 575 fp = fopen( statefile, "r" ); 537 576 unlink( statefile ); /* Why do it later? :-) */
Note: See TracChangeset
for help on using the changeset viewer.