]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2001-2024 Free Software Foundation, Inc. |
2ace5721 | 2 | This file is part of the GNU C Library. |
2ace5721 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
2ace5721 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
2ace5721 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
2ace5721 UD |
17 | |
18 | #include <netdb.h> | |
19 | #include <pthread.h> | |
20 | #include <stdlib.h> | |
f1762c0c | 21 | #include <gai_misc.h> |
2ace5721 | 22 | |
dbb949f5 FW |
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 | |
2ace5721 | 32 | |
f1762c0c UD |
33 | struct notify_func |
34 | { | |
35 | void (*func) (sigval_t); | |
36 | sigval_t value; | |
37 | }; | |
2ace5721 UD |
38 | |
39 | static void * | |
40 | notify_func_wrapper (void *arg) | |
41 | { | |
f1762c0c UD |
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); | |
2ace5721 UD |
48 | return NULL; |
49 | } | |
50 | ||
51 | ||
52 | int | |
2ace5721 UD |
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 | { | |
dbb949f5 FW |
67 | __pthread_attr_init (&attr); |
68 | __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | |
2ace5721 UD |
69 | pattr = &attr; |
70 | } | |
71 | ||
f1762c0c UD |
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) | |
2ace5721 | 81 | result = -1; |
f1762c0c UD |
82 | else |
83 | { | |
84 | nf->func = sigev->sigev_notify_function; | |
85 | nf->value = sigev->sigev_value; | |
dbb949f5 | 86 | if (__pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0) |
f1762c0c UD |
87 | { |
88 | free (nf); | |
89 | result = -1; | |
90 | } | |
91 | } | |
2ace5721 UD |
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 | |
2ace5721 UD |
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 | ||
2ace5721 | 114 | if (waitlist->sigevp == NULL) |
f1762c0c UD |
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 | } | |
2ace5721 | 125 | else |
c0c3f78a | 126 | /* This is part of an asynchronous `getaddrinfo_a' operation. If |
2ace5721 | 127 | this request is the last one, send the signal. */ |
f1762c0c | 128 | if (--*waitlist->counterp == 0) |
2ace5721 UD |
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 | } |