lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <bits/libc-lock.h> |
| 3 | #include <rpc/rpc.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include <bits/libc-lock.h> |
| 7 | #include <bits/libc-tsd.h> |
| 8 | |
| 9 | #ifdef _RPC_THREAD_SAFE_ |
| 10 | |
| 11 | /* Variable used in non-threaded applications or for the first thread. */ |
| 12 | static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem; |
| 13 | __libc_tsd_define (, struct rpc_thread_variables *, RPC_VARS) |
| 14 | |
| 15 | /* |
| 16 | * Task-variable destructor |
| 17 | */ |
| 18 | void __attribute__ ((section ("__libc_thread_freeres_fn"))) |
| 19 | __rpc_thread_destroy (void) |
| 20 | { |
| 21 | struct rpc_thread_variables *tvp |
| 22 | = __libc_tsd_get (struct rpc_thread_variables *, RPC_VARS); |
| 23 | |
| 24 | if (tvp != NULL) { |
| 25 | __rpc_thread_svc_cleanup (); |
| 26 | __rpc_thread_clnt_cleanup (); |
| 27 | __rpc_thread_key_cleanup (); |
| 28 | free (tvp->clnt_perr_buf_s); |
| 29 | free (tvp->clntraw_private_s); |
| 30 | free (tvp->svcraw_private_s); |
| 31 | free (tvp->authdes_cache_s); |
| 32 | free (tvp->authdes_lru_s); |
| 33 | free (tvp->svc_xports_s); |
| 34 | free (tvp->svc_pollfd_s); |
| 35 | if (tvp != &__libc_tsd_RPC_VARS_mem) |
| 36 | free (tvp); |
| 37 | __libc_tsd_set (struct rpc_thread_variables *, RPC_VARS, NULL); |
| 38 | } |
| 39 | } |
| 40 | #ifdef _LIBC_REENTRANT |
| 41 | text_set_element (__libc_thread_subfreeres, __rpc_thread_destroy); |
| 42 | #endif |
| 43 | text_set_element (__libc_subfreeres, __rpc_thread_destroy); |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Initialize RPC multi-threaded operation |
| 48 | */ |
| 49 | static void |
| 50 | rpc_thread_multi (void) |
| 51 | { |
| 52 | __libc_tsd_set (struct rpc_thread_variables *, RPC_VARS, |
| 53 | &__libc_tsd_RPC_VARS_mem); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | struct rpc_thread_variables * |
| 58 | __rpc_thread_variables (void) |
| 59 | { |
| 60 | __libc_once_define (static, once); |
| 61 | struct rpc_thread_variables *tvp; |
| 62 | |
| 63 | tvp = __libc_tsd_get (struct rpc_thread_variables *, RPC_VARS); |
| 64 | if (tvp == NULL) { |
| 65 | __libc_once (once, rpc_thread_multi); |
| 66 | tvp = __libc_tsd_get (struct rpc_thread_variables *, RPC_VARS); |
| 67 | if (tvp == NULL) { |
| 68 | tvp = calloc (1, sizeof *tvp); |
| 69 | if (tvp != NULL) |
| 70 | __libc_tsd_set (struct rpc_thread_variables *, |
| 71 | RPC_VARS, tvp); |
| 72 | else |
| 73 | tvp = __libc_tsd_get (struct rpc_thread_variables *, |
| 74 | RPC_VARS); |
| 75 | } |
| 76 | } |
| 77 | return tvp; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* Global variables If we're single-threaded, or if this is the first |
| 82 | thread using the variable, use the existing global variable. This |
| 83 | provides backwards compatibility for existing applications which |
| 84 | dynamically link against this code. */ |
| 85 | #undef svc_fdset |
| 86 | #undef rpc_createerr |
| 87 | #undef svc_pollfd |
| 88 | #undef svc_max_pollfd |
| 89 | |
| 90 | fd_set * |
| 91 | __rpc_thread_svc_fdset (void) |
| 92 | { |
| 93 | struct rpc_thread_variables *tvp; |
| 94 | |
| 95 | tvp = __rpc_thread_variables (); |
| 96 | if (tvp == &__libc_tsd_RPC_VARS_mem) |
| 97 | return &svc_fdset; |
| 98 | return &tvp->svc_fdset_s; |
| 99 | } |
| 100 | libc_hidden_nolink_sunrpc (__rpc_thread_svc_fdset, GLIBC_2_2_3) |
| 101 | |
| 102 | struct rpc_createerr * |
| 103 | __rpc_thread_createerr (void) |
| 104 | { |
| 105 | struct rpc_thread_variables *tvp; |
| 106 | |
| 107 | tvp = __rpc_thread_variables (); |
| 108 | if (tvp == &__libc_tsd_RPC_VARS_mem) |
| 109 | return &rpc_createerr; |
| 110 | return &tvp->rpc_createerr_s; |
| 111 | } |
| 112 | libc_hidden_nolink_sunrpc (__rpc_thread_createerr, GLIBC_2_2_3) |
| 113 | |
| 114 | struct pollfd ** |
| 115 | __rpc_thread_svc_pollfd (void) |
| 116 | { |
| 117 | struct rpc_thread_variables *tvp; |
| 118 | |
| 119 | tvp = __rpc_thread_variables (); |
| 120 | if (tvp == &__libc_tsd_RPC_VARS_mem) |
| 121 | return &svc_pollfd; |
| 122 | return &tvp->svc_pollfd_s; |
| 123 | } |
| 124 | #ifdef EXPORT_RPC_SYMBOLS |
| 125 | libc_hidden_def (__rpc_thread_svc_pollfd) |
| 126 | #else |
| 127 | libc_hidden_nolink_sunrpc (__rpc_thread_svc_pollfd, GLIBC_2_2_3) |
| 128 | #endif |
| 129 | |
| 130 | int * |
| 131 | __rpc_thread_svc_max_pollfd (void) |
| 132 | { |
| 133 | struct rpc_thread_variables *tvp; |
| 134 | |
| 135 | tvp = __rpc_thread_variables (); |
| 136 | if (tvp == &__libc_tsd_RPC_VARS_mem) |
| 137 | return &svc_max_pollfd; |
| 138 | return &tvp->svc_max_pollfd_s; |
| 139 | } |
| 140 | #ifdef EXPORT_RPC_SYMBOLS |
| 141 | libc_hidden_def (__rpc_thread_svc_max_pollfd) |
| 142 | #else |
| 143 | libc_hidden_nolink_sunrpc (__rpc_thread_svc_max_pollfd, GLIBC_2_2_3) |
| 144 | #endif |
| 145 | |
| 146 | #endif /* _RPC_THREAD_SAFE_ */ |