File: unix.c

package info (click to toggle)
libevent-perl 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 712 kB
  • ctags: 593
  • sloc: ansic: 3,034; perl: 1,128; makefile: 50
file content (463 lines) | stat: -rw-r--r-- 11,790 bytes parent folder | download | duplicates (5)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#if defined(HAS_DEVPOLL)
#include <sys/devpoll.h>
    static int dpfd=0;
    static struct pollfd *Pollfd=0;
    static int pollMax=200;
    static int Nfds;

#define MAXFD 65000

    typedef struct _fdToEvent {
        pe_io *ev;
    } FdToEvent;

    static FdToEvent fdToEvent[MAXFD];
#endif /*HAS_DEVPOLL*/

static void boot_devpoll() {
#if defined(HAS_DEVPOLL)
    memset(fdToEvent, 0, MAXFD*sizeof(FdToEvent));

    EvNew(9, Pollfd, pollMax, struct pollfd);

    /* Open /dev/poll driver */
    if (!dpfd) {
        fprintf(stderr, "INIT Open /dev/poll!!!\n");
        if ((dpfd = open("/dev/poll", O_RDWR)) < 0) {
            croak("Event: Can't open /dev/poll!\n");
        }
    }
#endif /*HAS_DEVPOLL*/
}

static int pe_sys_fileno(SV *sv, char *context) {
    IO *io;
    PerlIO *fp;
    
    if (!sv)
	croak("Event %s: no filehandle available", context);
    if (SvGMAGICAL(sv))
	mg_get(sv);
    if (SvIOK(sv)) /* maybe non-portable but nice for unixen */
	return SvIV(sv);
    if (SvROK(sv))
	sv = SvRV(sv);
    if (SvTYPE(sv) == SVt_PVGV) {
	if (!(io=GvIO((GV*)sv)) || !(fp = IoIFP(io))) {
	    croak("Event '%s': GLOB(0x%x) isn't a valid IO", context, sv);
	}
	return PerlIO_fileno(fp);
    }
    sv_dump(sv);
    croak("Event '%s': can't find fileno", context);
    return -1;
}

static void _queue_io(pe_io *wa, int got) {
    pe_ioevent *ev;
    got &= wa->poll;
    if (!got) {
	if (WaDEBUGx(wa) >= 3) {
	    STRLEN n_a;
	    warn("Event: io '%s' queued nothing", SvPV(wa->base.desc, n_a));
	}
	return;
    }
    ev = (pe_ioevent*) (*wa->base.vtbl->new_event)((pe_watcher*) wa);
    ++ev->base.hits;
    ev->got |= got;
    queueEvent((pe_event*) ev);
}

/************************************************* DEVPOLL */
#if defined(HAS_DEVPOLL) && !PE_SYS_IO
#define PE_SYS_IO 1

static void pe_sys_sleep(NV left) {
    int ret;
    NV t0 = NVtime();
    NV t1 = t0 + left;
    while (1) {
        ret = poll(0, 0, (int) (left * 1000)); /* hope zeroes okay */
        if (ret < 0 && errno != EAGAIN && errno != EINTR)
            croak("poll(%.2f) got errno %d", left, errno);
        left = t1 - NVtime();
        if (left > IntervalEpsilon) {
            if (ret==0) ++TimeoutTooEarly;
            continue;
        }
        break;
    }
}

static void pe_sys_io_add (pe_io *ev) {
    struct pollfd tmp_pfd;
    int bits=0;

    if (ev->fd <= 0 || ev->fd > MAXFD) {
        croak("pe_sys_io_add: non-valid fd (%d)", ev->fd);
        return;
    }

    if (ev->poll & PE_R) bits |= (POLLIN | POLLPRI);
    if (ev->poll & PE_W) bits |= POLLOUT;
    if (ev->poll & PE_E) bits |= (POLLRDBAND | POLLPRI);

    tmp_pfd.fd = ev->fd;
    tmp_pfd.events = bits;

    if (write(dpfd, &tmp_pfd, sizeof(struct pollfd)) != 
        sizeof(struct pollfd)) {
        fprintf(stderr, "pe_sys_io_add(fd %d): could not write fd to /dev/poll", 
            dpfd);
        return;
    }

    if (fdToEvent[ev->fd].ev != NULL) {
        fprintf(stderr, "pe_sys_io_add(fd %d): mapping between fd and event already exists!", ev->fd);
    } else {
        fdToEvent[ev->fd].ev = ev;
    }
}

