]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
When deleting a task from the scheduler, ignoring the return value could
authorTilghman Lesher <tilghman@meg.abyt.es>
Sun, 27 Jan 2008 21:59:53 +0000 (21:59 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Sun, 27 Jan 2008 21:59:53 +0000 (21:59 +0000)
possibly cause memory to be accessed after it is freed, which causes all
sorts of random memory corruption.  Instead, if a deletion fails, wait a
bit and try again (noting that another thread could change our taskid
value).
(closes issue #11386)
 Reported by: flujan
 Patches:
       20080124__bug11386.diff.txt uploaded by Corydon76 (license 14)
 Tested by: Corydon76, flujan, stuarth`

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@100465 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_h323.c
channels/chan_iax2.c
channels/chan_mgcp.c
channels/chan_misdn.c
channels/chan_sip.c
include/asterisk/sched.h
main/cdr.c
main/dnsmgr.c
main/file.c
main/rtp.c
pbx/pbx_dundi.c

index 1e0a45683f5321dbd0d0a839c645d852d4a507df..bcde79a79e4680cb326017c7d0cec9037eb11aa3 100644 (file)
@@ -373,14 +373,12 @@ static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
                if (pvt->newdigit == ' ') {             /* signalUpdate message */
                        f.subclass = pvt->curDTMF;
                        if (pvt->DTMFsched >= 0) {
-                               ast_sched_del(sched, pvt->DTMFsched);
-                               pvt->DTMFsched = -1;
+                               AST_SCHED_DEL(sched, pvt->DTMFsched);
                        }
                } else {                                                /* Regular input or signal message */
                        if (pvt->newduration) {         /* This is a signal, signalUpdate follows */
                                f.frametype = AST_FRAME_DTMF_BEGIN;
-                               if (pvt->DTMFsched >= 0)
-                                       ast_sched_del(sched, pvt->DTMFsched);
+                               AST_SCHED_DEL(sched, pvt->DTMFsched);
                                pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
                                if (h323debug)
                                        ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
@@ -453,10 +451,7 @@ static void __oh323_destroy(struct oh323_pvt *pvt)
 {
        struct oh323_pvt *cur, *prev = NULL;
 
-       if (pvt->DTMFsched >= 0) {
-               ast_sched_del(sched, pvt->DTMFsched);
-               pvt->DTMFsched = -1;
-       }
+       AST_SCHED_DEL(sched, pvt->DTMFsched);
 
        if (pvt->rtp) {
                ast_rtp_destroy(pvt->rtp);
@@ -1796,15 +1791,11 @@ static int receive_digit(unsigned call_reference, char digit, const char *token,
                        };
                        if (digit == ' ') {             /* signalUpdate message */
                                f.subclass = pvt->curDTMF;
-                               if (pvt->DTMFsched >= 0) {
-                                       ast_sched_del(sched, pvt->DTMFsched);
-                                       pvt->DTMFsched = -1;
-                               }
+                               AST_SCHED_DEL(sched, pvt->DTMFsched);
                        } else {                                /* Regular input or signal message */
                                if (pvt->DTMFsched >= 0) {
                                        /* We still don't send DTMF END from previous event, send it now */
-                                       ast_sched_del(sched, pvt->DTMFsched);
-                                       pvt->DTMFsched = -1;
+                                       AST_SCHED_DEL(sched, pvt->DTMFsched);
                                        f.subclass = pvt->curDTMF;
                                        f.samples = f.len = 0;
                                        ast_queue_frame(pvt->owner, &f);
index 66af3ead0dc24ae48fb83bbb9fcdd027196ef4dd..09aef0fae71f989ccfc169ac33dfc2b36a524a6c 100644 (file)
@@ -1015,7 +1015,7 @@ static void __send_lagrq(const void *data)
        int callno = (long)data;
        /* Ping only if it's real not if it's bridged */
        ast_mutex_lock(&iaxsl[callno]);
-       if (iaxs[callno] && iaxs[callno]->lagid != -1) {
+       if (iaxs[callno] && iaxs[callno]->lagid > -1) {
                send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
                iaxs[callno]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, data);
        }
@@ -1306,10 +1306,8 @@ static int make_trunk(unsigned short callno, int locked)
                        iaxs[x]->callno = x;
                        iaxs[callno] = NULL;
                        /* Update the two timers that should have been started */
-                       if (iaxs[x]->pingid > -1)
-                               ast_sched_del(sched, iaxs[x]->pingid);
-                       if (iaxs[x]->lagid > -1)
-                               ast_sched_del(sched, iaxs[x]->lagid);
+                       AST_SCHED_DEL(sched, iaxs[x]->pingid);
+                       AST_SCHED_DEL(sched, iaxs[x]->lagid);
                        iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
                        iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
                        if (locked)
@@ -1421,8 +1419,7 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
 
 static void iax2_frame_free(struct iax_frame *fr)
 {
-       if (fr->retrans > -1)
-               ast_sched_del(sched, fr->retrans);
+       AST_SCHED_DEL(sched, fr->retrans);
        iax_frame_free(fr);
 }
 
@@ -1908,24 +1905,12 @@ static void iax2_destroy_helper(struct chan_iax2_pvt *pvt)
                ast_clear_flag(pvt, IAX_MAXAUTHREQ);
        }
        /* No more pings or lagrq's */
-       if (pvt->pingid > -1)
-               ast_sched_del(sched, pvt->pingid);
-       pvt->pingid = -1;
-       if (pvt->lagid > -1)
-               ast_sched_del(sched, pvt->lagid);
-       pvt->lagid = -1;
-       if (pvt->autoid > -1)
-               ast_sched_del(sched, pvt->autoid);
-       pvt->autoid = -1;
-       if (pvt->authid > -1)
-               ast_sched_del(sched, pvt->authid);
-       pvt->authid = -1;
-       if (pvt->initid > -1)
-               ast_sched_del(sched, pvt->initid);
-       pvt->initid = -1;
-       if (pvt->jbid > -1)
-               ast_sched_del(sched, pvt->jbid);
-       pvt->jbid = -1;
+       AST_SCHED_DEL(sched, pvt->pingid);
+       AST_SCHED_DEL(sched, pvt->lagid);
+       AST_SCHED_DEL(sched, pvt->autoid);
+       AST_SCHED_DEL(sched, pvt->authid);
+       AST_SCHED_DEL(sched, pvt->initid);
+       AST_SCHED_DEL(sched, pvt->jbid);
 }
 
 /*!
@@ -2422,9 +2407,9 @@ static void update_jbsched(struct chan_iax2_pvt *pvt)
        when = ast_tvdiff_ms(ast_tvnow(), pvt->rxcore);
        
        when = jb_next(pvt->jb) - when;
-       
-       if(pvt->jbid > -1) ast_sched_del(sched, pvt->jbid);
-       
+
+       AST_SCHED_DEL(sched, pvt->jbid);
+
        if(when <= 0) {
                /* XXX should really just empty until when > 0.. */
                when = 1;
@@ -2579,10 +2564,7 @@ static int schedule_delivery(struct iax_frame *fr, int updatehistory, int fromtr
 
                jb_reset(iaxs[fr->callno]->jb);
 
-               if (iaxs[fr->callno]->jbid > -1)
-                       ast_sched_del(sched, iaxs[fr->callno]->jbid);
-
-               iaxs[fr->callno]->jbid = -1;
+               AST_SCHED_DEL(sched, iaxs[fr->callno]->jbid);
 
                /* deliver this frame now */
                if (tsout)
@@ -5752,8 +5734,7 @@ static int iax2_ack_registry(struct iax_ies *ies, struct sockaddr_in *sin, int c
           we are registering to
        */
        reg->refresh = refresh;
-       if (reg->expire > -1)
-               ast_sched_del(sched, reg->expire);
+       AST_SCHED_DEL(sched, reg->expire);
        reg->expire = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
        if (inaddrcmp(&oldus, &reg->us) || (reg->messages != oldmsgs)) {
                if (option_verbose > 2) {
@@ -6214,8 +6195,7 @@ static int auth_fail(int callno, int failcode)
        if (iaxs[callno]) {
                iaxs[callno]->authfail = failcode;
                if (delayreject) {
-                       if (iaxs[callno]->authid > -1)
-                               ast_sched_del(sched, iaxs[callno]->authid);
+                       AST_SCHED_DEL(sched, iaxs[callno]->authid);
                        iaxs[callno]->authid = iax2_sched_add(sched, 1000, auth_reject, (void *)(long)callno);
                } else
                        auth_reject((void *)(long)callno);
@@ -6257,8 +6237,7 @@ static void iax2_dprequest(struct iax2_dpcache *dp, int callno)
 {
        struct iax_ie_data ied;
        /* Auto-hangup with 30 seconds of inactivity */
-       if (iaxs[callno]->autoid > -1)
-               ast_sched_del(sched, iaxs[callno]->autoid);
+       AST_SCHED_DEL(sched, iaxs[callno]->autoid);
        iaxs[callno]->autoid = iax2_sched_add(sched, 30000, auto_hangup, (void *)(long)callno);
        memset(&ied, 0, sizeof(ied));
        iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, dp->exten);
@@ -7240,11 +7219,7 @@ retryowner:
                        }
                }
                if (f.frametype == AST_FRAME_IAX) {
-                       if (iaxs[fr->callno]->initid > -1) {
-                               /* Don't auto congest anymore since we've gotten something usefulb ack */
-                               ast_sched_del(sched, iaxs[fr->callno]->initid);
-                               iaxs[fr->callno]->initid = -1;
-                       }
+                       AST_SCHED_DEL(sched, iaxs[fr->callno]->initid);
                        /* Handle the IAX pseudo frame itself */
                        if (option_debug && iaxdebug)
                                ast_log(LOG_DEBUG, "IAX subclass %d received\n", f.subclass);
@@ -8447,8 +8422,7 @@ static int iax2_do_register(struct iax2_registry *reg)
                if (option_debug && iaxdebug)
                        ast_log(LOG_DEBUG, "Unable to send registration request for '%s' without IP address\n", reg->username);
                /* Setup the next registration attempt */
-               if (reg->expire > -1)
-                       ast_sched_del(sched, reg->expire);
+               AST_SCHED_DEL(sched, reg->expire);
                reg->expire  = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
                return -1;
        }
@@ -8465,8 +8439,7 @@ static int iax2_do_register(struct iax2_registry *reg)
                iaxs[reg->callno]->reg = reg;
        }
        /* Schedule the next registration attempt */
-       if (reg->expire > -1)
-               ast_sched_del(sched, reg->expire);
+       AST_SCHED_DEL(sched, reg->expire);
        /* Setup the next registration a little early */
        reg->expire  = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
        /* Send the request */
@@ -8524,8 +8497,7 @@ static int iax2_provision(struct sockaddr_in *end, int sockfd, char *dest, const
        ast_mutex_lock(&iaxsl[callno]);
        if (iaxs[callno]) {
                /* Schedule autodestruct in case they don't ever give us anything back */
-               if (iaxs[callno]->autoid > -1)
-                       ast_sched_del(sched, iaxs[callno]->autoid);
+               AST_SCHED_DEL(sched, iaxs[callno]->autoid);
                iaxs[callno]->autoid = iax2_sched_add(sched, 15000, auto_hangup, (void *)(long)callno);
                ast_set_flag(iaxs[callno], IAX_PROVISION);
                /* Got a call number now, so go ahead and send the provisioning information */
@@ -9171,9 +9143,7 @@ static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, st
                                        }
                                } else {
                                        /* Non-dynamic.  Make sure we become that way if we're not */
-                                       if (peer->expire > -1)
-                                               ast_sched_del(sched, peer->expire);
-                                       peer->expire = -1;
+                                       AST_SCHED_DEL(sched, peer->expire);
                                        ast_clear_flag(peer, IAX_DYNAMIC);
                                        if (ast_dnsmgr_lookup(v->value, &peer->addr.sin_addr, &peer->dnsmgr))
                                                return peer_unref(peer);
@@ -9542,8 +9512,7 @@ static void delete_users(void)
 
        AST_LIST_LOCK(&registrations);
        while ((reg = AST_LIST_REMOVE_HEAD(&registrations, entry))) {
-               if (reg->expire > -1)
-                       ast_sched_del(sched, reg->expire);
+               ast_sched_del(sched, reg->expire);
                if (reg->callno) {
                        ast_mutex_lock(&iaxsl[reg->callno]);
                        if (iaxs[reg->callno]) {
index 4c2fa9b41bb10f076f52f6a3fdf755bc4cf41edd..821af684a65d668365aa9f6a0aad609b4d22cd85 100644 (file)
@@ -3397,9 +3397,8 @@ static int mgcpsock_read(int *id, int fd, short events, void *ignore)
                        }
 
                        /* stop retrans timer if the queue is empty */
-                       if (!gw->msgs && (gw->retransid != -1)) {
-                               ast_sched_del(sched, gw->retransid);
-                               gw->retransid = -1;
+                       if (!gw->msgs) {
+                               AST_SCHED_DEL(sched, gw->retransid);
                        }
 
                        ast_mutex_unlock(&gw->msgs_lock);
@@ -3655,9 +3654,7 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
                                        }
                                } else {
                                        /* Non-dynamic.  Make sure we become that way if we're not */
-                                       if (gw->expire > -1)
-                                               ast_sched_del(sched, gw->expire);
-                                       gw->expire = -1;
+                                       AST_SCHED_DEL(sched, gw->expire);
                                        gw->dynamic = 0;
                                        if (ast_get_ip(&gw->addr, v->value)) {
                                                if (!gw_reload) {
index 870064418abdb3541e42a20e0137d57769c770e2..ccdef6f7a892e9e23e1ff9960962a71c654258f6 100644 (file)
@@ -639,7 +639,7 @@ static int misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const v
 
 static void misdn_tasks_remove (int task_id)
 {
-       ast_sched_del(misdn_tasks, task_id);
+       AST_SCHED_DEL(misdn_tasks, task_id);
 }
 
 static int misdn_l1_task (const void *data)
index dd8a6a55767bfd933da450bfd4a5e4a1904c5a36..5c33ce7dcb280373a92e81f82f74d858b8751777 100644 (file)
@@ -1269,7 +1269,7 @@ static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *si
                                 int useglobal_nat, const int intended_method);
 static int __sip_autodestruct(const void *data);
 static void sip_scheddestroy(struct sip_pvt *p, int ms);
-static void sip_cancel_destroy(struct sip_pvt *p);
+static int sip_cancel_destroy(struct sip_pvt *p);
 static void sip_destroy(struct sip_pvt *p);
 static void __sip_destroy(struct sip_pvt *p, int lockowner);
 static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod);
@@ -2037,10 +2037,9 @@ static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int res
        if (pkt->timer_t1)
                siptimer_a = pkt->timer_t1 * 2;
 
-       /* Schedule retransmission */
-       pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
        if (option_debug > 3 && sipdebug)
                ast_log(LOG_DEBUG, "*** SIP TIMER: Initializing retransmit timer on packet: Id  #%d\n", pkt->retransid);
+       pkt->retransid = -1;
        pkt->next = p->packets;
        p->packets = pkt;
        if (sipmethod == SIP_INVITE) {
@@ -2052,11 +2051,12 @@ static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int res
 
        if (xmitres == XMIT_ERROR) {    /* Serious network trouble, no need to try again */
                append_history(pkt->owner, "XmitErr", "%s", (ast_test_flag(pkt, FLAG_FATAL)) ? "(Critical)" : "(Non-critical)");
-               ast_sched_del(sched, pkt->retransid);   /* No more retransmission */
-               pkt->retransid = -1;
                return AST_FAILURE;
-       } else
+       } else {
+               /* Schedule retransmission */
+               pkt->retransid = ast_sched_add_variable(sched, siptimer_a, retrans_pkt, pkt, 1);
                return AST_SUCCESS;
+       }
 }
 
 /*! \brief Kill a SIP dialog (called by scheduler) */
@@ -2118,19 +2118,21 @@ static void sip_scheddestroy(struct sip_pvt *p, int ms)
        if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY))
                append_history(p, "SchedDestroy", "%d ms", ms);
 
-       if (p->autokillid > -1)
-               ast_sched_del(sched, p->autokillid);
+       AST_SCHED_DEL(sched, p->autokillid);
        p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, p);
 }
 
 /*! \brief Cancel destruction of SIP dialog */
-static void sip_cancel_destroy(struct sip_pvt *p)
+static int sip_cancel_destroy(struct sip_pvt *p)
 {
+       int res = 0;
        if (p->autokillid > -1) {
-               ast_sched_del(sched, p->autokillid);
-               append_history(p, "CancelDestroy", "");
-               p->autokillid = -1;
+               if (!(res = ast_sched_del(sched, p->autokillid))) {
+                       append_history(p, "CancelDestroy", "");
+                       p->autokillid = -1;
+               }
        }
+       return res;
 }
 
 /*! \brief Acknowledges receipt of a packet and stops retransmission */
@@ -2160,9 +2162,8 @@ static void __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
                        if (cur->retransid > -1) {
                                if (sipdebug && option_debug > 3)
                                        ast_log(LOG_DEBUG, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
-                               ast_sched_del(sched, cur->retransid);
-                               cur->retransid = -1;
                        }
+                       AST_SCHED_DEL(sched, cur->retransid);
                        free(cur);
                        break;
                }
@@ -2203,9 +2204,8 @@ static int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
                        if (cur->retransid > -1) {
                                if (option_debug > 3 && sipdebug)
                                        ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
-                               ast_sched_del(sched, cur->retransid);
-                               cur->retransid = -1;
                        }
+                       AST_SCHED_DEL(sched, cur->retransid);
                        res = 0;
                        break;
                }
@@ -2459,10 +2459,8 @@ static void sip_destroy_peer(struct sip_peer *peer)
         *
         * NOTE: once peer is refcounted, this probably is no longer necessary.
         */
-       while (peer->expire > -1 && ast_sched_del(sched, peer->expire))
-               usleep(1);
-       while (peer->pokeexpire > -1 && ast_sched_del(sched, peer->pokeexpire))
-               usleep(1);
+       AST_SCHED_DEL(sched, peer->expire);
+       AST_SCHED_DEL(sched, peer->pokeexpire);
 
        register_peer_exten(peer, FALSE);
        ast_free_ha(peer->ha);
@@ -2616,9 +2614,7 @@ static struct sip_peer *realtime_peer(const char *newpeername, struct sockaddr_i
                /* Cache peer */
                ast_copy_flags(&peer->flags[1],&global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
                if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
-                       if (peer->expire > -1) {
-                               ast_sched_del(sched, peer->expire);
-                       }
+                       AST_SCHED_DEL(sched, peer->expire);
                        peer->expire = ast_sched_add(sched, (global_rtautoclear) * 1000, expire_register, (void *)peer);
                }
                ASTOBJ_CONTAINER_LINK(&peerl,peer);
@@ -3020,8 +3016,7 @@ static int sip_call(struct ast_channel *ast, char *dest, int timeout)
                        p->invitestate = INV_CALLING;
 
                        /* Initialize auto-congest time */
-                       if (p->initid > -1)
-                               ast_sched_del(sched, p->initid);
+                       AST_SCHED_DEL(sched, p->initid);
                        p->initid = ast_sched_add(sched, p->maxtime ? (p->maxtime * 4) : SIP_TRANS_TIMEOUT, auto_congest, p);
                }
        }
@@ -3044,10 +3039,8 @@ static void sip_registry_destroy(struct sip_registry *reg)
                        ast_log(LOG_DEBUG, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
                sip_destroy(reg->call);
        }
-       if (reg->expire > -1)
-               ast_sched_del(sched, reg->expire);
-       if (reg->timeout > -1)
-               ast_sched_del(sched, reg->timeout);
+       AST_SCHED_DEL(sched, reg->expire);
+       AST_SCHED_DEL(sched, reg->timeout);
        ast_string_field_free_memory(reg);
        regobjs--;
        free(reg);
@@ -3081,12 +3074,9 @@ static void __sip_destroy(struct sip_pvt *p, int lockowner)
 
        if (p->stateid > -1)
                ast_extension_state_del(p->stateid, NULL);
-       if (p->initid > -1)
-               ast_sched_del(sched, p->initid);
-       if (p->waitid > -1)
-               ast_sched_del(sched, p->waitid);
-       if (p->autokillid > -1)
-               ast_sched_del(sched, p->autokillid);
+       AST_SCHED_DEL(sched, p->initid);
+       AST_SCHED_DEL(sched, p->waitid);
+       AST_SCHED_DEL(sched, p->autokillid);
 
        if (p->rtp)
                ast_rtp_destroy(p->rtp);
@@ -3141,8 +3131,7 @@ static void __sip_destroy(struct sip_pvt *p, int lockowner)
        /* remove all current packets in this dialog */
        while((cp = p->packets)) {
                p->packets = p->packets->next;
-               if (cp->retransid > -1)
-                       ast_sched_del(sched, cp->retransid);
+               AST_SCHED_DEL(sched, cp->retransid);
                free(cp);
        }
        if (p->chanvars) {
@@ -3477,8 +3466,8 @@ static int sip_hangup(struct ast_channel *ast)
                }
                if (option_debug >3)
                        ast_log(LOG_DEBUG, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
-               if (p->autokillid > -1)
-                       sip_cancel_destroy(p);
+               if (p->autokillid > -1 && sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
                ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);        /* Really hang up next time */
                ast_clear_flag(&p->flags[0], SIP_NEEDDESTROY);
@@ -3604,10 +3593,9 @@ static int sip_hangup(struct ast_channel *ast)
                                   but we can't send one while we have "INVITE" outstanding. */
                                ast_set_flag(&p->flags[0], SIP_PENDINGBYE);     
                                ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE); 
-                               if (p->waitid)
-                                       ast_sched_del(sched, p->waitid);
-                               p->waitid = -1;
-                               sip_cancel_destroy(p);
+                               AST_SCHED_DEL(sched, p->waitid);
+                               if (sip_cancel_destroy(p))
+                                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                        }
                }
        }
@@ -7503,14 +7491,14 @@ static int transmit_register(struct sip_registry *r, int sipmethod, const char *
                        /* we have what we hope is a temporary network error,
                         * probably DNS.  We need to reschedule a registration try */
                        sip_destroy(p);
-                       if (r->timeout > -1) {
-                               ast_sched_del(sched, r->timeout);
-                               r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
+
+                       if (r->timeout > -1)
                                ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
-                       } else {
-                               r->timeout = ast_sched_add(sched, global_reg_timeout*1000, sip_reg_timeout, r);
+                       else
                                ast_log(LOG_WARNING, "Probably a DNS error for registration to %s@%s, trying REGISTER again (after %d seconds)\n", r->username, r->hostname, global_reg_timeout);
-                       }
+
+                       AST_SCHED_DEL(sched, r->timeout);
+                       r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
                        r->regattempts++;
                        return 0;
                }
@@ -7555,10 +7543,9 @@ static int transmit_register(struct sip_registry *r, int sipmethod, const char *
 
        /* set up a timeout */
        if (auth == NULL)  {
-               if (r->timeout > -1) {
+               if (r->timeout > -1)
                        ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
-                       ast_sched_del(sched, r->timeout);
-               }
+               AST_SCHED_DEL(sched, r->timeout);
                r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
                if (option_debug)
                        ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id  #%d \n", r->hostname, r->timeout);
@@ -7912,13 +7899,11 @@ static void reg_source_db(struct sip_peer *peer)
        peer->addr.sin_port = htons(port);
        if (sipsock < 0) {
                /* SIP isn't up yet, so schedule a poke only, pretty soon */
-               if (peer->pokeexpire > -1)
-                       ast_sched_del(sched, peer->pokeexpire);
+               AST_SCHED_DEL(sched, peer->pokeexpire);
                peer->pokeexpire = ast_sched_add(sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer);
        } else
                sip_poke_peer(peer);
-       if (peer->expire > -1)
-               ast_sched_del(sched, peer->expire);
+       AST_SCHED_DEL(sched, peer->expire);
        peer->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, peer);
        register_peer_exten(peer, TRUE);
 }
@@ -8054,9 +8039,7 @@ static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, st
        } else if (!strcasecmp(curi, "*") || !expiry) { /* Unregister this peer */
                /* This means remove all registrations and return OK */
                memset(&peer->addr, 0, sizeof(peer->addr));
-               if (peer->expire > -1)
-                       ast_sched_del(sched, peer->expire);
-               peer->expire = -1;
+               AST_SCHED_DEL(sched, peer->expire);
 
                destroy_association(peer);
                
@@ -8121,10 +8104,7 @@ static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, st
        if (curi && ast_strlen_zero(peer->username))
                ast_copy_string(peer->username, curi, sizeof(peer->username));
 
-       if (peer->expire > -1) {
-               ast_sched_del(sched, peer->expire);
-               peer->expire = -1;
-       }
+       AST_SCHED_DEL(sched, peer->expire);
        if (expiry > max_expiry)
                expiry = max_expiry;
        if (expiry < min_expiry)
@@ -8489,8 +8469,8 @@ static int cb_extensionstate(char *context, char* exten, int state, void *data)
        switch(state) {
        case AST_EXTENSION_DEACTIVATED: /* Retry after a while */
        case AST_EXTENSION_REMOVED:     /* Extension is gone */
-               if (p->autokillid > -1)
-                       sip_cancel_destroy(p);  /* Remove subscription expiry for renewals */
+               if (p->autokillid > -1 && sip_cancel_destroy(p))        /* Remove subscription expiry for renewals */
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);     /* Delete subscription in 32 secs */
                ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
                p->stateid = -1;
@@ -8595,7 +8575,8 @@ static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr
                        ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT);
                        transmit_response(p, "100 Trying", req);
                        if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
-                               sip_cancel_destroy(p);
+                               if (sip_cancel_destroy(p))
+                                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
 
                                /* We have a succesful registration attemp with proper authentication,
                                   now, update the peer */
@@ -8628,7 +8609,8 @@ static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr
                peer = temp_peer(name);
                if (peer) {
                        ASTOBJ_CONTAINER_LINK(&peerl, peer);
-                       sip_cancel_destroy(p);
+                       if (sip_cancel_destroy(p))
+                               ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                        switch (parse_register_contact(p, peer, req)) {
                        case PARSE_REGISTER_FAILED:
                                ast_log(LOG_WARNING, "Failed to parse contact info\n");
@@ -9363,7 +9345,8 @@ static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_requ
                do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
 
                if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
-                       sip_cancel_destroy(p);
+                       if (sip_cancel_destroy(p))
+                               ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                        ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
                        ast_copy_flags(&p->flags[1], &user->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
                        /* Copy SIP extensions profile from INVITE */
@@ -12002,11 +11985,8 @@ static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, stru
        }
 
        /* Acknowledge sequence number - This only happens on INVITE from SIP-call */
-       if (p->initid > -1) {
-               /* Don't auto congest anymore since we've gotten something useful back */
-               ast_sched_del(sched, p->initid);
-               p->initid = -1;
-       }
+       /* Don't auto congest anymore since we've gotten something useful back */
+       AST_SCHED_DEL(sched, p->initid);
 
        /* RFC3261 says we must treat every 1xx response (but not 100)
           that we don't recognize as if it was 183.
@@ -12026,15 +12006,15 @@ static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, stru
        switch (resp) {
        case 100:       /* Trying */
        case 101:       /* Dialog establishment */
-               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED))
-                       sip_cancel_destroy(p);
+               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                check_pendings(p);
                break;
 
        case 180:       /* 180 Ringing */
        case 182:       /* 182 Queued */
-               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED))
-                       sip_cancel_destroy(p);
+               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                if (!ast_test_flag(req, SIP_PKT_IGNORE) && p->owner) {
                        ast_queue_control(p->owner, AST_CONTROL_RINGING);
                        if (p->owner->_state != AST_STATE_UP) {
@@ -12054,8 +12034,8 @@ static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, stru
                break;
 
        case 183:       /* Session progress */
-               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED))
-                       sip_cancel_destroy(p);
+               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                /* Ignore 183 Session progress without SDP */
                if (find_sdp(req)) {
                        if (p->invitestate != INV_CANCELLED)
@@ -12070,8 +12050,8 @@ static void handle_response_invite(struct sip_pvt *p, int resp, char *rest, stru
                break;
 
        case 200:       /* 200 OK on invite - someone's answering our call */
-               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED))
-                       sip_cancel_destroy(p);
+               if (!ast_test_flag(req, SIP_PKT_IGNORE) && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                p->authtries = 0;
                if (find_sdp(req)) {
                        if ((res = process_sdp(p, req)) && !ast_test_flag(req, SIP_PKT_IGNORE))
@@ -12394,8 +12374,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
                if (global_regattempts_max)
                        p->registry->regattempts = global_regattempts_max+1;
-               ast_sched_del(sched, r->timeout);
-               r->timeout = -1;
+               AST_SCHED_DEL(sched, r->timeout);
                ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
                break;
        case 404:       /* Not found */
@@ -12404,8 +12383,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                        p->registry->regattempts = global_regattempts_max+1;
                ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
                r->call = NULL;
-               ast_sched_del(sched, r->timeout);
-               r->timeout = -1;
+               AST_SCHED_DEL(sched, r->timeout);
                break;
        case 407:       /* Proxy auth */
                if ((p->authtries == MAX_AUTHTRIES) || do_register_auth(p, req, "Proxy-Authenticate", "Proxy-Authorization")) {
@@ -12418,8 +12396,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                        p->registry->regattempts = global_regattempts_max+1;
                ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
                r->call = NULL;
-               ast_sched_del(sched, r->timeout);
-               r->timeout = -1;
+               AST_SCHED_DEL(sched, r->timeout);
                break;
        case 479:       /* SER: Not able to process the URI - address is wrong in register*/
                ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username,p->registry->hostname);
@@ -12427,8 +12404,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                        p->registry->regattempts = global_regattempts_max+1;
                ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
                r->call = NULL;
-               ast_sched_del(sched, r->timeout);
-               r->timeout = -1;
+               AST_SCHED_DEL(sched, r->timeout);
                break;
        case 200:       /* 200 OK */
                if (!r) {
@@ -12446,9 +12422,8 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                if (r->timeout > -1) {
                        if (option_debug)
                                ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
-                       ast_sched_del(sched, r->timeout);
                }
-               r->timeout=-1;
+               AST_SCHED_DEL(sched, r->timeout);
                r->call = NULL;
                p->registry = NULL;
                /* Let this one hang around until we have all the responses */
@@ -12457,8 +12432,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
 
                /* set us up for re-registering */
                /* figure out how long we got registered for */
-               if (r->expire > -1)
-                       ast_sched_del(sched, r->expire);
+               AST_SCHED_DEL(sched, r->expire);
                /* according to section 6.13 of RFC, contact headers override
                   expires headers, so check those first */
                expires = 0;
@@ -12502,8 +12476,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
                r->refresh= (int) expires_ms / 1000;
 
                /* Schedule re-registration before we expire */
-               if (r->expire > -1)
-                       ast_sched_del(sched, r->expire);
+               AST_SCHED_DEL(sched, r->expire);
                r->expire = ast_sched_add(sched, expires_ms, sip_reregister, r); 
                ASTOBJ_UNREF(r, sip_registry_destroy);
        }
@@ -12547,8 +12520,7 @@ static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_req
                        peer->name, s, pingtime);
        }
 
-       if (peer->pokeexpire > -1)
-               ast_sched_del(sched, peer->pokeexpire);
+       AST_SCHED_DEL(sched, peer->pokeexpire);
        ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
 
        /* Try again eventually */
@@ -12845,8 +12817,8 @@ static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_
                                        ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);    
                        } else if ((resp >= 100) && (resp < 200)) {
                                if (sipmethod == SIP_INVITE) {
-                                       if (!ast_test_flag(req, SIP_PKT_IGNORE))
-                                               sip_cancel_destroy(p);
+                                       if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
+                                               ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                                        if (find_sdp(req))
                                                process_sdp(p, req);
                                        if (p->owner) {
@@ -12950,8 +12922,8 @@ static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_
                default:        /* Errors without handlers */
                        if ((resp >= 100) && (resp < 200)) {
                                if (sipmethod == SIP_INVITE) {  /* re-invite */
-                                       if (!ast_test_flag(req, SIP_PKT_IGNORE))
-                                               sip_cancel_destroy(p);
+                                       if (!ast_test_flag(req, SIP_PKT_IGNORE) && sip_cancel_destroy(p))
+                                               ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                                }
                        }
                        if ((resp >= 300) && (resp < 700)) {
@@ -12964,9 +12936,9 @@ static void handle_response(struct sip_pvt *p, int resp, char *rest, struct sip_
                                case 503: /* Service Unavailable */
                                case 504: /* Server timeout */
 
-                                       if (sipmethod == SIP_INVITE) {  /* re-invite failed */
-                                               sip_cancel_destroy(p);
-                                       }
+                                       /* re-invite failed */
+                                       if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
+                                               ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                                        break;
                                }
                        }
@@ -13765,7 +13737,8 @@ static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int
        if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
                int newcall = (p->initreq.headers ? TRUE : FALSE);
 
-               sip_cancel_destroy(p);
+               if (sip_cancel_destroy(p))
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                /* This also counts as a pending invite */
                p->pendinginvite = seqno;
                check_via(p, req);
@@ -14974,8 +14947,8 @@ static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req,
                        else
                                ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
                }
-               if (p->autokillid > -1)
-                       sip_cancel_destroy(p);  /* Remove subscription expiry for renewals */
+               if (p->autokillid > -1 && sip_cancel_destroy(p))        /* Remove subscription expiry for renewals */
+                       ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
                if (p->expiry > 0)
                        sip_scheddestroy(p, (p->expiry + 10) * 1000);   /* Set timer for destruction of call at expiration */
 
@@ -15688,8 +15661,7 @@ static int sip_poke_noanswer(const void *data)
        peer->lastms = -1;
        ast_device_state_changed("SIP/%s", peer->name);
        /* Try again quickly */
-       if (peer->pokeexpire > -1)
-               ast_sched_del(sched, peer->pokeexpire);
+       AST_SCHED_DEL(sched, peer->pokeexpire);
        peer->pokeexpire = ast_sched_add(sched, DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer);
        return 0;
 }
@@ -15705,10 +15677,8 @@ static int sip_poke_peer(struct sip_peer *peer)
        if (!peer->maxms || !peer->addr.sin_addr.s_addr) {
                /* IF we have no IP, or this isn't to be monitored, return
                  imeediately after clearing things out */
-               if (peer->pokeexpire > -1)
-                       ast_sched_del(sched, peer->pokeexpire);
+               AST_SCHED_DEL(sched, peer->pokeexpire);
                peer->lastms = 0;
-               peer->pokeexpire = -1;
                peer->call = NULL;
                return 0;
        }
@@ -15740,8 +15710,7 @@ static int sip_poke_peer(struct sip_peer *peer)
        build_via(p);
        build_callid_pvt(p);
 
-       if (peer->pokeexpire > -1)
-               ast_sched_del(sched, peer->pokeexpire);
+       AST_SCHED_DEL(sched, peer->pokeexpire);
        p->relatedpeer = peer;
        ast_set_flag(&p->flags[0], SIP_OUTGOING);
 #ifdef VOCAL_DATA_HACK
@@ -15754,8 +15723,7 @@ static int sip_poke_peer(struct sip_peer *peer)
        if (xmitres == XMIT_ERROR)
                sip_poke_noanswer(peer);        /* Immediately unreachable, network problems */
        else {
-               if (peer->pokeexpire > -1)
-                       ast_sched_del(sched, peer->pokeexpire);
+               AST_SCHED_DEL(sched, peer->pokeexpire);
                peer->pokeexpire = ast_sched_add(sched, peer->maxms * 2, sip_poke_noanswer, peer);
        }
 
@@ -16557,9 +16525,7 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                                }
                        } else {
                                /* Non-dynamic.  Make sure we become that way if we're not */
-                               if (peer->expire > -1)
-                                       ast_sched_del(sched, peer->expire);
-                               peer->expire = -1;
+                               AST_SCHED_DEL(sched, peer->expire);
                                ast_clear_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC);
                                if (!obproxyfound || !strcasecmp(v->name, "outboundproxy")) {
                                        if (ast_get_ip_or_srv(&peer->addr, v->value, srvlookup ? "_sip._udp" : NULL)) {
@@ -17730,8 +17696,7 @@ static void sip_poke_all_peers(void)
 
        ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
                ASTOBJ_WRLOCK(iterator);
-               if (iterator->pokeexpire > -1)
-                       ast_sched_del(sched, iterator->pokeexpire);
+               AST_SCHED_DEL(sched, iterator->pokeexpire);
                ms += 100;
                iterator->pokeexpire = ast_sched_add(sched, ms, sip_poke_peer_s, iterator);
                ASTOBJ_UNLOCK(iterator);
@@ -17752,8 +17717,7 @@ static void sip_send_all_registers(void)
        ms = regspacing;
        ASTOBJ_CONTAINER_TRAVERSE(&regl, 1, do {
                ASTOBJ_WRLOCK(iterator);
-               if (iterator->expire > -1)
-                       ast_sched_del(sched, iterator->expire);
+               AST_SCHED_DEL(sched, iterator->expire);
                ms += regspacing;
                iterator->expire = ast_sched_add(sched, ms, sip_reregister, iterator);
                ASTOBJ_UNLOCK(iterator);
index 7489c6377c668bcbba4fca87483c50f29e5e1e45..84a65a07ac809cca63e89b20748b9661ac6453a0 100644 (file)
@@ -35,6 +35,16 @@ extern "C" {
  */
 #define SCHED_MAX_CACHE 128
 
+#define AST_SCHED_DEL(sched, id) \
+       do { \
+               int _count = 0; \
+               while (id > -1 && ast_sched_del(sched, id) && _count++ < 10) \
+                       usleep(1); \
+               if (_count == 10) \
+                       ast_log(LOG_WARNING, "Unable to cancel schedule ID %d.  This is probably a bug (%s: %s, line %d).\n", id, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
+               id = -1; \
+       } while (0);
+
 struct sched_context;
 
 /*! \brief New schedule context
index 0bed0cfdaa3f0338d8db49a6165d287eeb9f6e43..b3fcf08ee516f983adb26e9fcfeceb2d4b341835 100644 (file)
@@ -1146,8 +1146,7 @@ static int submit_scheduled_batch(const void *data)
 static void submit_unscheduled_batch(void)
 {
        /* this is okay since we are not being called from within the scheduler */
-       if (cdr_sched > -1)
-               ast_sched_del(sched, cdr_sched);
+       AST_SCHED_DEL(sched, cdr_sched);
        /* schedule the submission to occur ASAP (1 ms) */
        cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
        /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
@@ -1328,8 +1327,7 @@ static int do_reload(void)
        batchmode = 0;
 
        /* don't run the next scheduled CDR posting while reloading */
-       if (cdr_sched > -1)
-               ast_sched_del(sched, cdr_sched);
+       AST_SCHED_DEL(sched, cdr_sched);
 
        if ((config = ast_config_load("cdr.conf"))) {
                if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
@@ -1382,7 +1380,7 @@ static int do_reload(void)
                ast_cond_init(&cdr_pending_cond, NULL);
                if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
                        ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
-                       ast_sched_del(sched, cdr_sched);
+                       AST_SCHED_DEL(sched, cdr_sched);
                } else {
                        ast_cli_register(&cli_submit);
                        ast_register_atexit(ast_cdr_engine_term);
index f44751b979d3eb27db193259d502705ac6dc75a2..5598523f8d9e61f938ff14f2acf5307859da8d14 100644 (file)
@@ -252,7 +252,7 @@ static int refresh_list(const void *data)
 void dnsmgr_start_refresh(void)
 {
        if (refresh_sched > -1) {
-               ast_sched_del(sched, refresh_sched);
+               AST_SCHED_DEL(sched, refresh_sched);
                refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
        }
 }
@@ -367,8 +367,7 @@ static int do_reload(int loading)
        was_enabled = enabled;
        enabled = 0;
 
-       if (refresh_sched > -1)
-               ast_sched_del(sched, refresh_sched);
+       AST_SCHED_DEL(sched, refresh_sched);
 
        if ((config = ast_config_load("dnsmgr.conf"))) {
                if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
index aae5093f76fc8be8a4701abb144b340c441ab7c0..332f284414732cb4539b639c23ff407e4fb70142 100644 (file)
@@ -767,17 +767,13 @@ int ast_closestream(struct ast_filestream *f)
        if (f->owner) {
                if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
                        f->owner->stream = NULL;
-                       if (f->owner->streamid > -1)
-                               ast_sched_del(f->owner->sched, f->owner->streamid);
-                       f->owner->streamid = -1;
+                       AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
 #ifdef HAVE_ZAPTEL
                        ast_settimeout(f->owner, 0, NULL, NULL);
 #endif                 
                } else {
                        f->owner->vstream = NULL;
-                       if (f->owner->vstreamid > -1)
-                               ast_sched_del(f->owner->sched, f->owner->vstreamid);
-                       f->owner->vstreamid = -1;
+                       AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
                }
        }
        /* destroy the translator on exit */
index f35319fbe498f9496d99d68374b6e072ffc08c31..949dd1cc82716abf4029e6e0d9fd8997c78cdeba 100644 (file)
@@ -2036,10 +2036,7 @@ struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
 
 void ast_rtp_stop(struct ast_rtp *rtp)
 {
-       if (rtp->rtcp && rtp->rtcp->schedid > 0) {
-               ast_sched_del(rtp->sched, rtp->rtcp->schedid);
-               rtp->rtcp->schedid = -1;
-       }
+       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
 
        memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
        memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
@@ -2143,8 +2140,7 @@ void ast_rtp_destroy(struct ast_rtp *rtp)
        if (rtp->s > -1)
                close(rtp->s);
        if (rtp->rtcp) {
-               if (rtp->rtcp->schedid > 0)
-                       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
+               AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
                close(rtp->rtcp->s);
                free(rtp->rtcp);
                rtp->rtcp=NULL;
@@ -2370,9 +2366,7 @@ static int ast_rtcp_write_sr(const void *data)
        
        if (!rtp->rtcp->them.sin_addr.s_addr) {  /* This'll stop rtcp for this rtp session */
                ast_verbose("RTCP SR transmission error, rtcp halted\n");
-               if (rtp->rtcp->schedid > 0)
-                       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
-               rtp->rtcp->schedid = -1;
+               AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
                return 0;
        }
 
@@ -2429,9 +2423,7 @@ static int ast_rtcp_write_sr(const void *data)
        res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
        if (res < 0) {
                ast_log(LOG_ERROR, "RTCP SR transmission error to %s:%d, rtcp halted %s\n",ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port), strerror(errno));
-               if (rtp->rtcp->schedid > 0)
-                       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
-               rtp->rtcp->schedid = -1;
+               AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
                return 0;
        }
        
@@ -2481,9 +2473,7 @@ static int ast_rtcp_write_rr(const void *data)
          
        if (!rtp->rtcp->them.sin_addr.s_addr) {
                ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted\n");
-               if (rtp->rtcp->schedid > 0)
-                       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
-               rtp->rtcp->schedid = -1;
+               AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
                return 0;
        }
 
@@ -2530,9 +2520,7 @@ static int ast_rtcp_write_rr(const void *data)
        if (res < 0) {
                ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
                /* Remove the scheduler */
-               if (rtp->rtcp->schedid > 0)
-                       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
-               rtp->rtcp->schedid = -1;
+               AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
                return 0;
        }
 
index 89a2729b4579e9b225cf5b9ef65c3c948ad56361..647cd1dc22380948bfd6e80618a303da1c831279 100644 (file)
@@ -1658,8 +1658,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
                                int expire = default_expiration;
                                char data[256];
                                int needqual = 0;
-                               if (peer->registerexpire > -1)
-                                       ast_sched_del(sched, peer->registerexpire);
+                               AST_SCHED_DEL(sched, peer->registerexpire);
                                peer->registerexpire = ast_sched_add(sched, (expire + 10) * 1000, do_register_expire, peer);
                                snprintf(data, sizeof(data), "%s:%d:%d", ast_inet_ntoa(trans->addr.sin_addr), 
                                        ntohs(trans->addr.sin_port), expire);
@@ -1945,8 +1944,7 @@ static void destroy_packets(struct packetlist *p)
        struct dundi_packet *pack;
        
        while ((pack = AST_LIST_REMOVE_HEAD(p, list))) {
-               if (pack->retransid > -1)
-                       ast_sched_del(sched, pack->retransid);
+               AST_SCHED_DEL(sched, pack->retransid);
                free(pack);
        }
 }
@@ -1965,9 +1963,7 @@ static int ack_trans(struct dundi_transaction *trans, int iseqno)
                                destroy_packets(&trans->lasttrans);
                        }
                        AST_LIST_INSERT_HEAD(&trans->lasttrans, pack, list);
-                       if (trans->autokillid > -1)
-                               ast_sched_del(sched, trans->autokillid);
-                       trans->autokillid = -1;
+                       AST_SCHED_DEL(sched, trans->autokillid);
                        return 1;
                }
        }
@@ -2861,12 +2857,9 @@ static void destroy_packet(struct dundi_packet *pack, int needfree)
 {
        if (pack->parent)
                AST_LIST_REMOVE(&pack->parent->packets, pack, list);
-       if (pack->retransid > -1)
-               ast_sched_del(sched, pack->retransid);
+       AST_SCHED_DEL(sched, pack->retransid);
        if (needfree)
                free(pack);
-       else
-               pack->retransid = -1;
 }
 
 static void destroy_trans(struct dundi_transaction *trans, int fromtimeout)
@@ -2942,9 +2935,7 @@ static void destroy_trans(struct dundi_transaction *trans, int fromtimeout)
        AST_LIST_REMOVE(&alltrans, trans, all);
        destroy_packets(&trans->packets);
        destroy_packets(&trans->lasttrans);
-       if (trans->autokillid > -1)
-               ast_sched_del(sched, trans->autokillid);
-       trans->autokillid = -1;
+       AST_SCHED_DEL(sched, trans->autokillid);
        if (trans->thread) {
                /* If used by a thread, mark as dead and be done */
                ast_set_flag(trans, FLAG_DEAD);
@@ -3889,12 +3880,10 @@ static void destroy_permissions(struct permissionlist *permlist)
 
 static void destroy_peer(struct dundi_peer *peer)
 {
-       if (peer->registerid > -1)
-               ast_sched_del(sched, peer->registerid);
+       AST_SCHED_DEL(sched, peer->registerid);
        if (peer->regtrans)
                destroy_trans(peer->regtrans, 0);
-       if (peer->qualifyid > -1)
-               ast_sched_del(sched, peer->qualifyid);
+       AST_SCHED_DEL(sched, peer->qualifyid);
        destroy_permissions(&peer->permit);
        destroy_permissions(&peer->include);
        free(peer);
@@ -4057,9 +4046,7 @@ static int do_qualify(const void *data)
 static void qualify_peer(struct dundi_peer *peer, int schedonly)
 {
        int when;
-       if (peer->qualifyid > -1)
-               ast_sched_del(sched, peer->qualifyid);
-       peer->qualifyid = -1;
+       AST_SCHED_DEL(sched, peer->qualifyid);
        if (peer->qualtrans)
                destroy_trans(peer->qualtrans, 0);
        peer->qualtrans = NULL;
@@ -4137,9 +4124,7 @@ static void build_peer(dundi_eid *eid, struct ast_variable *v, int *globalpcmode
        peer->us_eid = global_eid;
        destroy_permissions(&peer->permit);
        destroy_permissions(&peer->include);
-       if (peer->registerid > -1)
-               ast_sched_del(sched, peer->registerid);
-       peer->registerid = -1;
+       AST_SCHED_DEL(sched, peer->registerid);
        for (; v; v = v->next) {
                if (!strcasecmp(v->name, "inkey")) {
                        ast_copy_string(peer->inkey, v->value, sizeof(peer->inkey));