L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
ds_file_impl.h
1/*
2 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#include "ds_file.h"
10
11#include <limits.h>
12#include <sys/ioctl.h>
13
14#include <l4/re/env>
15
16namespace L4Re { namespace Core {
17
18Ds_file::~Ds_file() noexcept
19{
20 if (_addr)
21 L4Re::Env::env()->rm()->detach(l4_addr_t(_addr), 0);
22
23 L4Re::virt_cap_alloc->release(_ds);
24}
25
26int
27Ds_file::fstat(struct stat64 *buf) const noexcept
28{
29 static int fake = 0;
30
31 memset(buf, 0, sizeof(*buf));
32 buf->st_size = _size;
33 buf->st_mode = S_IFREG | 0644;
34 buf->st_dev = _ds.cap();
35 buf->st_ino = ++fake;
36 buf->st_blksize = L4_PAGESIZE;
37 buf->st_blocks = l4_round_page(_size);
38 return 0;
39}
40
41ssize_t
42Ds_file::read_single(const struct iovec *vec, off64_t pos) noexcept
43{
44 // POSIX declares read operations with a length > SSIZE_MAX to not be
45 // portable, so we do not have to support them. This check also ensures that
46 // casting the size_t variable vec->iov_len to an ssize_t does not overflow.
47 if (vec->iov_len > SSIZE_MAX)
48 return -EINVAL;
49
50 off64_t l = vec->iov_len;
51 if (_size - pos < l)
52 l = _size - pos;
53
54 if (l > 0)
55 {
56 Vfs_config::memcpy(vec->iov_base, _addr + pos, l);
57 return l;
58 }
59
60 return 0;
61}
62
63ssize_t
64Ds_file::preadv(const struct iovec *vec, int cnt, off64_t offset) noexcept
65{
66 if (cnt < 0 || offset < 0)
67 return -EINVAL;
68
69 if (!_size)
70 return 0;
71
72 if (!_addr)
73 {
74 // Region manager flags and dataspace cap need to be adjusted according
75 // to file permissions
76 Rm::Flags rm_flags = Rm::F::Search_addr | Rm::F::R;
78 if (_writable)
79 {
80 rm_flags |= Rm::F::W;
81 ds_cap = L4::Ipc::make_cap_rw(_ds);
82 }
83
84 void *file = reinterpret_cast<void*>(L4_PAGESIZE);
85 long err = L4Re::Env::env()->rm()->attach(&file, _size, rm_flags,
86 ds_cap, 0);
87
88 if (err < 0)
89 return err;
90
91 _addr = static_cast<char *>(file);
92 }
93
94 ssize_t l = 0;
95
96 while (cnt > 0)
97 {
98 ssize_t r = read_single(vec, offset);
99
100 // This check also ensures that casting r to a size_t does not cause
101 // overflows if r is negative.
102 if (r < 0)
103 return (l == 0) ? r : l;
104
105 offset += r;
106 l += r;
107
108 if (static_cast<size_t>(r) < vec->iov_len)
109 return l;
110
111 ++vec;
112 --cnt;
113 }
114 return l;
115}
116
117ssize_t
118Ds_file::write_single(const struct iovec *vec, off64_t pos) noexcept
119{
120 // POSIX declares write operations with a length > SSIZE_MAX to not be
121 // portable, so we do not have to support them. This check also ensures that
122 // casting the size_t variable vec->iov_len to an ssize_t does not overflow.
123 if (vec->iov_len > SSIZE_MAX)
124 return -EINVAL;
125
126 // The memcpy should never result in invalid memory accesses! Therefore,
127 // check for overflow, potentially performing a short write.
128 off64_t l = vec->iov_len;
129 if (_size - pos < l)
130 l = _size - pos;
131
132 if (l > 0)
133 {
134 Vfs_config::memcpy(_addr + pos, vec->iov_base, l);
135 return l;
136 }
137
138 // The write operation would happen entirely beyond EOF. For now, we cannot
139 // extend the file, so return an appropriate error code.
140 return -ENOSPC;
141}
142
143ssize_t
144Ds_file::pwritev(const struct iovec *vec, int cnt, off64_t offset) noexcept
145{
146 if (cnt < 0 || offset < 0)
147 return -EINVAL;
148
149 if (! _writable)
150 return -EBADF;
151
152 // Create a mapping if none is established yet
153 if (! _addr)
154 {
155 void *file = reinterpret_cast<void*>(L4_PAGESIZE);
156 long err = L4Re::Env::env()->rm()->attach(&file, _size,
158 L4::Ipc::make_cap_rw(_ds), 0);
159
160 if (err < 0)
161 return err;
162
163 _addr = static_cast<char *>(file);
164 }
165
166 // Copy loop, iterating over all I/O vectors
167 ssize_t l = 0; // No. of bytes copied so far
168 while (cnt > 0)
169 {
170 ssize_t r = write_single(vec, offset);
171
172 // This check also ensures that casting r to a size_t does not cause
173 // overflows if r is negative.
174 if (r < 0)
175 return (l == 0) ? r : l;
176
177 offset += r;
178 l += r;
179
180 // Check for short writes
181 if (static_cast<size_t>(r) < vec->iov_len)
182 return l;
183
184 ++vec;
185 --cnt;
186 }
187
188 return l;
189}
190
191int
192Ds_file::ioctl(unsigned long v, va_list args) noexcept
193{
194 switch (v)
195 {
196 case FIONREAD: // return amount of data still available
197 int *available = va_arg(args, int *);
198 *available = _size - pos();
199 return 0;
200 };
201 return -ENOTTY;
202}
203
204}}
int fstat(struct stat64 *buf) const noexcept override
Get status information for the file.
int ioctl(unsigned long, va_list) noexcept override
The famous IO control.
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:120
Capability type for RPC interfaces (see L4::Cap<T>).
Definition ipc_types:725
Environment interface.
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
Definition consts.h:484
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition consts.h:402
L4Re C++ Interfaces.
Definition cmd_control:14
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
Definition ipc_types:855
@ RW
Readable and writable region.
Definition rm:141
@ Search_addr
Search for a suitable address range.
Definition rm:115
@ R
Readable region.
Definition rm:135
@ W
Writable region.
Definition rm:137
@ Search_addr
Search for a suitable address range.
Definition rm:115