Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:17

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup IMFS
0007  *
0008  * @brief FIFO Support
0009  */
0010 
0011 /*
0012  * Author: Wei Shen <cquark@gmail.com>
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <rtems/imfsimpl.h>
0041 
0042 #include <sys/filio.h>
0043 
0044 #define JNODE2PIPE(_jnode)  ( ((IMFS_fifo_t *)(_jnode))->pipe )
0045 
0046 #define LIBIO2PIPE(_iop)  ( JNODE2PIPE((IMFS_jnode_t *)(_iop)->pathinfo.node_access) )
0047 
0048 /* Set errno and return -1 if error, else return _err */
0049 #define IMFS_FIFO_RETURN(_err) \
0050 do {  \
0051   if (_err < 0) \
0052     rtems_set_errno_and_return_minus_one(-_err); \
0053   return _err; \
0054 } while (0)
0055 
0056 static int IMFS_fifo_open(
0057   rtems_libio_t *iop,
0058   const char    *pathname,
0059   int            oflag,
0060   mode_t         mode
0061 )
0062 {
0063   IMFS_jnode_t *jnode = iop->pathinfo.node_access;
0064 
0065   int err = fifo_open(&JNODE2PIPE(jnode), iop);
0066   IMFS_FIFO_RETURN(err);
0067 }
0068 
0069 static int IMFS_fifo_close(
0070   rtems_libio_t *iop
0071 )
0072 {
0073   int err = 0;
0074   IMFS_jnode_t *jnode = iop->pathinfo.node_access;
0075 
0076   pipe_release(&JNODE2PIPE(jnode), iop);
0077 
0078   IMFS_FIFO_RETURN(err);
0079 }
0080 
0081 static ssize_t IMFS_fifo_read(
0082   rtems_libio_t *iop,
0083   void          *buffer,
0084   size_t         count
0085 )
0086 {
0087   IMFS_jnode_t *jnode = iop->pathinfo.node_access;
0088 
0089   int err = pipe_read(JNODE2PIPE(jnode), buffer, count, iop);
0090   if (err > 0)
0091     IMFS_update_atime(jnode);
0092 
0093   IMFS_FIFO_RETURN(err);
0094 }
0095 
0096 static ssize_t IMFS_fifo_write(
0097   rtems_libio_t *iop,
0098   const void    *buffer,
0099   size_t         count
0100 )
0101 {
0102   IMFS_jnode_t *jnode = iop->pathinfo.node_access;
0103 
0104   int err = pipe_write(JNODE2PIPE(jnode), buffer, count, iop);
0105   if (err > 0) {
0106     IMFS_mtime_ctime_update(jnode);
0107   }
0108 
0109   IMFS_FIFO_RETURN(err);
0110 }
0111 
0112 static int IMFS_fifo_ioctl(
0113   rtems_libio_t   *iop,
0114   ioctl_command_t  command,
0115   void            *buffer
0116 )
0117 {
0118   int err;
0119 
0120   if (command == FIONBIO) {
0121     if (buffer == NULL)
0122       err = -EFAULT;
0123     else {
0124       if (*(int *)buffer)
0125         rtems_libio_iop_flags_set( iop, LIBIO_FLAGS_NO_DELAY );
0126       else
0127         rtems_libio_iop_flags_clear( iop, LIBIO_FLAGS_NO_DELAY );
0128       return 0;
0129     }
0130   }
0131   else
0132     err = pipe_ioctl(LIBIO2PIPE(iop), command, buffer, iop);
0133 
0134   IMFS_FIFO_RETURN(err);
0135 }
0136 
0137 static const rtems_filesystem_file_handlers_r IMFS_fifo_handlers = {
0138   .open_h = IMFS_fifo_open,
0139   .close_h = IMFS_fifo_close,
0140   .read_h = IMFS_fifo_read,
0141   .write_h = IMFS_fifo_write,
0142   .ioctl_h = IMFS_fifo_ioctl,
0143   .lseek_h = rtems_filesystem_default_lseek,
0144   .fstat_h = IMFS_stat,
0145   .ftruncate_h = rtems_filesystem_default_ftruncate,
0146   .fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
0147   .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
0148   .fcntl_h = rtems_filesystem_default_fcntl,
0149   .kqfilter_h = rtems_filesystem_default_kqfilter,
0150   .mmap_h = rtems_filesystem_default_mmap,
0151   .poll_h = rtems_filesystem_default_poll,
0152   .readv_h = rtems_filesystem_default_readv,
0153   .writev_h = rtems_filesystem_default_writev
0154 };
0155 
0156 const IMFS_mknod_control IMFS_mknod_control_fifo = {
0157   {
0158     .handlers = &IMFS_fifo_handlers,
0159     .node_initialize = IMFS_node_initialize_default,
0160     .node_remove = IMFS_node_remove_default,
0161     .node_destroy = IMFS_node_destroy_default
0162   },
0163   .node_size = sizeof( IMFS_fifo_t )
0164 };