]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/gai_suspend.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / resolv / gai_suspend.c
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 <errno.h>
20 #include <netdb.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24
25 #include <gai_misc.h>
26
27 int
28 __gai_suspend_time64 (const struct gaicb *const list[], int ent,
29 const struct __timespec64 *timeout)
30 {
31 struct waitlist waitlist[ent];
32 struct requestlist *requestlist[ent];
33 #ifndef DONT_NEED_GAI_MISC_COND
34 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
35 #endif
36 int cnt;
37 unsigned int cntr = 1;
38 int none = 1;
39 int result;
40
41 /* Request the mutex. */
42 pthread_mutex_lock (&__gai_requests_mutex);
43
44 /* There is not yet a finished request. Signal the request that
45 we are working for it. */
46 for (cnt = 0; cnt < ent; ++cnt)
47 if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS)
48 {
49 requestlist[cnt] = __gai_find_request (list[cnt]);
50
51 if (requestlist[cnt] != NULL)
52 {
53 #ifndef DONT_NEED_GAI_MISC_COND
54 waitlist[cnt].cond = &cond;
55 #endif
56 waitlist[cnt].next = requestlist[cnt]->waiting;
57 waitlist[cnt].counterp = &cntr;
58 waitlist[cnt].sigevp = NULL;
59 waitlist[cnt].caller_pid = 0; /* Not needed. */
60 requestlist[cnt]->waiting = &waitlist[cnt];
61 none = 0;
62 }
63 }
64
65 struct __timespec64 ts;
66 if (timeout != NULL)
67 {
68 __clock_gettime64 (CLOCK_MONOTONIC, &ts);
69 ts.tv_sec += timeout->tv_sec;
70 ts.tv_nsec += timeout->tv_nsec;
71 if (ts.tv_nsec >= 1000000000)
72 {
73 ts.tv_nsec -= 1000000000;
74 ts.tv_sec++;
75 }
76 }
77
78 if (none)
79 {
80 if (cnt < ent)
81 /* There is an entry which is finished. */
82 result = 0;
83 else
84 result = EAI_ALLDONE;
85 }
86 else
87 {
88 /* There is no request done but some are still being worked on. */
89 int oldstate;
90
91 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
92 points we must be careful. We added entries to the waiting lists
93 which we must remove. So defer cancelation for now. */
94 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
95
96 #ifdef DONT_NEED_GAI_MISC_COND
97 result = 0;
98 GAI_MISC_WAIT (result, cntr, timeout == NULL ? NULL : &ts, 1);
99 #else
100 struct timespec ts32 = valid_timespec64_to_timespec (ts);
101 result = pthread_cond_timedwait (&cond, &__gai_requests_mutex,
102 timeout == NULL ? NULL : &ts32);
103 #endif
104
105 /* Now remove the entry in the waiting list for all requests
106 which didn't terminate. */
107 for (cnt = 0; cnt < ent; ++cnt)
108 if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS
109 && requestlist[cnt] != NULL)
110 {
111 struct waitlist **listp = &requestlist[cnt]->waiting;
112
113 /* There is the chance that we cannot find our entry anymore.
114 This could happen if the request terminated and restarted
115 again. */
116 while (*listp != NULL && *listp != &waitlist[cnt])
117 listp = &(*listp)->next;
118
119 if (*listp != NULL)
120 *listp = (*listp)->next;
121 }
122
123 /* Now it's time to restore the cancelation state. */
124 pthread_setcancelstate (oldstate, NULL);
125
126 #ifndef DONT_NEED_GAI_MISC_COND
127 /* Release the conditional variable. */
128 if (pthread_cond_destroy (&cond) != 0)
129 /* This must never happen. */
130 abort ();
131 #endif
132
133 if (result != 0)
134 {
135 /* An error occurred. Possibly it's EINTR. We have to translate
136 the timeout error report of `pthread_cond_timedwait' to the
137 form expected from `gai_suspend'. */
138 if (__glibc_likely (result == ETIMEDOUT))
139 result = EAI_AGAIN;
140 else if (result == EINTR)
141 result = EAI_INTR;
142 else
143 result = EAI_SYSTEM;
144 }
145 }
146
147 /* Release the mutex. */
148 pthread_mutex_unlock (&__gai_requests_mutex);
149
150 return result;
151 }
152
153 #if __TIMESIZE != 64
154 libanl_hidden_def (__gai_suspend_time64)
155
156 int
157 __gai_suspend (const struct gaicb *const list[], int ent,
158 const struct timespec *timeout)
159 {
160 struct __timespec64 ts64;
161
162 if (timeout != NULL)
163 ts64 = valid_timespec_to_timespec64 (*timeout);
164
165 return __gai_suspend_time64 (list, ent, timeout != NULL ? &ts64 : NULL);
166 }
167 #endif
168 weak_alias (__gai_suspend, gai_suspend)