1 /****************************************************************************
2 * drivers/pipes/fifo.c
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership. The
7 * ASF licenses this file to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations
17 * under the License.
18 *
19 ****************************************************************************/
20
21 /****************************************************************************
22 * Included Files
23 ****************************************************************************/
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <stdint.h>
27 #include <errno.h>
28 #include <fs/driver.h>
29 #include "pipe_common.h"
30
31 #if CONFIG_DEV_FIFO_SIZE > 0
32
33 /****************************************************************************
34 * Private Data
35 ****************************************************************************/
36
fifo_map(struct file * filep,LosVmMapRegion * region)37 static ssize_t fifo_map(struct file* filep, LosVmMapRegion *region)
38 {
39 PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__);
40 return 0;
41 }
42
43 static const struct file_operations_vfs fifo_fops =
44 {
45 .open = pipecommon_open, /* open */
46 .close = pipecommon_close, /* close */
47 .read = pipecommon_read, /* read */
48 .write = pipecommon_write, /* write */
49 .seek = NULL, /* seek */
50 .ioctl = NULL, /* ioctl */
51 .mmap = fifo_map, /* mmap */
52 .poll = NULL, /* poll */
53 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
54 .unlink = pipecommon_unlink, /* unlink */
55 #endif
56 };
57
58 /****************************************************************************
59 * Public Functions
60 ****************************************************************************/
61
62 /****************************************************************************
63 * Name: mkfifo2
64 *
65 * Description:
66 * mkfifo() makes a FIFO device driver file with name 'pathname.' Unlike
67 * Linux, a NuttX FIFO is not a special file type but simply a device
68 * driver instance. 'mode' specifies the FIFO's permissions.
69 *
70 * Once the FIFO has been created by mkfifo(), any thread can open it for
71 * reading or writing, in the same way as an ordinary file. However, it
72 * must have been opened from both reading and writing before input or
73 * output can be performed. This FIFO implementation will block all
74 * attempts to open a FIFO read-only until at least one thread has opened
75 * the FIFO for writing.
76 *
77 * If all threads that write to the FIFO have closed, subsequent calls to
78 * read() on the FIFO will return 0 (end-of-file).
79 *
80 * NOTE: mkfifo2 is a special, non-standard, NuttX-only interface. Since
81 * the NuttX FIFOs are based in in-memory, circular buffers, the ability
82 * to control the size of those buffers is critical for system tuning.
83 *
84 * Input Parameters:
85 * pathname - The full path to the FIFO instance to attach to or to create
86 * (if not already created).
87 * mode - Ignored for now
88 * bufsize - The size of the in-memory, circular buffer in bytes.
89 *
90 * Returned Value:
91 * 0 is returned on success; otherwise, -1 is returned with errno set
92 * appropriately.
93 *
94 ****************************************************************************/
95
mkfifo(const char * pathname,mode_t mode)96 int mkfifo(const char *pathname, mode_t mode)
97 {
98 struct pipe_dev_s *dev = NULL;
99 int ret;
100 size_t bufsize = 1024;
101
102 if (mode > 0777)
103 {
104 return -EINVAL;
105 }
106
107 if (pathname == NULL)
108 {
109 return -EINVAL;
110 }
111
112 if (strlen(pathname) > PATH_MAX)
113 {
114 return -EINVAL;
115 }
116
117 /* Allocate and initialize a new device structure instance */
118
119 dev = pipecommon_allocdev(bufsize, pathname);
120 if (!dev)
121 {
122 return -ENOMEM;
123 }
124
125 ret = register_driver(pathname, &fifo_fops, mode, (void *)dev);
126 if (ret != 0)
127 {
128 pipecommon_freedev(dev);
129 }
130
131 return ret;
132 }
133
134 #endif /* CONFIG_DEV_FIFO_SIZE > 0 */
135