]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/select.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / scheduler / select.c
index 96f12ab3a3c00dda2e5291846e0857f5e0f82ea8..06079c37a0860db5e4a24c2c2b59001cec62a3ac 100644 (file)
@@ -1,28 +1,10 @@
 /*
- * "$Id: select.c 7720 2008-07-11 22:46:21Z mike $"
+ * Select abstraction functions for the CUPS scheduler.
  *
- *   Select abstraction functions for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2016 by Apple Inc.
+ * Copyright 2006-2007 by Easy Software Products.
  *
- *   Copyright 2007-2009 by Apple Inc.
- *   Copyright 2006-2007 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   cupsdAddSelect()    - Add a file descriptor to the list.
- *   cupsdDoSelect()     - Do a select-like operation.
- *   cupsdIsSelecting()  - Determine whether we are monitoring a file
- *                         descriptor.
- *   cupsdRemoveSelect() - Remove a file descriptor from the list.
- *   cupsdStartSelect()  - Initialize the file polling engine.
- *   cupsdStopSelect()   - Shutdown the file polling engine.
- *   compare_fds()       - Compare file descriptors.
- *   find_fd()           - Find an existing file descriptor record.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
 
 #ifdef HAVE_EPOLL
 #  include <sys/epoll.h>
-#  include <sys/poll.h>
+#  include <poll.h>
 #elif defined(HAVE_KQUEUE)
 #  include <sys/event.h>
 #  include <sys/time.h>
 #elif defined(HAVE_POLL)
-#  include <sys/poll.h>
-#elif defined(__hpux)
-#  include <sys/time.h>
+#  include <poll.h>
 #else
 #  include <sys/select.h>
 #endif /* HAVE_EPOLL */
 /*
  * Design Notes for Poll/Select API in CUPSD
  * -----------------------------------------
- * 
+ *
  * SUPPORTED APIS
- * 
+ *
  *     OS              select  poll    epoll   kqueue  /dev/poll
  *     --------------  ------  ------  ------  ------  ---------
  *     AIX             YES     YES     NO      NO      NO
  *     FreeBSD         YES     YES     NO      YES     NO
  *     HP-UX           YES     YES     NO      NO      NO
- *     IRIX            YES     YES     NO      NO      NO
  *     Linux           YES     YES     YES     NO      NO
- *     MacOS X         YES     YES     NO      YES     NO
+ *     macOS           YES     YES     NO      YES     NO
  *     NetBSD          YES     YES     NO      YES     NO
  *     OpenBSD         YES     YES     NO      YES     NO
  *     Solaris         YES     YES     NO      NO      YES
  *     Tru64           YES     YES     NO      NO      NO
  *     Windows         YES     NO      NO      NO      NO
- * 
- * 
+ *
+ *
  * HIGH-LEVEL API
- * 
+ *
  *     typedef void (*cupsd_selfunc_t)(void *data);
- * 
+ *
  *     void cupsdStartSelect(void);
  *     void cupsdStopSelect(void);
  *     void cupsdAddSelect(int fd, cupsd_selfunc_t read_cb,
  *                         cupsd_selfunc_t write_cb, void *data);
  *     void cupsdRemoveSelect(int fd);
  *     int cupsdDoSelect(int timeout);
- * 
- * 
+ *
+ *
  * IMPLEMENTATION STRATEGY
- * 
+ *
  *     0. Common Stuff
  *         a. CUPS array of file descriptor to callback functions
  *            and data + temporary array of removed fd's.
  *            working sets.
  *         d. cupsdStopSelect() frees all of the memory used by the
  *            CUPS array and fd_set's.
- * 
+ *
  *     2. poll() - O(n log n)
  *         a. Regular array of pollfd, sorted the same as the CUPS
  *            array.
  *         e. cupsdRemoveSelect() flags the pollfd array as invalid.
  *         f. cupsdStopSelect() frees all of the memory used by the
  *            CUPS array and pollfd array.
- * 
+ *
  *     3. epoll() - O(n)
  *         a. cupsdStartSelect() creates epoll file descriptor using
  *            epoll_create() with the maximum fd count, and
  *            the callback record.
  *         d. cupsdStopSelect() closes the epoll file descriptor and
  *            frees all of the memory used by the event buffer.
- * 
+ *
  *     4. kqueue() - O(n)
  *         b. cupsdStartSelect() creates kqueue file descriptor
- *            using kqyeue() function and allocates a global event
+ *            using kqueue() function and allocates a global event
  *            buffer.
  *         c. cupsdAdd/RemoveSelect() uses EV_SET and kevent() to
  *            register the changes. The event user data field is a
  *         d. cupsdDoSelect() uses kevent() to poll for events and
  *            loops through the events, using the user data field to
  *            find the callback record.
- *         e. cupsdStopSelect() closes the kqyeye() file descriptor
+ *         e. cupsdStopSelect() closes the kqueue() file descriptor
  *            and frees all of the memory used by the event buffer.
- * 
+ *
  *     5. /dev/poll - O(n log n) - NOT YET IMPLEMENTED
  *         a. cupsdStartSelect() opens /dev/poll and allocates an
  *            array of pollfd structs; on failure to open /dev/poll,
@@ -241,11 +220,11 @@ static fd_set             cupsd_global_input,
 
 static int             compare_fds(_cupsd_fd_t *a, _cupsd_fd_t *b);
 static _cupsd_fd_t     *find_fd(int fd);
-#define                release_fd(f) { \
+#define                        release_fd(f) { \
                          (f)->use --; \
                          if (!(f)->use) free((f));\
                        }
-#define                retain_fd(f) (f)->use++
+#define                        retain_fd(f) (f)->use++
 
 
 /*
@@ -454,7 +433,8 @@ cupsdDoSelect(long timeout)         /* I - Timeout in seconds */
     if (fdptr->read_cb && event->filter == EVFILT_READ)
       (*(fdptr->read_cb))(fdptr->data);
 