static void pe_sys_io_del (pe_io *ev) {
    struct pollfd tmp_pfd;
    int bits=0;

    if (ev-> fd <= 0) {
        return;
    }

    if (ev->poll & PE_R) bits |= (POLLIN | POLLPRI);
    if (ev->poll & PE_W) bits |= POLLOUT;
    if (ev->poll & PE_E) bits |= (POLLRDBAND | POLLPRI);

    tmp_pfd.fd = ev->fd;
    tmp_pfd.events = POLLREMOVE;

    if (write(dpfd, &tmp_pfd, sizeof(struct pollfd)) != 
        sizeof(struct pollfd)) {
        fprintf(stderr, "pe_sys_io_del(fd %d): could not write fd to /dev/poll", dpfd);
    }

    fdToEvent[ev->fd].ev = NULL;
}

static void pe_sys_multiplex(NV timeout) {
    pe_io *ev;
    int xx, got, mask, fd;
    int ret;
    int err, m_rfds;
    struct dvpoll dopoll;

    if (pollMax < IOWatchCount) {
        if (Pollfd)
            EvFree(9, Pollfd);
        pollMax = IOWatchCount*2;
        EvNew(9, Pollfd, pollMax, struct pollfd);
        IOWatch_OK = 0;
    }


    if (!IOWatch_OK) {
        Nfds = 0;
        Zero(Pollfd, pollMax, struct pollfd);
        ev = (pe_io*) IOWatch.next->self;
        while (ev) {
            int fd = ev->fd;
            ev->xref = -1;
            assert(fd >= 0); {
                int bits=0;
                if (ev->poll & PE_R) bits |= (POLLIN | POLLPRI);
                if (ev->poll & PE_W) bits |= POLLOUT;
                if (ev->poll & PE_E) bits |= (POLLRDBAND | POLLPRI);
                assert(bits); {
                    Pollfd[Nfds].fd = fd;
                    Pollfd[Nfds].events |= bits;
                    Nfds++;
                }
            }
            ev = (pe_io*) ev->ioring.next->self;
        }
        IOWatch_OK = 1;
    }

    for (xx=0; xx < Nfds; xx++)
        Pollfd[xx].revents = 0; /* needed? XXX */

    if (timeout < 0)
        timeout = 0;

    dopoll.dp_timeout = (int) (timeout * 1000);
    dopoll.dp_nfds = pollMax;
    dopoll.dp_fds = Pollfd;

    /* Wait for I/O events the clients are interested in */
    m_rfds = ioctl(dpfd, DP_POLL, &dopoll);
    if (m_rfds == -1) {
        err = errno;
        fprintf(stderr, "pe_sys_multiplex: poll() returned -1, errno %d\n", err);
        return;
    }

    while (m_rfds >= 1) {
        m_rfds--;
        fd = Pollfd[m_rfds].fd;
        ev = fdToEvent[fd].ev;
        got = 0;
        mask = Pollfd[m_rfds].revents;
        if (mask & (POLLIN | POLLPRI | POLLHUP | POLLERR)) got |= PE_R;
        if (mask & (POLLOUT | POLLERR)) got |= PE_W;
        if (mask & (POLLRDBAND | POLLPRI | POLLHUP | POLLERR)) got |= PE_E;
        if (mask & POLLNVAL) {
            STRLEN n_a;
            warn("Event: '%s' was unexpectedly closed",
                 SvPV(ev->base.desc, n_a));
            pe_io_reset_handle((pe_watcher*) ev);
        } else {
            if ((mask & POLLHUP) && (ev->poll & PE_W) && (!(got & PE_W))
                && (!(ev->poll & PE_R)) && (!(ev->poll & PE_E))) {
              /* Must notify about POLLHUP _some_ way - Allen */
              got |= PE_W;
            }
        }

        if (got) _queue_io(ev, got);

    }
}
#endif /*HAS_DEVPOLL*/

