]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
4 * Common run loop APIs for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * backendDrainOutput() - Drain pending print data to the device.
20 * backendRunLoop() - Read and write print and back-channel data.
24 * Include necessary headers.
27 #include "backend-private.h"
29 # include <sys/time.h>
31 # include <sys/select.h>
36 * 'backendDrainOutput()' - Drain pending print data to the device.
39 int /* O - 0 on success, -1 on error */
40 backendDrainOutput(int print_fd
, /* I - Print file descriptor */
41 int device_fd
) /* I - Device file descriptor */
43 int nfds
; /* Maximum file descriptor value + 1 */
44 fd_set input
; /* Input set for reading */
45 ssize_t print_bytes
, /* Print bytes read */
46 bytes
; /* Bytes written */
47 char print_buffer
[8192], /* Print data buffer */
48 *print_ptr
; /* Pointer into print data buffer */
49 struct timeval timeout
; /* Timeout for read... */
52 fprintf(stderr
, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
56 * Figure out the maximum file descriptor value to use with select()...
59 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
62 * Now loop until we are out of data from print_fd...
68 * Use select() to determine whether we have data to copy around...
72 FD_SET(print_fd
, &input
);
77 if (select(nfds
, &input
, NULL
, NULL
, &timeout
) < 0)
80 if (!FD_ISSET(print_fd
, &input
))
83 if ((print_bytes
= read(print_fd
, print_buffer
,
84 sizeof(print_buffer
))) < 0)
87 * Read error - bail if we don't see EAGAIN or EINTR...
90 if (errno
!= EAGAIN
|| errno
!= EINTR
)
92 perror("ERROR: Unable to read print data");
98 else if (print_bytes
== 0)
101 * End of file, return...
107 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
110 for (print_ptr
= print_buffer
; print_bytes
> 0;)
112 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
115 * Write error - bail if we don't see an error we can retry...
118 if (errno
!= ENOSPC
&& errno
!= ENXIO
&& errno
!= EAGAIN
&&
119 errno
!= EINTR
&& errno
!= ENOTTY
)
121 fprintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
128 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
130 print_bytes
-= bytes
;
139 * 'backendRunLoop()' - Read and write print and back-channel data.
142 ssize_t
/* O - Total bytes on success, -1 on error */
144 int print_fd
, /* I - Print file descriptor */
145 int device_fd
, /* I - Device file descriptor */
146 int use_bc
, /* I - Use back-channel? */
147 void (*side_cb
)(int, int, int)) /* I - Side-channel callback */
149 int nfds
; /* Maximum file descriptor value + 1 */
150 fd_set input
, /* Input set for reading */
151 output
; /* Output set for writing */
152 ssize_t print_bytes
, /* Print bytes read */
153 bc_bytes
, /* Backchannel bytes read */
154 total_bytes
, /* Total bytes written */
155 bytes
; /* Bytes written */
156 int paperout
; /* "Paper out" status */
157 int offline
; /* "Off-line" status */
158 char print_buffer
[8192], /* Print data buffer */
159 *print_ptr
, /* Pointer into print data buffer */
160 bc_buffer
[1024]; /* Back-channel data buffer */
161 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
162 struct sigaction action
; /* Actions for POSIX signals */
163 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
167 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d, "
169 print_fd
, device_fd
, use_bc
, side_cb
);
172 * If we are printing data from a print driver on stdin, ignore SIGTERM
173 * so that the driver can finish out any page data, e.g. to eject the
174 * current page. We only do this for stdin printing as otherwise there
175 * is no way to cancel a raw print job...
180 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
181 sigset(SIGTERM
, SIG_IGN
);
182 #elif defined(HAVE_SIGACTION)
183 memset(&action
, 0, sizeof(action
));
185 sigemptyset(&action
.sa_mask
);
186 action
.sa_handler
= SIG_IGN
;
187 sigaction(SIGTERM
, &action
, NULL
);
189 signal(SIGTERM
, SIG_IGN
);
190 #endif /* HAVE_SIGSET */
194 * Figure out the maximum file descriptor value to use with select()...
197 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
200 * Now loop until we are out of data from print_fd...
203 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
204 paperout
= -1, total_bytes
= 0;;)
207 * Use select() to determine whether we have data to copy around...
212 FD_SET(print_fd
, &input
);
214 FD_SET(device_fd
, &input
);
216 FD_SET(CUPS_SC_FD
, &input
);
219 if (print_bytes
|| !use_bc
)
220 FD_SET(device_fd
, &output
);
222 if (use_bc
|| side_cb
)
224 if (select(nfds
, &input
, &output
, NULL
, NULL
) < 0)
227 * Pause printing to clear any pending errors...
230 if (errno
== ENXIO
&& offline
!= 1)
232 fputs("STATE: +offline-error\n", stderr
);
233 fputs(_("INFO: Printer is currently off-line.\n"), stderr
);
236 else if (errno
== EINTR
&& total_bytes
== 0)
238 fputs("DEBUG: Received an interrupt before any bytes were "
239 "written, aborting!\n", stderr
);
249 * Check if we have a side-channel request ready...
252 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
253 (*side_cb
)(print_fd
, device_fd
, use_bc
);
256 * Check if we have back-channel data ready...
259 if (FD_ISSET(device_fd
, &input
))
261 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
264 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
265 CUPS_LLCAST bc_bytes
);
266 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
271 * Check if we have print data ready...
274 if (FD_ISSET(print_fd
, &input
))
276 if ((print_bytes
= read(print_fd
, print_buffer
,
277 sizeof(print_buffer
))) < 0)
280 * Read error - bail if we don't see EAGAIN or EINTR...
283 if (errno
!= EAGAIN
|| errno
!= EINTR
)
285 perror("ERROR: Unable to read print data");
291 else if (print_bytes
== 0)
294 * End of file, break out of the loop...
300 print_ptr
= print_buffer
;
302 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
307 * Check if the device is ready to receive data and we have data to
311 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
313 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
316 * Write error - bail if we don't see an error we can retry...
323 fputs("STATE: +media-empty-error\n", stderr
);
324 fputs(_("ERROR: Out of paper!\n"), stderr
);
328 else if (errno
== ENXIO
)
332 fputs("STATE: +offline-error\n", stderr
);
333 fputs(_("INFO: Printer is currently off-line.\n"), stderr
);
337 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
339 fprintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
348 fputs("STATE: -media-empty-error\n", stderr
);
354 fputs("STATE: -offline-error\n", stderr
);
355 fputs(_("INFO: Printer is now on-line.\n"), stderr
);
359 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
361 print_bytes
-= bytes
;
363 total_bytes
+= bytes
;
369 * Return with success...
372 return (total_bytes
);