fakebank_stop.c (5322B)
1 /* 2 This file is part of TALER 3 (C) 2016-2023 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License 7 as published by the Free Software Foundation; either version 3, 8 or (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, 17 see <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file bank-lib/fakebank_stop.c 21 * @brief library that fakes being a Taler bank for testcases 22 * @author Christian Grothoff <christian@grothoff.org> 23 */ 24 #include <pthread.h> 25 #include <poll.h> 26 #ifdef __linux__ 27 #include <sys/eventfd.h> 28 #endif 29 #include "taler/taler_fakebank_lib.h" 30 #include "taler/taler_bank_service.h" 31 #include "taler/taler_mhd_lib.h" 32 #include <gnunet/gnunet_mhd_compat.h> 33 #include "fakebank.h" 34 #include "fakebank_common_lp.h" 35 36 37 /** 38 * Helper function to free memory when finished. 39 * 40 * @param cls NULL 41 * @param key key of the account to free (ignored) 42 * @param val a `struct Account` to free. 43 */ 44 static enum GNUNET_GenericReturnValue 45 free_account (void *cls, 46 const struct GNUNET_HashCode *key, 47 void *val) 48 { 49 struct Account *account = val; 50 51 (void) cls; 52 (void) key; 53 GNUNET_assert (NULL == account->lp_head); 54 GNUNET_free (account->account_name); 55 GNUNET_free (account->receiver_name); 56 GNUNET_free (account->payto_uri); 57 GNUNET_free (account->password); 58 GNUNET_free (account); 59 return GNUNET_OK; 60 } 61 62 63 /** 64 * Helper function to free memory when finished. 65 * 66 * @param cls NULL 67 * @param key key of the operation to free (ignored) 68 * @param val a `struct WithdrawalOperation *` to free. 69 */ 70 static enum GNUNET_GenericReturnValue 71 free_withdraw_op (void *cls, 72 const struct GNUNET_ShortHashCode *key, 73 void *val) 74 { 75 struct WithdrawalOperation *wo = val; 76 77 (void) cls; 78 (void) key; 79 GNUNET_free (wo->amount); 80 GNUNET_free (wo); 81 return GNUNET_OK; 82 } 83 84 85 void 86 TALER_FAKEBANK_stop (struct TALER_FAKEBANK_Handle *h) 87 { 88 if (NULL != h->lp_task) 89 { 90 GNUNET_SCHEDULER_cancel (h->lp_task); 91 h->lp_task = NULL; 92 } 93 #if EPOLL_SUPPORT 94 if (NULL != h->mhd_rfd) 95 { 96 GNUNET_NETWORK_socket_free_memory_only_ (h->mhd_rfd); 97 h->mhd_rfd = NULL; 98 } 99 #endif 100 #ifdef __linux__ 101 if (-1 != h->lp_event) 102 #else 103 if (-1 != h->lp_event_in && -1 != h->lp_event_out) 104 #endif 105 { 106 uint64_t val = 1; 107 void *ret; 108 struct LongPoller *lp; 109 110 GNUNET_assert (0 == 111 pthread_mutex_lock (&h->big_lock)); 112 h->in_shutdown = true; 113 while (NULL != (lp = GNUNET_CONTAINER_heap_remove_root (h->lp_heap))) 114 TALER_FAKEBANK_lp_trigger_ (lp); 115 GNUNET_assert (0 == 116 pthread_mutex_unlock (&h->big_lock)); 117 #ifdef __linux__ 118 GNUNET_break (sizeof (val) == 119 write (h->lp_event, 120 &val, 121 sizeof (val))); 122 #else 123 GNUNET_break (sizeof (val) == 124 write (h->lp_event_in, 125 &val, 126 sizeof (val))); 127 #endif 128 GNUNET_break (0 == 129 pthread_join (h->lp_thread, 130 &ret)); 131 GNUNET_break (NULL == ret); 132 #ifdef __linux__ 133 GNUNET_break (0 == close (h->lp_event)); 134 h->lp_event = -1; 135 #else 136 GNUNET_break (0 == close (h->lp_event_in)); 137 GNUNET_break (0 == close (h->lp_event_out)); 138 h->lp_event_in = -1; 139 h->lp_event_out = -1; 140 #endif 141 } 142 else 143 { 144 struct LongPoller *lp; 145 146 while (NULL != (lp = GNUNET_CONTAINER_heap_remove_root (h->lp_heap))) 147 TALER_FAKEBANK_lp_trigger_ (lp); 148 } 149 if (NULL != h->mhd_bank) 150 { 151 MHD_stop_daemon (h->mhd_bank); 152 h->mhd_bank = NULL; 153 } 154 if (NULL != h->mhd_task) 155 { 156 GNUNET_SCHEDULER_cancel (h->mhd_task); 157 h->mhd_task = NULL; 158 } 159 if (NULL != h->accounts) 160 { 161 GNUNET_CONTAINER_multihashmap_iterate (h->accounts, 162 &free_account, 163 NULL); 164 GNUNET_CONTAINER_multihashmap_destroy (h->accounts); 165 } 166 if (NULL != h->wops) 167 { 168 GNUNET_CONTAINER_multishortmap_iterate (h->wops, 169 &free_withdraw_op, 170 NULL); 171 GNUNET_CONTAINER_multishortmap_destroy (h->wops); 172 } 173 GNUNET_CONTAINER_multihashmap_destroy (h->uuid_map); 174 GNUNET_CONTAINER_multipeermap_destroy (h->rpubs); 175 GNUNET_CONTAINER_heap_destroy (h->lp_heap); 176 GNUNET_assert (0 == 177 pthread_mutex_destroy (&h->big_lock)); 178 GNUNET_assert (0 == 179 pthread_mutex_destroy (&h->uuid_map_lock)); 180 GNUNET_assert (0 == 181 pthread_mutex_destroy (&h->accounts_lock)); 182 GNUNET_assert (0 == 183 pthread_mutex_destroy (&h->rpubs_lock)); 184 for (uint64_t i = 0; i<h->ram_limit; i++) 185 GNUNET_free (h->transactions[i]); 186 GNUNET_free (h->transactions); 187 GNUNET_free (h->my_baseurl); 188 GNUNET_free (h->currency); 189 GNUNET_free (h->exchange_url); 190 GNUNET_free (h->hostname); 191 GNUNET_free (h); 192 }