source: rtems/cpukit/libfs/src/imfs/imfs_fifo.c

Last change on this file was 8dc651f, checked in by Sebastian Huber <sebastian.huber@…>, on 07/25/22 at 07:52:41

imfs: Add <rtems/imfsimpl.h>

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup IMFS
7 *
8 * @brief FIFO Support
9 */
10
11/*
12 * Author: Wei Shen <cquark@gmail.com>
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <rtems/imfsimpl.h>
41
42#include <sys/filio.h>
43
44#define JNODE2PIPE(_jnode)  ( ((IMFS_fifo_t *)(_jnode))->pipe )
45
46#define LIBIO2PIPE(_iop)  ( JNODE2PIPE((IMFS_jnode_t *)(_iop)->pathinfo.node_access) )
47
48/* Set errno and return -1 if error, else return _err */
49#define IMFS_FIFO_RETURN(_err) \
50do {  \
51  if (_err < 0) \
52    rtems_set_errno_and_return_minus_one(-_err); \
53  return _err; \
54} while (0)
55
56static int IMFS_fifo_open(
57  rtems_libio_t *iop,
58  const char    *pathname,
59  int            oflag,
60  mode_t         mode
61)
62{
63  IMFS_jnode_t *jnode = iop->pathinfo.node_access;
64
65  int err = fifo_open(&JNODE2PIPE(jnode), iop);
66  IMFS_FIFO_RETURN(err);
67}
68
69static int IMFS_fifo_close(
70  rtems_libio_t *iop
71)
72{
73  int err = 0;
74  IMFS_jnode_t *jnode = iop->pathinfo.node_access;
75
76  pipe_release(&JNODE2PIPE(jnode), iop);
77
78  IMFS_FIFO_RETURN(err);
79}
80
81static ssize_t IMFS_fifo_read(
82  rtems_libio_t *iop,
83  void          *buffer,
84  size_t         count
85)
86{
87  IMFS_jnode_t *jnode = iop->pathinfo.node_access;
88
89  int err = pipe_read(JNODE2PIPE(jnode), buffer, count, iop);
90  if (err > 0)
91    IMFS_update_atime(jnode);
92
93  IMFS_FIFO_RETURN(err);
94}
95
96static ssize_t IMFS_fifo_write(
97  rtems_libio_t *iop,
98  const void    *buffer,
99  size_t         count
100)
101{
102  IMFS_jnode_t *jnode = iop->pathinfo.node_access;
103
104  int err = pipe_write(JNODE2PIPE(jnode), buffer, count, iop);
105  if (err > 0) {
106    IMFS_mtime_ctime_update(jnode);
107  }
108
109  IMFS_FIFO_RETURN(err);
110}
111
112static int IMFS_fifo_ioctl(
113  rtems_libio_t   *iop,
114  ioctl_command_t  command,
115  void            *buffer
116)
117{
118  int err;
119
120  if (command == FIONBIO) {
121    if (buffer == NULL)
122      err = -EFAULT;
123    else {
124      if (*(int *)buffer)
125        rtems_libio_iop_flags_set( iop, LIBIO_FLAGS_NO_DELAY );
126      else
127        rtems_libio_iop_flags_clear( iop, LIBIO_FLAGS_NO_DELAY );
128      return 0;
129    }
130  }
131  else
132    err = pipe_ioctl(LIBIO2PIPE(iop), command, buffer, iop);
133
134  IMFS_FIFO_RETURN(err);
135}
136
137static const rtems_filesystem_file_handlers_r IMFS_fifo_handlers = {
138  .open_h = IMFS_fifo_open,
139  .close_h = IMFS_fifo_close,
140  .read_h = IMFS_fifo_read,
141  .write_h = IMFS_fifo_write,
142  .ioctl_h = IMFS_fifo_ioctl,
143  .lseek_h = rtems_filesystem_default_lseek,
144  .fstat_h = IMFS_stat,
145  .ftruncate_h = rtems_filesystem_default_ftruncate,
146  .fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
147  .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
148  .fcntl_h = rtems_filesystem_default_fcntl,
149  .kqfilter_h = rtems_filesystem_default_kqfilter,
150  .mmap_h = rtems_filesystem_default_mmap,
151  .poll_h = rtems_filesystem_default_poll,
152  .readv_h = rtems_filesystem_default_readv,
153  .writev_h = rtems_filesystem_default_writev
154};
155
156const IMFS_mknod_control IMFS_mknod_control_fifo = {
157  {
158    .handlers = &IMFS_fifo_handlers,
159    .node_initialize = IMFS_node_initialize_default,
160    .node_remove = IMFS_node_remove_default,
161    .node_destroy = IMFS_node_destroy_default
162  },
163  .node_size = sizeof( IMFS_fifo_t )
164};
Note: See TracBrowser for help on using the repository browser.