]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/runloop.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / runloop.c
CommitLineData
ed486911 1/*
09a101d6 2 * "$Id: runloop.c 6591 2007-06-21 20:35:28Z mike $"
ed486911 3 *
09a101d6 4 * Common run loop APIs for the Common UNIX Printing System (CUPS).
ed486911 5 *
f7deaa1a 6 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
ed486911 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 *
09a101d6 28 * backendDrainOutput() - Drain pending print data to the device.
29 * backendRunLoop() - Read and write print and back-channel data.
ed486911 30 */
31
32/*
33 * Include necessary headers.
34 */
35
36#include "backend-private.h"
d09495fa 37#ifdef __hpux
38# include <sys/time.h>
39#else
40# include <sys/select.h>
41#endif /* __hpux */
ed486911 42
43
09a101d6 44/*
45 * 'backendDrainOutput()' - Drain pending print data to the device.
46 */
47
48int /* O - 0 on success, -1 on error */
49backendDrainOutput(int print_fd, /* I - Print file descriptor */
50 int device_fd) /* I - Device file descriptor */
51{
52 int nfds; /* Maximum file descriptor value + 1 */
53 fd_set input; /* Input set for reading */
54 ssize_t print_bytes, /* Print bytes read */
55 bytes; /* Bytes written */
56 char print_buffer[8192], /* Print data buffer */
57 *print_ptr; /* Pointer into print data buffer */
58 struct timeval timeout; /* Timeout for read... */
59
60
61 fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
62 print_fd, device_fd);
63
64 /*
65 * Figure out the maximum file descriptor value to use with select()...
66 */
67
68 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
69
70 /*
71 * Now loop until we are out of data from print_fd...
72 */
73
74 for (;;)
75 {
76 /*
77 * Use select() to determine whether we have data to copy around...
78 */
79
80 FD_ZERO(&input);
81 FD_SET(print_fd, &input);
82
83 timeout.tv_sec = 0;
84 timeout.tv_usec = 0;
85
86 if (select(nfds, &input, NULL, NULL, &timeout) < 0)
87 return (-1);
88
89 if (!FD_ISSET(print_fd, &input))
90 return (0);
91
92 if ((print_bytes = read(print_fd, print_buffer,
93 sizeof(print_buffer))) < 0)
94 {
95 /*
96 * Read error - bail if we don't see EAGAIN or EINTR...
97 */
98
99 if (errno != EAGAIN || errno != EINTR)
100 {
101 perror("ERROR: Unable to read print data");
102 return (-1);
103 }
104
105 print_bytes = 0;
106 }
107 else if (print_bytes == 0)
108 {
109 /*
110 * End of file, return...
111 */
112
113 return (0);
114 }
115
116 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
117 (int)print_bytes);
118
119 for (print_ptr = print_buffer; print_bytes > 0;)
120 {
121 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
122 {
123 /*
124 * Write error - bail if we don't see an error we can retry...
125 */
126
127 if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN &&
128 errno != EINTR && errno != ENOTTY)
129 {
130 fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
131 strerror(errno));
132 return (-1);
133 }
134 }
135 else
136 {
137 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
138
139 print_bytes -= bytes;
140 print_ptr += bytes;
141 }
142 }
143 }
144}
145
146
ed486911 147/*
148 * 'backendRunLoop()' - Read and write print and back-channel data.
149 */
150
151ssize_t /* O - Total bytes on success, -1 on error */
f7deaa1a 152backendRunLoop(
153 int print_fd, /* I - Print file descriptor */
154 int device_fd, /* I - Device file descriptor */
155 int use_bc, /* I - Use back-channel? */
156 void (*side_cb)(int, int, int)) /* I - Side-channel callback */
ed486911 157{
158 int nfds; /* Maximum file descriptor value + 1 */
159 fd_set input, /* Input set for reading */
160 output; /* Output set for writing */
161 ssize_t print_bytes, /* Print bytes read */
162 bc_bytes, /* Backchannel bytes read */
163 total_bytes, /* Total bytes written */
164 bytes; /* Bytes written */
165 int paperout; /* "Paper out" status */
8ca02f3c 166 int offline; /* "Off-line" status */
ed486911 167 char print_buffer[8192], /* Print data buffer */
168 *print_ptr, /* Pointer into print data buffer */
169 bc_buffer[1024]; /* Back-channel data buffer */
170#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
171 struct sigaction action; /* Actions for POSIX signals */
172#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
173
174
c0e1af83 175 fprintf(stderr,
09a101d6 176 "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d, "
177 "side_cb=%p)\n",
178 print_fd, device_fd, use_bc, side_cb);
8ca02f3c 179
ed486911 180 /*
181 * If we are printing data from a print driver on stdin, ignore SIGTERM
182 * so that the driver can finish out any page data, e.g. to eject the
183 * current page. We only do this for stdin printing as otherwise there
184 * is no way to cancel a raw print job...
185 */
186
187 if (!print_fd)
188 {
189#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
190 sigset(SIGTERM, SIG_IGN);
191#elif defined(HAVE_SIGACTION)
192 memset(&action, 0, sizeof(action));
193
194 sigemptyset(&action.sa_mask);
195 action.sa_handler = SIG_IGN;
196 sigaction(SIGTERM, &action, NULL);
197#else
198 signal(SIGTERM, SIG_IGN);
199#endif /* HAVE_SIGSET */
200 }
201
202 /*
203 * Figure out the maximum file descriptor value to use with select()...
204 */
205
206 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
207
208 /*
209 * Now loop until we are out of data from print_fd...
210 */
211
b94498cf 212 for (print_bytes = 0, print_ptr = print_buffer, offline = -1,
213 paperout = -1, total_bytes = 0;;)
ed486911 214 {
215 /*
216 * Use select() to determine whether we have data to copy around...
217 */
218
219 FD_ZERO(&input);
220 if (!print_bytes)
221 FD_SET(print_fd, &input);
222 if (use_bc)
223 FD_SET(device_fd, &input);
f7deaa1a 224 if (side_cb)
225 FD_SET(CUPS_SC_FD, &input);
ed486911 226
227 FD_ZERO(&output);
8ca02f3c 228 if (print_bytes || !use_bc)
ed486911 229 FD_SET(device_fd, &output);
230
f7deaa1a 231 if (use_bc || side_cb)
8ca02f3c 232 {
233 if (select(nfds, &input, &output, NULL, NULL) < 0)
234 {
235 /*
236 * Pause printing to clear any pending errors...
237 */
238
b94498cf 239 if (errno == ENXIO && offline != 1)
8ca02f3c 240 {
241 fputs("STATE: +offline-error\n", stderr);
c0e1af83 242 fputs(_("INFO: Printer is currently off-line.\n"), stderr);
8ca02f3c 243 offline = 1;
244 }
b94498cf 245 else if (errno == EINTR && total_bytes == 0)
246 {
247 fputs("DEBUG: Received an interrupt before any bytes were "
248 "written, aborting!\n", stderr);
249 return (0);
250 }
8ca02f3c 251
252 sleep(1);
253 continue;
254 }
255 }
ed486911 256
f7deaa1a 257 /*
258 * Check if we have a side-channel request ready...
259 */
260
261 if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
262 (*side_cb)(print_fd, device_fd, use_bc);
263
ed486911 264 /*
265 * Check if we have back-channel data ready...
266 */
267
268 if (FD_ISSET(device_fd, &input))
269 {
270 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
271 {
272 fprintf(stderr,
273 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
274 CUPS_LLCAST bc_bytes);
275 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
276 }
277 }
278
279 /*
280 * Check if we have print data ready...
281 */
282
283 if (FD_ISSET(print_fd, &input))
284 {
285 if ((print_bytes = read(print_fd, print_buffer,
286 sizeof(print_buffer))) < 0)
287 {
288 /*
289 * Read error - bail if we don't see EAGAIN or EINTR...
290 */
291
292 if (errno != EAGAIN || errno != EINTR)
293 {
294 perror("ERROR: Unable to read print data");
295 return (-1);
296 }
297
298 print_bytes = 0;
299 }
300 else if (print_bytes == 0)
301 {
302 /*
303 * End of file, break out of the loop...
304 */
305
306 break;
307 }
308
309 print_ptr = print_buffer;
8ca02f3c 310
311 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
312 (int)print_bytes);
ed486911 313 }
314
315 /*
316 * Check if the device is ready to receive data and we have data to
317 * send...
318 */
319
320 if (print_bytes && FD_ISSET(device_fd, &output))
321 {
322 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
323 {
324 /*
325 * Write error - bail if we don't see an error we can retry...
326 */
327
328 if (errno == ENOSPC)
329 {
b94498cf 330 if (paperout != 1)
ed486911 331 {
b86bc4cf 332 fputs("STATE: +media-empty-error\n", stderr);
c0e1af83 333 fputs(_("ERROR: Out of paper!\n"), stderr);
ed486911 334 paperout = 1;
335 }
336 }
8ca02f3c 337 else if (errno == ENXIO)
338 {
b94498cf 339 if (offline != 1)
8ca02f3c 340 {
341 fputs("STATE: +offline-error\n", stderr);
c0e1af83 342 fputs(_("INFO: Printer is currently off-line.\n"), stderr);
8ca02f3c 343 offline = 1;
344 }
345 }
ed486911 346 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
347 {
c0e1af83 348 fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
349 strerror(errno));
ed486911 350 return (-1);
351 }
352 }
353 else
354 {
355 if (paperout)
356 {
b86bc4cf 357 fputs("STATE: -media-empty-error\n", stderr);
ed486911 358 paperout = 0;
359 }
360
8ca02f3c 361 if (offline)
362 {
363 fputs("STATE: -offline-error\n", stderr);
c0e1af83 364 fputs(_("INFO: Printer is now on-line.\n"), stderr);
8ca02f3c 365 offline = 0;
366 }
367
368 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
ed486911 369
370 print_bytes -= bytes;
371 print_ptr += bytes;
372 total_bytes += bytes;
373 }
374 }
375 }
376
377 /*
378 * Return with success...
379 */
380
381 return (total_bytes);
382}
383
384
385/*
09a101d6 386 * End of "$Id: runloop.c 6591 2007-06-21 20:35:28Z mike $".
ed486911 387 */