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