LCOV - code coverage report
Current view: top level - bank-lib - fakebank_stop.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 52 63 82.5 %
Date: 2025-06-22 12:09:43 Functions: 2 3 66.7 %

          Line data    Source code
       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 "taler/platform.h"
      25             : #include <pthread.h>
      26             : #include <poll.h>
      27             : #ifdef __linux__
      28             : #include <sys/eventfd.h>
      29             : #endif
      30             : #include "taler/taler_fakebank_lib.h"
      31             : #include "taler/taler_bank_service.h"
      32             : #include "taler/taler_mhd_lib.h"
      33             : #include <gnunet/gnunet_mhd_compat.h>
      34             : #include "fakebank.h"
      35             : #include "fakebank_common_lp.h"
      36             : 
      37             : 
      38             : /**
      39             :  * Helper function to free memory when finished.
      40             :  *
      41             :  * @param cls NULL
      42             :  * @param key key of the account to free (ignored)
      43             :  * @param val a `struct Account` to free.
      44             :  */
      45             : static enum GNUNET_GenericReturnValue
      46          45 : free_account (void *cls,
      47             :               const struct GNUNET_HashCode *key,
      48             :               void *val)
      49             : {
      50          45 :   struct Account *account = val;
      51             : 
      52             :   (void) cls;
      53             :   (void) key;
      54          45 :   GNUNET_assert (NULL == account->lp_head);
      55          45 :   GNUNET_free (account->account_name);
      56          45 :   GNUNET_free (account->receiver_name);
      57          45 :   GNUNET_free (account->payto_uri);
      58          45 :   GNUNET_free (account->password);
      59          45 :   GNUNET_free (account);
      60          45 :   return GNUNET_OK;
      61             : }
      62             : 
      63             : 
      64             : /**
      65             :  * Helper function to free memory when finished.
      66             :  *
      67             :  * @param cls NULL
      68             :  * @param key key of the operation to free (ignored)
      69             :  * @param val a `struct WithdrawalOperation *` to free.
      70             :  */
      71             : static enum GNUNET_GenericReturnValue
      72           0 : free_withdraw_op (void *cls,
      73             :                   const struct GNUNET_ShortHashCode *key,
      74             :                   void *val)
      75             : {
      76           0 :   struct WithdrawalOperation *wo = val;
      77             : 
      78             :   (void) cls;
      79             :   (void) key;
      80           0 :   GNUNET_free (wo->amount);
      81           0 :   GNUNET_free (wo);
      82           0 :   return GNUNET_OK;
      83             : }
      84             : 
      85             : 
      86             : void
      87          15 : TALER_FAKEBANK_stop (struct TALER_FAKEBANK_Handle *h)
      88             : {
      89          15 :   if (NULL != h->lp_task)
      90             :   {
      91           0 :     GNUNET_SCHEDULER_cancel (h->lp_task);
      92           0 :     h->lp_task = NULL;
      93             :   }
      94             : #if EPOLL_SUPPORT
      95          15 :   if (NULL != h->mhd_rfd)
      96             :   {
      97           1 :     GNUNET_NETWORK_socket_free_memory_only_ (h->mhd_rfd);
      98           1 :     h->mhd_rfd = NULL;
      99             :   }
     100             : #endif
     101             : #ifdef __linux__
     102          15 :   if (-1 != h->lp_event)
     103             : #else
     104             :   if (-1 != h->lp_event_in && -1 != h->lp_event_out)
     105             : #endif
     106             :   {
     107          14 :     uint64_t val = 1;
     108             :     void *ret;
     109             :     struct LongPoller *lp;
     110             : 
     111          14 :     GNUNET_assert (0 ==
     112             :                    pthread_mutex_lock (&h->big_lock));
     113          14 :     h->in_shutdown = true;
     114          14 :     while (NULL != (lp = GNUNET_CONTAINER_heap_remove_root (h->lp_heap)))
     115           0 :       TALER_FAKEBANK_lp_trigger_ (lp);
     116          14 :     GNUNET_assert (0 ==
     117             :                    pthread_mutex_unlock (&h->big_lock));
     118             : #ifdef __linux__
     119          14 :     GNUNET_break (sizeof (val) ==
     120             :                   write (h->lp_event,
     121             :                          &val,
     122             :                          sizeof (val)));
     123             : #else
     124             :     GNUNET_break (sizeof (val) ==
     125             :                   write (h->lp_event_in,
     126             :                          &val,
     127             :                          sizeof (val)));
     128             : #endif
     129          14 :     GNUNET_break (0 ==
     130             :                   pthread_join (h->lp_thread,
     131             :                                 &ret));
     132          14 :     GNUNET_break (NULL == ret);
     133             : #ifdef __linux__
     134          14 :     GNUNET_break (0 == close (h->lp_event));
     135          14 :     h->lp_event = -1;
     136             : #else
     137             :     GNUNET_break (0 == close (h->lp_event_in));
     138             :     GNUNET_break (0 == close (h->lp_event_out));
     139             :     h->lp_event_in = -1;
     140             :     h->lp_event_out = -1;
     141             : #endif
     142             :   }
     143             :   else
     144             :   {
     145             :     struct LongPoller *lp;
     146             : 
     147           1 :     while (NULL != (lp = GNUNET_CONTAINER_heap_remove_root (h->lp_heap)))
     148           0 :       TALER_FAKEBANK_lp_trigger_ (lp);
     149             :   }
     150          15 :   if (NULL != h->mhd_bank)
     151             :   {
     152          15 :     MHD_stop_daemon (h->mhd_bank);
     153          15 :     h->mhd_bank = NULL;
     154             :   }
     155          15 :   if (NULL != h->mhd_task)
     156             :   {
     157           1 :     GNUNET_SCHEDULER_cancel (h->mhd_task);
     158           1 :     h->mhd_task = NULL;
     159             :   }
     160          15 :   if (NULL != h->accounts)
     161             :   {
     162          15 :     GNUNET_CONTAINER_multihashmap_iterate (h->accounts,
     163             :                                            &free_account,
     164             :                                            NULL);
     165          15 :     GNUNET_CONTAINER_multihashmap_destroy (h->accounts);
     166             :   }
     167          15 :   if (NULL != h->wops)
     168             :   {
     169           0 :     GNUNET_CONTAINER_multishortmap_iterate (h->wops,
     170             :                                             &free_withdraw_op,
     171             :                                             NULL);
     172           0 :     GNUNET_CONTAINER_multishortmap_destroy (h->wops);
     173             :   }
     174          15 :   GNUNET_CONTAINER_multihashmap_destroy (h->uuid_map);
     175          15 :   GNUNET_CONTAINER_multipeermap_destroy (h->rpubs);
     176          15 :   GNUNET_CONTAINER_heap_destroy (h->lp_heap);
     177          15 :   GNUNET_assert (0 ==
     178             :                  pthread_mutex_destroy (&h->big_lock));
     179          15 :   GNUNET_assert (0 ==
     180             :                  pthread_mutex_destroy (&h->uuid_map_lock));
     181          15 :   GNUNET_assert (0 ==
     182             :                  pthread_mutex_destroy (&h->accounts_lock));
     183          15 :   GNUNET_assert (0 ==
     184             :                  pthread_mutex_destroy (&h->rpubs_lock));
     185      983087 :   for (uint64_t i = 0; i<h->ram_limit; i++)
     186      983072 :     GNUNET_free (h->transactions[i]);
     187          15 :   GNUNET_free (h->transactions);
     188          15 :   GNUNET_free (h->my_baseurl);
     189          15 :   GNUNET_free (h->currency);
     190          15 :   GNUNET_free (h->exchange_url);
     191          15 :   GNUNET_free (h->hostname);
     192          15 :   GNUNET_free (h);
     193          15 : }

Generated by: LCOV version 1.16