/************************************************* POLL */
#if defined(HAS_POLL) && !PE_SYS_IO
#define PE_SYS_IO 1

static struct pollfd *Pollfd=0;
static int pollMax=0;
static int Nfds;

static void pe_sys_sleep(NV left) {
    int ret;
    NV t0 = NVtime();
    NV t1 = t0 + left;
    while (1) {
	ret = poll(0, 0, (int) (left * 1000)); /* hope zeroes okay */
	if (ret < 0 && errno != EAGAIN && errno != EINTR)
	    croak("poll(%.2f) got errno %d", left, errno);
	left = t1 - NVtime();
	if (left > IntervalEpsilon) {
	    if (ret==0) ++TimeoutTooEarly;
	    continue;
	}
	break;
    }
}

static void pe_sys_io_add (pe_io *ev) {}
static void pe_sys_io_del (pe_io *ev) {}

static void pe_sys_multiplex(NV timeout) {
    pe_io *ev;
    int xx;
    int ret;
    if (pollMax < IOWatchCount) {
	if (Pollfd)
	    EvFree(9, Pollfd);
	pollMax = IOWatchCount+5;
	EvNew(9, Pollfd, pollMax, struct pollfd);
	IOWatch_OK = 0;
    }
    if (!IOWatch_OK) {
	Nfds = 0;
	Zero(Pollfd, pollMax, struct pollfd);
	ev = (pe_io*) IOWatch.next->self;
	while (ev) {
	    int fd = ev->fd;
	    ev->xref = -1;
	    assert(fd >= 0); {
		int bits=0;
		if (ev->poll & PE_R) bits |= (POLLIN | POLLPRI);
		if (ev->poll & PE_W) bits |= POLLOUT;
		if (ev->poll & PE_E) bits |= (POLLRDBAND | POLLPRI);
		assert(bits); {
		    int ok=0;;
		    for (xx = 0; xx < Nfds; xx++) {
			if (Pollfd[xx].fd == fd) { ok=1; break; }
		    }
		    if (!ok) xx = Nfds++;
		    Pollfd[xx].fd = fd;
		    Pollfd[xx].events |= bits;
		    ev->xref = xx;
		}
	    }
	    ev = (pe_io*) ev->ioring.next->self;
	}
	IOWatch_OK = 1;
    }
    for (xx=0; xx < Nfds; xx++)
	Pollfd[xx].revents = 0; /* needed? XXX */
    if (timeout < 0)
	timeout = 0;
    ret = poll(Pollfd, Nfds, (int) (timeout * 1000));
  
    if (ret < 0) {
	if (errno == EINTR || errno == EAGAIN)
	    return;
	if (errno == EINVAL) {
	    warn("poll: bad args %d %.2f", Nfds, timeout);
	    return;
	}
	warn("poll got errno %d", errno);
	return;
    }
    ev = (pe_io*) IOWatch.next->self;
    while (ev) {
	pe_io *next_ev = (pe_io*) ev->ioring.next->self;
	STRLEN n_a;
	int xref = ev->xref;
	if (xref >= 0) {
	    int got = 0;
	    int mask = Pollfd[xref].revents;
	    if (mask & (POLLIN | POLLPRI | POLLHUP | POLLERR)) got |= PE_R;
	    if (mask & (POLLOUT | POLLERR)) got |= PE_W;
	    if (mask & (POLLRDBAND | POLLPRI | POLLHUP | POLLERR)) got |= PE_E;
	    if (mask & POLLNVAL) {
		warn("Event: '%s' was unexpectedly closed",
		     SvPV(ev->base.desc, n_a));
		pe_io_reset_handle((pe_watcher*) ev);
	    } else {
	      if ((mask & POLLHUP) && (ev->poll & PE_W) && (!(got & PE_W))
		  && (!(ev->poll & PE_R)) && (!(ev->poll & PE_E))) {
		/* Must notify about POLLHUP _some_ way - Allen */
		got |= PE_W;
	    }

	      if (got) _queue_io(ev, got);
	    /*
	      Can only do this if fd-to-watcher is 1-to-1
	      if (--ret == 0) { ev=0; continue; }
	    */
	    }
	}
	ev = next_ev;
    }
}
#endif /*HAS_POLL*/


