]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
2 * Common run loop APIs for CUPS backends.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers.
20 #include "backend-private.h"
22 #include <sys/select.h>
26 * 'backendDrainOutput()' - Drain pending print data to the device.
29 int /* O - 0 on success, -1 on error */
30 backendDrainOutput(int print_fd
, /* I - Print file descriptor */
31 int device_fd
) /* I - Device file descriptor */
33 int nfds
; /* Maximum file descriptor value + 1 */
34 fd_set input
; /* Input set for reading */
35 ssize_t print_bytes
, /* Print bytes read */
36 bytes
; /* Bytes written */
37 char print_buffer
[8192], /* Print data buffer */
38 *print_ptr
; /* Pointer into print data buffer */
39 struct timeval timeout
; /* Timeout for read... */
42 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
46 * Figure out the maximum file descriptor value to use with select()...
49 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
52 * Now loop until we are out of data from print_fd...
58 * Use select() to determine whether we have data to copy around...
62 FD_SET(print_fd
, &input
);
67 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
70 if (!FD_ISSET(print_fd
, &input
))
73 if ((print_bytes
= read(print_fd
, print_buffer
,
74 sizeof(print_buffer
))) < 0)
77 * Read error - bail if we don't see EAGAIN or EINTR...
80 if (errno
!= EAGAIN
&& errno
!= EINTR
)
82 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
83 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to read print data."));
89 else if (print_bytes
== 0)
92 * End of file, return...
98 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
101 for (print_ptr
= print_buffer
; print_bytes
> 0;)
103 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
106 * Write error - bail if we don't see an error we can retry...
109 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
110 errno
!= EINTR
&& errno
!= ENOTTY
)
112 _cupsLangPrintError("ERROR", _("Unable to write print data"));
118 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
120 print_bytes
-= bytes
;
129 * 'backendRunLoop()' - Read and write print and back-channel data.
132 ssize_t
/* O - Total bytes on success, -1 on error */
134 int print_fd
, /* I - Print file descriptor */
135 int device_fd
, /* I - Device file descriptor */
136 int snmp_fd
, /* I - SNMP socket or -1 if none */
137 http_addr_t
*addr
, /* I - Address of device */
138 int use_bc
, /* I - Use back-channel? */
139 int update_state
, /* I - Update printer-state-reasons? */
140 _cups_sccb_t side_cb
) /* I - Side-channel callback */
142 int nfds
; /* Maximum file descriptor value + 1 */
143 fd_set input
, /* Input set for reading */
144 output
; /* Output set for writing */
145 ssize_t print_bytes
, /* Print bytes read */
146 bc_bytes
, /* Backchannel bytes read */
147 total_bytes
, /* Total bytes written */
148 bytes
; /* Bytes written */
149 int paperout
; /* "Paper out" status */
150 int offline
; /* "Off-line" status */
151 char print_buffer
[8192], /* Print data buffer */
152 *print_ptr
, /* Pointer into print data buffer */
153 bc_buffer
[1024]; /* Back-channel data buffer */
154 struct timeval timeout
; /* Timeout for select() */
155 time_t curtime
, /* Current time */
157 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
158 struct sigaction action
; /* Actions for POSIX signals */
159 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
163 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
164 "addr=%p, use_bc=%d, side_cb=%p)\n",
165 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
168 * If we are printing data from a print driver on stdin, ignore SIGTERM
169 * so that the driver can finish out any page data, e.g. to eject the
170 * current page. We only do this for stdin printing as otherwise there
171 * is no way to cancel a raw print job...
176 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
177 sigset(SIGTERM
, SIG_IGN
);
178 #elif defined(HAVE_SIGACTION)
179 memset(&action
, 0, sizeof(action
));
181 sigemptyset(&action
.sa_mask
);
182 action
.sa_handler
= SIG_IGN
;
183 sigaction(SIGTERM
, &action
, NULL
);
185 signal(SIGTERM
, SIG_IGN
);
186 #endif /* HAVE_SIGSET */
188 else if (print_fd
< 0)
191 * Copy print data from stdin, but don't mess with the signal handlers...
198 * Figure out the maximum file descriptor value to use with select()...
201 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
204 * Now loop until we are out of data from print_fd...
207 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
208 paperout
= -1, total_bytes
= 0;;)
211 * Use select() to determine whether we have data to copy around...
216 FD_SET(print_fd
, &input
);
218 FD_SET(device_fd
, &input
);
219 if (!print_bytes
&& side_cb
)
220 FD_SET(CUPS_SC_FD
, &input
);
223 if (print_bytes
|| (!use_bc
&& !side_cb
))
224 FD_SET(device_fd
, &output
);
226 if (use_bc
|| side_cb
)
231 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
234 * Pause printing to clear any pending errors...
237 if (errno
== ENXIO
&& offline
!= 1 && update_state
)
239 fputs("STATE: +offline-report\n", stderr
);
240 _cupsLangPrintFilter(stderr
, "INFO",
241 _("The printer is not connected."));
244 else if (errno
== EINTR
&& total_bytes
== 0)
246 fputs("DEBUG: Received an interrupt before any bytes were "
247 "written, aborting.\n", stderr
);
257 * Check if we have a side-channel request ready...
260 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
263 * Do the side-channel request, then start back over in the select
264 * loop since it may have read from print_fd...
267 if ((*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
))
273 * Check if we have back-channel data ready...
276 if (FD_ISSET(device_fd
, &input
))
278 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
281 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data\n",
282 CUPS_LLCAST bc_bytes
);
283 cupsBackChannelWrite(bc_buffer
, (size_t)bc_bytes
, 1.0);
285 else if (bc_bytes
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
287 fprintf(stderr
, "DEBUG: Error reading back-channel data: %s\n",
291 else if (bc_bytes
== 0)
296 * Check if we have print data ready...
299 if (FD_ISSET(print_fd
, &input
))
301 if ((print_bytes
= read(print_fd
, print_buffer
,
302 sizeof(print_buffer
))) < 0)
305 * Read error - bail if we don't see EAGAIN or EINTR...
308 if (errno
!= EAGAIN
&& errno
!= EINTR
)
310 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
311 _cupsLangPrintFilter(stderr
, "ERROR",
312 _("Unable to read print data."));
318 else if (print_bytes
== 0)
321 * End of file, break out of the loop...
327 print_ptr
= print_buffer
;
329 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
334 * Check if the device is ready to receive data and we have data to
338 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
340 if ((bytes
= write(device_fd
, print_ptr
, (size_t)print_bytes
)) < 0)
343 * Write error - bail if we don't see an error we can retry...
348 if (paperout
!= 1 && update_state
)
350 fputs("STATE: +media-empty-warning\n", stderr
);
351 fputs("DEBUG: Out of paper\n", stderr
);
355 else if (errno
== ENXIO
)
357 if (offline
!= 1 && update_state
)
359 fputs("STATE: +offline-report\n", stderr
);
360 _cupsLangPrintFilter(stderr
, "INFO",
361 _("The printer is not connected."));
365 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
367 _cupsLangPrintError("ERROR", _("Unable to write print data"));
373 if (paperout
&& update_state
)
375 fputs("STATE: -media-empty-warning\n", stderr
);
379 if (offline
&& update_state
)
381 fputs("STATE: -offline-report\n", stderr
);
382 _cupsLangPrintFilter(stderr
, "INFO",
383 _("The printer is now connected."));
387 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
389 print_bytes
-= bytes
;
391 total_bytes
+= bytes
;
396 * Do SNMP updates periodically...
399 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
401 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
402 snmp_update
= INT_MAX
;
404 snmp_update
= curtime
+ 5;
409 * Return with success...
412 return (total_bytes
);
417 * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
421 int /* O - 1 if data is ready, 0 if not */
423 int snmp_fd
, /* I - SNMP socket or -1 if none */
424 http_addr_t
*addr
, /* I - Address of device */
425 int use_bc
, /* I - Use back-channel? */
426 _cups_sccb_t side_cb
) /* I - Side-channel callback */
428 int nfds
; /* Number of file descriptors */
429 fd_set input
; /* Input set for reading */
430 time_t curtime
= 0, /* Current time */
431 snmp_update
= 0;/* Last SNMP status update */
432 struct timeval timeout
; /* Timeout for select() */
435 fprintf(stderr
, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
436 snmp_fd
, addr
, side_cb
);
439 * Now loop until we receive data from stdin...
443 snmp_update
= time(NULL
) + 5;
448 * Use select() to determine whether we have data to copy around...
454 FD_SET(CUPS_SC_FD
, &input
);
458 curtime
= time(NULL
);
459 timeout
.tv_sec
= curtime
>= snmp_update
? 0 : snmp_update
- curtime
;
462 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, &timeout
);
465 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, NULL
);
470 * Pause printing to clear any pending errors...
475 fputs("DEBUG: Received an interrupt before any bytes were "
476 "written, aborting.\n", stderr
);
485 * Check for input on stdin...
488 if (FD_ISSET(0, &input
))
492 * Check if we have a side-channel request ready...
495 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
498 * Do the side-channel request, then start back over in the select
499 * loop since it may have read from print_fd...
502 if ((*side_cb
)(0, -1, snmp_fd
, addr
, use_bc
))
508 * Do SNMP updates periodically...
511 if (snmp_fd
>= 0 && curtime
>= snmp_update
)
513 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
516 snmp_update
= curtime
+ 5;
521 * Return with success...