]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/runloop.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / runloop.c
1 /*
2 * "$Id: runloop.c 5776 2006-07-26 20:55:13Z mike $"
3 *
4 * Common run loop API for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2006 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * backendRunLoop() - Read and write print and back-channel data.
29 */
30
31 /*
32 * Include necessary headers.
33 */
34
35 #include "backend-private.h"
36 #ifdef __hpux
37 # include <sys/time.h>
38 #else
39 # include <sys/select.h>
40 #endif /* __hpux */
41
42
43 /*
44 * 'backendRunLoop()' - Read and write print and back-channel data.
45 */
46
47 ssize_t /* O - Total bytes on success, -1 on error */
48 backendRunLoop(int print_fd, /* I - Print file descriptor */
49 int device_fd, /* I - Device file descriptor */
50 int use_bc) /* I - Use back-channel? */
51 {
52 int nfds; /* Maximum file descriptor value + 1 */
53 fd_set input, /* Input set for reading */
54 output; /* Output set for writing */
55 ssize_t print_bytes, /* Print bytes read */
56 bc_bytes, /* Backchannel bytes read */
57 total_bytes, /* Total bytes written */
58 bytes; /* Bytes written */
59 int paperout; /* "Paper out" status */
60 int offline; /* "Off-line" status */
61 char print_buffer[8192], /* Print data buffer */
62 *print_ptr, /* Pointer into print data buffer */
63 bc_buffer[1024]; /* Back-channel data buffer */
64 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
65 struct sigaction action; /* Actions for POSIX signals */
66 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
67
68
69 fprintf(stderr, "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
70 print_fd, device_fd, use_bc);
71
72 /*
73 * If we are printing data from a print driver on stdin, ignore SIGTERM
74 * so that the driver can finish out any page data, e.g. to eject the
75 * current page. We only do this for stdin printing as otherwise there
76 * is no way to cancel a raw print job...
77 */
78
79 if (!print_fd)
80 {
81 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
82 sigset(SIGTERM, SIG_IGN);
83 #elif defined(HAVE_SIGACTION)
84 memset(&action, 0, sizeof(action));
85
86 sigemptyset(&action.sa_mask);
87 action.sa_handler = SIG_IGN;
88 sigaction(SIGTERM, &action, NULL);
89 #else
90 signal(SIGTERM, SIG_IGN);
91 #endif /* HAVE_SIGSET */
92 }
93
94 /*
95 * Figure out the maximum file descriptor value to use with select()...
96 */
97
98 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
99
100 /*
101 * Now loop until we are out of data from print_fd...
102 */
103
104 for (print_bytes = 0, print_ptr = print_buffer, offline = 0, paperout = 0, total_bytes = 0;;)
105 {
106 /*
107 * Use select() to determine whether we have data to copy around...
108 */
109
110 FD_ZERO(&input);
111 if (!print_bytes)
112 FD_SET(print_fd, &input);
113 if (use_bc)
114 FD_SET(device_fd, &input);
115
116 FD_ZERO(&output);
117 if (print_bytes || !use_bc)
118 FD_SET(device_fd, &output);
119
120 if (use_bc)
121 {
122 if (select(nfds, &input, &output, NULL, NULL) < 0)
123 {
124 /*
125 * Pause printing to clear any pending errors...
126 */
127
128 if (errno == ENXIO && !offline)
129 {
130 fputs("STATE: +offline-error\n", stderr);
131 fputs("INFO: Printer is currently off-line.\n", stderr);
132 offline = 1;
133 }
134
135 sleep(1);
136 continue;
137 }
138 }
139
140 /*
141 * Check if we have back-channel data ready...
142 */
143
144 if (FD_ISSET(device_fd, &input))
145 {
146 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
147 {
148 fprintf(stderr,
149 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
150 CUPS_LLCAST bc_bytes);
151 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
152 }
153 }
154
155 /*
156 * Check if we have print data ready...
157 */
158
159 if (FD_ISSET(print_fd, &input))
160 {
161 if ((print_bytes = read(print_fd, print_buffer,
162 sizeof(print_buffer))) < 0)
163 {
164 /*
165 * Read error - bail if we don't see EAGAIN or EINTR...
166 */
167
168 if (errno != EAGAIN || errno != EINTR)
169 {
170 perror("ERROR: Unable to read print data");
171 return (-1);
172 }
173
174 print_bytes = 0;
175 }
176 else if (print_bytes == 0)
177 {
178 /*
179 * End of file, break out of the loop...
180 */
181
182 break;
183 }
184
185 print_ptr = print_buffer;
186
187 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
188 (int)print_bytes);
189 }
190
191 /*
192 * Check if the device is ready to receive data and we have data to
193 * send...
194 */
195
196 if (print_bytes && FD_ISSET(device_fd, &output))
197 {
198 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
199 {
200 /*
201 * Write error - bail if we don't see an error we can retry...
202 */
203
204 if (errno == ENOSPC)
205 {
206 if (!paperout)
207 {
208 fputs("ERROR: Out of paper!\n", stderr);
209 fputs("STATUS: +media-tray-empty-error\n", stderr);
210 paperout = 1;
211 }
212 }
213 else if (errno == ENXIO)
214 {
215 if (!offline)
216 {
217 fputs("STATE: +offline-error\n", stderr);
218 fputs("INFO: Printer is currently off-line.\n", stderr);
219 offline = 1;
220 }
221 }
222 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
223 {
224 perror("ERROR: Unable to write print data");
225 return (-1);
226 }
227 }
228 else
229 {
230 if (paperout)
231 {
232 fputs("STATUS: -media-tray-empty-error\n", stderr);
233 paperout = 0;
234 }
235
236 if (offline)
237 {
238 fputs("STATE: -offline-error\n", stderr);
239 fputs("INFO: Printer is now on-line.\n", stderr);
240 offline = 0;
241 }
242
243 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
244
245 print_bytes -= bytes;
246 print_ptr += bytes;
247 total_bytes += bytes;
248 }
249 }
250 }
251
252 /*
253 * Return with success...
254 */
255
256 return (total_bytes);
257 }
258
259
260 /*
261 * End of "$Id: runloop.c 5776 2006-07-26 20:55:13Z mike $".
262 */