summaryrefslogtreecommitdiff
path: root/devel/pth/patches/patch-ac
blob: 5577fef00acea092ec503197263a6e4a13467bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
$NetBSD: patch-ac,v 1.3 2002/10/23 10:04:16 drochner Exp $

--- pth_high.c.orig	Sun Jan 27 14:14:36 2002
+++ pth_high.c	Wed Oct 23 11:38:37 2002
@@ -35,6 +35,42 @@
 
 #include "pth_p.h"
 
+/* Pth variant of nanosleep(2) */
+int pth_nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
+{
+    pth_time_t until;
+    pth_time_t offset;
+    pth_time_t now;
+    pth_event_t ev;
+    static pth_key_t ev_key = PTH_KEY_INIT;
+
+    if (rqtp == NULL) {
+        return_errno(-1, EFAULT);
+    }
+
+    /* short-circuit */
+    if (rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
+        return 0;
+
+    /* calculate asleep time */
+    offset = pth_time((long)rqtp->tv_sec, (long)(rqtp->tv_nsec / 1000));
+    pth_time_set(&until, PTH_TIME_NOW);
+    pth_time_add(&until, &offset);
+
+    /* and let thread sleep until this time is elapsed */
+    ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, until);
+    pth_wait(ev);
+
+    if (rmtp != NULL) {
+        pth_time_set(&now, PTH_TIME_NOW);
+        pth_time_sub(&until, &now);
+        rmtp->tv_sec = until.tv_sec;
+        rmtp->tv_nsec = until.tv_usec * 1000;
+    }
+
+    return 0;
+}
+
 /* Pth variant of usleep(3) */
 int pth_usleep(unsigned int usec)
 {
@@ -141,8 +177,43 @@
     return 0;
 }
 
+#ifdef HAVE_SYS_RESOURCE_H
+/* Pth variant of wait4(2) */
+pid_t pth_wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
+{
+    pth_event_t ev;
+    static pth_key_t ev_key = PTH_KEY_INIT;
+    pid_t pid;
+
+    pth_debug2("pth_wait4: called from thread \"%s\"", pth_current->name);
+
+    for (;;) {
+        /* do a non-blocking poll for the pid */
+        while (   (pid = pth_sc(wait4)(wpid, status, options|WNOHANG, rusage)) < 0 
+               && errno == EINTR) ;
+
+        /* if pid was found or caller requested a polling return immediately */
+        if (pid == -1 || pid > 0 || (pid == 0 && (options & WNOHANG)))
+            break;
+
+        /* else wait a little bit */
+        ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, pth_timeout(0,250000));
+        pth_wait(ev);
+    }
+
+    pth_debug2("pth_wait4: leave to thread \"%s\"", pth_current->name);
+    return pid;
+}
+
 /* Pth variant of waitpid(2) */
-pid_t pth_waitpid(pid_t wpid, int *status, int options)
+pid_t pth_waitpid(pid_t wpid, int *status, int options) 
+{
+    return pth_wait4(wpid, status, options, 0);
+}
+
+#else
+/* Pth variant of wait4(2) */
+pid_t pth_waitpid(pid_t wpid, int *status, int options) 
 {
     pth_event_t ev;
     static pth_key_t ev_key = PTH_KEY_INIT;
@@ -167,6 +238,8 @@
     pth_debug2("pth_waitpid: leave to thread \"%s\"", pth_current->name);
     return pid;
 }
+#endif
+
 
 /* Pth variant of system(3) */
 int pth_system(const char *cmd)
@@ -195,7 +268,7 @@
     /* block SIGCHLD signal */
     sigemptyset(&ss_block);
     sigaddset(&ss_block, SIGCHLD);
-    sigprocmask(SIG_BLOCK, &ss_block, &ss_old);
+    pth_sc(sigprocmask)(SIG_BLOCK, &ss_block, &ss_old);
 
     /* fork the current process */
     pstat = -1;
@@ -207,7 +280,7 @@
             /* restore original signal dispositions and execute the command */
             sigaction(SIGINT,  &sa_int,  NULL);
             sigaction(SIGQUIT, &sa_quit, NULL);
-            sigprocmask(SIG_SETMASK, &ss_old, NULL);
+            pth_sc(sigprocmask)(SIG_SETMASK, &ss_old, NULL);
 
             /* stop the Pth scheduling */
             pth_scheduler_kill();
@@ -227,7 +300,7 @@
     /* restore original signal dispositions and execute the command */
     sigaction(SIGINT,  &sa_int,  NULL);
     sigaction(SIGQUIT, &sa_quit, NULL);
-    sigprocmask(SIG_SETMASK, &ss_old, NULL);
+    pth_sc(sigprocmask)(SIG_SETMASK, &ss_old, NULL);
 
     /* return error or child process result code */
     return (pid == -1 ? -1 : pstat);
