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