2 * Backchannel functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
15 #include "sidechannel.h"
21 # include <sys/time.h>
29 static void cups_setup(fd_set *set, struct timeval *tval,
34 * 'cupsBackChannelRead()' - Read data from the backchannel.
36 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
37 * parameter controls how many seconds to wait for the data - use 0.0 to
38 * return immediately if there is no data, -1.0 to wait for data indefinitely.
40 * @since CUPS 1.2/macOS 10.5@
43 ssize_t /* O - Bytes read or -1 on error */
44 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
45 size_t bytes, /* I - Bytes to read */
46 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
48 fd_set input; /* Input set */
49 struct timeval tval; /* Timeout value */
50 int status; /* Select status */
54 * Wait for input ready.
59 cups_setup(&input, &tval, timeout);
62 status = select(4, &input, NULL, NULL, NULL);
64 status = select(4, &input, NULL, NULL, &tval);
66 while (status < 0 && errno != EINTR && errno != EAGAIN);
69 return (-1); /* Timeout! */
72 * Read bytes from the pipe...
76 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
78 return (read(3, buffer, bytes));
84 * 'cupsBackChannelWrite()' - Write data to the backchannel.
86 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
87 * controls how many seconds to wait for the data to be written - use
88 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
91 * @since CUPS 1.2/macOS 10.5@
94 ssize_t /* O - Bytes written or -1 on error */
96 const char *buffer, /* I - Buffer to write */
97 size_t bytes, /* I - Bytes to write */
98 double timeout) /* I - Timeout in seconds, typically 1.0 */
100 fd_set output; /* Output set */
101 struct timeval tval; /* Timeout value */
102 int status; /* Select status */
103 ssize_t count; /* Current bytes */
104 size_t total; /* Total bytes */
113 while (total < bytes)
116 * Wait for write-ready...
121 cups_setup(&output, &tval, timeout);
124 status = select(4, NULL, &output, NULL, NULL);
126 status = select(4, NULL, &output, NULL, &tval);
128 while (status < 0 && errno != EINTR && errno != EAGAIN);
131 return (-1); /* Timeout! */
134 * Write bytes to the pipe...
138 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
140 count = write(3, buffer, bytes - total);
146 * Write error - abort on fatal errors...
149 if (errno != EINTR && errno != EAGAIN)
155 * Write succeeded, update buffer pointer and total count...
159 total += (size_t)count;
163 return ((ssize_t)bytes);
168 * 'cups_setup()' - Setup select()
172 cups_setup(fd_set *set, /* I - Set for select() */
173 struct timeval *tval, /* I - Timer value */
174 double timeout) /* I - Timeout in seconds */
176 tval->tv_sec = (int)timeout;
177 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));