]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/select.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / scheduler / select.c
CommitLineData
f7deaa1a 1/*
f2d18633 2 * "$Id$"
f7deaa1a 3 *
5a1d7a17 4 * Select abstraction functions for the CUPS scheduler.
f7deaa1a 5 *
7e86f2f6 6 * Copyright 2007-2014 by Apple Inc.
5a1d7a17 7 * Copyright 2006-2007 by Easy Software Products.
f7deaa1a 8 *
5a1d7a17
MS
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
f7deaa1a 14 */
15
16/*
17 * Include necessary headers...
18 */
19
20#include "cupsd.h"
21
22#ifdef HAVE_EPOLL
23# include <sys/epoll.h>
dcb445bc 24# include <poll.h>
f7deaa1a 25#elif defined(HAVE_KQUEUE)
26# include <sys/event.h>
27# include <sys/time.h>
28#elif defined(HAVE_POLL)
dcb445bc 29# include <poll.h>
f7deaa1a 30#else
31# include <sys/select.h>
32#endif /* HAVE_EPOLL */
33
34
35/*
36 * Design Notes for Poll/Select API in CUPSD
37 * -----------------------------------------
3dd9c340 38 *
f7deaa1a 39 * SUPPORTED APIS
3dd9c340 40 *
f7deaa1a 41 * OS select poll epoll kqueue /dev/poll
42 * -------------- ------ ------ ------ ------ ---------
43 * AIX YES YES NO NO NO
44 * FreeBSD YES YES NO YES NO
45 * HP-UX YES YES NO NO NO
f7deaa1a 46 * Linux YES YES YES NO NO
47 * MacOS X YES YES NO YES NO
48 * NetBSD YES YES NO YES NO
49 * OpenBSD YES YES NO YES NO
50 * Solaris YES YES NO NO YES
51 * Tru64 YES YES NO NO NO
52 * Windows YES NO NO NO NO
3dd9c340
MS
53 *
54 *
f7deaa1a 55 * HIGH-LEVEL API
3dd9c340 56 *
f7deaa1a 57 * typedef void (*cupsd_selfunc_t)(void *data);
3dd9c340 58 *
f7deaa1a 59 * void cupsdStartSelect(void);
60 * void cupsdStopSelect(void);
61 * void cupsdAddSelect(int fd, cupsd_selfunc_t read_cb,
62 * cupsd_selfunc_t write_cb, void *data);
63 * void cupsdRemoveSelect(int fd);
64 * int cupsdDoSelect(int timeout);
3dd9c340
MS
65 *
66 *
f7deaa1a 67 * IMPLEMENTATION STRATEGY
3dd9c340 68 *
f7deaa1a 69 * 0. Common Stuff
70 * a. CUPS array of file descriptor to callback functions
c0e1af83 71 * and data + temporary array of removed fd's.
72 * b. cupsdStartSelect() creates the arrays
73 * c. cupsdStopSelect() destroys the arrays and all elements.
f7deaa1a 74 * d. cupsdAddSelect() adds to the array and allocates a
75 * new callback element.
c0e1af83 76 * e. cupsdRemoveSelect() removes from the active array and
77 * adds to the inactive array.
f7deaa1a 78 * f. _cupsd_fd_t provides a reference-counted structure for
79 * tracking file descriptors that are monitored.
c0e1af83 80 * g. cupsdDoSelect() frees all inactive FDs.
81 *
f7deaa1a 82 * 1. select() O(n)
83 * a. Input/Output fd_set variables, copied to working
84 * copies and then used with select().
85 * b. Loop through CUPS array, using FD_ISSET and calling
86 * the read/write callbacks as needed.
87 * c. cupsdRemoveSelect() clears fd_set bit from main and
88 * working sets.
89 * d. cupsdStopSelect() frees all of the memory used by the
90 * CUPS array and fd_set's.
3dd9c340 91 *
f7deaa1a 92 * 2. poll() - O(n log n)
93 * a. Regular array of pollfd, sorted the same as the CUPS
94 * array.
95 * b. Loop through pollfd array, call the corresponding
96 * read/write callbacks as needed.
97 * c. cupsdAddSelect() adds first to CUPS array and flags the
98 * pollfd array as invalid.
99 * d. cupsdDoSelect() rebuilds pollfd array as needed, calls
100 * poll(), then loops through the pollfd array looking up
101 * as needed.
102 * e. cupsdRemoveSelect() flags the pollfd array as invalid.
103 * f. cupsdStopSelect() frees all of the memory used by the
104 * CUPS array and pollfd array.
3dd9c340 105 *
f7deaa1a 106 * 3. epoll() - O(n)
107 * a. cupsdStartSelect() creates epoll file descriptor using
108 * epoll_create() with the maximum fd count, and
109 * allocates an events buffer for the maximum fd count.
110 * b. cupsdAdd/RemoveSelect() uses epoll_ctl() to add
111 * (EPOLL_CTL_ADD) or remove (EPOLL_CTL_DEL) a single
112 * event using the level-triggered semantics. The event
113 * user data field is a pointer to the new callback array
114 * element.
115 * c. cupsdDoSelect() uses epoll_wait() with the global event
116 * buffer allocated in cupsdStartSelect() and then loops
117 * through the events, using the user data field to find
118 * the callback record.
119 * d. cupsdStopSelect() closes the epoll file descriptor and
120 * frees all of the memory used by the event buffer.
3dd9c340 121 *
f7deaa1a 122 * 4. kqueue() - O(n)
123 * b. cupsdStartSelect() creates kqueue file descriptor
41681883 124 * using kqueue() function and allocates a global event
f7deaa1a 125 * buffer.
126 * c. cupsdAdd/RemoveSelect() uses EV_SET and kevent() to
127 * register the changes. The event user data field is a
128 * pointer to the new callback array element.
129 * d. cupsdDoSelect() uses kevent() to poll for events and
130 * loops through the events, using the user data field to
131 * find the callback record.
41681883 132 * e. cupsdStopSelect() closes the kqueue() file descriptor
f7deaa1a 133 * and frees all of the memory used by the event buffer.
3dd9c340 134 *
f7deaa1a 135 * 5. /dev/poll - O(n log n) - NOT YET IMPLEMENTED
136 * a. cupsdStartSelect() opens /dev/poll and allocates an
137 * array of pollfd structs; on failure to open /dev/poll,
138 * revert to poll() system call.
139 * b. cupsdAddSelect() writes a single pollfd struct to
140 * /dev/poll with the new file descriptor and the
141 * POLLIN/POLLOUT flags.
142 * c. cupsdRemoveSelect() writes a single pollfd struct to
143 * /dev/poll with the file descriptor and the POLLREMOVE
144 * flag.
145 * d. cupsdDoSelect() uses the DP_POLL ioctl to retrieve
146 * events from /dev/poll and then loops through the
147 * returned pollfd array, looking up the file descriptors
148 * as needed.
149 * e. cupsdStopSelect() closes /dev/poll and frees the
150 * pollfd array.
151 *
152 * PERFORMANCE
153 *
154 * In tests using the "make test" target with option 0 (keep cupsd
155 * running) and the "testspeed" program with "-c 50 -r 1000", epoll()
0a682745
MS
156 * performed 5.5% slower than select(), followed by kqueue() at 16%
157 * slower than select() and poll() at 18% slower than select(). Similar
f7deaa1a 158 * results were seen with twice the number of client connections.
159 *
160 * The epoll() and kqueue() performance is likely limited by the
161 * number of system calls used to add/modify/remove file
162 * descriptors dynamically. Further optimizations may be possible
163 * in the area of limiting use of cupsdAddSelect() and
164 * cupsdRemoveSelect(), however extreme care will be needed to avoid
165 * excess CPU usage and deadlock conditions.
166 *
167 * We may be able to improve the poll() implementation simply by
168 * keeping the pollfd array sync'd with the _cupsd_fd_t array, as that
169 * will eliminate the rebuilding of the array whenever there is a
170 * change and eliminate the fd array lookups in the inner loop of
171 * cupsdDoSelect().
172 *
173 * Since /dev/poll will never be able to use a shadow array, it may
174 * not make sense to implement support for it. ioctl() overhead will
175 * impact performance as well, so my guess would be that, for CUPS,
176 * /dev/poll will yield a net performance loss.
177 */
178
179/*
180 * Local structures...
181 */
182
183typedef struct _cupsd_fd_s
184{
185 int fd, /* File descriptor */
186 use; /* Use count */
187 cupsd_selfunc_t read_cb, /* Read callback */
188 write_cb; /* Write callback */
189 void *data; /* Data pointer for callbacks */
190} _cupsd_fd_t;
191
192
193/*
194 * Local globals...
195 */
196
197static cups_array_t *cupsd_fds = NULL;
c0e1af83 198#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
199static cups_array_t *cupsd_inactive_fds = NULL;
200static int cupsd_in_select = 0;
201#endif /* HAVE_EPOLL || HAVE_KQUEUE */
f7deaa1a 202
0a682745 203#ifdef HAVE_KQUEUE
f7deaa1a 204static int cupsd_kqueue_fd = -1,
205 cupsd_kqueue_changes = 0;
206static struct kevent *cupsd_kqueue_events = NULL;
207#elif defined(HAVE_POLL)
208static int cupsd_alloc_pollfds = 0,
209 cupsd_update_pollfds = 0;
210static struct pollfd *cupsd_pollfds = NULL;
0a682745
MS
211# ifdef HAVE_EPOLL
212static int cupsd_epoll_fd = -1;
213static struct epoll_event *cupsd_epoll_events = NULL;
214# endif /* HAVE_EPOLL */
f7deaa1a 215#else /* select() */
216static fd_set cupsd_global_input,
217 cupsd_global_output,
218 cupsd_current_input,
219 cupsd_current_output;
0a682745 220#endif /* HAVE_KQUEUE */
f7deaa1a 221
222
223/*
224 * Local functions...
225 */
226
227static int compare_fds(_cupsd_fd_t *a, _cupsd_fd_t *b);
228static _cupsd_fd_t *find_fd(int fd);
c7017ecc 229#define release_fd(f) { \
f7deaa1a 230 (f)->use --; \
231 if (!(f)->use) free((f));\
232 }
c7017ecc 233#define retain_fd(f) (f)->use++
f7deaa1a 234
235
236/*
237 * 'cupsdAddSelect()' - Add a file descriptor to the list.
238 */
239
240int /* O - 1 on success, 0 on error */
241cupsdAddSelect(int fd, /* I - File descriptor */
242 cupsd_selfunc_t read_cb, /* I - Read callback */
243 cupsd_selfunc_t write_cb,/* I - Write callback */
244 void *data) /* I - Data to pass to callback */
245{
246 _cupsd_fd_t *fdptr; /* File descriptor record */
1f0275e3 247#ifdef HAVE_EPOLL
f7deaa1a 248 int added; /* 1 if added, 0 if modified */
1f0275e3 249#endif /* HAVE_EPOLL */
f7deaa1a 250
251
252 /*
253 * Range check input...
254 */
255
1bc82dd9 256 cupsdLogMessage(CUPSD_LOG_DEBUG2,
b9faaae1 257 "cupsdAddSelect(fd=%d, read_cb=%p, write_cb=%p, data=%p)",
f7deaa1a 258 fd, read_cb, write_cb, data);
259
260 if (fd < 0)
261 return (0);
262
263 /*
264 * See if this FD has already been added...
265 */
266
267 if ((fdptr = find_fd(fd)) == NULL)
268 {
269 /*
270 * No, add a new entry...
271 */
272
273 if ((fdptr = calloc(1, sizeof(_cupsd_fd_t))) == NULL)
274 return (0);
275
276 fdptr->fd = fd;
277 fdptr->use = 1;
278
279 if (!cupsArrayAdd(cupsd_fds, fdptr))
280 {
281 cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to add fd %d to array!", fd);
282 free(fdptr);
283 return (0);
284 }
285
1f0275e3 286#ifdef HAVE_EPOLL
f7deaa1a 287 added = 1;
288 }
289 else
290 added = 0;
1f0275e3
MS
291#else
292 }
293#endif /* HAVE_EPOLL */
f7deaa1a 294
0a682745 295#ifdef HAVE_KQUEUE
f7deaa1a 296 {
297 struct kevent event; /* Event data */
298 struct timespec timeout; /* Timeout value */
299
300
301 timeout.tv_sec = 0;
302 timeout.tv_nsec = 0;
303
304 if (fdptr->read_cb != read_cb)
305 {
306 if (read_cb)
307 EV_SET(&event, fd, EVFILT_READ, EV_ADD, 0, 0, fdptr);
308 else
309 EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, fdptr);
310
311 if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
312 {
b9faaae1 313 cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
f7deaa1a 314 strerror(errno));
315 return (0);
316 }
317 }
318
319 if (fdptr->write_cb != write_cb)
320 {
321 if (write_cb)
322 EV_SET(&event, fd, EVFILT_WRITE, EV_ADD, 0, 0, fdptr);
323 else
324 EV_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, fdptr);
325
326 if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
327 {
b9faaae1 328 cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
f7deaa1a 329 strerror(errno));
330 return (0);
331 }
332 }
333 }
334
335#elif defined(HAVE_POLL)
0a682745
MS
336# ifdef HAVE_EPOLL
337 if (cupsd_epoll_fd >= 0)
338 {
339 struct epoll_event event; /* Event data */
340
341
342 event.events = 0;
343
344 if (read_cb)
345 event.events |= EPOLLIN;
346
347 if (write_cb)
348 event.events |= EPOLLOUT;
349
350 event.data.ptr = fdptr;
351
352 if (epoll_ctl(cupsd_epoll_fd, added ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, fd,
353 &event))
354 {
355 close(cupsd_epoll_fd);
356 cupsd_epoll_fd = -1;
357 cupsd_update_pollfds = 1;
358 }
359 }
360 else
361# endif /* HAVE_EPOLL */
362
f7deaa1a 363 cupsd_update_pollfds = 1;
364
365#else /* select() */
366 /*
367 * Add or remove the file descriptor in the input and output sets
368 * for select()...
369 */
370
371 if (read_cb)
f7deaa1a 372 FD_SET(fd, &cupsd_global_input);
f7deaa1a 373 else
374 {
f7deaa1a 375 FD_CLR(fd, &cupsd_global_input);
376 FD_CLR(fd, &cupsd_current_input);
377 }
378
379 if (write_cb)
f7deaa1a 380 FD_SET(fd, &cupsd_global_output);
f7deaa1a 381 else
382 {
f7deaa1a 383 FD_CLR(fd, &cupsd_global_output);
384 FD_CLR(fd, &cupsd_current_output);
385 }
0a682745 386#endif /* HAVE_KQUEUE */
f7deaa1a 387
388 /*
389 * Save the (new) read and write callbacks...
390 */
391
392 fdptr->read_cb = read_cb;
393 fdptr->write_cb = write_cb;
394 fdptr->data = data;
395
396 return (1);
397}
398
399
400/*
401 * 'cupsdDoSelect()' - Do a select-like operation.
402 */
403
404int /* O - Number of files or -1 on error */
405cupsdDoSelect(long timeout) /* I - Timeout in seconds */
406{
407 int nfds; /* Number of file descriptors */
408 _cupsd_fd_t *fdptr; /* Current file descriptor */
0a682745 409#ifdef HAVE_KQUEUE
f7deaa1a 410 int i; /* Looping var */
411 struct kevent *event; /* Current event */
412 struct timespec ktimeout; /* kevent() timeout */
413
414
c0e1af83 415 cupsd_in_select = 1;
416
f7deaa1a 417 if (timeout >= 0 && timeout < 86400)
418 {
419 ktimeout.tv_sec = timeout;
420 ktimeout.tv_nsec = 0;
421
422 nfds = kevent(cupsd_kqueue_fd, NULL, 0, cupsd_kqueue_events, MaxFDs,
423 &ktimeout);
424 }
425 else
426 nfds = kevent(cupsd_kqueue_fd, NULL, 0, cupsd_kqueue_events, MaxFDs, NULL);
427
f7deaa1a 428 cupsd_kqueue_changes = 0;
429
430 for (i = nfds, event = cupsd_kqueue_events; i > 0; i --, event ++)
431 {
432 fdptr = (_cupsd_fd_t *)event->udata;
433
c0e1af83 434 if (cupsArrayFind(cupsd_inactive_fds, fdptr))
435 continue;
436
f7deaa1a 437 retain_fd(fdptr);
438
439 if (fdptr->read_cb && event->filter == EVFILT_READ)
f7deaa1a 440 (*(fdptr->read_cb))(fdptr->data);
f7deaa1a 441
c7017ecc
MS
442 if (fdptr->use > 1 && fdptr->write_cb && event->filter == EVFILT_WRITE &&
443 !cupsArrayFind(cupsd_inactive_fds, fdptr))
f7deaa1a 444 (*(fdptr->write_cb))(fdptr->data);
f7deaa1a 445
446 release_fd(fdptr);
447 }
448
449#elif defined(HAVE_POLL)
450 struct pollfd *pfd; /* Current pollfd structure */
451 int count; /* Number of file descriptors */
452
453
0a682745
MS
454# ifdef HAVE_EPOLL
455 cupsd_in_select = 1;
456
457 if (cupsd_epoll_fd >= 0)
458 {
459 int i; /* Looping var */
460 struct epoll_event *event; /* Current event */
461
462
463 if (timeout >= 0 && timeout < 86400)
464 nfds = epoll_wait(cupsd_epoll_fd, cupsd_epoll_events, MaxFDs,
465 timeout * 1000);
466 else
467 nfds = epoll_wait(cupsd_epoll_fd, cupsd_epoll_events, MaxFDs, -1);
468
0a682745
MS
469 if (nfds < 0 && errno != EINTR)
470 {
471 close(cupsd_epoll_fd);
472 cupsd_epoll_fd = -1;
473 }
474 else
475 {
476 for (i = nfds, event = cupsd_epoll_events; i > 0; i --, event ++)
477 {
478 fdptr = (_cupsd_fd_t *)event->data.ptr;
479
480 if (cupsArrayFind(cupsd_inactive_fds, fdptr))
481 continue;
482
483 retain_fd(fdptr);
484
485 if (fdptr->read_cb && (event->events & (EPOLLIN | EPOLLERR | EPOLLHUP)))
0a682745 486 (*(fdptr->read_cb))(fdptr->data);
0a682745 487
ef55b745 488 if (fdptr->use > 1 && fdptr->write_cb &&
c7017ecc
MS
489 (event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) &&
490 !cupsArrayFind(cupsd_inactive_fds, fdptr))
0a682745 491 (*(fdptr->write_cb))(fdptr->data);
0a682745
MS
492
493 release_fd(fdptr);
494 }
495
496 goto release_inactive;
497 }
498 }
499# endif /* HAVE_EPOLL */
500
f7deaa1a 501 count = cupsArrayCount(cupsd_fds);
502
503 if (cupsd_update_pollfds)
504 {
505 /*
506 * Update the cupsd_pollfds array to match the current FD array...
507 */
508
509 cupsd_update_pollfds = 0;
510
f7deaa1a 511 /*
512 * (Re)allocate memory as needed...
513 */
514
515 if (count > cupsd_alloc_pollfds)
516 {
517 int allocfds = count + 16;
518
519
520 if (cupsd_pollfds)
521 pfd = realloc(cupsd_pollfds, allocfds * sizeof(struct pollfd));
522 else
523 pfd = malloc(allocfds * sizeof(struct pollfd));
524
525 if (!pfd)
526 {
527 cupsdLogMessage(CUPSD_LOG_EMERG,
528 "Unable to allocate %d bytes for polling!",
529 (int)(allocfds * sizeof(struct pollfd)));
530
531 return (-1);
532 }
533
534 cupsd_pollfds = pfd;
535 cupsd_alloc_pollfds = allocfds;
536 }
537
538 /*
539 * Rebuild the array...
540 */
541
542 for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds), pfd = cupsd_pollfds;
543 fdptr;
544 fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds), pfd ++)
545 {
546 pfd->fd = fdptr->fd;
547 pfd->events = 0;
548
549 if (fdptr->read_cb)
550 pfd->events |= POLLIN;
551
552 if (fdptr->write_cb)
553 pfd->events |= POLLOUT;
554 }
555 }
556
f7deaa1a 557 if (timeout >= 0 && timeout < 86400)
558 nfds = poll(cupsd_pollfds, count, timeout * 1000);
559 else
560 nfds = poll(cupsd_pollfds, count, -1);
561
f7deaa1a 562 if (nfds > 0)
563 {
564 /*
565 * Do callbacks for each file descriptor...
566 */
567
568 for (pfd = cupsd_pollfds; count > 0; pfd ++, count --)
569 {
f7deaa1a 570 if (!pfd->revents)
571 continue;
572
573 if ((fdptr = find_fd(pfd->fd)) == NULL)
574 continue;
575
576 retain_fd(fdptr);
577
578 if (fdptr->read_cb && (pfd->revents & (POLLIN | POLLERR | POLLHUP)))
f7deaa1a 579 (*(fdptr->read_cb))(fdptr->data);
f7deaa1a 580
ef55b745
MS
581 if (fdptr->use > 1 && fdptr->write_cb &&
582 (pfd->revents & (POLLOUT | POLLERR | POLLHUP)))
f7deaa1a 583 (*(fdptr->write_cb))(fdptr->data);
f7deaa1a 584
585 release_fd(fdptr);
586 }
587 }
588
589#else /* select() */
590 struct timeval stimeout; /* Timeout for select() */
591 int maxfd; /* Maximum file descriptor */
592
593
594 /*
595 * Figure out the highest file descriptor number...
596 */
597
598 if ((fdptr = (_cupsd_fd_t *)cupsArrayLast(cupsd_fds)) == NULL)
599 maxfd = 1;
600 else
601 maxfd = fdptr->fd + 1;
602
603 /*
604 * Do the select()...
605 */
606
607 cupsd_current_input = cupsd_global_input;
608 cupsd_current_output = cupsd_global_output;
609
f7deaa1a 610 if (timeout >= 0 && timeout < 86400)
611 {
612 stimeout.tv_sec = timeout;
613 stimeout.tv_usec = 0;
614
615 nfds = select(maxfd, &cupsd_current_input, &cupsd_current_output, NULL,
616 &stimeout);
617 }
618 else
619 nfds = select(maxfd, &cupsd_current_input, &cupsd_current_output, NULL,
620 NULL);
621
f7deaa1a 622 if (nfds > 0)
623 {
624 /*
625 * Do callbacks for each file descriptor...
626 */
627
628 for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds);
629 fdptr;
630 fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds))
631 {
632 retain_fd(fdptr);
633
634 if (fdptr->read_cb && FD_ISSET(fdptr->fd, &cupsd_current_input))
f7deaa1a 635 (*(fdptr->read_cb))(fdptr->data);
f7deaa1a 636
ef55b745
MS
637 if (fdptr->use > 1 && fdptr->write_cb &&
638 FD_ISSET(fdptr->fd, &cupsd_current_output))
f7deaa1a 639 (*(fdptr->write_cb))(fdptr->data);
f7deaa1a 640
641 release_fd(fdptr);
642 }
643 }
644
0a682745 645#endif /* HAVE_KQUEUE */
f7deaa1a 646
c0e1af83 647#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
648 /*
649 * Release all inactive file descriptors...
650 */
651
0a682745
MS
652# ifndef HAVE_KQUEUE
653 release_inactive:
654# endif /* !HAVE_KQUEUE */
655
c0e1af83 656 cupsd_in_select = 0;
657
658 for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_inactive_fds);
659 fdptr;
660 fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_inactive_fds))
661 {
662 cupsArrayRemove(cupsd_inactive_fds, fdptr);
663 release_fd(fdptr);
664 }
665#endif /* HAVE_EPOLL || HAVE_KQUEUE */
666
f7deaa1a 667 /*
668 * Return the number of file descriptors handled...
669 */
670
671 return (nfds);
672}
673
674
f899b121 675#ifdef CUPSD_IS_SELECTING
f7deaa1a 676/*
677 * 'cupsdIsSelecting()' - Determine whether we are monitoring a file
678 * descriptor.
679 */
680
681int /* O - 1 if selecting, 0 otherwise */
682cupsdIsSelecting(int fd) /* I - File descriptor */
683{
684 return (find_fd(fd) != NULL);
685}
f899b121 686#endif /* CUPSD_IS_SELECTING */
f7deaa1a 687
688
689/*
690 * 'cupsdRemoveSelect()' - Remove a file descriptor from the list.
691 */
692
693void
694cupsdRemoveSelect(int fd) /* I - File descriptor */
695{
696 _cupsd_fd_t *fdptr; /* File descriptor record */
697#ifdef HAVE_EPOLL
698 struct epoll_event event; /* Event data */
699#elif defined(HAVE_KQUEUE)
700 struct kevent event; /* Event data */
701 struct timespec timeout; /* Timeout value */
702#elif defined(HAVE_POLL)
703 /* No variables for poll() */
704#endif /* HAVE_EPOLL */
705
706
707 /*
708 * Range check input...
709 */
710
1bc82dd9 711 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdRemoveSelect(fd=%d)", fd);
f7deaa1a 712
713 if (fd < 0)
714 return;
715
716 /*
717 * Find the file descriptor...
718 */
719
720 if ((fdptr = find_fd(fd)) == NULL)
721 return;
722
723#ifdef HAVE_EPOLL
0a682745
MS
724 if (epoll_ctl(cupsd_epoll_fd, EPOLL_CTL_DEL, fd, &event))
725 {
726 close(cupsd_epoll_fd);
727 cupsd_epoll_fd = -1;
728 cupsd_update_pollfds = 1;
729 }
f7deaa1a 730
731#elif defined(HAVE_KQUEUE)
732 timeout.tv_sec = 0;
733 timeout.tv_nsec = 0;
734
735 if (fdptr->read_cb)
736 {
737 EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, fdptr);
738
739 if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
740 {
b9faaae1 741 cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
f7deaa1a 742 strerror(errno));
4a4b4f99 743 goto cleanup;
f7deaa1a 744 }
745 }
746
747 if (fdptr->write_cb)
748 {
749 EV_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, fdptr);
750
751 if (kevent(cupsd_kqueue_fd, &event, 1, NULL, 0, &timeout))
752 {
b9faaae1 753 cupsdLogMessage(CUPSD_LOG_EMERG, "kevent() returned %s",
f7deaa1a 754 strerror(errno));
4a4b4f99 755 goto cleanup;
f7deaa1a 756 }
757 }
758
f7deaa1a 759#elif defined(HAVE_POLL)
760 /*
761 * Update the pollfds array...
762 */
763
764 cupsd_update_pollfds = 1;
765
766#else /* select() */
f7deaa1a 767 FD_CLR(fd, &cupsd_global_input);
768 FD_CLR(fd, &cupsd_global_output);
769 FD_CLR(fd, &cupsd_current_input);
770 FD_CLR(fd, &cupsd_current_output);
771#endif /* HAVE_EPOLL */
772
4a4b4f99
MS
773#ifdef HAVE_KQUEUE
774 cleanup:
775#endif /* HAVE_KQUEUE */
776
f7deaa1a 777 /*
c0e1af83 778 * Remove the file descriptor from the active array and add to the
779 * inactive array (or release, if we don't need the inactive array...)
f7deaa1a 780 */
781
782 cupsArrayRemove(cupsd_fds, fdptr);
c0e1af83 783
784#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
785 if (cupsd_in_select)
786 cupsArrayAdd(cupsd_inactive_fds, fdptr);
787 else
788#endif /* HAVE_EPOLL || HAVE_KQUEUE */
789
f7deaa1a 790 release_fd(fdptr);
791}
792
793
794/*
795 * 'cupsdStartSelect()' - Initialize the file polling engine.
796 */
797
798void
799cupsdStartSelect(void)
800{
f2d18633 801 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartSelect()");
b9faaae1 802
f7deaa1a 803 cupsd_fds = cupsArrayNew((cups_array_func_t)compare_fds, NULL);
804
c0e1af83 805#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
806 cupsd_inactive_fds = cupsArrayNew((cups_array_func_t)compare_fds, NULL);
807#endif /* HAVE_EPOLL || HAVE_KQUEUE */
808
f7deaa1a 809#ifdef HAVE_EPOLL
0a682745
MS
810 cupsd_epoll_fd = epoll_create(MaxFDs);
811 cupsd_epoll_events = calloc(MaxFDs, sizeof(struct epoll_event));
812 cupsd_update_pollfds = 0;
f7deaa1a 813
814#elif defined(HAVE_KQUEUE)
815 cupsd_kqueue_fd = kqueue();
816 cupsd_kqueue_changes = 0;
7e86f2f6 817 cupsd_kqueue_events = calloc((size_t)MaxFDs, sizeof(struct kevent));
f7deaa1a 818
819#elif defined(HAVE_POLL)
820 cupsd_update_pollfds = 0;
821
822#else /* select() */
823 FD_ZERO(&cupsd_global_input);
824 FD_ZERO(&cupsd_global_output);
825#endif /* HAVE_EPOLL */
826}
827
828
829/*
830 * 'cupsdStopSelect()' - Shutdown the file polling engine.
831 */
832
833void
834cupsdStopSelect(void)
835{
836 _cupsd_fd_t *fdptr; /* Current file descriptor */
837
838
f2d18633 839 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStopSelect()");
b9faaae1 840
f7deaa1a 841 for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds);
842 fdptr;
843 fdptr = (_cupsd_fd_t *)cupsArrayNext(cupsd_fds))
844 free(fdptr);
845
846 cupsArrayDelete(cupsd_fds);
847 cupsd_fds = NULL;
848
c0e1af83 849#if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
850 cupsArrayDelete(cupsd_inactive_fds);
851 cupsd_inactive_fds = NULL;
852#endif /* HAVE_EPOLL || HAVE_KQUEUE */
853
0a682745 854#ifdef HAVE_KQUEUE
f7deaa1a 855 if (cupsd_kqueue_events)
856 {
857 free(cupsd_kqueue_events);
858 cupsd_kqueue_events = NULL;
859 }
860
861 if (cupsd_kqueue_fd >= 0)
862 {
863 close(cupsd_kqueue_fd);
864 cupsd_kqueue_fd = -1;
865 }
866
867 cupsd_kqueue_changes = 0;
868
869#elif defined(HAVE_POLL)
0a682745
MS
870# ifdef HAVE_EPOLL
871 if (cupsd_epoll_events)
872 {
873 free(cupsd_epoll_events);
874 cupsd_epoll_events = NULL;
875 }
876
877 if (cupsd_epoll_fd >= 0)
878 {
879 close(cupsd_epoll_fd);
880 cupsd_epoll_fd = -1;
881 }
882# endif /* HAVE_EPOLL */
883
f7deaa1a 884 if (cupsd_pollfds)
885 {
886 free(cupsd_pollfds);
887 cupsd_pollfds = NULL;
888 cupsd_alloc_pollfds = 0;
889 }
890
891 cupsd_update_pollfds = 0;
892
893#else /* select() */
894 FD_ZERO(&cupsd_global_input);
895 FD_ZERO(&cupsd_global_output);
896#endif /* HAVE_EPOLL */
897}
898
899
900/*
901 * 'compare_fds()' - Compare file descriptors.
902 */
903
904static int /* O - Result of comparison */
905compare_fds(_cupsd_fd_t *a, /* I - First file descriptor */
906 _cupsd_fd_t *b) /* I - Second file descriptor */
907{
908 return (a->fd - b->fd);
909}
910
911
912/*
913 * 'find_fd()' - Find an existing file descriptor record.
914 */
915
916static _cupsd_fd_t * /* O - FD record pointer or NULL */
917find_fd(int fd) /* I - File descriptor */
918{
919 _cupsd_fd_t *fdptr, /* Matching record (if any) */
920 key; /* Search key */
921
922
923 cupsArraySave(cupsd_fds);
924
925 key.fd = fd;
926 fdptr = (_cupsd_fd_t *)cupsArrayFind(cupsd_fds, &key);
927
928 cupsArrayRestore(cupsd_fds);
929
930 return (fdptr);
931}
932
933
934/*
f2d18633 935 * End of "$Id$".
f7deaa1a 936 */