2 * "$Id: select.c 6649 2007-07-11 21:46:42Z mike $"
4 * Select abstraction functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products.
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/".
17 * cupsdAddSelect() - Add a file descriptor to the list.
18 * cupsdDoSelect() - Do a select-like operation.
19 * cupsdIsSelecting() - Determine whether we are monitoring a file
21 * cupsdRemoveSelect() - Remove a file descriptor from the list.
22 * cupsdStartSelect() - Initialize the file polling engine.
23 * cupsdStopSelect() - Shutdown the file polling engine.
24 * compare_fds() - Compare file descriptors.
25 * find_fd() - Find an existing file descriptor record.
29 * Include necessary headers...
35 # include <sys/epoll.h>
36 # include <sys/poll.h>
37 #elif defined(HAVE_KQUEUE)
38 # include <sys/event.h>
39 # include <sys/time.h>
40 #elif defined(HAVE_POLL)
41 # include <sys/poll.h>
43 # include <sys/time.h>
45 # include <sys/select.h>
46 #endif /* HAVE_EPOLL */
50 * Design Notes for Poll/Select API in CUPSD
51 * -----------------------------------------
55 * OS select poll epoll kqueue /dev/poll
56 * -------------- ------ ------ ------ ------ ---------
57 * AIX YES YES NO NO NO
58 * FreeBSD YES YES NO YES NO
59 * HP-UX YES YES NO NO NO
60 * IRIX YES YES NO NO NO
61 * Linux YES YES YES NO NO
62 * MacOS X YES YES NO YES NO
63 * NetBSD YES YES NO YES NO
64 * OpenBSD YES YES NO YES NO
65 * Solaris YES YES NO NO YES
66 * Tru64 YES YES NO NO NO
67 * Windows YES NO NO NO NO
72 * typedef void (*cupsd_selfunc_t)(void *data);
74 * void cupsdStartSelect(void);
75 * void cupsdStopSelect(void);
76 * void cupsdAddSelect(int fd, cupsd_selfunc_t read_cb,
77 * cupsd_selfunc_t write_cb, void *data);
78 * void cupsdRemoveSelect(int fd);
79 * int cupsdDoSelect(int timeout);
82 * IMPLEMENTATION STRATEGY
85 * a. CUPS array of file descriptor to callback functions
86 * and data + temporary array of removed fd's.
87 * b. cupsdStartSelect() creates the arrays
88 * c. cupsdStopSelect() destroys the arrays and all elements.
89 * d. cupsdAddSelect() adds to the array and allocates a
90 * new callback element.
91 * e. cupsdRemoveSelect() removes from the active array and
92 * adds to the inactive array.
93 * f. _cupsd_fd_t provides a reference-counted structure for
94 * tracking file descriptors that are monitored.
95 * g. cupsdDoSelect() frees all inactive FDs.
98 * a. Input/Output fd_set variables, copied to working
99 * copies and then used with select().
100 * b. Loop through CUPS array, using FD_ISSET and calling
101 * the read/write callbacks as needed.
102 * c. cupsdRemoveSelect() clears fd_set bit from main and
104 * d. cupsdStopSelect() frees all of the memory used by the
105 * CUPS array and fd_set's.
107 * 2. poll() - O(n log n)
108 * a. Regular array of pollfd, sorted the same as the CUPS
110 * b. Loop through pollfd array, call the corresponding
111 * read/write callbacks as needed.
112 * c. cupsdAddSelect() adds first to CUPS array and flags the
113 * pollfd array as invalid.
114 * d. cupsdDoSelect() rebuilds pollfd array as needed, calls
115 * poll(), then loops through the pollfd array looking up
117 * e. cupsdRemoveSelect() flags the pollfd array as invalid.
118 * f. cupsdStopSelect() frees all of the memory used by the
119 * CUPS array and pollfd array.
122 * a. cupsdStartSelect() creates epoll file descriptor using
123 * epoll_create() with the maximum fd count, and
124 * allocates an events buffer for the maximum fd count.
125 * b. cupsdAdd/RemoveSelect() uses epoll_ctl() to add
126 * (EPOLL_CTL_ADD) or remove (EPOLL_CTL_DEL) a single
127 * event using the level-triggered semantics. The event
128 * user data field is a pointer to the new callback array
130 * c. cupsdDoSelect() uses epoll_wait() with the global event
131 * buffer allocated in cupsdStartSelect() and then loops
132 * through the events, using the user data field to find
133 * the callback record.
134 * d. cupsdStopSelect() closes the epoll file descriptor and
135 * frees all of the memory used by the event buffer.
138 * b. cupsdStartSelect() creates kqueue file descriptor
139 * using kqyeue() function and allocates a global event
141 * c. cupsdAdd/RemoveSelect() uses EV_SET and kevent() to
142 * register the changes. The event user data field is a
143 * pointer to the new callback array element.
144 * d. cupsdDoSelect() uses kevent() to poll for events and
145 * loops through the events, using the user data field to
146 * find the callback record.
147 * e. cupsdStopSelect() closes the kqyeye() file descriptor
148 * and frees all of the memory used by the event buffer.
150 * 5. /dev/poll - O(n log n) - NOT YET IMPLEMENTED
151 * a. cupsdStartSelect() opens /dev/poll and allocates an
152 * array of pollfd structs; on failure to open /dev/poll,
153 * revert to poll() system call.
154 * b. cupsdAddSelect() writes a single pollfd struct to
155 * /dev/poll with the new file descriptor and the
156 * POLLIN/POLLOUT flags.
157 * c. cupsdRemoveSelect() writes a single pollfd struct to
158 * /dev/poll with the file descriptor and the POLLREMOVE
160 * d. cupsdDoSelect() uses the DP_POLL ioctl to retrieve
161 * events from /dev/poll and then loops through the
162 * returned pollfd array, looking up the file descriptors
164 * e. cupsdStopSelect() closes /dev/poll and frees the
169 * In tests using the "make test" target with option 0 (keep cupsd
170 * running) and the "testspeed" program with "-c 50 -r 1000", epoll()
171 * performed 5.5% slower than select(), followed by kqueue() at 16%
172 * slower than select() and poll() at 18% slower than select(). Similar
173 * results were seen with twice the number of client connections.
175 * The epoll() and kqueue() performance is likely limited by the
176 * number of system calls used to add/modify/remove file
177 * descriptors dynamically. Further optimizations may be possible
178 * in the area of limiting use of cupsdAddSelect() and
179 * cupsdRemoveSelect(), however extreme care will be needed to avoid
180 * excess CPU usage and deadlock conditions.
182 * We may be able to improve the poll() implementation simply by
183 * keeping the pollfd array sync'd with the _cupsd_fd_t array, as that
184 * will eliminate the rebuilding of the array whenever there is a
185 * change and eliminate the fd array lookups in the inner loop of
188 * Since /dev/poll will never be able to use a shadow array, it may
189 * not make sense to implement support for it. ioctl() overhead will
190 * impact performance as well, so my guess would be that, for CUPS,
191 * /dev/poll will yield a net performance loss.
195 * Local structures...
198 typedef struct _cupsd_fd_s
200 int fd
, /* File descriptor */
202 cupsd_selfunc_t read_cb
, /* Read callback */
203 write_cb
; /* Write callback */
204 void *data
; /* Data pointer for callbacks */
212 static cups_array_t
*cupsd_fds
= NULL
;
213 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
214 static cups_array_t
*cupsd_inactive_fds
= NULL
;
215 static int cupsd_in_select
= 0;
216 #endif /* HAVE_EPOLL || HAVE_KQUEUE */
219 static int cupsd_kqueue_fd
= -1,
220 cupsd_kqueue_changes
= 0;
221 static struct kevent
*cupsd_kqueue_events
= NULL
;
222 #elif defined(HAVE_POLL)
223 static int cupsd_alloc_pollfds
= 0,
224 cupsd_update_pollfds
= 0;
225 static struct pollfd
*cupsd_pollfds
= NULL
;
227 static int cupsd_epoll_fd
= -1;
228 static struct epoll_event
*cupsd_epoll_events
= NULL
;
229 # endif /* HAVE_EPOLL */
231 static fd_set cupsd_global_input
,
234 cupsd_current_output
;
235 #endif /* HAVE_KQUEUE */
242 static int compare_fds(_cupsd_fd_t
*a
, _cupsd_fd_t
*b
);
243 static _cupsd_fd_t
*find_fd(int fd
);
244 #define release_fd(f) { \
246 if (!(f)->use) free((f));\
248 #define retain_fd(f) (f)->use++
252 * 'cupsdAddSelect()' - Add a file descriptor to the list.
255 int /* O - 1 on success, 0 on error */
256 cupsdAddSelect(int fd
, /* I - File descriptor */
257 cupsd_selfunc_t read_cb
, /* I - Read callback */
258 cupsd_selfunc_t write_cb
,/* I - Write callback */
259 void *data
) /* I - Data to pass to callback */
261 _cupsd_fd_t
*fdptr
; /* File descriptor record */
262 int added
; /* 1 if added, 0 if modified */
266 * Range check input...
269 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
270 "cupsdAddSelect: fd=%d, read_cb=%p, write_cb=%p, data=%p",
271 fd
, read_cb
, write_cb
, data
);
277 * See if this FD has already been added...
280 if ((fdptr
= find_fd(fd
)) == NULL
)
283 * No, add a new entry...
286 if ((fdptr
= calloc(1, sizeof(_cupsd_fd_t
))) == NULL
)
292 if (!cupsArrayAdd(cupsd_fds
, fdptr
))
294 cupsdLogMessage(CUPSD_LOG_EMERG
, "Unable to add fd %d to array!", fd
);
306 struct kevent event
; /* Event data */
307 struct timespec timeout
; /* Timeout value */
313 if (fdptr
->read_cb
!= read_cb
)
316 EV_SET(&event
, fd
, EVFILT_READ
, EV_ADD
, 0, 0, fdptr
);
318 EV_SET(&event
, fd
, EVFILT_READ
, EV_DELETE
, 0, 0, fdptr
);
320 if (kevent(cupsd_kqueue_fd
, &event
, 1, NULL
, 0, &timeout
))
322 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
323 "cupsdAddSelect: kevent() returned %s",
329 if (fdptr
->write_cb
!= write_cb
)
332 EV_SET(&event
, fd
, EVFILT_WRITE
, EV_ADD
, 0, 0, fdptr
);
334 EV_SET(&event
, fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, fdptr
);
336 if (kevent(cupsd_kqueue_fd
, &event
, 1, NULL
, 0, &timeout
))
338 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
339 "cupsdAddSelect: kevent() returned %s",
346 #elif defined(HAVE_POLL)
348 if (cupsd_epoll_fd
>= 0)
350 struct epoll_event event
; /* Event data */
356 event
.events
|= EPOLLIN
;
359 event
.events
|= EPOLLOUT
;
361 event
.data
.ptr
= fdptr
;
363 if (epoll_ctl(cupsd_epoll_fd
, added
? EPOLL_CTL_ADD
: EPOLL_CTL_MOD
, fd
,
366 close(cupsd_epoll_fd
);
368 cupsd_update_pollfds
= 1;
372 # endif /* HAVE_EPOLL */
374 cupsd_update_pollfds
= 1;
378 * Add or remove the file descriptor in the input and output sets
384 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
385 "cupsdAddSelect: Adding fd %d to input set...", fd
);
386 FD_SET(fd
, &cupsd_global_input
);
390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
391 "cupsdAddSelect: Removing fd %d from input set...", fd
);
392 FD_CLR(fd
, &cupsd_global_input
);
393 FD_CLR(fd
, &cupsd_current_input
);
398 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
399 "cupsdAddSelect: Adding fd %d to output set...", fd
);
400 FD_SET(fd
, &cupsd_global_output
);
404 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
405 "cupsdAddSelect: Removing fd %d from output set...", fd
);
406 FD_CLR(fd
, &cupsd_global_output
);
407 FD_CLR(fd
, &cupsd_current_output
);
409 #endif /* HAVE_KQUEUE */
412 * Save the (new) read and write callbacks...
415 fdptr
->read_cb
= read_cb
;
416 fdptr
->write_cb
= write_cb
;
424 * 'cupsdDoSelect()' - Do a select-like operation.
427 int /* O - Number of files or -1 on error */
428 cupsdDoSelect(long timeout
) /* I - Timeout in seconds */
430 int nfds
; /* Number of file descriptors */
431 _cupsd_fd_t
*fdptr
; /* Current file descriptor */
433 int i
; /* Looping var */
434 struct kevent
*event
; /* Current event */
435 struct timespec ktimeout
; /* kevent() timeout */
438 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
439 "cupsdDoSelect: polling %d fds for %ld seconds...",
440 cupsArrayCount(cupsd_fds
), timeout
);
444 if (timeout
>= 0 && timeout
< 86400)
446 ktimeout
.tv_sec
= timeout
;
447 ktimeout
.tv_nsec
= 0;
449 nfds
= kevent(cupsd_kqueue_fd
, NULL
, 0, cupsd_kqueue_events
, MaxFDs
,
453 nfds
= kevent(cupsd_kqueue_fd
, NULL
, 0, cupsd_kqueue_events
, MaxFDs
, NULL
);
455 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
456 "cupsdDoSelect: kevent(%d, ..., %d, ...) returned %d...",
457 cupsd_kqueue_fd
, MaxFDs
, nfds
);
459 cupsd_kqueue_changes
= 0;
461 for (i
= nfds
, event
= cupsd_kqueue_events
; i
> 0; i
--, event
++)
463 fdptr
= (_cupsd_fd_t
*)event
->udata
;
465 if (cupsArrayFind(cupsd_inactive_fds
, fdptr
))
468 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "event->filter=%d, event->ident=%d",
469 event
->filter
, (int)event
->ident
);
473 if (fdptr
->read_cb
&& event
->filter
== EVFILT_READ
)
475 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Read on fd %d...",
477 (*(fdptr
->read_cb
))(fdptr
->data
);
480 if (fdptr
->write_cb
&& event
->filter
== EVFILT_WRITE
)
482 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Write on fd %d...",
484 (*(fdptr
->write_cb
))(fdptr
->data
);
490 #elif defined(HAVE_POLL)
491 struct pollfd
*pfd
; /* Current pollfd structure */
492 int count
; /* Number of file descriptors */
495 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
496 "cupsdDoSelect: polling %d fds for %ld seconds...",
497 cupsArrayCount(cupsd_fds
), timeout
);
502 if (cupsd_epoll_fd
>= 0)
504 int i
; /* Looping var */
505 struct epoll_event
*event
; /* Current event */
508 if (timeout
>= 0 && timeout
< 86400)
509 nfds
= epoll_wait(cupsd_epoll_fd
, cupsd_epoll_events
, MaxFDs
,
512 nfds
= epoll_wait(cupsd_epoll_fd
, cupsd_epoll_events
, MaxFDs
, -1);
514 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: epoll() returned %d...",
517 if (nfds
< 0 && errno
!= EINTR
)
519 close(cupsd_epoll_fd
);
524 for (i
= nfds
, event
= cupsd_epoll_events
; i
> 0; i
--, event
++)
526 fdptr
= (_cupsd_fd_t
*)event
->data
.ptr
;
528 if (cupsArrayFind(cupsd_inactive_fds
, fdptr
))
533 if (fdptr
->read_cb
&& (event
->events
& (EPOLLIN
| EPOLLERR
| EPOLLHUP
)))
535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Read on fd %d...",
537 (*(fdptr
->read_cb
))(fdptr
->data
);
540 if (fdptr
->write_cb
&& (event
->events
& (EPOLLOUT
| EPOLLERR
| EPOLLHUP
)))
542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Write on fd %d...",
544 (*(fdptr
->write_cb
))(fdptr
->data
);
550 goto release_inactive
;
553 # endif /* HAVE_EPOLL */
555 count
= cupsArrayCount(cupsd_fds
);
557 if (cupsd_update_pollfds
)
560 * Update the cupsd_pollfds array to match the current FD array...
563 cupsd_update_pollfds
= 0;
565 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Updating pollfd array...");
568 * (Re)allocate memory as needed...
571 if (count
> cupsd_alloc_pollfds
)
573 int allocfds
= count
+ 16;
577 pfd
= realloc(cupsd_pollfds
, allocfds
* sizeof(struct pollfd
));
579 pfd
= malloc(allocfds
* sizeof(struct pollfd
));
583 cupsdLogMessage(CUPSD_LOG_EMERG
,
584 "Unable to allocate %d bytes for polling!",
585 (int)(allocfds
* sizeof(struct pollfd
)));
591 cupsd_alloc_pollfds
= allocfds
;
595 * Rebuild the array...
598 for (fdptr
= (_cupsd_fd_t
*)cupsArrayFirst(cupsd_fds
), pfd
= cupsd_pollfds
;
600 fdptr
= (_cupsd_fd_t
*)cupsArrayNext(cupsd_fds
), pfd
++)
606 pfd
->events
|= POLLIN
;
609 pfd
->events
|= POLLOUT
;
613 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
614 "cupsdDoSelect: polling %d fds for %ld seconds...",
617 if (timeout
>= 0 && timeout
< 86400)
618 nfds
= poll(cupsd_pollfds
, count
, timeout
* 1000);
620 nfds
= poll(cupsd_pollfds
, count
, -1);
622 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: poll() returned %d...",
628 * Do callbacks for each file descriptor...
631 for (pfd
= cupsd_pollfds
; count
> 0; pfd
++, count
--)
633 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
634 "cupsdDoSelect: pollfds[%d]={fd=%d, revents=%x}",
635 pfd
- cupsd_pollfds
, pfd
->fd
, pfd
->revents
);
640 if ((fdptr
= find_fd(pfd
->fd
)) == NULL
)
645 if (fdptr
->read_cb
&& (pfd
->revents
& (POLLIN
| POLLERR
| POLLHUP
)))
647 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Read on fd %d...",
649 (*(fdptr
->read_cb
))(fdptr
->data
);
652 if (fdptr
->write_cb
&& (pfd
->revents
& (POLLOUT
| POLLERR
| POLLHUP
)))
654 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Write on fd %d...",
656 (*(fdptr
->write_cb
))(fdptr
->data
);
664 struct timeval stimeout
; /* Timeout for select() */
665 int maxfd
; /* Maximum file descriptor */
669 * Figure out the highest file descriptor number...
672 if ((fdptr
= (_cupsd_fd_t
*)cupsArrayLast(cupsd_fds
)) == NULL
)
675 maxfd
= fdptr
->fd
+ 1;
681 cupsd_current_input
= cupsd_global_input
;
682 cupsd_current_output
= cupsd_global_output
;
684 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
685 "cupsdDoSelect: selecting %d fds for %ld seconds...",
688 if (timeout
>= 0 && timeout
< 86400)
690 stimeout
.tv_sec
= timeout
;
691 stimeout
.tv_usec
= 0;
693 nfds
= select(maxfd
, &cupsd_current_input
, &cupsd_current_output
, NULL
,
697 nfds
= select(maxfd
, &cupsd_current_input
, &cupsd_current_output
, NULL
,
700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: select() returned %d...",
706 * Do callbacks for each file descriptor...
709 for (fdptr
= (_cupsd_fd_t
*)cupsArrayFirst(cupsd_fds
);
711 fdptr
= (_cupsd_fd_t
*)cupsArrayNext(cupsd_fds
))
715 if (fdptr
->read_cb
&& FD_ISSET(fdptr
->fd
, &cupsd_current_input
))
717 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Read on fd %d...",
719 (*(fdptr
->read_cb
))(fdptr
->data
);
722 if (fdptr
->write_cb
&& FD_ISSET(fdptr
->fd
, &cupsd_current_output
))
724 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDoSelect: Write on fd %d...",
726 (*(fdptr
->write_cb
))(fdptr
->data
);
733 #endif /* HAVE_KQUEUE */
735 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
737 * Release all inactive file descriptors...
742 # endif /* !HAVE_KQUEUE */
746 for (fdptr
= (_cupsd_fd_t
*)cupsArrayFirst(cupsd_inactive_fds
);
748 fdptr
= (_cupsd_fd_t
*)cupsArrayNext(cupsd_inactive_fds
))
750 cupsArrayRemove(cupsd_inactive_fds
, fdptr
);
753 #endif /* HAVE_EPOLL || HAVE_KQUEUE */
756 * Return the number of file descriptors handled...
763 #ifdef CUPSD_IS_SELECTING
765 * 'cupsdIsSelecting()' - Determine whether we are monitoring a file
769 int /* O - 1 if selecting, 0 otherwise */
770 cupsdIsSelecting(int fd
) /* I - File descriptor */
772 return (find_fd(fd
) != NULL
);
774 #endif /* CUPSD_IS_SELECTING */
778 * 'cupsdRemoveSelect()' - Remove a file descriptor from the list.
782 cupsdRemoveSelect(int fd
) /* I - File descriptor */
784 _cupsd_fd_t
*fdptr
; /* File descriptor record */
786 struct epoll_event event
; /* Event data */
787 #elif defined(HAVE_KQUEUE)
788 struct kevent event
; /* Event data */
789 struct timespec timeout
; /* Timeout value */
790 #elif defined(HAVE_POLL)
791 /* No variables for poll() */
792 #endif /* HAVE_EPOLL */
796 * Range check input...
799 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdRemoveSelect: fd=%d", fd
);
805 * Find the file descriptor...
808 if ((fdptr
= find_fd(fd
)) == NULL
)
812 if (epoll_ctl(cupsd_epoll_fd
, EPOLL_CTL_DEL
, fd
, &event
))
814 close(cupsd_epoll_fd
);
816 cupsd_update_pollfds
= 1;
819 #elif defined(HAVE_KQUEUE)
825 EV_SET(&event
, fd
, EVFILT_READ
, EV_DELETE
, 0, 0, fdptr
);
827 if (kevent(cupsd_kqueue_fd
, &event
, 1, NULL
, 0, &timeout
))
829 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
830 "cupsdRemoveSelect: kevent() returned %s",
838 EV_SET(&event
, fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, fdptr
);
840 if (kevent(cupsd_kqueue_fd
, &event
, 1, NULL
, 0, &timeout
))
842 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
843 "cupsdRemoveSelect: kevent() returned %s",
850 #elif defined(HAVE_POLL)
852 * Update the pollfds array...
855 cupsd_update_pollfds
= 1;
858 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
859 "cupsdRemoveSelect: Removing fd %d from input and output "
861 FD_CLR(fd
, &cupsd_global_input
);
862 FD_CLR(fd
, &cupsd_global_output
);
863 FD_CLR(fd
, &cupsd_current_input
);
864 FD_CLR(fd
, &cupsd_current_output
);
865 #endif /* HAVE_EPOLL */
868 * Remove the file descriptor from the active array and add to the
869 * inactive array (or release, if we don't need the inactive array...)
872 cupsArrayRemove(cupsd_fds
, fdptr
);
874 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
876 cupsArrayAdd(cupsd_inactive_fds
, fdptr
);
878 #endif /* HAVE_EPOLL || HAVE_KQUEUE */
885 * 'cupsdStartSelect()' - Initialize the file polling engine.
889 cupsdStartSelect(void)
891 cupsd_fds
= cupsArrayNew((cups_array_func_t
)compare_fds
, NULL
);
893 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
894 cupsd_inactive_fds
= cupsArrayNew((cups_array_func_t
)compare_fds
, NULL
);
895 #endif /* HAVE_EPOLL || HAVE_KQUEUE */
898 cupsd_epoll_fd
= epoll_create(MaxFDs
);
899 cupsd_epoll_events
= calloc(MaxFDs
, sizeof(struct epoll_event
));
900 cupsd_update_pollfds
= 0;
902 #elif defined(HAVE_KQUEUE)
903 cupsd_kqueue_fd
= kqueue();
904 cupsd_kqueue_changes
= 0;
905 cupsd_kqueue_events
= calloc(MaxFDs
, sizeof(struct kevent
));
907 #elif defined(HAVE_POLL)
908 cupsd_update_pollfds
= 0;
911 FD_ZERO(&cupsd_global_input
);
912 FD_ZERO(&cupsd_global_output
);
913 #endif /* HAVE_EPOLL */
918 * 'cupsdStopSelect()' - Shutdown the file polling engine.
922 cupsdStopSelect(void)
924 _cupsd_fd_t
*fdptr
; /* Current file descriptor */
927 for (fdptr
= (_cupsd_fd_t
*)cupsArrayFirst(cupsd_fds
);
929 fdptr
= (_cupsd_fd_t
*)cupsArrayNext(cupsd_fds
))
932 cupsArrayDelete(cupsd_fds
);
935 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
936 cupsArrayDelete(cupsd_inactive_fds
);
937 cupsd_inactive_fds
= NULL
;
938 #endif /* HAVE_EPOLL || HAVE_KQUEUE */
941 if (cupsd_kqueue_events
)
943 free(cupsd_kqueue_events
);
944 cupsd_kqueue_events
= NULL
;
947 if (cupsd_kqueue_fd
>= 0)
949 close(cupsd_kqueue_fd
);
950 cupsd_kqueue_fd
= -1;
953 cupsd_kqueue_changes
= 0;
955 #elif defined(HAVE_POLL)
957 if (cupsd_epoll_events
)
959 free(cupsd_epoll_events
);
960 cupsd_epoll_events
= NULL
;
963 if (cupsd_epoll_fd
>= 0)
965 close(cupsd_epoll_fd
);
968 # endif /* HAVE_EPOLL */
973 cupsd_pollfds
= NULL
;
974 cupsd_alloc_pollfds
= 0;
977 cupsd_update_pollfds
= 0;
980 FD_ZERO(&cupsd_global_input
);
981 FD_ZERO(&cupsd_global_output
);
982 #endif /* HAVE_EPOLL */
987 * 'compare_fds()' - Compare file descriptors.
990 static int /* O - Result of comparison */
991 compare_fds(_cupsd_fd_t
*a
, /* I - First file descriptor */
992 _cupsd_fd_t
*b
) /* I - Second file descriptor */
994 return (a
->fd
- b
->fd
);
999 * 'find_fd()' - Find an existing file descriptor record.
1002 static _cupsd_fd_t
* /* O - FD record pointer or NULL */
1003 find_fd(int fd
) /* I - File descriptor */
1005 _cupsd_fd_t
*fdptr
, /* Matching record (if any) */
1006 key
; /* Search key */
1009 cupsArraySave(cupsd_fds
);
1012 fdptr
= (_cupsd_fd_t
*)cupsArrayFind(cupsd_fds
, &key
);
1014 cupsArrayRestore(cupsd_fds
);
1021 * End of "$Id: select.c 6649 2007-07-11 21:46:42Z mike $".