[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / posix_io.c
blob6b49f220f12c0733d20a79a23b3bda28202efc8f
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <gpxe/list.h>
25 #include <gpxe/xfer.h>
26 #include <gpxe/open.h>
27 #include <gpxe/process.h>
28 #include <gpxe/posix_io.h>
30 /** @file
32 * POSIX-like I/O
34 * These functions provide traditional blocking I/O semantics. They
35 * are designed to be used by the PXE TFTP API. Because they block,
36 * they may not be used by most other portions of the gPXE codebase.
39 /** An open file */
40 struct posix_file {
41 /** Reference count for this object */
42 struct refcnt refcnt;
43 /** List of open files */
44 struct list_head list;
45 /** File descriptor */
46 int fd;
47 /** Overall status
49 * Set to -EINPROGRESS while data transfer is in progress.
51 int rc;
52 /** Data transfer interface */
53 struct xfer_interface xfer;
54 /** Current seek position */
55 size_t pos;
56 /** File size */
57 size_t filesize;
58 /** Received data queue */
59 struct list_head data;
62 /** List of open files */
63 static LIST_HEAD ( posix_files );
65 /**
66 * Free open file
68 * @v refcnt Reference counter
70 static void posix_file_free ( struct refcnt *refcnt ) {
71 struct posix_file *file =
72 container_of ( refcnt, struct posix_file, refcnt );
73 struct io_buffer *iobuf;
74 struct io_buffer *tmp;
76 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
77 list_del ( &iobuf->list );
78 free_iob ( iobuf );
80 free ( file );
83 /**
84 * Terminate file data transfer
86 * @v file POSIX file
87 * @v rc Reason for termination
89 static void posix_file_finished ( struct posix_file *file, int rc ) {
90 xfer_nullify ( &file->xfer );
91 xfer_close ( &file->xfer, rc );
92 file->rc = rc;
95 /**
96 * Handle close() event
98 * @v xfer POSIX file data transfer interface
99 * @v rc Reason for close
101 static void posix_file_xfer_close ( struct xfer_interface *xfer, int rc ) {
102 struct posix_file *file =
103 container_of ( xfer, struct posix_file, xfer );
105 posix_file_finished ( file, rc );
109 * Handle deliver_iob() event
111 * @v xfer POSIX file data transfer interface
112 * @v iobuf I/O buffer
113 * @v meta Data transfer metadata
114 * @ret rc Return status code
116 static int
117 posix_file_xfer_deliver_iob ( struct xfer_interface *xfer,
118 struct io_buffer *iobuf,
119 struct xfer_metadata *meta ) {
120 struct posix_file *file =
121 container_of ( xfer, struct posix_file, xfer );
123 /* Keep track of file position solely for the filesize */
124 if ( meta->whence != SEEK_CUR )
125 file->pos = 0;
126 file->pos += meta->offset;
127 if ( file->filesize < file->pos )
128 file->filesize = file->pos;
130 if ( iob_len ( iobuf ) ) {
131 list_add_tail ( &iobuf->list, &file->data );
132 } else {
133 free_iob ( iobuf );
136 return 0;
139 /** POSIX file data transfer interface operations */
140 static struct xfer_interface_operations posix_file_xfer_operations = {
141 .close = posix_file_xfer_close,
142 .vredirect = xfer_vreopen,
143 .window = unlimited_xfer_window,
144 .alloc_iob = default_xfer_alloc_iob,
145 .deliver_iob = posix_file_xfer_deliver_iob,
146 .deliver_raw = xfer_deliver_as_iob,
150 * Identify file by file descriptor
152 * @v fd File descriptor
153 * @ret file Corresponding file, or NULL
155 static struct posix_file * posix_fd_to_file ( int fd ) {
156 struct posix_file *file;
158 list_for_each_entry ( file, &posix_files, list ) {
159 if ( file->fd == fd )
160 return file;
162 return NULL;
166 * Find an available file descriptor
168 * @ret fd File descriptor, or negative error number
170 static int posix_find_free_fd ( void ) {
171 int fd;
173 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
174 if ( ! posix_fd_to_file ( fd ) )
175 return fd;
177 DBG ( "POSIX could not find free file descriptor\n" );
178 return -ENFILE;
182 * Open file
184 * @v uri_string URI string
185 * @ret fd File descriptor, or negative error number
187 int open ( const char *uri_string ) {
188 struct posix_file *file;
189 int fd;
190 int rc;
192 /* Find a free file descriptor to use */
193 fd = posix_find_free_fd();
194 if ( fd < 0 )
195 return fd;
197 /* Allocate and initialise structure */
198 file = zalloc ( sizeof ( *file ) );
199 if ( ! file )
200 return -ENOMEM;
201 ref_init ( &file->refcnt, posix_file_free );
202 file->fd = fd;
203 file->rc = -EINPROGRESS;
204 xfer_init ( &file->xfer, &posix_file_xfer_operations,
205 &file->refcnt );
206 INIT_LIST_HEAD ( &file->data );
208 /* Open URI on data transfer interface */
209 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
210 goto err;
212 /* Wait for open to succeed or fail */
213 while ( list_empty ( &file->data ) ) {
214 step();
215 if ( file->rc == 0 )
216 break;
217 if ( file->rc != -EINPROGRESS ) {
218 rc = file->rc;
219 goto err;
223 /* Add to list of open files. List takes reference ownership. */
224 list_add ( &file->list, &posix_files );
225 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
226 return fd;
228 err:
229 posix_file_finished ( file, rc );
230 ref_put ( &file->refcnt );
231 return rc;
235 * Check file descriptors for readiness
237 * @v readfds File descriptors to check
238 * @v wait Wait until data is ready
239 * @ret nready Number of ready file descriptors
241 int select ( fd_set *readfds, int wait ) {
242 struct posix_file *file;
243 int fd;
245 do {
246 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
247 if ( ! FD_ISSET ( fd, readfds ) )
248 continue;
249 file = posix_fd_to_file ( fd );
250 if ( ! file )
251 return -EBADF;
252 if ( ( list_empty ( &file->data ) ) &&
253 ( file->rc == -EINPROGRESS ) )
254 continue;
255 /* Data is available or status has changed */
256 FD_ZERO ( readfds );
257 FD_SET ( fd, readfds );
258 return 1;
260 step();
261 } while ( wait );
263 return 0;
267 * Read data from file
269 * @v buffer Data buffer
270 * @v offset Starting offset within data buffer
271 * @v len Maximum length to read
272 * @ret len Actual length read, or negative error number
274 * This call is non-blocking; if no data is available to read then
275 * -EWOULDBLOCK will be returned.
277 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
278 struct posix_file *file;
279 struct io_buffer *iobuf;
280 size_t len;
282 /* Identify file */
283 file = posix_fd_to_file ( fd );
284 if ( ! file )
285 return -EBADF;
287 /* Try to fetch more data if none available */
288 if ( list_empty ( &file->data ) )
289 step();
291 /* Dequeue at most one received I/O buffer into user buffer */
292 list_for_each_entry ( iobuf, &file->data, list ) {
293 len = iob_len ( iobuf );
294 if ( len > max_len )
295 len = max_len;
296 copy_to_user ( buffer, offset, iobuf->data, len );
297 iob_pull ( iobuf, len );
298 if ( ! iob_len ( iobuf ) ) {
299 list_del ( &iobuf->list );
300 free_iob ( iobuf );
302 file->pos += len;
303 assert ( len != 0 );
304 return len;
307 /* If file has completed, return (after returning all data) */
308 if ( file->rc != -EINPROGRESS ) {
309 assert ( list_empty ( &file->data ) );
310 return file->rc;
313 /* No data ready and file still in progress; return -WOULDBLOCK */
314 return -EWOULDBLOCK;
318 * Determine file size
320 * @v fd File descriptor
321 * @ret size File size, or negative error number
323 ssize_t fsize ( int fd ) {
324 struct posix_file *file;
326 /* Identify file */
327 file = posix_fd_to_file ( fd );
328 if ( ! file )
329 return -EBADF;
331 return file->filesize;
335 * Close file
337 * @v fd File descriptor
338 * @ret rc Return status code
340 int close ( int fd ) {
341 struct posix_file *file;
343 /* Identify file */
344 file = posix_fd_to_file ( fd );
345 if ( ! file )
346 return -EBADF;
348 /* Terminate data transfer */
349 posix_file_finished ( file, 0 );
351 /* Remove from list of open files and drop reference */
352 list_del ( &file->list );
353 ref_put ( &file->refcnt );
354 return 0;