2 * Copyright (c) 2015-2017 Red Hat, Inc.
6 * Author: Jan Friesse (jfriesse@redhat.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the Red Hat, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
42 #include "qdevice-heuristics-io.h"
44 #define QDEVICE_HEURISTICS_IO_BUFFER_SIZE 256
47 qdevice_heuristics_io_blocking_write(int fd
, const void *buf
, size_t count
)
49 ssize_t bytes_written
;
50 ssize_t tmp_bytes_written
;
55 tmp_bytes_written
= write(fd
, (const char *)buf
+ bytes_written
,
56 (count
- bytes_written
> SSIZE_MAX
) ? SSIZE_MAX
: count
- bytes_written
);
57 if (tmp_bytes_written
== -1) {
58 if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= EWOULDBLOCK
) {
62 bytes_written
+= tmp_bytes_written
;
64 } while ((size_t)bytes_written
!= count
);
66 return (bytes_written
);
70 * 1 Full line readed (at least one \n found)
71 * 0 Partial read (no error)
72 * -1 End of connection
77 qdevice_heuristics_io_read(int fd
, struct dynar
*dest
)
79 char buf
[QDEVICE_HEURISTICS_IO_BUFFER_SIZE
];
85 readed
= read(fd
, buf
, sizeof(buf
));
87 if (dynar_cat(dest
, buf
, readed
) == -1) {
92 for (zi
= 0; zi
< (size_t)readed
; zi
++) {
93 if (buf
[zi
] == '\n') {
103 if (readed
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
) {
112 * 1 All data succesfully sent
113 * 0 Partial send (no error)
114 * -1 send returned 0,
118 qdevice_heuristics_io_write(int fd
, const struct dynar
*msg
, size_t *already_sent_bytes
)
126 to_send
= dynar_size(msg
) - *already_sent_bytes
;
127 if (to_send
> QDEVICE_HEURISTICS_IO_BUFFER_SIZE
) {
128 to_send
= QDEVICE_HEURISTICS_IO_BUFFER_SIZE
;
131 sent
= write(fd
, dynar_data(msg
) + *already_sent_bytes
,
135 *already_sent_bytes
+= sent
;
137 if (*already_sent_bytes
== dynar_size(msg
)) {
146 if (sent
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
) {