/************************************************* SELECT */
#if defined(HAS_SELECT) && !PE_SYS_IO
#define PE_SYS_IO 1

static int Nfds;
static fd_set Rfds, Wfds, Efds;

static void pe_sys_sleep(NV left) {
    struct timeval tm;
    NV t0 = NVtime();
    NV t1 = t0 + left;
    int ret;
    while (1) {
	tm.tv_sec = left;
	tm.tv_usec = (left - tm.tv_sec) * 1000000;
	ret = select(0, 0, 0, 0, &tm);
	if (ret < 0 && errno != EINTR && errno != EAGAIN)
	    croak("select(%.2f) got errno %d", left, errno);
	left = t1 - NVtime();
	if (left > IntervalEpsilon) {
	    if (ret==0) ++TimeoutTooEarly;
	    continue;
	}
	break;
    }
}

static void pe_sys_io_add (pe_io *ev) {}
static void pe_sys_io_del (pe_io *ev) {}

static void pe_sys_multiplex(NV timeout) {
    struct timeval tm;
    int ret;
    fd_set rfds, wfds, efds;
    pe_io *ev;

    if (!IOWatch_OK) {
	Nfds = -1;
	FD_ZERO(&Rfds);
	FD_ZERO(&Wfds);
	FD_ZERO(&Efds);
	ev = IOWatch.next->self;
	while (ev) {
	    int fd = ev->fd;
	    if (fd >= 0) {
		int bits=0;
		if (ev->poll & PE_R) { FD_SET(fd, &Rfds); ++bits; }
		if (ev->poll & PE_W) { FD_SET(fd, &Wfds); ++bits; }
		if (ev->poll & PE_E) { FD_SET(fd, &Efds); ++bits; }
		if (bits && fd > Nfds) Nfds = fd;
	    }
	    ev = ev->ioring.next->self;
	}
	IOWatch_OK = 1;
    }

    if (timeout < 0)
	timeout = 0;
    tm.tv_sec = timeout;
    tm.tv_usec = (timeout - tm.tv_sec) * 1000000;
    if (Nfds > -1) {
	memcpy(&rfds, &Rfds, sizeof(fd_set));
	memcpy(&wfds, &Wfds, sizeof(fd_set));
	memcpy(&efds, &Efds, sizeof(fd_set));
	ret = select(Nfds+1, &rfds, &wfds, &efds, &tm);
    }
    else
	ret = select(0, 0, 0, 0, &tm);

    if (ret < 0) {
	if (errno == EINTR)
	    return;
	if (errno == EBADF) {
	    STRLEN n_a;
	    ev = IOWatch.next->self;
	    while (ev) {
		int fd = ev->fd;
		struct stat buf;
		if (fd >= 0 && PerlLIO_fstat(fd, &buf) < 0 && errno == EBADF) {
		    warn("Event: '%s' was unexpectedly closed",
			 SvPV(ev->base.desc, n_a));
		    pe_io_reset_handle((pe_watcher*) ev);
		    return;
		}
		ev = ev->ioring.next->self;
	    }
	    warn("select: couldn't find cause of EBADF");
	    return;
	}
	if (errno == EINVAL) {
	    warn("select: bad args %d %.2f", Nfds, timeout);
	    return;
	}
	warn("select got errno %d", errno);
	return;
    }
    ev = IOWatch.next->self;
    while (ev) {
	pe_io *next_ev = (pe_io*) ev->ioring.next->self;
	int fd = ev->fd;
	if (fd >= 0) {
	    int got = 0;
	    if (FD_ISSET(fd, &rfds)) got |= PE_R;
	    if (FD_ISSET(fd, &wfds)) got |= PE_W;
	    if (FD_ISSET(fd, &efds)) got |= PE_E;
	    if (got) _queue_io(ev, got);
	    /*
	      Can only do this if fd-to-watcher is 1-to-1
	  
	      if (--ret == 0) { ev=0; continue; }
	    */
	}
	ev = next_ev;
    }
}
#endif /*HAS_SELECT*/