]>
Commit | Line | Data |
---|---|---|
ed486911 | 1 | /* |
b19ccc9e | 2 | * "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $" |
ed486911 | 3 | * |
7cf5915e | 4 | * Common run loop APIs for CUPS. |
ed486911 | 5 | * |
7cf5915e | 6 | * Copyright 2007-2010 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" | |
568fa3fa | 28 | #include <limits.h> |
d09495fa | 29 | #ifdef __hpux |
30 | # include <sys/time.h> | |
31 | #else | |
32 | # include <sys/select.h> | |
33 | #endif /* __hpux */ | |
ed486911 | 34 | |
35 | ||
09a101d6 | 36 | /* |
37 | * 'backendDrainOutput()' - Drain pending print data to the device. | |
38 | */ | |
39 | ||
40 | int /* O - 0 on success, -1 on error */ | |
41 | backendDrainOutput(int print_fd, /* I - Print file descriptor */ | |
42 | int device_fd) /* I - Device file descriptor */ | |
43 | { | |
44 | int nfds; /* Maximum file descriptor value + 1 */ | |
45 | fd_set input; /* Input set for reading */ | |
46 | ssize_t print_bytes, /* Print bytes read */ | |
47 | bytes; /* Bytes written */ | |
48 | char print_buffer[8192], /* Print data buffer */ | |
49 | *print_ptr; /* Pointer into print data buffer */ | |
50 | struct timeval timeout; /* Timeout for read... */ | |
51 | ||
52 | ||
53 | fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n", | |
54 | print_fd, device_fd); | |
55 | ||
56 | /* | |
57 | * Figure out the maximum file descriptor value to use with select()... | |
58 | */ | |
59 | ||
60 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
61 | ||
62 | /* | |
63 | * Now loop until we are out of data from print_fd... | |
64 | */ | |
65 | ||
66 | for (;;) | |
67 | { | |
68 | /* | |
69 | * Use select() to determine whether we have data to copy around... | |
70 | */ | |
71 | ||
72 | FD_ZERO(&input); | |
73 | FD_SET(print_fd, &input); | |
74 | ||
75 | timeout.tv_sec = 0; | |
76 | timeout.tv_usec = 0; | |
77 | ||
78 | if (select(nfds, &input, NULL, NULL, &timeout) < 0) | |
79 | return (-1); | |
80 | ||
81 | if (!FD_ISSET(print_fd, &input)) | |
82 | return (0); | |
83 | ||
84 | if ((print_bytes = read(print_fd, print_buffer, | |
85 | sizeof(print_buffer))) < 0) | |
86 | { | |
87 | /* | |
88 | * Read error - bail if we don't see EAGAIN or EINTR... | |
89 | */ | |
90 | ||
91 | if (errno != EAGAIN || errno != EINTR) | |
92 | { | |
0837b7e8 | 93 | _cupsLangPrintError("ERROR", _("Unable to read print data")); |
09a101d6 | 94 | return (-1); |
95 | } | |
96 | ||
97 | print_bytes = 0; | |
98 | } | |
99 | else if (print_bytes == 0) | |
100 | { | |
101 | /* | |
102 | * End of file, return... | |
103 | */ | |
104 | ||
105 | return (0); | |
106 | } | |
107 | ||
108 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
109 | (int)print_bytes); | |
110 | ||
111 | for (print_ptr = print_buffer; print_bytes > 0;) | |
112 | { | |
113 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
114 | { | |
115 | /* | |
116 | * Write error - bail if we don't see an error we can retry... | |
117 | */ | |
118 | ||
119 | if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN && | |
120 | errno != EINTR && errno != ENOTTY) | |
121 | { | |
0837b7e8 | 122 | _cupsLangPrintError("ERROR", _("Unable to write print data")); |
09a101d6 | 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( |
568fa3fa MS |
144 | int print_fd, /* I - Print file descriptor */ |
145 | int device_fd, /* I - Device file descriptor */ | |
146 | int snmp_fd, /* I - SNMP socket or -1 if none */ | |
147 | http_addr_t *addr, /* I - Address of device */ | |
148 | int use_bc, /* I - Use back-channel? */ | |
ef55b745 | 149 | int update_state, /* I - Update printer-state-reasons? */ |
18ecb428 | 150 | int (*side_cb)(int, int, int, http_addr_t *, int)) |
568fa3fa | 151 | /* I - Side-channel callback */ |
ed486911 | 152 | { |
153 | int nfds; /* Maximum file descriptor value + 1 */ | |
154 | fd_set input, /* Input set for reading */ | |
155 | output; /* Output set for writing */ | |
156 | ssize_t print_bytes, /* Print bytes read */ | |
157 | bc_bytes, /* Backchannel bytes read */ | |
158 | total_bytes, /* Total bytes written */ | |
159 | bytes; /* Bytes written */ | |
160 | int paperout; /* "Paper out" status */ | |
8ca02f3c | 161 | int offline; /* "Off-line" status */ |
ed486911 | 162 | char print_buffer[8192], /* Print data buffer */ |
163 | *print_ptr, /* Pointer into print data buffer */ | |
164 | bc_buffer[1024]; /* Back-channel data buffer */ | |
568fa3fa MS |
165 | struct timeval timeout; /* Timeout for select() */ |
166 | time_t curtime, /* Current time */ | |
167 | snmp_update = 0; | |
ed486911 | 168 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
169 | struct sigaction action; /* Actions for POSIX signals */ | |
170 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
171 | ||
172 | ||
c0e1af83 | 173 | fprintf(stderr, |
568fa3fa MS |
174 | "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, " |
175 | "addr=%p, use_bc=%d, side_cb=%p)\n", | |
176 | print_fd, device_fd, snmp_fd, addr, use_bc, side_cb); | |
8ca02f3c | 177 | |
ed486911 | 178 | /* |
179 | * If we are printing data from a print driver on stdin, ignore SIGTERM | |
180 | * so that the driver can finish out any page data, e.g. to eject the | |
181 | * current page. We only do this for stdin printing as otherwise there | |
182 | * is no way to cancel a raw print job... | |
183 | */ | |
184 | ||
185 | if (!print_fd) | |
186 | { | |
187 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
188 | sigset(SIGTERM, SIG_IGN); | |
189 | #elif defined(HAVE_SIGACTION) | |
190 | memset(&action, 0, sizeof(action)); | |
191 | ||
192 | sigemptyset(&action.sa_mask); | |
193 | action.sa_handler = SIG_IGN; | |
194 | sigaction(SIGTERM, &action, NULL); | |
195 | #else | |
196 | signal(SIGTERM, SIG_IGN); | |
197 | #endif /* HAVE_SIGSET */ | |
198 | } | |
4e6f60f0 MS |
199 | else if (print_fd < 0) |
200 | { | |
201 | /* | |
202 | * Copy print data from stdin, but don't mess with the signal handlers... | |
203 | */ | |
204 | ||
205 | print_fd = 0; | |
206 | } | |
ed486911 | 207 | |
208 | /* | |
209 | * Figure out the maximum file descriptor value to use with select()... | |
210 | */ | |
211 | ||
212 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
213 | ||
214 | /* | |
215 | * Now loop until we are out of data from print_fd... | |
216 | */ | |
217 | ||
b94498cf | 218 | for (print_bytes = 0, print_ptr = print_buffer, offline = -1, |
219 | paperout = -1, total_bytes = 0;;) | |
ed486911 | 220 | { |
221 | /* | |
222 | * Use select() to determine whether we have data to copy around... | |
223 | */ | |
224 | ||
225 | FD_ZERO(&input); | |
226 | if (!print_bytes) | |
227 | FD_SET(print_fd, &input); | |
228 | if (use_bc) | |
229 | FD_SET(device_fd, &input); | |
dd1abb6b | 230 | if (!print_bytes && side_cb) |
f7deaa1a | 231 | FD_SET(CUPS_SC_FD, &input); |
ed486911 | 232 | |
233 | FD_ZERO(&output); | |
91c84a35 | 234 | if (print_bytes || (!use_bc && !side_cb)) |
ed486911 | 235 | FD_SET(device_fd, &output); |
236 | ||
f7deaa1a | 237 | if (use_bc || side_cb) |
8ca02f3c | 238 | { |
568fa3fa MS |
239 | timeout.tv_sec = 5; |
240 | timeout.tv_usec = 0; | |
241 | ||
242 | if (select(nfds, &input, &output, NULL, &timeout) < 0) | |
8ca02f3c | 243 | { |
244 | /* | |
245 | * Pause printing to clear any pending errors... | |
246 | */ | |
247 | ||
ef55b745 | 248 | if (errno == ENXIO && offline != 1 && update_state) |
8ca02f3c | 249 | { |
c5571a1d | 250 | fputs("STATE: +offline-report\n", stderr); |
0837b7e8 MS |
251 | _cupsLangPrintFilter(stderr, "INFO", |
252 | _("Printer is not currently connected.")); | |
8ca02f3c | 253 | offline = 1; |
254 | } | |
b94498cf | 255 | else if (errno == EINTR && total_bytes == 0) |
256 | { | |
257 | fputs("DEBUG: Received an interrupt before any bytes were " | |
4d301e69 | 258 | "written, aborting\n", stderr); |
b94498cf | 259 | return (0); |
260 | } | |
8ca02f3c | 261 | |
262 | sleep(1); | |
263 | continue; | |
264 | } | |
265 | } | |
ed486911 | 266 | |
f7deaa1a | 267 | /* |
268 | * Check if we have a side-channel request ready... | |
269 | */ | |
270 | ||
271 | if (side_cb && FD_ISSET(CUPS_SC_FD, &input)) | |
dd1abb6b MS |
272 | { |
273 | /* | |
274 | * Do the side-channel request, then start back over in the select | |
275 | * loop since it may have read from print_fd... | |
276 | */ | |
277 | ||
18ecb428 MS |
278 | if ((*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc)) |
279 | side_cb = NULL; | |
dd1abb6b MS |
280 | continue; |
281 | } | |
f7deaa1a | 282 | |
ed486911 | 283 | /* |
284 | * Check if we have back-channel data ready... | |
285 | */ | |
286 | ||
287 | if (FD_ISSET(device_fd, &input)) | |
288 | { | |
289 | if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0) | |
290 | { | |
291 | fprintf(stderr, | |
4d301e69 | 292 | "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data\n", |
ed486911 | 293 | CUPS_LLCAST bc_bytes); |
294 | cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0); | |
295 | } | |
4b3f67ff MS |
296 | else if (bc_bytes < 0 && errno != EAGAIN && errno != EINTR) |
297 | { | |
298 | fprintf(stderr, "DEBUG: Error reading back-channel data: %s\n", | |
299 | strerror(errno)); | |
300 | use_bc = 0; | |
301 | } | |
7cf5915e MS |
302 | else if (bc_bytes == 0) |
303 | use_bc = 0; | |
ed486911 | 304 | } |
305 | ||
306 | /* | |
307 | * Check if we have print data ready... | |
308 | */ | |
309 | ||
310 | if (FD_ISSET(print_fd, &input)) | |
311 | { | |
312 | if ((print_bytes = read(print_fd, print_buffer, | |
313 | sizeof(print_buffer))) < 0) | |
314 | { | |
315 | /* | |
316 | * Read error - bail if we don't see EAGAIN or EINTR... | |
317 | */ | |
318 | ||
319 | if (errno != EAGAIN || errno != EINTR) | |
320 | { | |
0837b7e8 | 321 | _cupsLangPrintError("ERROR", _("Unable to read print data")); |
ed486911 | 322 | return (-1); |
323 | } | |
324 | ||
325 | print_bytes = 0; | |
326 | } | |
327 | else if (print_bytes == 0) | |
328 | { | |
329 | /* | |
330 | * End of file, break out of the loop... | |
331 | */ | |
332 | ||
333 | break; | |
334 | } | |
335 | ||
336 | print_ptr = print_buffer; | |
8ca02f3c | 337 | |
338 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
339 | (int)print_bytes); | |
ed486911 | 340 | } |
341 | ||
342 | /* | |
343 | * Check if the device is ready to receive data and we have data to | |
344 | * send... | |
345 | */ | |
346 | ||
347 | if (print_bytes && FD_ISSET(device_fd, &output)) | |
348 | { | |
349 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
350 | { | |
351 | /* | |
352 | * Write error - bail if we don't see an error we can retry... | |
353 | */ | |
354 | ||
355 | if (errno == ENOSPC) | |
356 | { | |
ef55b745 | 357 | if (paperout != 1 && update_state) |
ed486911 | 358 | { |
c5571a1d | 359 | fputs("STATE: +media-empty-warning\n", stderr); |
0837b7e8 | 360 | fputs("DEBUG: Out of paper\n", stderr); |
ed486911 | 361 | paperout = 1; |
362 | } | |
363 | } | |
8ca02f3c | 364 | else if (errno == ENXIO) |
365 | { | |
ef55b745 | 366 | if (offline != 1 && update_state) |
8ca02f3c | 367 | { |
c5571a1d | 368 | fputs("STATE: +offline-report\n", stderr); |
0837b7e8 MS |
369 | _cupsLangPrintFilter(stderr, "INFO", |
370 | _("Printer is not currently connected.")); | |
8ca02f3c | 371 | offline = 1; |
372 | } | |
373 | } | |
ed486911 | 374 | else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY) |
375 | { | |
0837b7e8 | 376 | _cupsLangPrintError("ERROR", _("Unable to write print data")); |
ed486911 | 377 | return (-1); |
378 | } | |
379 | } | |
380 | else | |
381 | { | |
ef55b745 | 382 | if (paperout && update_state) |
ed486911 | 383 | { |
c5571a1d | 384 | fputs("STATE: -media-empty-warning\n", stderr); |
ed486911 | 385 | paperout = 0; |
386 | } | |
387 | ||
ef55b745 | 388 | if (offline && update_state) |
8ca02f3c | 389 | { |
c5571a1d | 390 | fputs("STATE: -offline-report\n", stderr); |
0837b7e8 | 391 | _cupsLangPrintFilter(stderr, "INFO", _("Printer is now connected.")); |
8ca02f3c | 392 | offline = 0; |
393 | } | |
394 | ||
395 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
ed486911 | 396 | |
397 | print_bytes -= bytes; | |
398 | print_ptr += bytes; | |
399 | total_bytes += bytes; | |
400 | } | |
401 | } | |
568fa3fa MS |
402 | |
403 | /* | |
404 | * Do SNMP updates periodically... | |
405 | */ | |
406 | ||
407 | if (snmp_fd >= 0 && time(&curtime) >= snmp_update) | |
408 | { | |
409 | if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL)) | |
410 | snmp_update = INT_MAX; | |
411 | else | |
412 | snmp_update = curtime + 5; | |
413 | } | |
ed486911 | 414 | } |
415 | ||
416 | /* | |
417 | * Return with success... | |
418 | */ | |
419 | ||
420 | return (total_bytes); | |
421 | } | |
422 | ||
423 | ||
424 | /* | |
b19ccc9e | 425 | * End of "$Id: runloop.c 7895 2008-09-02 19:19:43Z mike $". |
ed486911 | 426 | */ |