@@ -258,6 +331,9 @@
     pth_implicit_init();
     pth_debug2("pth_select_ev: called from thread \"%s\"", pth_current->name);
 
+    if (nfd < 0 || nfd > FD_SETSIZE)
+      return_errno(-1, EINVAL);
+
     /* first deal with the special situation of a plain microsecond delay */
     if (nfd == 0 && rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL) {
         if (timeout->tv_sec < 0 || timeout->tv_usec < 0)
@@ -280,10 +356,11 @@
                     return_errno(-1, EINTR);
             }
         }
+
         /* POSIX compliance */
-        if (rfds != NULL) FD_ZERO(rfds);
-        if (wfds != NULL) FD_ZERO(wfds);
-        if (efds != NULL) FD_ZERO(efds);
+        if (rfds != NULL) pth_util_fd_zero(nfd, rfds);
+        if (wfds != NULL) pth_util_fd_zero(nfd, wfds);
+        if (efds != NULL) pth_util_fd_zero(nfd, efds);
         return 0;
     }
 
@@ -295,36 +372,42 @@
     delay.tv_sec  = 0;
     delay.tv_usec = 0;
     rtmp = NULL;
+
     if (rfds != NULL) {
-        rspare = *rfds;
+        pth_util_fd_copy(nfd, &rspare, rfds);
         rtmp = &rspare;
     }
     wtmp = NULL;
     if (wfds != NULL) {
-        wspare = *wfds;
+        pth_util_fd_copy(nfd, &wspare, wfds);
         wtmp = &wspare;
     }
     etmp = NULL;
     if (efds != NULL) {
-        espare = *efds;
+        pth_util_fd_copy(nfd, &espare, efds);
         etmp = &espare;
     }
     while ((rc = pth_sc(select)(nfd, rtmp, wtmp, etmp, &delay)) < 0 && errno == EINTR) ;
+    if (rc < 0)
+      return_errno(-1, errno);
     if (rc > 0) {
-        if (rfds != NULL)
-            *rfds = rspare;
-        if (wfds != NULL)
-            *wfds = wspare;
-        if (efds != NULL)
-            *efds = espare;
+        if (rfds != NULL) {
+            pth_util_fd_copy(nfd, rfds, &rspare);
+        }
+        if (wfds != NULL) {
+            pth_util_fd_copy(nfd, wfds, &wspare);
+        }
+        if (efds != NULL) {
+            pth_util_fd_copy(nfd, efds, &espare);
+        }
         return rc;
     }
     if (rc == 0 && timeout != NULL) {
         if (pth_time_cmp(timeout, PTH_TIME_ZERO) == 0) {
             /* POSIX compliance */
-            if (rfds != NULL) FD_ZERO(rfds);
-            if (wfds != NULL) FD_ZERO(wfds);
-            if (efds != NULL) FD_ZERO(efds);
+            if (rfds != NULL) pth_util_fd_zero(nfd, rfds);
+            if (wfds != NULL) pth_util_fd_zero(nfd, wfds);
+            if (efds != NULL) pth_util_fd_zero(nfd, efds);
             return 0;
         }
     }
@@ -350,10 +433,9 @@
         pth_event_isolate(ev_timeout);
         if (pth_event_occurred(ev_timeout)) {
             selected = TRUE;
-            /* POSIX compliance */
-            if (rfds != NULL) FD_ZERO(rfds);
-            if (wfds != NULL) FD_ZERO(wfds);
-            if (efds != NULL) FD_ZERO(efds);
+            if (rfds != NULL) pth_util_fd_zero(nfd, rfds);
+            if (wfds != NULL) pth_util_fd_zero(nfd, wfds);
+            if (efds != NULL) pth_util_fd_zero(nfd, efds);
             rc = 0;
         }
     }
@@ -362,7 +444,7 @@
     return rc;
 }
 
