]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/backchannel.c
06a6d2308fe4ccc0af373da8a4133ff43ef2b5e5
[thirdparty/cups.git] / cups / backchannel.c
1 /*
2 * "$Id$"
3 *
4 * Backchannel functions for CUPS.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
8 *
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 */
17
18 /*
19 * Include necessary headers...
20 */
21
22 #include "cups.h"
23 #include <errno.h>
24 #ifdef WIN32
25 # include <io.h>
26 # include <fcntl.h>
27 #else
28 # include <sys/time.h>
29 #endif /* WIN32 */
30
31
32 /*
33 * Local functions...
34 */
35
36 static void cups_setup(fd_set *set, struct timeval *tval,
37 double timeout);
38
39
40 /*
41 * 'cupsBackChannelRead()' - Read data from the backchannel.
42 *
43 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
44 * parameter controls how many seconds to wait for the data - use 0.0 to
45 * return immediately if there is no data, -1.0 to wait for data indefinitely.
46 *
47 * @since CUPS 1.2/OS X 10.5@
48 */
49
50 ssize_t /* O - Bytes read or -1 on error */
51 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
52 size_t bytes, /* I - Bytes to read */
53 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
54 {
55 fd_set input; /* Input set */
56 struct timeval tval; /* Timeout value */
57 int status; /* Select status */
58
59
60 /*
61 * Wait for input ready.
62 */
63
64 do
65 {
66 cups_setup(&input, &tval, timeout);
67
68 if (timeout < 0.0)
69 status = select(4, &input, NULL, NULL, NULL);
70 else
71 status = select(4, &input, NULL, NULL, &tval);
72 }
73 while (status < 0 && errno != EINTR && errno != EAGAIN);
74
75 if (status < 0)
76 return (-1); /* Timeout! */
77
78 /*
79 * Read bytes from the pipe...
80 */
81
82 #ifdef WIN32
83 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
84 #else
85 return (read(3, buffer, bytes));
86 #endif /* WIN32 */
87 }
88
89
90 /*
91 * 'cupsBackChannelWrite()' - Write data to the backchannel.
92 *
93 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
94 * controls how many seconds to wait for the data to be written - use
95 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
96 * indefinitely.
97 *
98 * @since CUPS 1.2/OS X 10.5@
99 */
100
101 ssize_t /* O - Bytes written or -1 on error */
102 cupsBackChannelWrite(
103 const char *buffer, /* I - Buffer to write */
104 size_t bytes, /* I - Bytes to write */
105 double timeout) /* I - Timeout in seconds, typically 1.0 */
106 {
107 fd_set output; /* Output set */
108 struct timeval tval; /* Timeout value */
109 int status; /* Select status */
110 ssize_t count; /* Current bytes */
111 size_t total; /* Total bytes */
112
113
114 /*
115 * Write all bytes...
116 */
117
118 total = 0;
119
120 while (total < bytes)
121 {
122 /*
123 * Wait for write-ready...
124 */
125
126 do
127 {
128 cups_setup(&output, &tval, timeout);
129
130 if (timeout < 0.0)
131 status = select(4, NULL, &output, NULL, NULL);
132 else
133 status = select(4, NULL, &output, NULL, &tval);
134 }
135 while (status < 0 && errno != EINTR && errno != EAGAIN);
136
137 if (status <= 0)
138 return (-1); /* Timeout! */
139
140 /*
141 * Write bytes to the pipe...
142 */
143
144 #ifdef WIN32
145 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
146 #else
147 count = write(3, buffer, bytes - total);
148 #endif /* WIN32 */
149
150 if (count < 0)
151 {
152 /*
153 * Write error - abort on fatal errors...
154 */
155
156 if (errno != EINTR && errno != EAGAIN)
157 return (-1);
158 }
159 else
160 {
161 /*
162 * Write succeeded, update buffer pointer and total count...
163 */
164
165 buffer += count;
166 total += (size_t)count;
167 }
168 }
169
170 return ((ssize_t)bytes);
171 }
172
173
174 /*
175 * 'cups_setup()' - Setup select()
176 */
177
178 static void
179 cups_setup(fd_set *set, /* I - Set for select() */
180 struct timeval *tval, /* I - Timer value */
181 double timeout) /* I - Timeout in seconds */
182 {
183 tval->tv_sec = (int)timeout;
184 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
185
186 FD_ZERO(set);
187 FD_SET(3, set);
188 }
189
190
191 /*
192 * End of "$Id$".
193 */