| 1 | /* Copyright (C) 2001-2023 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <netdb.h> |
| 19 | #include <pthread.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <gai_misc.h> |
| 22 | |
| 23 | #if !PTHREAD_IN_LIBC |
| 24 | /* The available function names differ outside of libc. (In libc, we |
| 25 | need to use hidden aliases to avoid the PLT.) */ |
| 26 | #define __pthread_attr_init pthread_attr_init |
| 27 | #define __pthread_attr_setdetachstate pthread_attr_setdetachstate |
| 28 | #define __pthread_cond_signal pthread_cond_signal |
| 29 | #define __pthread_cond_timedwait pthread_cond_timedwait |
| 30 | #define __pthread_create pthread_create |
| 31 | #endif |
| 32 | |
| 33 | struct notify_func |
| 34 | { |
| 35 | void (*func) (sigval_t); |
| 36 | sigval_t value; |
| 37 | }; |
| 38 | |
| 39 | static void * |
| 40 | notify_func_wrapper (void *arg) |
| 41 | { |
| 42 | gai_start_notify_thread (); |
| 43 | struct notify_func *const n = arg; |
| 44 | void (*func) (sigval_t) = n->func; |
| 45 | sigval_t value = n->value; |
| 46 | free (n); |
| 47 | (*func) (value); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | int |
| 53 | __gai_notify_only (struct sigevent *sigev, pid_t caller_pid) |
| 54 | { |
| 55 | int result = 0; |
| 56 | |
| 57 | /* Send the signal to notify about finished processing of the request. */ |
| 58 | if (sigev->sigev_notify == SIGEV_THREAD) |
| 59 | { |
| 60 | /* We have to start a thread. */ |
| 61 | pthread_t tid; |
| 62 | pthread_attr_t attr, *pattr; |
| 63 | |
| 64 | pattr = (pthread_attr_t *) sigev->sigev_notify_attributes; |
| 65 | if (pattr == NULL) |
| 66 | { |
| 67 | __pthread_attr_init (&attr); |
| 68 | __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
| 69 | pattr = &attr; |
| 70 | } |
| 71 | |
| 72 | /* SIGEV may be freed as soon as we return, so we cannot let the |
| 73 | notification thread use that pointer. Even though a sigval_t is |
| 74 | only one word and the same size as a void *, we cannot just pass |
| 75 | the value through pthread_create as the argument and have the new |
| 76 | thread run the user's function directly, because on some machines |
| 77 | the calling convention for a union like sigval_t is different from |
| 78 | that for a pointer type like void *. */ |
| 79 | struct notify_func *nf = malloc (sizeof *nf); |
| 80 | if (nf == NULL) |
| 81 | result = -1; |
| 82 | else |
| 83 | { |
| 84 | nf->func = sigev->sigev_notify_function; |
| 85 | nf->value = sigev->sigev_value; |
| 86 | if (__pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0) |
| 87 | { |
| 88 | free (nf); |
| 89 | result = -1; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | else if (sigev->sigev_notify == SIGEV_SIGNAL) |
| 94 | /* We have to send a signal. */ |
| 95 | if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid) |
| 96 | < 0) |
| 97 | result = -1; |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void |
| 104 | __gai_notify (struct requestlist *req) |
| 105 | { |
| 106 | struct waitlist *waitlist; |
| 107 | |
| 108 | /* Now also notify possibly waiting threads. */ |
| 109 | waitlist = req->waiting; |
| 110 | while (waitlist != NULL) |
| 111 | { |
| 112 | struct waitlist *next = waitlist->next; |
| 113 | |
| 114 | if (waitlist->sigevp == NULL) |
| 115 | { |
| 116 | #ifdef DONT_NEED_GAI_MISC_COND |
| 117 | GAI_MISC_NOTIFY (waitlist); |
| 118 | #else |
| 119 | /* Decrement the counter. */ |
| 120 | --*waitlist->counterp; |
| 121 | |
| 122 | pthread_cond_signal (waitlist->cond); |
| 123 | #endif |
| 124 | } |
| 125 | else |
| 126 | /* This is part of an asynchronous `getaddrinfo_a' operation. If |
| 127 | this request is the last one, send the signal. */ |
| 128 | if (--*waitlist->counterp == 0) |
| 129 | { |
| 130 | __gai_notify_only (waitlist->sigevp, waitlist->caller_pid); |
| 131 | /* This is tricky. See getaddrinfo_a.c for the reason why |
| 132 | this works. */ |
| 133 | free ((void *) waitlist->counterp); |
| 134 | } |
| 135 | |
| 136 | waitlist = next; |
| 137 | } |
| 138 | } |
| 139 | |