-/* Pth variant of select(2) */
+/* Pth variant of poll(2) */
 int pth_poll(struct pollfd *pfd, nfds_t nfd, int timeout)
 {
     return pth_poll_ev(pfd, nfd, timeout, NULL);
@@ -373,7 +455,7 @@
            INTERNALLY THE SCHEDULER IS ONLY select(2) BASED!! */
 int pth_poll_ev(struct pollfd *pfd, nfds_t nfd, int timeout, pth_event_t ev_extra)
 {
-    fd_set rfds, wfds, efds;
+    fd_set rfds, wfds, efds, cfds;
     struct timeval tv, *ptv;
     int maxfd, rc, ok;
     unsigned int i;
@@ -410,58 +492,64 @@
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&efds);
+    FD_ZERO(&cfds);
     for(i = 0; i < nfd; i++) {
-        if (!pth_util_fd_valid(pfd[i].fd))
-            return_errno(-1, EBADF);
-        if (pfd[i].events & POLLIN)
-            FD_SET(pfd[i].fd, &rfds);
-        if (pfd[i].events & POLLOUT)
-            FD_SET(pfd[i].fd, &wfds);
-        if (pfd[i].events & POLLPRI)
-            FD_SET(pfd[i].fd, &efds);
-        if (pfd[i].fd >= maxfd && (pfd[i].events & (POLLIN|POLLOUT|POLLPRI)))
-            maxfd = pfd[i].fd;
+	if (fcntl(pfd[i].fd, F_GETFL) == -1 && errno == EBADF) {
+	    FD_SET(pfd[i].fd, &cfds);
+	} else {
+	    if (pfd[i].events & POLLIN)
+		FD_SET(pfd[i].fd, &rfds);
+	    if (pfd[i].events & POLLOUT)
+		FD_SET(pfd[i].fd, &wfds);
+	    if (pfd[i].events & POLLPRI)
+                FD_SET(pfd[i].fd, &efds);
+	    if (pfd[i].fd >= maxfd && (pfd[i].events & (POLLIN|POLLOUT|POLLPRI)))
+                maxfd = pfd[i].fd;
+	}
+    }
+    if (maxfd != -1) {
+        /* examine fd sets */
+        rc = pth_select_ev(maxfd+1, &rfds, &wfds, &efds, ptv, ev_extra);
+        if (rc < 0)
+            return rc;
     }
-    if (maxfd == -1)
-        return_errno(-1, EINVAL);
-
-    /* examine fd sets */
-    rc = pth_select_ev(maxfd+1, &rfds, &wfds, &efds, ptv, ev_extra);
 
     /* establish results */
-    if (rc > 0) {
-        rc = 0;
-        for (i = 0; i < nfd; i++) {
-            ok = 0;
-            pfd[i].revents = 0;
-            if (pfd[i].fd < 0) {
-                pfd[i].revents |= POLLNVAL;
-                continue;
-            }
-            if (FD_ISSET(pfd[i].fd, &rfds)) {
-                pfd[i].revents |= POLLIN;
-                ok++;
-                /* support for POLLHUP */
-                if (recv(pfd[i].fd, data, 64, MSG_PEEK) == -1) {
-                    if (   errno == ESHUTDOWN    || errno == ECONNRESET
-                        || errno == ECONNABORTED || errno == ENETRESET) {
-                        pfd[i].revents &= ~(POLLIN);
-                        pfd[i].revents |= POLLHUP;
-                        ok--;
-                    }
+    rc = 0;
+    for (i = 0; i < nfd; i++) {
+        ok = 0;
+        pfd[i].revents = 0;
+        if (FD_ISSET(pfd[i].fd, &cfds)) {
+            pfd[i].revents |= POLLNVAL;
+            if (pfd[i].fd >= 0)
+                rc++;
+            continue;
+        }
+        if (maxfd == -1)
+	    continue;
+        if (FD_ISSET(pfd[i].fd, &rfds)) {
+            pfd[i].revents |= POLLIN;
+            ok++;
+            /* support for POLLHUP */
+            if (recv(pfd[i].fd, data, 64, MSG_PEEK) == -1) {
+                if (   errno == ESHUTDOWN    || errno == ECONNRESET
+                    || errno == ECONNABORTED || errno == ENETRESET) {
+                    pfd[i].revents &= ~(POLLIN);
+                    pfd[i].revents |= POLLHUP;
+                    ok--;
                 }
             }
-            if (FD_ISSET(pfd[i].fd, &wfds)) {
-                pfd[i].revents |= POLLOUT;
-                ok++;
-            }
-            if (FD_ISSET(pfd[i].fd, &efds)) {
-                pfd[i].revents |= POLLPRI;
-                ok++;
-            }
-            if (ok)
-                rc++;
         }
+        if (FD_ISSET(pfd[i].fd, &wfds)) {
+            pfd[i].revents |= POLLOUT;
+            ok++;
+        }
+        if (FD_ISSET(pfd[i].fd, &efds)) {
+            pfd[i].revents |= POLLPRI;
+            ok++;
+        }
+        if (ok)
+            rc++;
     }
     return rc;
 }
@@ -498,6 +586,13 @@
     /* if it is still on progress wait until socket is really writeable */
     if (rv == -1 && errno == EINPROGRESS && fdmode != PTH_FDMODE_NONBLOCK) {
         ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_WRITEABLE|PTH_MODE_STATIC, &ev_key, s);
+        /*
+         * This will fail if the fd is > FD_SETSIZE so pass the error back to the
+         * caller.
+         */
+        if (ev == NULL) {
+           return_errno(-1, errno);
+        }
         if (ev_extra != NULL)
             pth_event_concat(ev, ev_extra, NULL);
         pth_wait(ev);