]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Backchannel functions for CUPS. |
ef416fc2 | 3 | * |
7e86f2f6 MS |
4 | * Copyright 2007-2014 by Apple Inc. |
5 | * Copyright 1997-2007 by Easy Software Products. | |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 8 | */ |
9 | ||
10 | /* | |
11 | * Include necessary headers... | |
12 | */ | |
13 | ||
14 | #include "cups.h" | |
15 | #include <errno.h> | |
16 | #ifdef WIN32 | |
17 | # include <io.h> | |
18 | # include <fcntl.h> | |
19 | #else | |
20 | # include <sys/time.h> | |
21 | #endif /* WIN32 */ | |
22 | ||
23 | ||
24 | /* | |
25 | * Local functions... | |
26 | */ | |
27 | ||
28 | static void cups_setup(fd_set *set, struct timeval *tval, | |
29 | double timeout); | |
30 | ||
31 | ||
32 | /* | |
33 | * 'cupsBackChannelRead()' - Read data from the backchannel. | |
34 | * | |
79e1d494 MS |
35 | * Reads up to "bytes" bytes from the backchannel/backend. The "timeout" |
36 | * parameter controls how many seconds to wait for the data - use 0.0 to | |
37 | * return immediately if there is no data, -1.0 to wait for data indefinitely. | |
ef416fc2 | 38 | * |
8072030b | 39 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 40 | */ |
41 | ||
ecdc0628 | 42 | ssize_t /* O - Bytes read or -1 on error */ |
79e1d494 | 43 | cupsBackChannelRead(char *buffer, /* I - Buffer to read into */ |
ecdc0628 | 44 | size_t bytes, /* I - Bytes to read */ |
79e1d494 | 45 | double timeout) /* I - Timeout in seconds, typically 0.0 to poll */ |
ef416fc2 | 46 | { |
47 | fd_set input; /* Input set */ | |
48 | struct timeval tval; /* Timeout value */ | |
49 | int status; /* Select status */ | |
50 | ||
51 | ||
52 | /* | |
53 | * Wait for input ready. | |
54 | */ | |
55 | ||
56 | do | |
57 | { | |
58 | cups_setup(&input, &tval, timeout); | |
59 | ||
60 | if (timeout < 0.0) | |
61 | status = select(4, &input, NULL, NULL, NULL); | |
62 | else | |
63 | status = select(4, &input, NULL, NULL, &tval); | |
64 | } | |
e07d4801 | 65 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 66 | |
67 | if (status < 0) | |
68 | return (-1); /* Timeout! */ | |
69 | ||
70 | /* | |
71 | * Read bytes from the pipe... | |
72 | */ | |
73 | ||
b86bc4cf | 74 | #ifdef WIN32 |
536bc2c6 | 75 | return ((ssize_t)_read(3, buffer, (unsigned)bytes)); |
b86bc4cf | 76 | #else |
ef416fc2 | 77 | return (read(3, buffer, bytes)); |
b86bc4cf | 78 | #endif /* WIN32 */ |
ef416fc2 | 79 | } |
80 | ||
81 | ||
82 | /* | |
83 | * 'cupsBackChannelWrite()' - Write data to the backchannel. | |
84 | * | |
79e1d494 | 85 | * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter |
ef416fc2 | 86 | * controls how many seconds to wait for the data to be written - use |
87 | * 0.0 to return immediately if the data cannot be written, -1.0 to wait | |
88 | * indefinitely. | |
89 | * | |
8072030b | 90 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 91 | */ |
92 | ||
ecdc0628 | 93 | ssize_t /* O - Bytes written or -1 on error */ |
bd7854cb | 94 | cupsBackChannelWrite( |
ef416fc2 | 95 | const char *buffer, /* I - Buffer to write */ |
ecdc0628 | 96 | size_t bytes, /* I - Bytes to write */ |
79e1d494 | 97 | double timeout) /* I - Timeout in seconds, typically 1.0 */ |
ef416fc2 | 98 | { |
99 | fd_set output; /* Output set */ | |
100 | struct timeval tval; /* Timeout value */ | |
101 | int status; /* Select status */ | |
ecdc0628 | 102 | ssize_t count; /* Current bytes */ |
103 | size_t total; /* Total bytes */ | |
ef416fc2 | 104 | |
105 | ||
106 | /* | |
107 | * Write all bytes... | |
108 | */ | |
109 | ||
110 | total = 0; | |
111 | ||
112 | while (total < bytes) | |
113 | { | |
114 | /* | |
115 | * Wait for write-ready... | |
116 | */ | |
117 | ||
118 | do | |
119 | { | |
120 | cups_setup(&output, &tval, timeout); | |
121 | ||
122 | if (timeout < 0.0) | |
123 | status = select(4, NULL, &output, NULL, NULL); | |
124 | else | |
125 | status = select(4, NULL, &output, NULL, &tval); | |
126 | } | |
e07d4801 | 127 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 128 | |
e07d4801 | 129 | if (status <= 0) |
ef416fc2 | 130 | return (-1); /* Timeout! */ |
131 | ||
132 | /* | |
133 | * Write bytes to the pipe... | |
134 | */ | |
135 | ||
b86bc4cf | 136 | #ifdef WIN32 |
536bc2c6 | 137 | count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total)); |
b86bc4cf | 138 | #else |
ef416fc2 | 139 | count = write(3, buffer, bytes - total); |
b86bc4cf | 140 | #endif /* WIN32 */ |
ef416fc2 | 141 | |
142 | if (count < 0) | |
143 | { | |
144 | /* | |
145 | * Write error - abort on fatal errors... | |
146 | */ | |
147 | ||
e07d4801 | 148 | if (errno != EINTR && errno != EAGAIN) |
ef416fc2 | 149 | return (-1); |
150 | } | |
151 | else | |
152 | { | |
153 | /* | |
154 | * Write succeeded, update buffer pointer and total count... | |
155 | */ | |
156 | ||
157 | buffer += count; | |
7e86f2f6 | 158 | total += (size_t)count; |
ef416fc2 | 159 | } |
160 | } | |
161 | ||
b86bc4cf | 162 | return ((ssize_t)bytes); |
ef416fc2 | 163 | } |
164 | ||
165 | ||
166 | /* | |
f3c17241 | 167 | * 'cups_setup()' - Setup select() |
ef416fc2 | 168 | */ |
169 | ||
170 | static void | |
171 | cups_setup(fd_set *set, /* I - Set for select() */ | |
172 | struct timeval *tval, /* I - Timer value */ | |
173 | double timeout) /* I - Timeout in seconds */ | |
174 | { | |
175 | tval->tv_sec = (int)timeout; | |
176 | tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec)); | |
177 | ||
178 | FD_ZERO(set); | |
179 | FD_SET(3, set); | |
180 | } |