Navigation


Changeset 691:78b665400097 in freeDiameter


Ignore:
Timestamp:
Jan 20, 2011, 7:44:27 PM (13 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Cleanup all pthread_cleanup_push / pop pairs so that pop is always called after push, or ASSERT(0) is some grave errors

Files:
13 edited

Legend:

Unmodified
Added
Removed
  • extensions/test_netemul/tne_process.c

    r656 r691  
    247247       
    248248        CHECK_POSIX_DO( pthread_mutex_lock(&mtx), goto error );
     249        pthread_cleanup_push( fd_cleanup_mutex, &mtx );
    249250       
    250251        /* The loop */
    251252        while (1) {
    252253                /* First, test if we are canceled */
    253                 CHECK_POSIX_DO( pthread_mutex_unlock(&mtx), goto error );
    254254                pthread_testcancel();
    255                 CHECK_POSIX_DO( pthread_mutex_lock(&mtx), goto error );
    256                
    257                 pthread_cleanup_push( fd_cleanup_mutex, &mtx );
    258255               
    259256                /* Send all messages that are ready (free resources before using new ones) */
    260                 CHECK_FCT_DO( send_all_ready(), goto error_ul );
     257                CHECK_FCT_DO( send_all_ready(), break );
    261258               
    262259                /* Now process the new messages in input list for duplicate filter */
    263                 CHECK_FCT_DO( do_duplicates(), goto error_ul );
     260                CHECK_FCT_DO( do_duplicates(), break );
    264261               
    265262                /* Now compute the latency for each new item */
    266                 CHECK_FCT_DO( do_latency(), goto error_ul );
     263                CHECK_FCT_DO( do_latency(), break );
    267264               
    268265                /* Now, wait then loop */
    269266                if (FD_IS_LIST_EMPTY(&waitlist)) {
    270                         CHECK_POSIX_DO( pthread_cond_wait(&cnd, &mtx), goto error_ul );
     267                        CHECK_POSIX_DO( pthread_cond_wait(&cnd, &mtx), break );
    271268                } else {
    272269                        CHECK_POSIX_DO2( pthread_cond_timedwait(&cnd, &mtx, &((struct process_item *)(waitlist.next))->ts),
    273270                                ETIMEDOUT, /* ETIMEDOUT is a normal return value, continue */,
    274                                         /* on other error, */ goto error_ul );
     271                                        /* on other error, */ break );
    275272                }
    276273               
    277                 pthread_cleanup_pop( 0 );
    278274                /* loop */
    279275        }
    280276
    281 error_ul:
     277        pthread_cleanup_pop( 0 );
    282278        CHECK_POSIX_DO( pthread_mutex_unlock(&mtx),  );
    283279error:
  • libfdcore/core.c

    r689 r691  
    241241int fd_core_waitstartcomplete(void)
    242242{
     243        int ret = 0;
     244       
    243245        TRACE_ENTRY("");
    244246       
     
    246248        pthread_cleanup_push( fd_cleanup_mutex, &is_ready_mtx );
    247249       
    248         while (!is_ready) {
    249                 CHECK_POSIX( pthread_cond_wait( &is_ready_cnd, &is_ready_mtx ) );
     250        while (!ret && !is_ready) {
     251                CHECK_POSIX_DO( ret = pthread_cond_wait( &is_ready_cnd, &is_ready_mtx ), );
    250252        }
    251253       
     
    253255        CHECK_POSIX( pthread_mutex_unlock( &is_ready_mtx ) );
    254256       
    255         return 0;
     257        return ret;
    256258}
    257259
  • libfdcore/p_cnx.c

    r662 r691  
    211211        struct next_conn * nc = NULL;
    212212        int rebuilt = 0;
     213        int fatal_error=0;
    213214       
    214215        TRACE_ENTRY("%p", arg);
     
    226227                if (FD_IS_LIST_EMPTY(&peer->p_connparams)) {
    227228                        if (! rebuilt) {
    228                                 CHECK_FCT_DO( prepare_connection_list(peer), goto fatal_error );
     229                                CHECK_FCT_DO( fatal_error = prepare_connection_list(peer), goto out );
    229230                                rebuilt ++;
    230231                        }
     
    232233                                /* We encountered an error or we have looped over all the addresses of the peer. */
    233234                                TRACE_DEBUG(INFO, "Unable to connect to the peer %s, aborting attempts for now.", peer->p_hdr.info.pi_diamid);
    234                                 CHECK_FCT_DO( fd_event_send(peer->p_events, FDEVP_CNX_FAILED, 0, NULL), goto fatal_error );
     235                                CHECK_FCT_DO( fatal_error = fd_event_send(peer->p_events, FDEVP_CNX_FAILED, 0, NULL), goto out );
    235236                                return NULL;
    236237                        }
     
    277278                                empty_connection_list(peer);
    278279                                fd_ep_filter(&peer->p_hdr.info.pi_endpoints, EP_FL_CONF);
    279                                 return NULL;
     280                                goto out_pop;
    280281                        } );
    281282        } else {
    282283                /* Prepare to receive the next message */
    283                 CHECK_FCT_DO( fd_cnx_start_clear(cnx, 0), goto fatal_error );
     284                CHECK_FCT_DO( fatal_error = fd_cnx_start_clear(cnx, 0), goto out_pop );
    284285        }
    285286       
    286287        /* Upon success, generate FDEVP_CNX_ESTABLISHED */
    287         CHECK_FCT_DO( fd_event_send(peer->p_events, FDEVP_CNX_ESTABLISHED, 0, cnx), goto fatal_error );
    288        
     288        CHECK_FCT_DO( fatal_error = fd_event_send(peer->p_events, FDEVP_CNX_ESTABLISHED, 0, cnx),  );
     289out_pop:
     290        ;       
    289291        pthread_cleanup_pop(0);
    290292       
    291         return NULL;
    292        
    293 fatal_error:
    294         /* Cleanup the connection */
    295         if (cnx)
    296                 fd_cnx_destroy(cnx);
    297 
    298         /* Generate a termination event */
    299         CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), );
     293out:
     294       
     295        if (fatal_error) {
     296       
     297                /* Cleanup the connection */
     298                if (cnx)
     299                        fd_cnx_destroy(cnx);
     300
     301                /* Generate a termination event */
     302                CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), );
     303        }
    300304       
    301305        return NULL;
  • libfdcore/p_expiry.c

    r686 r691  
    9898        TRACE_ENTRY( "%p", arg );
    9999       
    100         CHECK_POSIX_DO( pthread_mutex_lock(&exp_mtx),  goto error );
     100        CHECK_POSIX_DO( pthread_mutex_lock(&exp_mtx), { ASSERT(0); } );
    101101        pthread_cleanup_push( fd_cleanup_mutex, &exp_mtx );
    102102       
     
    108108                if (FD_IS_LIST_EMPTY(&exp_list)) {
    109109                        /* Just wait for a change or cancelation */
    110                         CHECK_POSIX_DO( pthread_cond_wait( &exp_cnd, &exp_mtx ), goto error );
     110                        CHECK_POSIX_DO( pthread_cond_wait( &exp_cnd, &exp_mtx ), { ASSERT(0); } );
    111111                        /* Restart the loop on wakeup */
    112112                        continue;
     
    118118               
    119119                /* Get the current time */
    120                 CHECK_SYS_DO(  clock_gettime(CLOCK_REALTIME, &now),  goto error  );
     120                CHECK_SYS_DO(  clock_gettime(CLOCK_REALTIME, &now),  { ASSERT(0); }  );
    121121
    122122                /* If first peer is not expired, we just wait until it happens */
     
    125125                        CHECK_POSIX_DO2(  pthread_cond_timedwait( &exp_cnd, &exp_mtx, &first->p_exp_timer ), 
    126126                                        ETIMEDOUT, /* ETIMEDOUT is a normal return value, continue */,
    127                                         /* on other error, */ goto error );
     127                                        /* on other error, */ { ASSERT(0); } );
    128128       
    129129                        /* on wakeup, loop */
     
    133133                /* Now, the first peer in the list is expired; signal it */
    134134                fd_list_unlink( &first->p_expiry );
    135                 CHECK_FCT_DO( fd_event_send(first->p_events, FDEVP_TERMINATE, 0, "DO_NOT_WANT_TO_TALK_TO_YOU"), goto error );
     135                CHECK_FCT_DO( fd_event_send(first->p_events, FDEVP_TERMINATE, 0, "DO_NOT_WANT_TO_TALK_TO_YOU"), break );
    136136               
    137137        } while (1);
    138138       
    139139        pthread_cleanup_pop( 1 );
    140 error:
     140
    141141        TRACE_DEBUG(INFO, "An error occurred in peers module! Expiry thread is terminating...");
    142         ASSERT(0);
    143142        CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), );
    144143        return NULL;
  • libfdcore/p_out.c

    r688 r691  
    6969        /* Save a request before sending so that there is no race condition with the answer */
    7070        if (msg_is_a_req) {
    71                 CHECK_FCT_DO( ret = fd_p_sr_store(srl, msg, &hdr->msg_hbhid, bkp_hbh), { free(buf); return ret; } );
     71                CHECK_FCT_DO( ret = fd_p_sr_store(srl, msg, &hdr->msg_hbhid, bkp_hbh), goto out );
    7272        }
    7373       
    7474        /* Send the message */
    75         CHECK_FCT_DO( ret = fd_cnx_send(cnx, buf, sz, flags), { free(buf); return ret; } );
     75        CHECK_FCT_DO( ret = fd_cnx_send(cnx, buf, sz, flags), );
     76out:
     77        ;       
    7678        pthread_cleanup_pop(1);
     79       
     80        if (ret)
     81                return ret;
    7782       
    7883        /* Free remaining messages (i.e. answers) */
  • libfdcore/p_psm.c

    r688 r691  
    5555static int fd_psm_waitstart()
    5656{
     57        int ret = 0;
    5758        TRACE_ENTRY("");
    5859        CHECK_POSIX( pthread_mutex_lock(&started_mtx) );
    5960awake: 
    60         if (! started) {
     61        if (!ret && !started) {
    6162                pthread_cleanup_push( fd_cleanup_mutex, &started_mtx );
    62                 CHECK_POSIX( pthread_cond_wait(&started_cnd, &started_mtx) );
     63                CHECK_POSIX_DO( ret = pthread_cond_wait(&started_cnd, &started_mtx), );
    6364                pthread_cleanup_pop( 0 );
    6465                goto awake;
    6566        }
    6667        CHECK_POSIX( pthread_mutex_unlock(&started_mtx) );
    67         return 0;
     68        return ret;
    6869}
    6970
  • libfdcore/p_sr.c

    r688 r691  
    117117}
    118118
    119 /* thread that handles messages expiring. The thread is started / stopped only when needed */
     119/* thread that handles messages expiring. The thread is started / cancelled only when needed */
    120120static void * sr_expiry_th(void * arg) {
    121121        struct sr_list * srlist = arg;
     
    184184        ; /* pthread_cleanup_pop sometimes expands as "} ..." and the label beofre this cause some compilers to complain... */
    185185        pthread_cleanup_pop( 1 );
     186        ASSERT(0); /* we have encountered a problem, maybe time to signal the framework to terminate? */
    186187        return NULL;
    187188}
  • libfdcore/peers.c

    r688 r691  
    533533                int auth = 0;
    534534                pthread_cleanup_push(fd_cleanup_rwlock, &validators_rw);
    535                 CHECK_FCT_DO( ret = ((int(*)(struct peer_info *, int *, int (**)(struct peer_info *)))(v->o)) (&peer->p_hdr.info, &auth, &peer->p_cb2), goto out );
     535                CHECK_FCT_DO( ret = ((int(*)(struct peer_info *, int *, int (**)(struct peer_info *)))(v->o)) (&peer->p_hdr.info, &auth, &peer->p_cb2), );
    536536                pthread_cleanup_pop(0);
     537                if (ret)
     538                        goto out;
    537539                if (auth) {
    538540                        ret = (auth > 0) ? 0 : -1;
  • libfdcore/routing_dispatch.c

    r688 r691  
    10001000                {
    10011001                        int must_stop;
    1002                         CHECK_POSIX_DO( pthread_mutex_lock(&order_lock), goto end ); /* we lock to flush the caches */
     1002                        CHECK_POSIX_DO( pthread_mutex_lock(&order_lock), { ASSERT(0); } ); /* we lock to flush the caches */
    10031003                        must_stop = (order_val == STOP);
    1004                         CHECK_POSIX_DO( pthread_mutex_unlock(&order_lock), goto end );
     1004                        CHECK_POSIX_DO( pthread_mutex_unlock(&order_lock), { ASSERT(0); } );
    10051005                        if (must_stop)
    10061006                                goto end;
  • libfdcore/sctp.c

    r662 r691  
    822822       
    823823        /* Set the socket options */
    824         CHECK_FCT_DO( ret = fd_setsockopt_prebind(*sock), goto fail );
     824        CHECK_FCT_DO( ret = fd_setsockopt_prebind(*sock), goto out );
    825825       
    826826        /* Create the array of addresses, add first the configured addresses, then the discovered, then the other ones */
    827         CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF,              EP_FL_CONF        ), goto fail );
    828         CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF | EP_FL_DISC, EP_FL_DISC        ), goto fail );
    829         CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF | EP_FL_DISC, 0         ), goto fail );
     827        CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF,              EP_FL_CONF        ), goto out );
     828        CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF | EP_FL_DISC, EP_FL_DISC        ), goto out );
     829        CHECK_FCT_DO( ret = add_addresses_from_list_mask(&sar.buf, &size, &count, family, htons(port), list, EP_FL_CONF | EP_FL_DISC, 0         ), goto out );
    830830       
    831831        /* Try connecting */
     
    866866                /* Some errors are expected, we log at different level */
    867867                TRACE_DEBUG( lvl, "sctp_connectx returned an error: %s", strerror(ret));
    868                 goto fail;
     868                goto out;
    869869        }
    870870       
     
    872872       
    873873        /* Set the remaining sockopts */
    874         CHECK_FCT_DO( ret = fd_setsockopt_postbind(*sock, 1), goto fail_deco );
    875        
    876         /* Done! */
     874        CHECK_FCT_DO( ret = fd_setsockopt_postbind(*sock, 1),
     875                {
     876                        CHECK_SYS_DO( shutdown(*sock, SHUT_RDWR), /* continue */ );
     877                } );
     878       
     879out:
     880        ;
    877881        pthread_cleanup_pop(0);
    878         return 0;
    879        
    880 fail_deco:
    881         CHECK_SYS_DO( shutdown(*sock, SHUT_RDWR), /* continue */ );
    882 fail:
    883         if (*sock > 0) {
    884                 CHECK_SYS_DO( close(*sock), /* continue */ );
    885                 *sock = -1;
    886         }
    887         free(sar.buf);
     882       
     883        if (ret) {
     884                if (*sock > 0) {
     885                        CHECK_SYS_DO( close(*sock), /* continue */ );
     886                        *sock = -1;
     887                }
     888                free(sar.buf);
     889        }
    888890        return ret;
    889891}
  • libfdcore/server.c

    r688 r691  
    151151        pthread_cleanup_push((void *)fd_cnx_destroy, c->conn);
    152152        pthread_cleanup_push((void *)fd_msg_free, msg);
    153         CHECK_FCT_DO( fd_peer_handle_newCER( &msg, &c->conn ), goto cleanup );
     153        CHECK_FCT_DO( fd_peer_handle_newCER( &msg, &c->conn ), );
    154154        pthread_cleanup_pop(0);
    155155        pthread_cleanup_pop(0);
  • libfdcore/tcp.c

    r662 r691  
    134134        CHECK_SYS(  s = socket(sa->sa_family, SOCK_STREAM, IPPROTO_TCP)  );
    135135       
     136        /* Set the socket options */
     137        CHECK_FCT(  fd_tcp_setsockopt(sa->sa_family, s)  );
     138       
    136139        /* Cleanup if we are cancelled */
    137140        pthread_cleanup_push(fd_cleanup_socket, &s);
    138        
    139         /* Set the socket options */
    140         CHECK_FCT(  fd_tcp_setsockopt(sa->sa_family, s)  );
    141141       
    142142        TRACE_DEBUG_sSA(FULL, "Attempting TCP connection with peer: ", sa, NI_NUMERICHOST | NI_NUMERICSERV, "..." );
  • libfdproto/sessions.c

    r686 r691  
    176176                if (FD_IS_LIST_EMPTY(&exp_sentinel)) {
    177177                        /* Just wait for a change or cancelation */
    178                         CHECK_POSIX_DO( pthread_cond_wait( &exp_cond, &exp_lock ), break );
     178                        CHECK_POSIX_DO( pthread_cond_wait( &exp_cond, &exp_lock ), break /* this might not pop the cleanup handler, but since we ASSERT(0), it is not the big issue... */ );
    179179                        /* Restart the loop on wakeup */
    180180                        goto again;
     
    207207        } while (1);
    208208       
    209         TRACE_DEBUG(INFO, "An error occurred in session module! Expiry thread is terminating...");
     209        TRACE_DEBUG(INFO, "A system error occurred in session module! Expiry thread is terminating...");
    210210        ASSERT(0);
    211211        return NULL;
     
    442442                /* We added a new expiring element, we must signal */
    443443                if (li == &exp_sentinel) {
    444                         CHECK_POSIX( pthread_cond_signal(&exp_cond) );
     444                        CHECK_POSIX_DO( pthread_cond_signal(&exp_cond), { ASSERT(0); } ); /* if it fails, we might not pop the cleanup handlers, but this should not happen -- and we'd have a serious problem otherwise */
    445445                }
    446446               
     
    458458                /* We're done */
    459459                pthread_cleanup_pop(0);
    460                 CHECK_POSIX( pthread_mutex_unlock( &exp_lock ) );
     460                CHECK_POSIX_DO( pthread_mutex_unlock( &exp_lock ), { ASSERT(0); } ); /* if it fails, we might not pop the cleanup handler, but this should not happen -- and we'd have a serious problem otherwise */
    461461        }
    462462       
     
    540540        /* We added a new expiring element, we must signal if it was in first position */
    541541        if (session->expire.prev == &exp_sentinel) {
    542                 CHECK_POSIX( pthread_cond_signal(&exp_cond) );
     542                CHECK_POSIX_DO( pthread_cond_signal(&exp_cond), { ASSERT(0); /* so that we don't have a pending cancellation handler */ } );
    543543        }
    544544
     
    573573       
    574574        /* Unlink and invalidate */
    575         CHECK_FCT( pthread_mutex_lock( H_LOCK(sess->hash) ) );
     575        CHECK_POSIX( pthread_mutex_lock( H_LOCK(sess->hash) ) );
    576576        pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(sess->hash) );
    577         CHECK_FCT( pthread_mutex_lock( &exp_lock ) );
     577        CHECK_POSIX_DO( pthread_mutex_lock( &exp_lock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
    578578        fd_list_unlink( &sess->chain_h );
    579579        fd_list_unlink( &sess->expire ); /* no need to signal the condition here */
    580580        sess->eyec = 0xdead;
    581         CHECK_FCT( pthread_mutex_unlock( &exp_lock ) );
     581        CHECK_POSIX_DO( pthread_mutex_unlock( &exp_lock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
    582582        pthread_cleanup_pop(0);
    583         CHECK_FCT( pthread_mutex_unlock( H_LOCK(sess->hash) ) );
     583        CHECK_POSIX( pthread_mutex_unlock( H_LOCK(sess->hash) ) );
    584584       
    585585        /* Now destroy all states associated -- we don't take the lock since nobody can access this session anymore (in theory) */
     
    614614        CHECK_POSIX( pthread_mutex_lock( H_LOCK(sess->hash) ) );
    615615        pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(sess->hash) );
    616         CHECK_POSIX( pthread_mutex_lock( &exp_lock ) );
     616        CHECK_POSIX_DO( pthread_mutex_lock( &exp_lock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
    617617        if (FD_IS_LIST_EMPTY(&sess->states)) {
    618618                fd_list_unlink( &sess->chain_h );
     
    622622                free(sess);
    623623        }
    624         CHECK_POSIX( pthread_mutex_unlock( &exp_lock ) );
     624        CHECK_POSIX_DO( pthread_mutex_unlock( &exp_lock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
    625625        pthread_cleanup_pop(0);
    626626        CHECK_POSIX( pthread_mutex_unlock( H_LOCK(hash) ) );
     
    635635        struct fd_list * li;
    636636        int already = 0;
     637        int ret = 0;
    637638       
    638639        TRACE_ENTRY("%p %p %p", handler, session, state);
     
    644645                       
    645646        /* Create the new state object */
    646         CHECK_MALLOC(new = malloc(sizeof(struct state)) );
     647        CHECK_MALLOC_DO(new = malloc(sizeof(struct state)), { ret = ENOMEM; goto out; } );
    647648        memset(new, 0, sizeof(struct state));
    648649       
     
    661662                if (st->hdl->id == handler->id) {
    662663                        TRACE_DEBUG(INFO, "A state was already stored for session '%s' and handler '%p', at location %p", session->sid, st->hdl, st->state);
    663                         already = 1;
     664                        already = EALREADY;
    664665                }
    665666               
     
    673674                free(new);
    674675        }
    675        
     676out:
     677        ;       
    676678        pthread_cleanup_pop(0);
    677679        CHECK_POSIX( pthread_mutex_unlock(&session->stlock) );
    678680       
    679         return already ? EALREADY : 0;
     681        return ret ?: already;
    680682}
    681683
Note: See TracChangeset for help on using the changeset viewer.