L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ro_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 "ro_file.h"
10
11#include <sys/ioctl.h>
12
13#include <l4/re/env>
14
15namespace L4Re { namespace Core {
16
17Ro_file::~Ro_file() noexcept
18{
19 if (_addr)
20 L4Re::Env::env()->rm()->detach(l4_addr_t(_addr), 0);
21
22 L4Re::virt_cap_alloc->release(_ds);
23}
24
25int
26Ro_file::fstat64(struct stat64 *buf) const noexcept
27{
28 static int fake = 0;
29
30 memset(buf, 0, sizeof(*buf));
31 buf->st_size = _size;
32 buf->st_mode = S_IFREG | 0644;
33 buf->st_dev = _ds.cap();
34 buf->st_ino = ++fake;
35 buf->st_blksize = L4_PAGESIZE;
36 buf->st_blocks = l4_round_page(_size);
37 return 0;
38}
39
40ssize_t
41Ro_file::read_single(const struct iovec *vec, off64_t pos) noexcept
42{
43 off64_t l = vec->iov_len;
44 if (_size - pos < l)
45 l = _size - pos;
46
47 if (l > 0)
48 {
49 Vfs_config::memcpy(vec->iov_base, _addr + pos, l);
50 return l;
51 }
52
53 return 0;
54}
55
56ssize_t
57Ro_file::preadv(const struct iovec *vec, int cnt, off64_t offset) noexcept
58{
59 if (!_addr)
60 {
61 void const *file = reinterpret_cast<void*>(L4_PAGESIZE);
62 long err = L4Re::Env::env()->rm()->attach(&file, _size,
64 _ds, 0);
65
66 if (err < 0)
67 return err;
68
69 _addr = static_cast<char const *>(file);
70 }
71
72 ssize_t l = 0;
73
74 while (cnt > 0)
75 {
76 ssize_t r = read_single(vec, offset);
77 offset += r;
78 l += r;
79
80 if (static_cast<size_t>(r) < vec->iov_len)
81 return l;
82
83 ++vec;
84 --cnt;
85 }
86 return l;
87}
88
89ssize_t
90Ro_file::pwritev(const struct iovec *, int, off64_t) noexcept
91{
92 return -EROFS;
93}
94
95int
96Ro_file::ioctl(unsigned long v, va_list args) noexcept
97{
98 switch (v)
99 {
100 case FIONREAD: // return amount of data still available
101 int *available = va_arg(args, int *);
102 *available = _size - pos();
103 return 0;
104 };
105 return -ENOTTY;
106}
107
108}}
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:95
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:119
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:477
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition consts.h:395
L4Re C++ Interfaces.
Definition cmd_control:14
@ R
Readable region.
Definition rm:133
@ Search_addr
Search for a suitable address range.
Definition rm:114