Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | #include "fdcore-internal.h" |
| 37 | |
| 38 | /* Delay for garbage collection of expired peers, in seconds */ |
| 39 | #define GC_TIME 120 |
| 40 | |
| 41 | static pthread_t exp_thr = (pthread_t)NULL; |
| 42 | static pthread_t gc_thr = (pthread_t)NULL; |
| 43 | static struct fd_list exp_list = FD_LIST_INITIALIZER( exp_list ); |
| 44 | static pthread_cond_t exp_cnd = PTHREAD_COND_INITIALIZER; |
| 45 | static pthread_mutex_t exp_mtx = PTHREAD_MUTEX_INITIALIZER; |
| 46 | |
| 47 | static void * gc_th_fct(void * arg) |
| 48 | { |
| 49 | fd_log_threadname ( "Peers/garb. col." ); |
| 50 | TRACE_ENTRY( "%p", arg ); |
| 51 | |
| 52 | do { |
| 53 | struct fd_list * li, purge = FD_LIST_INITIALIZER(purge); |
| 54 | |
| 55 | sleep(GC_TIME); /* sleep is a cancellation point */ |
| 56 | |
| 57 | /* Now check in the peers list if any peer can be deleted */ |
| 58 | CHECK_FCT_DO( pthread_rwlock_wrlock(&fd_g_peers_rw), goto error ); |
| 59 | |
| 60 | for (li = fd_g_peers.next; li != &fd_g_peers; li = li->next) { |
| 61 | struct fd_peer * peer = (struct fd_peer *)li->o; |
| 62 | |
| 63 | if (fd_peer_getstate(peer) != STATE_ZOMBIE) |
| 64 | continue; |
| 65 | |
| 66 | if (peer->p_hdr.info.config.pic_flags.persist == PI_PRST_ALWAYS) |
| 67 | continue; /* This peer was not supposed to terminate, keep it in the list for debug */ |
| 68 | |
| 69 | /* Ok, the peer was expired, let's remove it */ |
| 70 | li = li->prev; /* to avoid breaking the loop */ |
| 71 | fd_list_unlink(&peer->p_hdr.chain); |
| 72 | fd_list_insert_before(&purge, &peer->p_hdr.chain); |
| 73 | } |
| 74 | |
| 75 | CHECK_FCT_DO( pthread_rwlock_unlock(&fd_g_peers_rw), goto error ); |
| 76 | |
| 77 | /* Now delete peers that are in the purge list */ |
| 78 | while (!FD_IS_LIST_EMPTY(&purge)) { |
| 79 | struct fd_peer * peer = (struct fd_peer *)(purge.next->o); |
| 80 | fd_list_unlink(&peer->p_hdr.chain); |
| 81 | TRACE_DEBUG(INFO, "Garbage Collect: delete zombie peer '%s'", peer->p_hdr.info.pi_diamid); |
| 82 | CHECK_FCT_DO( fd_peer_free(&peer), /* Continue... what else to do ? */ ); |
| 83 | } |
| 84 | } while (1); |
| 85 | |
| 86 | error: |
| 87 | TRACE_DEBUG(INFO, "An error occurred in peers module! GC thread is terminating..."); |
| 88 | ASSERT(0); |
| 89 | CHECK_FCT_DO(fd_core_shutdown(), ); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static void * exp_th_fct(void * arg) |
| 95 | { |
| 96 | fd_log_threadname ( "Peers/expire" ); |
| 97 | TRACE_ENTRY( "%p", arg ); |
| 98 | |
| 99 | CHECK_POSIX_DO( pthread_mutex_lock(&exp_mtx), { ASSERT(0); } ); |
| 100 | pthread_cleanup_push( fd_cleanup_mutex, &exp_mtx ); |
| 101 | |
| 102 | do { |
| 103 | struct timespec now; |
| 104 | struct fd_peer * first; |
| 105 | |
| 106 | /* Check if there are expiring peers available */ |
| 107 | if (FD_IS_LIST_EMPTY(&exp_list)) { |
| 108 | /* Just wait for a change or cancelation */ |
| 109 | CHECK_POSIX_DO( pthread_cond_wait( &exp_cnd, &exp_mtx ), { ASSERT(0); } ); |
| 110 | /* Restart the loop on wakeup */ |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | /* Get the pointer to the peer that expires first */ |
| 115 | first = (struct fd_peer *)(exp_list.next->o); |
| 116 | ASSERT( CHECK_PEER(first) ); |
| 117 | |
| 118 | /* Get the current time */ |
| 119 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), { ASSERT(0); } ); |
| 120 | |
| 121 | /* If first peer is not expired, we just wait until it happens */ |
| 122 | if ( TS_IS_INFERIOR( &now, &first->p_exp_timer ) ) { |
| 123 | |
| 124 | CHECK_POSIX_DO2( pthread_cond_timedwait( &exp_cnd, &exp_mtx, &first->p_exp_timer ), |
| 125 | ETIMEDOUT, /* ETIMEDOUT is a normal return value, continue */, |
| 126 | /* on other error, */ { ASSERT(0); } ); |
| 127 | |
| 128 | /* on wakeup, loop */ |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | /* Now, the first peer in the list is expired; signal it */ |
| 133 | fd_list_unlink( &first->p_expiry ); |
| 134 | CHECK_FCT_DO( fd_event_send(first->p_events, FDEVP_TERMINATE, 0, "DO_NOT_WANT_TO_TALK_TO_YOU"), break ); |
| 135 | |
| 136 | } while (1); |
| 137 | |
| 138 | pthread_cleanup_pop( 1 ); |
| 139 | |
| 140 | TRACE_DEBUG(INFO, "An error occurred in peers module! Expiry thread is terminating..."); |
| 141 | CHECK_FCT_DO(fd_core_shutdown(), ); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | /* Initialize peers expiry mechanism */ |
| 146 | int fd_p_expi_init(void) |
| 147 | { |
| 148 | TRACE_ENTRY(); |
| 149 | CHECK_FCT( pthread_create( &exp_thr, NULL, exp_th_fct, NULL ) ); |
| 150 | CHECK_FCT( pthread_create( &gc_thr, NULL, gc_th_fct, NULL ) ); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* Finish peers expiry mechanism */ |
| 155 | int fd_p_expi_fini(void) |
| 156 | { |
| 157 | CHECK_FCT_DO( fd_thr_term(&exp_thr), ); |
| 158 | CHECK_POSIX( pthread_mutex_lock(&exp_mtx) ); |
| 159 | while (!FD_IS_LIST_EMPTY(&exp_list)) { |
| 160 | struct fd_peer * peer = (struct fd_peer *)(exp_list.next->o); |
| 161 | fd_list_unlink(&peer->p_expiry ); |
| 162 | } |
| 163 | CHECK_POSIX( pthread_mutex_unlock(&exp_mtx) ); |
| 164 | |
| 165 | CHECK_FCT_DO( fd_thr_term(&gc_thr), ); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* Add / requeue a peer in the expiry list */ |
| 170 | int fd_p_expi_update(struct fd_peer * peer ) |
| 171 | { |
| 172 | TRACE_ENTRY("%p", peer); |
| 173 | CHECK_PARAMS( CHECK_PEER(peer) ); |
| 174 | |
| 175 | CHECK_POSIX( pthread_mutex_lock(&exp_mtx) ); |
| 176 | |
| 177 | fd_list_unlink(&peer->p_expiry ); |
| 178 | |
| 179 | /* if peer expires */ |
| 180 | if (peer->p_hdr.info.config.pic_flags.exp) { |
| 181 | struct fd_list * li; |
| 182 | |
| 183 | /* update the p_exp_timer value */ |
| 184 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &peer->p_exp_timer), { ASSERT(0); } ); |
| 185 | peer->p_exp_timer.tv_sec += peer->p_hdr.info.config.pic_lft; |
| 186 | |
| 187 | /* add to the expiry list in appropriate position (probably around the end) */ |
| 188 | for (li = exp_list.prev; li != &exp_list; li = li->prev) { |
| 189 | struct fd_peer * p = (struct fd_peer *)(li->o); |
| 190 | if (TS_IS_INFERIOR( &p->p_exp_timer, &peer->p_exp_timer ) ) |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | fd_list_insert_after(li, &peer->p_expiry); |
| 195 | |
| 196 | /* signal the expiry thread if we added in first position */ |
| 197 | if (li == &exp_list) { |
| 198 | CHECK_POSIX( pthread_cond_signal(&exp_cnd) ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | CHECK_POSIX( pthread_mutex_unlock(&exp_mtx) ); |
| 203 | return 0; |
| 204 | } |
| 205 | |