Navigation


Changeset 691:78b665400097 in freeDiameter for libfdcore


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

Location:
libfdcore
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • 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, "..." );
Note: See TracChangeset for help on using the changeset viewer.