Index: pbx.c =================================================================== RCS file: /usr/cvsroot/asterisk/pbx.c,v retrieving revision 1.271 diff -u -r1.271 pbx.c --- pbx.c 2 Sep 2005 18:07:26 -0000 1.271 +++ pbx.c 6 Sep 2005 08:37:59 -0000 @@ -2426,6 +2426,7 @@ ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n"); return -1; } + ast_clear_flag(c, AST_FLAG_PBX_CALL_LIMIT_EXCEEDED); /* Start a new thread, and get something handling this channel. */ pthread_attr_init(&attr); @@ -2444,6 +2445,7 @@ if (option_maxcalls) { if (countcalls >= option_maxcalls) { ast_log(LOG_NOTICE, "Maximum call limit of %d calls exceeded by '%s'!\n", option_maxcalls, c->name); + ast_set_flag(c, AST_FLAG_PBX_CALL_LIMIT_EXCEEDED); res = -1; } } Index: include/asterisk/channel.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/channel.h,v retrieving revision 1.99 diff -u -r1.99 channel.h --- include/asterisk/channel.h 1 Sep 2005 00:10:49 -0000 1.99 +++ include/asterisk/channel.h 6 Sep 2005 08:37:59 -0000 @@ -149,7 +149,7 @@ enum ast_bridge_result (* const bridge)(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc); - /*! Indicate a particular condition (e.g. AST_CONTROL_BUSY or AST_CONTROL_RINGING or AST_CONTROL_CONGESTION */ + /*! Indicate a particular condition (e.g. AST_CONTROL_BUSY or AST_CONTROL_RINGING or AST_CONTROL_CONGESTION) */ int (* const indicate)(struct ast_channel *c, int condition); /*! Fix up a channel: If a channel is consumed, this is called. Basically update any ->owner links */ @@ -372,6 +372,7 @@ so when ->priority is set, it will get incremented before finding the next priority to run */ +#define AST_FLAG_PBX_CALL_LIMIT_EXCEEDED (1 << 10) /* PBX could not start, call limit exceeded */ #define AST_FEATURE_PLAY_WARNING (1 << 0) #define AST_FEATURE_REDIRECT (1 << 1) @@ -461,6 +462,8 @@ /* Bits 16-32 of state are reserved for flags */ /*! Do not transmit voice data */ #define AST_STATE_MUTE (1 << 16) +/*! Call blocked by Maxcalls PBX call limit */ +#define AST_STATE_MAXCALL_LIMIT_HIT (1 << 17) /*! Create a channel structure */ /*! Returns NULL on failure to allocate. New channels are Index: channels/chan_sip.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v retrieving revision 1.836 diff -u -r1.836 chan_sip.c --- channels/chan_sip.c 2 Sep 2005 19:24:32 -0000 1.836 +++ channels/chan_sip.c 6 Sep 2005 08:38:02 -0000 @@ -9712,17 +9712,31 @@ transmit_response(p, "100 Trying", req); ast_setstate(c, AST_STATE_RING); if (strcmp(p->exten, ast_pickup_ext())) { - if (ast_pbx_start(c)) { - ast_log(LOG_WARNING, "Failed to start PBX :(\n"); + /* Normal call */ + int res = ast_pbx_start(c); + if (!res) /* PBX thread started */ + usleep(10); /* Wait for pbx thread to settle */ + if (res || ast_test_flag(c, AST_FLAG_PBX_CALL_LIMIT_EXCEEDED)) { + if (!res) { + /* Call limit exceeded - se option maxcalls in asterisk.conf */ + ast_log(LOG_WARNING, "Failed to start PBX - too many concurrent calls :-( (See option maxcalls in asterisk.conf)\n"); + if (ignore) + transmit_response(p, "480 Temporarily Unavailable (PBX Call limit)", req); + else + transmit_response_reliable(p, "480 Temporarily Unavailable (PBX Call limit)", req, 1); + } else { + ast_log(LOG_WARNING, "Failed to start PBX :-(\n"); + if (ignore) + transmit_response(p, "503 Service Unavailable", req); + else + transmit_response_reliable(p, "503 Service Unavailable", req, 1); + } /* Unlock locks so ast_hangup can do its magic */ + ast_set_flag(p, SIP_ALREADYGONE); /* Don't send BYE :-) */ ast_mutex_unlock(&c->lock); ast_mutex_unlock(&p->lock); ast_hangup(c); ast_mutex_lock(&p->lock); - if (ignore) - transmit_response(p, "503 Unavailable", req); - else - transmit_response_reliable(p, "503 Unavailable", req, 1); c = NULL; } } else { Index: channels/chan_iax2.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/chan_iax2.c,v retrieving revision 1.340 diff -u -r1.340 chan_iax2.c --- channels/chan_iax2.c 2 Sep 2005 19:17:19 -0000 1.340 +++ channels/chan_iax2.c 6 Sep 2005 08:38:04 -0000 @@ -3319,15 +3319,18 @@ usecnt++; ast_mutex_unlock(&usecnt_lock); ast_update_use_count(); + for (v = i->vars ; v; v = v->next) + pbx_builtin_setvar_helper(tmp, v->name, v->value); if (state != AST_STATE_DOWN) { - if (ast_pbx_start(tmp)) { - ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name); + int res = ast_pbx_start(tmp); + if (!res) + usleep(10); /* Wait for PBX thread to start */ + if (res || ast_test_flag(tmp, AST_FLAG_PBX_CALL_LIMIT_EXCEEDED)) { + ast_log(LOG_WARNING, "Unable to start PBX on %s %s\n", tmp->name, res ? "" : "(PBX call limit exceeded)"); ast_hangup(tmp); tmp = NULL; } } - for (v = i->vars ; v ; v = v->next) - pbx_builtin_setvar_helper(tmp,v->name,v->value); } return tmp;