]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
fd0bb8f0499ac6b5b71518dd7bcbafa1e5678841
2 * "$Id: runloop.c 6498 2007-04-30 21:40:33Z mike $"
4 * Common run loop API for the Common UNIX Printing System (CUPS).
6 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * backendRunLoop() - Read and write print and back-channel data.
32 * Include necessary headers.
35 #include "backend-private.h"
37 # include <sys/time.h>
39 # include <sys/select.h>
44 * 'backendRunLoop()' - Read and write print and back-channel data.
47 ssize_t
/* O - Total bytes on success, -1 on error */
49 int print_fd
, /* I - Print file descriptor */
50 int device_fd
, /* I - Device file descriptor */
51 int use_bc
, /* I - Use back-channel? */
52 void (*side_cb
)(int, int, int)) /* I - Side-channel callback */
54 int nfds
; /* Maximum file descriptor value + 1 */
55 fd_set input
, /* Input set for reading */
56 output
; /* Output set for writing */
57 ssize_t print_bytes
, /* Print bytes read */
58 bc_bytes
, /* Backchannel bytes read */
59 total_bytes
, /* Total bytes written */
60 bytes
; /* Bytes written */
61 int paperout
; /* "Paper out" status */
62 int offline
; /* "Off-line" status */
63 char print_buffer
[8192], /* Print data buffer */
64 *print_ptr
, /* Pointer into print data buffer */
65 bc_buffer
[1024]; /* Back-channel data buffer */
66 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
67 struct sigaction action
; /* Actions for POSIX signals */
68 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
72 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
73 print_fd
, device_fd
, use_bc
);
76 * If we are printing data from a print driver on stdin, ignore SIGTERM
77 * so that the driver can finish out any page data, e.g. to eject the
78 * current page. We only do this for stdin printing as otherwise there
79 * is no way to cancel a raw print job...
84 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
85 sigset(SIGTERM
, SIG_IGN
);
86 #elif defined(HAVE_SIGACTION)
87 memset(&action
, 0, sizeof(action
));
89 sigemptyset(&action
.sa_mask
);
90 action
.sa_handler
= SIG_IGN
;
91 sigaction(SIGTERM
, &action
, NULL
);
93 signal(SIGTERM
, SIG_IGN
);
94 #endif /* HAVE_SIGSET */
98 * Figure out the maximum file descriptor value to use with select()...
101 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
104 * Now loop until we are out of data from print_fd...
107 for (print_bytes
= 0, print_ptr
= print_buffer
, offline
= -1,
108 paperout
= -1, total_bytes
= 0;;)
111 * Use select() to determine whether we have data to copy around...
116 FD_SET(print_fd
, &input
);
118 FD_SET(device_fd
, &input
);
120 FD_SET(CUPS_SC_FD
, &input
);
123 if (print_bytes
|| !use_bc
)
124 FD_SET(device_fd
, &output
);
126 if (use_bc
|| side_cb
)
128 if (select(nfds
, &input
, &output
, NULL
, NULL
) < 0)
131 * Pause printing to clear any pending errors...
134 if (errno
== ENXIO
&& offline
!= 1)
136 fputs("STATE: +offline-error\n", stderr
);
137 fputs(_("INFO: Printer is currently off-line.\n"), stderr
);
140 else if (errno
== EINTR
&& total_bytes
== 0)
142 fputs("DEBUG: Received an interrupt before any bytes were "
143 "written, aborting!\n", stderr
);
153 * Check if we have a side-channel request ready...
156 if (side_cb
&& FD_ISSET(CUPS_SC_FD
, &input
))
157 (*side_cb
)(print_fd
, device_fd
, use_bc
);
160 * Check if we have back-channel data ready...
163 if (FD_ISSET(device_fd
, &input
))
165 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
168 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
169 CUPS_LLCAST bc_bytes
);
170 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
175 * Check if we have print data ready...
178 if (FD_ISSET(print_fd
, &input
))
180 if ((print_bytes
= read(print_fd
, print_buffer
,
181 sizeof(print_buffer
))) < 0)
184 * Read error - bail if we don't see EAGAIN or EINTR...
187 if (errno
!= EAGAIN
|| errno
!= EINTR
)
189 perror("ERROR: Unable to read print data");
195 else if (print_bytes
== 0)
198 * End of file, break out of the loop...
204 print_ptr
= print_buffer
;
206 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
211 * Check if the device is ready to receive data and we have data to
215 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
217 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
220 * Write error - bail if we don't see an error we can retry...
227 fputs("STATE: +media-empty-error\n", stderr
);
228 fputs(_("ERROR: Out of paper!\n"), stderr
);
232 else if (errno
== ENXIO
)
236 fputs("STATE: +offline-error\n", stderr
);
237 fputs(_("INFO: Printer is currently off-line.\n"), stderr
);
241 else if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
243 fprintf(stderr
, _("ERROR: Unable to write print data: %s\n"),
252 fputs("STATE: -media-empty-error\n", stderr
);
258 fputs("STATE: -offline-error\n", stderr
);
259 fputs(_("INFO: Printer is now on-line.\n"), stderr
);
263 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
265 print_bytes
-= bytes
;
267 total_bytes
+= bytes
;
273 * Return with success...
276 return (total_bytes
);
281 * End of "$Id: runloop.c 6498 2007-04-30 21:40:33Z mike $".