]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
4 * Common run loop APIs for CUPS backends.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers.
22 #include "backend-private.h"
24 #include <sys/select.h>
28 * 'backendDrainOutput()' - Drain pending print data to the device.
31 int /* O - 0 on success, -1 on error */
32 backendDrainOutput(int print_fd
, /* I - Print file descriptor */
33 int device_fd
) /* I - Device file descriptor */
35 int nfds
; /* Maximum file descriptor value + 1 */
36 fd_set input
; /* Input set for reading */
37 ssize_t print_bytes
, /* Print bytes read */
38 bytes
; /* Bytes written */
39 char print_buffer
[8192], /* Print data buffer */
40 *print_ptr
; /* Pointer into print data buffer */
41 struct timeval timeout
; /* Timeout for read... */
44 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
48 * Figure out the maximum file descriptor value to use with select()...
51 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
54 * Now loop until we are out of data from print_fd...
60 * Use select() to determine whether we have data to copy around...
64 FD_SET(print_fd
, &input
);
69 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
72 if (!FD_ISSET(print_fd
, &input
))
75 if ((print_bytes
= read(print_fd
, print_buffer
,
76 sizeof(print_buffer
))) < 0)
79 * Read error - bail if we don't see EAGAIN or EINTR...
82 if (errno
!= EAGAIN
|| errno
!= EINTR
)
84 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
85 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to read print data."));
91 else if (print_bytes
== 0)
94 * End of file, return...
100 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
103 for (print_ptr
= print_buffer
; print_bytes
> 0;)
105 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
108 * Write error - bail if we don't see an error we can retry...
111 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
112 errno
!= EINTR
&& errno
!= ENOTTY
)
114 _cupsLangPrintError("ERROR", _("Unable to write print data"));
120 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
122 print_bytes
-= bytes
;
131 * 'backendRunLoop()' - Read and write print and back-channel data.
134 ssize_t
/* O - Total bytes on success, -1 on error */
136 int print_fd
, /* I - Print file descriptor */
137 int device_fd
, /* I - Device file descriptor */
138 int snmp_fd
, /* I - SNMP socket or -1 if none */
139 http_addr_t
*addr
, /* I - Address of device */
140 int use_bc
, /* I - Use back-channel? */
141 int update_state
, /* I - Update printer-state-reasons? */
142 _cups_sccb_t side_cb
) /* I - Side-channel callback */
144 int nfds
; /* Maximum file descriptor value + 1 */
145 fd_set input
, /* Input set for reading */
146 output
; /* Output set for writing */
147 ssize_t print_bytes
, /* Print bytes read */
148 bc_bytes
, /* Backchannel bytes read */
149 total_bytes
, /* Total bytes written */
150 bytes
; /* Bytes written */
151 int paperout
; /* "Paper out" status */
152 int offline
; /* "Off-line" status */
153 char print_buffer
[8192], /* Print data buffer */
154 *print_ptr
, /* Pointer into print data buffer */
155 bc_buffer
[1024]; /* Back-channel data buffer */
156 struct timeval timeout
; /* Timeout for select() */
157 time_t curtime
, /* Current time */
159 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
160 struct sigaction action
; /* Actions for POSIX signals */
161 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
165 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
166 "addr=%p, use_bc=%d, side_cb=%p)\n",
167 print_fd
, device_fd
, snmp_fd
, addr
, use_bc
, side_cb
);
170 * If we are printing data from a print driver on stdin, ignore SIGTERM
171 * so that the driver can finish out any page data, e.g. to eject the
172 * current page. We only do this for stdin printing as otherwise there
173 * is no way to cancel a raw print job...
178 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
179 sigset(SIGTERM
, SIG_IGN
);
180 #elif defined(HAVE_SIGACTION)
181 memset(&action
, 0, sizeof(action
));
183 sigemptyset(&action
.sa_mask
);
184 action
.sa_handler
= SIG_IGN
;
185 sigaction(SIGTERM
, &action
, NULL
);
187 signal(SIGTERM
, SIG_IGN
);
188 #endif /* HAVE_SIGSET */
190 else if (print_fd
< 0)
193 * Copy print data from stdin, but don't mess with the signal handlers...
200 * Figure out the maximum file descriptor value to use with select()...
203 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
206 * Now loop until we are out of data from print_fd...
209 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
210 paperout
= -1, total_bytes
= 0;;)
213 * Use select() to determine whether we have data to copy around...
218 FD_SET(print_fd
, &input
);
220 FD_SET(device_fd
, &input
);
221 if (!print_bytes
&& side_cb
)
222 FD_SET(CUPS_SC_FD
, &input
);
225 if (print_bytes
|| (!use_bc
&& !side_cb
))
226 FD_SET(device_fd
, &output
);
228 if (use_bc
|| side_cb
)
233 if (select(nfds
, &input
, &output
, NULL
, &timeout
) < 0)
236 * Pause printing to clear any pending errors...
239 if (errno
== ENXIO
&& offline
!= 1 && update_state
)
241 fputs("STATE: +offline-report\n", stderr
);
242 _cupsLangPrintFilter(stderr
, "INFO",
243 _("The printer is not connected."));
246 else if (errno
== EINTR
&& total_bytes
== 0)
248 fputs("DEBUG: Received an interrupt before any bytes were "
249 "written, aborting.\n", stderr
);
259 * Check if we have a side-channel request ready...
262 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
265 * Do the side-channel request, then start back over in the select
266 * loop since it may have read from print_fd...
269 if ((*side_cb
)(print_fd
, device_fd
, snmp_fd
, addr
, use_bc
))
275 * Check if we have back-channel data ready...
278 if (FD_ISSET(device_fd
, &input
))
280 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
283 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data\n",
284 CUPS_LLCAST bc_bytes
);
285 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
287 else if (bc_bytes
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
289 fprintf(stderr
, "DEBUG: Error reading back-channel data: %s\n",
293 else if (bc_bytes
== 0)
298 * Check if we have print data ready...
301 if (FD_ISSET(print_fd
, &input
))
303 if ((print_bytes
= read(print_fd
, print_buffer
,
304 sizeof(print_buffer
))) < 0)
307 * Read error - bail if we don't see EAGAIN or EINTR...
310 if (errno
!= EAGAIN
|| errno
!= EINTR
)
312 fprintf(stderr
, "DEBUG: Read failed: %s\n", strerror(errno
));
313 _cupsLangPrintFilter(stderr
, "ERROR",
314 _("Unable to read print data."));
320 else if (print_bytes
== 0)
323 * End of file, break out of the loop...
329 print_ptr
= print_buffer
;
331 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
336 * Check if the device is ready to receive data and we have data to
340 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
342 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
345 * Write error - bail if we don't see an error we can retry...
350 if (paperout
!= 1 && update_state
)
352 fputs("STATE: +media-empty-warning\n", stderr
);
353 fputs("DEBUG: Out of paper\n", stderr
);
357 else if (errno
== ENXIO
)
359 if (offline
!= 1 && update_state
)
361 fputs("STATE: +offline-report\n", stderr
);
362 _cupsLangPrintFilter(stderr
, "INFO",
363 _("The printer is not connected."));
367 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
369 _cupsLangPrintError("ERROR", _("Unable to write print data"));
375 if (paperout
&& update_state
)
377 fputs("STATE: -media-empty-warning\n", stderr
);
381 if (offline
&& update_state
)
383 fputs("STATE: -offline-report\n", stderr
);
384 _cupsLangPrintFilter(stderr
, "INFO",
385 _("The printer is now connected."));
389 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
391 print_bytes
-= bytes
;
393 total_bytes
+= bytes
;
398 * Do SNMP updates periodically...
401 if (snmp_fd
>= 0 && time(&curtime
) >= snmp_update
)
403 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
404 snmp_update
= INT_MAX
;
406 snmp_update
= curtime
+ 5;
411 * Return with success...
414 return (total_bytes
);
419 * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
423 int /* O - 1 if data is ready, 0 if not */
425 int snmp_fd
, /* I - SNMP socket or -1 if none */
426 http_addr_t
*addr
, /* I - Address of device */
427 int use_bc
, /* I - Use back-channel? */
428 _cups_sccb_t side_cb
) /* I - Side-channel callback */
430 int nfds
; /* Number of file descriptors */
431 fd_set input
; /* Input set for reading */
432 time_t curtime
= 0, /* Current time */
433 snmp_update
= 0;/* Last SNMP status update */
434 struct timeval timeout
; /* Timeout for select() */
437 fprintf(stderr
, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
438 snmp_fd
, addr
, side_cb
);
441 * Now loop until we receive data from stdin...
445 snmp_update
= time(NULL
) + 5;
450 * Use select() to determine whether we have data to copy around...
456 FD_SET(CUPS_SC_FD
, &input
);
460 curtime
= time(NULL
);
461 timeout
.tv_sec
= curtime
>= snmp_update
? 0 : snmp_update
- curtime
;
464 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, &timeout
);
467 nfds
= select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, NULL
);
472 * Pause printing to clear any pending errors...
477 fputs("DEBUG: Received an interrupt before any bytes were "
478 "written, aborting.\n", stderr
);
487 * Check for input on stdin...
490 if (FD_ISSET(0, &input
))
494 * Check if we have a side-channel request ready...
497 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
500 * Do the side-channel request, then start back over in the select
501 * loop since it may have read from print_fd...
504 if ((*side_cb
)(0, -1, snmp_fd
, addr
, use_bc
))
510 * Do SNMP updates periodically...
513 if (snmp_fd
>= 0 && curtime
>= snmp_update
)
515 if (backendSNMPSupplies(snmp_fd
, addr
, NULL
, NULL
))
518 snmp_update
= curtime
+ 5;
523 * Return with success...