1 /*	$OpenBSD: uthread_writev.c,v 1.10 2004/12/12 21:56:07 brad Exp $	*/
2 /*
3  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: uthread_writev.c,v 1.12 1999/08/28 00:03:55 peter Exp $
34  *
35  */
36 #include <sys/types.h>
37 #include <sys/fcntl.h>
38 #include <sys/uio.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #ifdef _THREAD_SAFE
44 #include <pthread.h>
45 #include "pthread_private.h"
46 
47 ssize_t
writev(int fd,const struct iovec * iov,int iovcnt)48 writev(int fd, const struct iovec * iov, int iovcnt)
49 {
50 	struct pthread	*curthread = _get_curthread();
51 	int	blocking;
52 	int	idx = 0;
53 	int	type;
54 	size_t num = 0;
55 	size_t cnt;
56 	ssize_t n;
57 	ssize_t	ret;
58 	struct iovec liov[20];
59 	struct iovec *p_iov = liov;
60 
61 	/* This is a cancellation point: */
62 	_thread_enter_cancellation_point();
63 
64 	/* Check if the array size exceeds to compiled in size: */
65 	if (iovcnt > (int) (sizeof(liov) / sizeof(struct iovec))) {
66 		/* Allocate memory for the local array: */
67 		if ((p_iov = (struct iovec *)
68 		    malloc(iovcnt * sizeof(struct iovec))) == NULL) {
69 			/* Insufficient memory: */
70 			errno = ENOMEM;
71 			_thread_leave_cancellation_point();
72 			return (-1);
73 		}
74 	}
75 
76 	/* Copy the caller's array so that it can be modified locally: */
77 	memcpy(p_iov,iov,iovcnt * sizeof(struct iovec));
78 
79 	/* Lock the file descriptor for write: */
80 	if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
81 		/* Get the read/write mode type: */
82 		type = _thread_fd_table[fd]->flags & O_ACCMODE;
83 
84 		/* Check if the file is not open for write: */
85 		if (type != O_WRONLY && type != O_RDWR) {
86 			/* File is not open for write: */
87 			errno = EBADF;
88 			_FD_UNLOCK(fd, FD_WRITE);
89 			if (p_iov != liov)
90 				free(p_iov);
91 			_thread_leave_cancellation_point();
92 			return (-1);
93 		}
94 
95 		/* Check if file operations are to block */
96 		blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
97 
98 		/*
99 		 * Loop while no error occurs and until the expected number
100 		 * of bytes are written if performing a blocking write:
101 		 */
102 		while (ret == 0) {
103 			/* Perform a non-blocking write syscall: */
104 			n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx);
105 
106 			/* Check if one or more bytes were written: */
107 			if (n > 0) {
108 				/*
109 				 * Keep a count of the number of bytes
110 				 * written:
111 				 */
112 				num += n;
113 
114 				/*
115 				 * Enter a loop to check if a short write
116 				 * occurred and move the index to the
117 				 * array entry where the short write
118 				 * ended:
119 				 */
120 				cnt = n;
121 				while (cnt > 0 && idx < iovcnt) {
122 					/*
123 					 * If the residual count exceeds
124 					 * the size of this vector, then
125 					 * it was completely written:
126 					 */
127 					if (cnt >= p_iov[idx].iov_len)
128 						/*
129 						 * Decrement the residual
130 						 * count and increment the
131 						 * index to the next array
132 						 * entry:
133 						 */
134 						cnt -= p_iov[idx++].iov_len;
135 					else {
136 						/*
137 						 * This entry was only
138 						 * partially written, so
139 						 * adjust it's length
140 						 * and base pointer ready
141 						 * for the next write:
142 						 */
143 						p_iov[idx].iov_len -= cnt;
144 						p_iov[idx].iov_base =
145 						    (char *)p_iov[idx].iov_base
146 						    + cnt;
147 						cnt = 0;
148 					}
149 				}
150 			} else if (n == 0) {
151 				/*
152 				 * Avoid an infinite loop if the last iov_len is
153 				 * 0.
154 				 */
155 				while (idx < iovcnt && p_iov[idx].iov_len == 0)
156 					idx++;
157 
158 				if (idx == iovcnt) {
159 					ret = num;
160 					break;
161 				}
162 			}
163 
164 			/*
165 			 * If performing a blocking write, check if the
166 			 * write would have blocked or if some bytes
167 			 * were written but there are still more to
168 			 * write:
169 			 */
170 			if (blocking && ((n < 0 && (errno == EWOULDBLOCK ||
171 			    errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) {
172 				curthread->data.fd.fd = fd;
173 				_thread_kern_set_timeout(NULL);
174 
175 				/* Reset the interrupted operation flag: */
176 				curthread->interrupted = 0;
177 
178 				_thread_kern_sched_state(PS_FDW_WAIT,
179 				    __FILE__, __LINE__);
180 
181 				/*
182 				 * Check if the operation was
183 				 * interrupted by a signal
184 				 */
185 				if (curthread->interrupted) {
186 					if (num > 0) {
187 						/* Return partial success: */
188 						ret = num;
189 					} else {
190 						/* Return an error: */
191 						errno = EINTR;
192 						ret = -1;
193 					}
194 				}
195 
196 			/*
197 			 * If performing a non-blocking write,
198 			 * just return whatever the write syscall did:
199 			 */
200 			} else if (!blocking) {
201 				/* A non-blocking call might return zero: */
202 				ret = n;
203 				break;
204 
205 			/*
206 			 * If there was an error, return partial success
207 			 * (if any bytes were written) or else the error:
208 			 */
209 			} else if (n < 0) {
210 				if (num > 0)
211 					ret = num;
212 				else
213 					ret = n;
214 
215 			/* Check if the write has completed: */
216 			} else if (idx == iovcnt)
217 				/* Return the number of bytes written: */
218 				ret = num;
219 		}
220 		_FD_UNLOCK(fd, FD_WRITE);
221 	}
222 
223 	/* If memory was allocated for the array, free it: */
224 	if (p_iov != liov)
225 		free(p_iov);
226 
227 	/* No longer in a cancellation point: */
228 	_thread_leave_cancellation_point();
229 
230 	return (ret);
231 }
232 #endif
233