]>
Commit | Line | Data |
---|---|---|
ed486911 | 1 | /* |
bc44d920 | 2 | * "$Id: runloop.c 6649 2007-07-11 21:46:42Z mike $" |
ed486911 | 3 | * |
09a101d6 | 4 | * Common run loop APIs for the Common UNIX Printing System (CUPS). |
ed486911 | 5 | * |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
f7deaa1a | 7 | * Copyright 2006-2007 by Easy Software Products, all rights reserved. |
ed486911 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
ed486911 | 12 | * "LICENSE" which should have been included with this file. If this |
bc44d920 | 13 | * file is missing or damaged, see the license at "http://www.cups.org/". |
ed486911 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
09a101d6 | 19 | * backendDrainOutput() - Drain pending print data to the device. |
20 | * backendRunLoop() - Read and write print and back-channel data. | |
ed486911 | 21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers. | |
25 | */ | |
26 | ||
27 | #include "backend-private.h" | |
d09495fa | 28 | #ifdef __hpux |
29 | # include <sys/time.h> | |
30 | #else | |
31 | # include <sys/select.h> | |
32 | #endif /* __hpux */ | |
ed486911 | 33 | |
34 | ||
09a101d6 | 35 | /* |
36 | * 'backendDrainOutput()' - Drain pending print data to the device. | |
37 | */ | |
38 | ||
39 | int /* O - 0 on success, -1 on error */ | |
40 | backendDrainOutput(int print_fd, /* I - Print file descriptor */ | |
41 | int device_fd) /* I - Device file descriptor */ | |
42 | { | |
43 | int nfds; /* Maximum file descriptor value + 1 */ | |
44 | fd_set input; /* Input set for reading */ | |
45 | ssize_t print_bytes, /* Print bytes read */ | |
46 | bytes; /* Bytes written */ | |
47 | char print_buffer[8192], /* Print data buffer */ | |
48 | *print_ptr; /* Pointer into print data buffer */ | |
49 | struct timeval timeout; /* Timeout for read... */ | |
50 | ||
51 | ||
52 | fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n", | |
53 | print_fd, device_fd); | |
54 | ||
55 | /* | |
56 | * Figure out the maximum file descriptor value to use with select()... | |
57 | */ | |
58 | ||
59 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
60 | ||
61 | /* | |
62 | * Now loop until we are out of data from print_fd... | |
63 | */ | |
64 | ||
65 | for (;;) | |
66 | { | |
67 | /* | |
68 | * Use select() to determine whether we have data to copy around... | |
69 | */ | |
70 | ||
71 | FD_ZERO(&input); | |
72 | FD_SET(print_fd, &input); | |
73 | ||
74 | timeout.tv_sec = 0; | |
75 | timeout.tv_usec = 0; | |
76 | ||
77 | if (select(nfds, &input, NULL, NULL, &timeout) < 0) | |
78 | return (-1); | |
79 | ||
80 | if (!FD_ISSET(print_fd, &input)) | |
81 | return (0); | |
82 | ||
83 | if ((print_bytes = read(print_fd, print_buffer, | |
84 | sizeof(print_buffer))) < 0) | |
85 | { | |
86 | /* | |
87 | * Read error - bail if we don't see EAGAIN or EINTR... | |
88 | */ | |
89 | ||
90 | if (errno != EAGAIN || errno != EINTR) | |
91 | { | |
92 | perror("ERROR: Unable to read print data"); | |
93 | return (-1); | |
94 | } | |
95 | ||
96 | print_bytes = 0; | |
97 | } | |
98 | else if (print_bytes == 0) | |
99 | { | |
100 | /* | |
101 | * End of file, return... | |
102 | */ | |
103 | ||
104 | return (0); | |
105 | } | |
106 | ||
107 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
108 | (int)print_bytes); | |
109 | ||
110 | for (print_ptr = print_buffer; print_bytes > 0;) | |
111 | { | |
112 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
113 | { | |
114 | /* | |
115 | * Write error - bail if we don't see an error we can retry... | |
116 | */ | |
117 | ||
118 | if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN && | |
119 | errno != EINTR && errno != ENOTTY) | |
120 | { | |
121 | fprintf(stderr, _("ERROR: Unable to write print data: %s\n"), | |
122 | strerror(errno)); | |
123 | return (-1); | |
124 | } | |
125 | } | |
126 | else | |
127 | { | |
128 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
129 | ||
130 | print_bytes -= bytes; | |
131 | print_ptr += bytes; | |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | ||
ed486911 | 138 | /* |
139 | * 'backendRunLoop()' - Read and write print and back-channel data. | |
140 | */ | |
141 | ||
142 | ssize_t /* O - Total bytes on success, -1 on error */ | |
f7deaa1a | 143 | backendRunLoop( |
144 | int print_fd, /* I - Print file descriptor */ | |
145 | int device_fd, /* I - Device file descriptor */ | |
146 | int use_bc, /* I - Use back-channel? */ | |
147 | void (*side_cb)(int, int, int)) /* I - Side-channel callback */ | |
ed486911 | 148 | { |
149 | int nfds; /* Maximum file descriptor value + 1 */ | |
150 | fd_set input, /* Input set for reading */ | |
151 | output; /* Output set for writing */ | |
152 | ssize_t print_bytes, /* Print bytes read */ | |
153 | bc_bytes, /* Backchannel bytes read */ | |
154 | total_bytes, /* Total bytes written */ | |
155 | bytes; /* Bytes written */ | |
156 | int paperout; /* "Paper out" status */ | |
8ca02f3c | 157 | int offline; /* "Off-line" status */ |
ed486911 | 158 | char print_buffer[8192], /* Print data buffer */ |
159 | *print_ptr, /* Pointer into print data buffer */ | |
160 | bc_buffer[1024]; /* Back-channel data buffer */ | |
161 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
162 | struct sigaction action; /* Actions for POSIX signals */ | |
163 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
164 | ||
165 | ||
c0e1af83 | 166 | fprintf(stderr, |
09a101d6 | 167 | "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d, " |
168 | "side_cb=%p)\n", | |
169 | print_fd, device_fd, use_bc, side_cb); | |
8ca02f3c | 170 | |
ed486911 | 171 | /* |
172 | * If we are printing data from a print driver on stdin, ignore SIGTERM | |
173 | * so that the driver can finish out any page data, e.g. to eject the | |
174 | * current page. We only do this for stdin printing as otherwise there | |
175 | * is no way to cancel a raw print job... | |
176 | */ | |
177 | ||
178 | if (!print_fd) | |
179 | { | |
180 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
181 | sigset(SIGTERM, SIG_IGN); | |
182 | #elif defined(HAVE_SIGACTION) | |
183 | memset(&action, 0, sizeof(action)); | |
184 | ||
185 | sigemptyset(&action.sa_mask); | |
186 | action.sa_handler = SIG_IGN; | |
187 | sigaction(SIGTERM, &action, NULL); | |
188 | #else | |
189 | signal(SIGTERM, SIG_IGN); | |
190 | #endif /* HAVE_SIGSET */ | |
191 | } | |
192 | ||
193 | /* | |
194 | * Figure out the maximum file descriptor value to use with select()... | |
195 | */ | |
196 | ||
197 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
198 | ||
199 | /* | |
200 | * Now loop until we are out of data from print_fd... | |
201 | */ | |
202 | ||
b94498cf | 203 | for (print_bytes = 0, print_ptr = print_buffer, offline = -1, |
204 | paperout = -1, total_bytes = 0;;) | |
ed486911 | 205 | { |
206 | /* | |
207 | * Use select() to determine whether we have data to copy around... | |
208 | */ | |
209 | ||
210 | FD_ZERO(&input); | |
211 | if (!print_bytes) | |
212 | FD_SET(print_fd, &input); | |
213 | if (use_bc) | |
214 | FD_SET(device_fd, &input); | |
f7deaa1a | 215 | if (side_cb) |
216 | FD_SET(CUPS_SC_FD, &input); | |
ed486911 | 217 | |
218 | FD_ZERO(&output); | |
8ca02f3c | 219 | if (print_bytes || !use_bc) |
ed486911 | 220 | FD_SET(device_fd, &output); |
221 | ||
f7deaa1a | 222 | if (use_bc || side_cb) |
8ca02f3c | 223 | { |
224 | if (select(nfds, &input, &output, NULL, NULL) < 0) | |
225 | { | |
226 | /* | |
227 | * Pause printing to clear any pending errors... | |
228 | */ | |
229 | ||
b94498cf | 230 | if (errno == ENXIO && offline != 1) |
8ca02f3c | 231 | { |
232 | fputs("STATE: +offline-error\n", stderr); | |
c0e1af83 | 233 | fputs(_("INFO: Printer is currently off-line.\n"), stderr); |
8ca02f3c | 234 | offline = 1; |
235 | } | |
b94498cf | 236 | else if (errno == EINTR && total_bytes == 0) |
237 | { | |
238 | fputs("DEBUG: Received an interrupt before any bytes were " | |
239 | "written, aborting!\n", stderr); | |
240 | return (0); | |
241 | } | |
8ca02f3c | 242 | |
243 | sleep(1); | |
244 | continue; | |
245 | } | |
246 | } | |
ed486911 | 247 | |
f7deaa1a | 248 | /* |
249 | * Check if we have a side-channel request ready... | |
250 | */ | |
251 | ||
252 | if (side_cb && FD_ISSET(CUPS_SC_FD, &input)) | |
253 | (*side_cb)(print_fd, device_fd, use_bc); | |
254 | ||
ed486911 | 255 | /* |
256 | * Check if we have back-channel data ready... | |
257 | */ | |
258 | ||
259 | if (FD_ISSET(device_fd, &input)) | |
260 | { | |
261 | if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0) | |
262 | { | |
263 | fprintf(stderr, | |
264 | "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n", | |
265 | CUPS_LLCAST bc_bytes); | |
266 | cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0); | |
267 | } | |
268 | } | |
269 | ||
270 | /* | |
271 | * Check if we have print data ready... | |
272 | */ | |
273 | ||
274 | if (FD_ISSET(print_fd, &input)) | |
275 | { | |
276 | if ((print_bytes = read(print_fd, print_buffer, | |
277 | sizeof(print_buffer))) < 0) | |
278 | { | |
279 | /* | |
280 | * Read error - bail if we don't see EAGAIN or EINTR... | |
281 | */ | |
282 | ||
283 | if (errno != EAGAIN || errno != EINTR) | |
284 | { | |
285 | perror("ERROR: Unable to read print data"); | |
286 | return (-1); | |
287 | } | |
288 | ||
289 | print_bytes = 0; | |
290 | } | |
291 | else if (print_bytes == 0) | |
292 | { | |
293 | /* | |
294 | * End of file, break out of the loop... | |
295 | */ | |
296 | ||
297 | break; | |
298 | } | |
299 | ||
300 | print_ptr = print_buffer; | |
8ca02f3c | 301 | |
302 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
303 | (int)print_bytes); | |
ed486911 | 304 | } |
305 | ||
306 | /* | |
307 | * Check if the device is ready to receive data and we have data to | |
308 | * send... | |
309 | */ | |
310 | ||
311 | if (print_bytes && FD_ISSET(device_fd, &output)) | |
312 | { | |
313 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
314 | { | |
315 | /* | |
316 | * Write error - bail if we don't see an error we can retry... | |
317 | */ | |
318 | ||
319 | if (errno == ENOSPC) | |
320 | { | |
b94498cf | 321 | if (paperout != 1) |
ed486911 | 322 | { |
b86bc4cf | 323 | fputs("STATE: +media-empty-error\n", stderr); |
c0e1af83 | 324 | fputs(_("ERROR: Out of paper!\n"), stderr); |
ed486911 | 325 | paperout = 1; |
326 | } | |
327 | } | |
8ca02f3c | 328 | else if (errno == ENXIO) |
329 | { | |
b94498cf | 330 | if (offline != 1) |
8ca02f3c | 331 | { |
332 | fputs("STATE: +offline-error\n", stderr); | |
c0e1af83 | 333 | fputs(_("INFO: Printer is currently off-line.\n"), stderr); |
8ca02f3c | 334 | offline = 1; |
335 | } | |
336 | } | |
ed486911 | 337 | else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY) |
338 | { | |
c0e1af83 | 339 | fprintf(stderr, _("ERROR: Unable to write print data: %s\n"), |
340 | strerror(errno)); | |
ed486911 | 341 | return (-1); |
342 | } | |
343 | } | |
344 | else | |
345 | { | |
346 | if (paperout) | |
347 | { | |
b86bc4cf | 348 | fputs("STATE: -media-empty-error\n", stderr); |
ed486911 | 349 | paperout = 0; |
350 | } | |
351 | ||
8ca02f3c | 352 | if (offline) |
353 | { | |
354 | fputs("STATE: -offline-error\n", stderr); | |
c0e1af83 | 355 | fputs(_("INFO: Printer is now on-line.\n"), stderr); |
8ca02f3c | 356 | offline = 0; |
357 | } | |
358 | ||
359 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
ed486911 | 360 | |
361 | print_bytes -= bytes; | |
362 | print_ptr += bytes; | |
363 | total_bytes += bytes; | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | /* | |
369 | * Return with success... | |
370 | */ | |
371 | ||
372 | return (total_bytes); | |
373 | } | |
374 | ||
375 | ||
376 | /* | |
bc44d920 | 377 | * End of "$Id: runloop.c 6649 2007-07-11 21:46:42Z mike $". |
ed486911 | 378 | */ |