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