]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/backchannel.c
2 * Backchannel functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
20 # include <sys/time.h>
28 static void cups_setup(fd_set
*set
, struct timeval
*tval
,
33 * 'cupsBackChannelRead()' - Read data from the backchannel.
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.
39 * @since CUPS 1.2/macOS 10.5@
42 ssize_t
/* O - Bytes read or -1 on error */
43 cupsBackChannelRead(char *buffer
, /* I - Buffer to read into */
44 size_t bytes
, /* I - Bytes to read */
45 double timeout
) /* I - Timeout in seconds, typically 0.0 to poll */
47 fd_set input
; /* Input set */
48 struct timeval tval
; /* Timeout value */
49 int status
; /* Select status */
53 * Wait for input ready.
58 cups_setup(&input
, &tval
, timeout
);
61 status
= select(4, &input
, NULL
, NULL
, NULL
);
63 status
= select(4, &input
, NULL
, NULL
, &tval
);
65 while (status
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
);
68 return (-1); /* Timeout! */
71 * Read bytes from the pipe...
75 return ((ssize_t
)_read(3, buffer
, (unsigned)bytes
));
77 return (read(3, buffer
, bytes
));
83 * 'cupsBackChannelWrite()' - Write data to the backchannel.
85 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
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
90 * @since CUPS 1.2/macOS 10.5@
93 ssize_t
/* O - Bytes written or -1 on error */
95 const char *buffer
, /* I - Buffer to write */
96 size_t bytes
, /* I - Bytes to write */
97 double timeout
) /* I - Timeout in seconds, typically 1.0 */
99 fd_set output
; /* Output set */
100 struct timeval tval
; /* Timeout value */
101 int status
; /* Select status */
102 ssize_t count
; /* Current bytes */
103 size_t total
; /* Total bytes */
112 while (total
< bytes
)
115 * Wait for write-ready...
120 cups_setup(&output
, &tval
, timeout
);
123 status
= select(4, NULL
, &output
, NULL
, NULL
);
125 status
= select(4, NULL
, &output
, NULL
, &tval
);
127 while (status
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
);
130 return (-1); /* Timeout! */
133 * Write bytes to the pipe...
137 count
= (ssize_t
)_write(3, buffer
, (unsigned)(bytes
- total
));
139 count
= write(3, buffer
, bytes
- total
);
145 * Write error - abort on fatal errors...
148 if (errno
!= EINTR
&& errno
!= EAGAIN
)
154 * Write succeeded, update buffer pointer and total count...
158 total
+= (size_t)count
;
162 return ((ssize_t
)bytes
);
167 * 'cups_setup()' - Setup select()
171 cups_setup(fd_set
*set
, /* I - Set for select() */
172 struct timeval
*tval
, /* I - Timer value */
173 double timeout
) /* I - Timeout in seconds */
175 tval
->tv_sec
= (int)timeout
;
176 tval
->tv_usec
= (int)(1000000.0 * (timeout
- tval
->tv_sec
));