1 /****************************************************************************
2 * drivers/pipes/fifo.c
3 *
4 * Copyright (C) 2008-2009, 2014-2015 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX nor the names of its contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdint.h>
42 #include <errno.h>
43 #include <fs/driver.h>
44 #include "pipe_common.h"
45
46 #if CONFIG_DEV_FIFO_SIZE > 0
47
48 /****************************************************************************
49 * Private Data
50 ****************************************************************************/
51
fifo_map(struct file * filep,LosVmMapRegion * region)52 static ssize_t fifo_map(struct file* filep, LosVmMapRegion *region)
53 {
54 PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__);
55 return 0;
56 }
57
58 static const struct file_operations_vfs fifo_fops =
59 {
60 .open = pipecommon_open, /* open */
61 .close = pipecommon_close, /* close */
62 .read = pipecommon_read, /* read */
63 .write = pipecommon_write, /* write */
64 .seek = NULL, /* seek */
65 .ioctl = NULL, /* ioctl */
66 .mmap = fifo_map, /* mmap */
67 .poll = NULL, /* poll */
68 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
69 .unlink = pipecommon_unlink, /* unlink */
70 #endif
71 };
72
73 /****************************************************************************
74 * Public Functions
75 ****************************************************************************/
76
77 /****************************************************************************
78 * Name: mkfifo2
79 *
80 * Description:
81 * mkfifo() makes a FIFO device driver file with name 'pathname.' Unlike
82 * Linux, a NuttX FIFO is not a special file type but simply a device
83 * driver instance. 'mode' specifies the FIFO's permissions.
84 *
85 * Once the FIFO has been created by mkfifo(), any thread can open it for
86 * reading or writing, in the same way as an ordinary file. However, it
87 * must have been opened from both reading and writing before input or
88 * output can be performed. This FIFO implementation will block all
89 * attempts to open a FIFO read-only until at least one thread has opened
90 * the FIFO for writing.
91 *
92 * If all threads that write to the FIFO have closed, subsequent calls to
93 * read() on the FIFO will return 0 (end-of-file).
94 *
95 * NOTE: mkfifo2 is a special, non-standard, NuttX-only interface. Since
96 * the NuttX FIFOs are based in in-memory, circular buffers, the ability
97 * to control the size of those buffers is critical for system tuning.
98 *
99 * Input Parameters:
100 * pathname - The full path to the FIFO instance to attach to or to create
101 * (if not already created).
102 * mode - Ignored for now
103 * bufsize - The size of the in-memory, circular buffer in bytes.
104 *
105 * Returned Value:
106 * 0 is returned on success; otherwise, -1 is returned with errno set
107 * appropriately.
108 *
109 ****************************************************************************/
110
mkfifo(const char * pathname,mode_t mode)111 int mkfifo(const char *pathname, mode_t mode)
112 {
113 struct pipe_dev_s *dev = NULL;
114 int ret;
115 size_t bufsize = 1024;
116
117 if (mode > 0777)
118 {
119 return -EINVAL;
120 }
121
122 if (pathname == NULL)
123 {
124 return -EINVAL;
125 }
126
127 if (strlen(pathname) > PATH_MAX)
128 {
129 return -EINVAL;
130 }
131
132 /* Allocate and initialize a new device structure instance */
133
134 dev = pipecommon_allocdev(bufsize, pathname);
135 if (!dev)
136 {
137 return -ENOMEM;
138 }
139
140 ret = register_driver(pathname, &fifo_fops, mode, (void *)dev);
141 if (ret != 0)
142 {
143 pipecommon_freedev(dev);
144 }
145
146 return ret;
147 }
148
149 #endif /* CONFIG_DEV_FIFO_SIZE > 0 */
150