]>
Commit | Line | Data |
---|---|---|
ed486911 | 1 | /* |
c0e1af83 | 2 | * "$Id: runloop.c 6403 2007-03-27 16:00:56Z mike $" |
ed486911 | 3 | * |
4 | * Common run loop API for the Common UNIX Printing System (CUPS). | |
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 | * | |
28 | * backendRunLoop() - Read and write print and back-channel data. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers. | |
33 | */ | |
34 | ||
35 | #include "backend-private.h" | |
d09495fa | 36 | #ifdef __hpux |
37 | # include <sys/time.h> | |
38 | #else | |
39 | # include <sys/select.h> | |
40 | #endif /* __hpux */ | |
ed486911 | 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 */ | |
f7deaa1a | 48 | backendRunLoop( |
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 */ | |
ed486911 | 53 | { |
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 */ | |
8ca02f3c | 62 | int offline; /* "Off-line" status */ |
ed486911 | 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 */ | |
69 | ||
70 | ||
c0e1af83 | 71 | fprintf(stderr, |
72 | "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n", | |
8ca02f3c | 73 | print_fd, device_fd, use_bc); |
74 | ||
ed486911 | 75 | /* |
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... | |
80 | */ | |
81 | ||
82 | if (!print_fd) | |
83 | { | |
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)); | |
88 | ||
89 | sigemptyset(&action.sa_mask); | |
90 | action.sa_handler = SIG_IGN; | |
91 | sigaction(SIGTERM, &action, NULL); | |
92 | #else | |
93 | signal(SIGTERM, SIG_IGN); | |
94 | #endif /* HAVE_SIGSET */ | |
95 | } | |
96 | ||
97 | /* | |
98 | * Figure out the maximum file descriptor value to use with select()... | |
99 | */ | |
100 | ||
101 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; | |
102 | ||
103 | /* | |
104 | * Now loop until we are out of data from print_fd... | |
105 | */ | |
106 | ||
8ca02f3c | 107 | for (print_bytes = 0, print_ptr = print_buffer, offline = 0, paperout = 0, total_bytes = 0;;) |
ed486911 | 108 | { |
109 | /* | |
110 | * Use select() to determine whether we have data to copy around... | |
111 | */ | |
112 | ||
113 | FD_ZERO(&input); | |
114 | if (!print_bytes) | |
115 | FD_SET(print_fd, &input); | |
116 | if (use_bc) | |
117 | FD_SET(device_fd, &input); | |
f7deaa1a | 118 | if (side_cb) |
119 | FD_SET(CUPS_SC_FD, &input); | |
ed486911 | 120 | |
121 | FD_ZERO(&output); | |
8ca02f3c | 122 | if (print_bytes || !use_bc) |
ed486911 | 123 | FD_SET(device_fd, &output); |
124 | ||
f7deaa1a | 125 | if (use_bc || side_cb) |
8ca02f3c | 126 | { |
127 | if (select(nfds, &input, &output, NULL, NULL) < 0) | |
128 | { | |
129 | /* | |
130 | * Pause printing to clear any pending errors... | |
131 | */ | |
132 | ||
133 | if (errno == ENXIO && !offline) | |
134 | { | |
135 | fputs("STATE: +offline-error\n", stderr); | |
c0e1af83 | 136 | fputs(_("INFO: Printer is currently off-line.\n"), stderr); |
8ca02f3c | 137 | offline = 1; |
138 | } | |
139 | ||
140 | sleep(1); | |
141 | continue; | |
142 | } | |
143 | } | |
ed486911 | 144 | |
f7deaa1a | 145 | /* |
146 | * Check if we have a side-channel request ready... | |
147 | */ | |
148 | ||
149 | if (side_cb && FD_ISSET(CUPS_SC_FD, &input)) | |
150 | (*side_cb)(print_fd, device_fd, use_bc); | |
151 | ||
ed486911 | 152 | /* |
153 | * Check if we have back-channel data ready... | |
154 | */ | |
155 | ||
156 | if (FD_ISSET(device_fd, &input)) | |
157 | { | |
158 | if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0) | |
159 | { | |
160 | fprintf(stderr, | |
161 | "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n", | |
162 | CUPS_LLCAST bc_bytes); | |
163 | cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0); | |
164 | } | |
165 | } | |
166 | ||
167 | /* | |
168 | * Check if we have print data ready... | |
169 | */ | |
170 | ||
171 | if (FD_ISSET(print_fd, &input)) | |
172 | { | |
173 | if ((print_bytes = read(print_fd, print_buffer, | |
174 | sizeof(print_buffer))) < 0) | |
175 | { | |
176 | /* | |
177 | * Read error - bail if we don't see EAGAIN or EINTR... | |
178 | */ | |
179 | ||
180 | if (errno != EAGAIN || errno != EINTR) | |
181 | { | |
182 | perror("ERROR: Unable to read print data"); | |
183 | return (-1); | |
184 | } | |
185 | ||
186 | print_bytes = 0; | |
187 | } | |
188 | else if (print_bytes == 0) | |
189 | { | |
190 | /* | |
191 | * End of file, break out of the loop... | |
192 | */ | |
193 | ||
194 | break; | |
195 | } | |
196 | ||
197 | print_ptr = print_buffer; | |
8ca02f3c | 198 | |
199 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
200 | (int)print_bytes); | |
ed486911 | 201 | } |
202 | ||
203 | /* | |
204 | * Check if the device is ready to receive data and we have data to | |
205 | * send... | |
206 | */ | |
207 | ||
208 | if (print_bytes && FD_ISSET(device_fd, &output)) | |
209 | { | |
210 | if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) | |
211 | { | |
212 | /* | |
213 | * Write error - bail if we don't see an error we can retry... | |
214 | */ | |
215 | ||
216 | if (errno == ENOSPC) | |
217 | { | |
218 | if (!paperout) | |
219 | { | |
b86bc4cf | 220 | fputs("STATE: +media-empty-error\n", stderr); |
c0e1af83 | 221 | fputs(_("ERROR: Out of paper!\n"), stderr); |
ed486911 | 222 | paperout = 1; |
223 | } | |
224 | } | |
8ca02f3c | 225 | else if (errno == ENXIO) |
226 | { | |
227 | if (!offline) | |
228 | { | |
229 | fputs("STATE: +offline-error\n", stderr); | |
c0e1af83 | 230 | fputs(_("INFO: Printer is currently off-line.\n"), stderr); |
8ca02f3c | 231 | offline = 1; |
232 | } | |
233 | } | |
ed486911 | 234 | else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY) |
235 | { | |
c0e1af83 | 236 | fprintf(stderr, _("ERROR: Unable to write print data: %s\n"), |
237 | strerror(errno)); | |
ed486911 | 238 | return (-1); |
239 | } | |
240 | } | |
241 | else | |
242 | { | |
243 | if (paperout) | |
244 | { | |
b86bc4cf | 245 | fputs("STATE: -media-empty-error\n", stderr); |
ed486911 | 246 | paperout = 0; |
247 | } | |
248 | ||
8ca02f3c | 249 | if (offline) |
250 | { | |
251 | fputs("STATE: -offline-error\n", stderr); | |
c0e1af83 | 252 | fputs(_("INFO: Printer is now on-line.\n"), stderr); |
8ca02f3c | 253 | offline = 0; |
254 | } | |
255 | ||
256 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
ed486911 | 257 | |
258 | print_bytes -= bytes; | |
259 | print_ptr += bytes; | |
260 | total_bytes += bytes; | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | /* | |
266 | * Return with success... | |
267 | */ | |
268 | ||
269 | return (total_bytes); | |
270 | } | |
271 | ||
272 | ||
273 | /* | |
c0e1af83 | 274 | * End of "$Id: runloop.c 6403 2007-03-27 16:00:56Z mike $". |
ed486911 | 275 | */ |