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