]> 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 5594 2006-05-27 03:14:03Z 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 #include <sys/select.h>
37
38
39 /*
40 * 'backendRunLoop()' - Read and write print and back-channel data.
41 */
42
43 ssize_t /* O - Total bytes on success, -1 on error */
44 backendRunLoop(int print_fd, /* I - Print file descriptor */
45 int device_fd, /* I - Device file descriptor */
46 int use_bc) /* I - Use back-channel? */
47 {
48 int nfds; /* Maximum file descriptor value + 1 */
49 fd_set input, /* Input set for reading */
50 output; /* Output set for writing */
51 ssize_t print_bytes, /* Print bytes read */
52 bc_bytes, /* Backchannel bytes read */
53 total_bytes, /* Total bytes written */
54 bytes; /* Bytes written */
55 int paperout; /* "Paper out" status */
56 char print_buffer[8192], /* Print data buffer */
57 *print_ptr, /* Pointer into print data buffer */
58 bc_buffer[1024]; /* Back-channel data buffer */
59 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
60 struct sigaction action; /* Actions for POSIX signals */
61 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
62
63
64 /*
65 * If we are printing data from a print driver on stdin, ignore SIGTERM
66 * so that the driver can finish out any page data, e.g. to eject the
67 * current page. We only do this for stdin printing as otherwise there
68 * is no way to cancel a raw print job...
69 */
70
71 if (!print_fd)
72 {
73 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
74 sigset(SIGTERM, SIG_IGN);
75 #elif defined(HAVE_SIGACTION)
76 memset(&action, 0, sizeof(action));
77
78 sigemptyset(&action.sa_mask);
79 action.sa_handler = SIG_IGN;
80 sigaction(SIGTERM, &action, NULL);
81 #else
82 signal(SIGTERM, SIG_IGN);
83 #endif /* HAVE_SIGSET */
84 }
85
86 /*
87 * Figure out the maximum file descriptor value to use with select()...
88 */
89
90 nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
91
92 /*
93 * Now loop until we are out of data from print_fd...
94 */
95
96 for (print_bytes = 0, print_ptr = print_buffer, paperout = 0, total_bytes = 0;;)
97 {
98 /*
99 * Use select() to determine whether we have data to copy around...
100 */
101
102 FD_ZERO(&input);
103 if (!print_bytes)
104 FD_SET(print_fd, &input);
105 if (use_bc)
106 FD_SET(device_fd, &input);
107
108 FD_ZERO(&output);
109 if (print_bytes)
110 FD_SET(device_fd, &output);
111
112 if (select(nfds, &input, &output, NULL, NULL) < 0)
113 continue; /* Ignore errors here */
114
115 /*
116 * Check if we have back-channel data ready...
117 */
118
119 if (FD_ISSET(device_fd, &input))
120 {
121 if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
122 {
123 fprintf(stderr,
124 "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
125 CUPS_LLCAST bc_bytes);
126 cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
127 }
128 }
129
130 /*
131 * Check if we have print data ready...
132 */
133
134 if (FD_ISSET(print_fd, &input))
135 {
136 if ((print_bytes = read(print_fd, print_buffer,
137 sizeof(print_buffer))) < 0)
138 {
139 /*
140 * Read error - bail if we don't see EAGAIN or EINTR...
141 */
142
143 if (errno != EAGAIN || errno != EINTR)
144 {
145 perror("ERROR: Unable to read print data");
146 return (-1);
147 }
148
149 print_bytes = 0;
150 }
151 else if (print_bytes == 0)
152 {
153 /*
154 * End of file, break out of the loop...
155 */
156
157 break;
158 }
159
160 print_ptr = print_buffer;
161 }
162
163 /*
164 * Check if the device is ready to receive data and we have data to
165 * send...
166 */
167
168 if (print_bytes && FD_ISSET(device_fd, &output))
169 {
170 if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
171 {
172 /*
173 * Write error - bail if we don't see an error we can retry...
174 */
175
176 if (errno == ENOSPC)
177 {
178 if (!paperout)
179 {
180 fputs("ERROR: Out of paper!\n", stderr);
181 fputs("STATUS: +media-tray-empty-error\n", stderr);
182 paperout = 1;
183 }
184 }
185 else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
186 {
187 perror("ERROR: Unable to write print data");
188 return (-1);
189 }
190 }
191 else
192 {
193 if (paperout)
194 {
195 fputs("STATUS: -media-tray-empty-error\n", stderr);
196 paperout = 0;
197 }
198
199 fprintf(stderr, "DEBUG: Wrote %d bytes...\n", (int)bytes);
200
201 print_bytes -= bytes;
202 print_ptr += bytes;
203 total_bytes += bytes;
204 }
205 }
206 }
207
208 /*
209 * Return with success...
210 */
211
212 return (total_bytes);
213 }
214
215
216 /*
217 * End of "$Id: runloop.c 5594 2006-05-27 03:14:03Z mike $".
218 */