-    if (fdptr->use > 1 && fdptr->write_cb && event->filter == EVFILT_WRITE)
+    if (fdptr->use > 1 && fdptr->write_cb && event->filter == EVFILT_WRITE &&
+        !cupsArrayFind(cupsd_inactive_fds, fdptr))
       (*(fdptr->write_cb))(fdptr->data);
 
     release_fd(fdptr);
@@ -500,7 +480,8 @@ cupsdDoSelect(long timeout)         /* I - Timeout in seconds */
          (*(fdptr->read_cb))(fdptr->data);
 
        if (fdptr->use > 1 && fdptr->write_cb &&
-           (event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)))
+            (event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) &&
+            !cupsArrayFind(cupsd_inactive_fds, fdptr))
          (*(fdptr->write_cb))(fdptr->data);
 
        release_fd(fdptr);
@@ -531,15 +512,13 @@ cupsdDoSelect(long timeout)               /* I - Timeout in seconds */
 
 
       if (cupsd_pollfds)
-       pfd = realloc(cupsd_pollfds, allocfds * sizeof(struct pollfd));
+       pfd = realloc(cupsd_pollfds, (size_t)allocfds * sizeof(struct pollfd));
       else
-       pfd = malloc(allocfds * sizeof(struct pollfd));
+       pfd = malloc((size_t)allocfds * sizeof(struct pollfd));
 
       if (!pfd)
       {
-       cupsdLogMessage(CUPSD_LOG_EMERG,
-                       "Unable to allocate %d bytes for polling!",
-                       (int)(allocfds * sizeof(struct pollfd)));
+       cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to allocate %d bytes for polling.", (int)((size_t)allocfds * sizeof(struct pollfd)));
 
        return (-1);
       }
@@ -568,9 +547,9 @@ cupsdDoSelect(long timeout)         /* I - Timeout in seconds */
   }
 
   if (timeout >= 0 && timeout < 86400)
-    nfds = poll(cupsd_pollfds, count, timeout * 1000);
+    nfds = poll(cupsd_pollfds, (nfds_t)count, timeout * 1000);
   else
-    nfds = poll(cupsd_pollfds, count, -1);
+    nfds = poll(cupsd_pollfds, (nfds_t)count, -1);
 
   if (nfds > 0)
   {
@@ -811,7 +790,7 @@ cupsdRemoveSelect(int fd)           /* I - File descriptor */
 void
 cupsdStartSelect(void)
 {
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartSelect()");
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStartSelect()");
 
   cupsd_fds = cupsArrayNew((cups_array_func_t)compare_fds, NULL);
 
@@ -821,13 +800,13 @@ cupsdStartSelect(void)
 
 #ifdef HAVE_EPOLL
   cupsd_epoll_fd       = epoll_create(MaxFDs);
-  cupsd_epoll_events   = calloc(MaxFDs, sizeof(struct epoll_event));
+  cupsd_epoll_events   = calloc((size_t)MaxFDs, sizeof(struct epoll_event));
   cupsd_update_pollfds = 0;
 
 #elif defined(HAVE_KQUEUE)
   cupsd_kqueue_fd      = kqueue();
   cupsd_kqueue_changes = 0;
-  cupsd_kqueue_events  = calloc(MaxFDs, sizeof(struct kevent));
+  cupsd_kqueue_events  = calloc((size_t)MaxFDs, sizeof(struct kevent));
 
 #elif defined(HAVE_POLL)
   cupsd_update_pollfds = 0;
@@ -849,7 +828,7 @@ cupsdStopSelect(void)
   _cupsd_fd_t  *fdptr;                 /* Current file descriptor */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStopSelect()");
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdStopSelect()");
 
   for (fdptr = (_cupsd_fd_t *)cupsArrayFirst(cupsd_fds);
        fdptr;
@@ -942,8 +921,3 @@ find_fd(int fd)                             /* I - File descriptor */
 
   return (fdptr);
 }
-
-
-/*
- * End of "$Id: select.c 7720 2008-07-11 22:46:21Z mike $".
- */