1 | /* |
---|
2 | * writev() - POSIX 1003.1 - Read a Vector |
---|
3 | * |
---|
4 | * OpenGroup URL: |
---|
5 | * |
---|
6 | * http://www.opengroup.org/onlinepubs/009695399/functions/writev.html |
---|
7 | * |
---|
8 | * COPYRIGHT (c) 1989-2007. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/uio.h> |
---|
24 | |
---|
25 | #include <rtems/libio_.h> |
---|
26 | #include <rtems/seterr.h> |
---|
27 | |
---|
28 | ssize_t writev( |
---|
29 | int fd, |
---|
30 | const struct iovec *iov, |
---|
31 | int iovcnt |
---|
32 | ) |
---|
33 | { |
---|
34 | ssize_t total; |
---|
35 | int v; |
---|
36 | int bytes; |
---|
37 | rtems_libio_t *iop; |
---|
38 | ssize_t old; |
---|
39 | bool all_zeros; |
---|
40 | |
---|
41 | rtems_libio_check_fd( fd ); |
---|
42 | iop = rtems_libio_iop( fd ); |
---|
43 | rtems_libio_check_is_open( iop ); |
---|
44 | rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE ); |
---|
45 | |
---|
46 | /* |
---|
47 | * Argument validation on IO vector |
---|
48 | */ |
---|
49 | if ( !iov ) |
---|
50 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
51 | |
---|
52 | if ( iovcnt <= 0 ) |
---|
53 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
54 | |
---|
55 | if ( iovcnt > IOV_MAX ) |
---|
56 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
57 | |
---|
58 | if ( !iop->handlers->write_h ) |
---|
59 | rtems_set_errno_and_return_minus_one( ENOTSUP ); |
---|
60 | |
---|
61 | /* |
---|
62 | * OpenGroup says that you are supposed to return EINVAL if the |
---|
63 | * sum of the iov_len values in the iov array would overflow a |
---|
64 | * ssize_t. |
---|
65 | * |
---|
66 | * Also we would like to ensure that no IO is performed if there |
---|
67 | * are obvious errors in the iovec. So this extra loop ensures |
---|
68 | * that we do not do anything if there is an argument error. |
---|
69 | * |
---|
70 | * In addition,the OpenGroup specification says that if all the |
---|
71 | * iov_len entries are zero, then the call has no effect. So |
---|
72 | * this loop does that check as well and sets "all-zero" appropriately. |
---|
73 | * The variable "all_zero" is used as an early exit point before |
---|
74 | * entering the write loop. |
---|
75 | */ |
---|
76 | all_zeros = true; |
---|
77 | for ( old=0, total=0, v=0 ; v < iovcnt ; v++ ) { |
---|
78 | |
---|
79 | if ( !iov[v].iov_base ) |
---|
80 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
81 | |
---|
82 | if ( iov[v].iov_len < 0 ) |
---|
83 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
84 | |
---|
85 | if ( iov[v].iov_len ) |
---|
86 | all_zeros = false; |
---|
87 | |
---|
88 | /* check for wrap */ |
---|
89 | old = total; |
---|
90 | total += iov[v].iov_len; |
---|
91 | if ( total < old || total > SSIZE_MAX ) |
---|
92 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * A writev with all zeros is supposed to have no effect per OpenGroup. |
---|
97 | */ |
---|
98 | if ( all_zeros == true ) { |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | * Now process the writev(). |
---|
104 | */ |
---|
105 | for ( total=0, v=0 ; v < iovcnt ; v++ ) { |
---|
106 | /* all zero lengths has no effect */ |
---|
107 | if ( iov[v].iov_len == 0 ) |
---|
108 | continue; |
---|
109 | |
---|
110 | bytes = (*iop->handlers->write_h)( iop, iov[v].iov_base, iov[v].iov_len ); |
---|
111 | |
---|
112 | if ( bytes < 0 ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | if ( bytes > 0 ) { |
---|
116 | iop->offset += bytes; |
---|
117 | total += bytes; |
---|
118 | } |
---|
119 | |
---|
120 | if (bytes != iov[ v ].iov_len) |
---|
121 | break; |
---|
122 | } |
---|
123 | |
---|
124 | return total; |
---|
125 | } |
---|
126 | |
---|