Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain
Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany
This software was partially supported by the
EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
This software was partially supported by the
ADA-FS project under the SPPEXA project funded by the DFG.
This file is part of GekkoFS.
GekkoFS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GekkoFS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GekkoFS. If not, see <https://www.gnu.org/licenses/>.
SPDX-License-Identifier: GPL-3.0-or-later
*/
/* C++ includes */
#include <CLI11/CLI11.hpp>
#include <nlohmann/json.hpp>
#include <memory>
#include <fmt/format.h>
#include <commands.hpp>
#include <reflection.hpp>
#include <serialize.hpp>
#include <binary_buffer.hpp>
/* C includes */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using json = nlohmann::json;
struct syscall_coverage_options {
bool verbose{};
std::string pathname;
REFL_DECL_STRUCT(syscall_coverage_options, REFL_DECL_MEMBER(bool, verbose),
REFL_DECL_MEMBER(std::string, pathname));
};
struct syscall_coverage_output {
int retval;
int errnum;
REFL_DECL_STRUCT(syscall_coverage_output, REFL_DECL_MEMBER(int, retval),
REFL_DECL_MEMBER(int, errnum));
};
void
to_json(json& record, const syscall_coverage_output& out) {
record = serialize(out);
}
void output (const std::string syscall, const int ret, const syscall_coverage_options& opts) {
if(opts.verbose) {
fmt::print(
"syscall_coverage[{}](pathname=\"{}\") = {}, errno: {} [{}]\n",
syscall, opts.pathname, ret, errno, ::strerror(errno));
}
json out = syscall_coverage_output{ret, errno};
fmt::print("{}\n", out.dump(2));
}
void
syscall_coverage_exec(const syscall_coverage_options& opts) {
int fd = ::open(opts.pathname.c_str(), O_RDWR);
if(fd == -1) {
output ("open", fd, opts);
return;
}
// Internal
auto rv = ::faccessat(AT_FDCWD, opts.pathname.c_str(), F_OK, 0);
if(rv < 0) {
output ("faccessat", rv, opts);
return;
}
// External
rv = ::faccessat(AT_FDCWD, "/tmp", F_OK, 0);
if(rv < 0) {
output ("faccessat, external", rv, opts);
return;
}
// lstat
struct stat st;
rv = ::lstat(opts.pathname.c_str(), &st);
if (rv < 0) {
output ("lstat", rv, opts);
return;
}
rv = ::fchmod(fd, 0777);
if (errno != ENOTSUP) {
output ("fchmod", rv, opts);
return;
}
rv = ::fchmodat(AT_FDCWD, opts.pathname.c_str(), 0777, 0);
if (errno != ENOTSUP) {
output ("fchmodat", rv, opts);
return;
}
rv = ::dup3(fd, 0, 0);
if (errno != ENOTSUP) {
output ("dup3", rv, opts);
return;
}
// fcntl
rv = ::fcntl(fd, F_GETFD);
if (rv < 0) {
output ("fcntl, F_GETFD", rv, opts);
return;
}
rv = ::fcntl(fd, F_GETFL);
if (rv < 0 || rv != O_RDWR) {
output ("fcntl, F_GETFL", rv, opts);
return;
}
rv = ::fcntl(fd, F_SETFD, 0);
if (rv < 0) {
output ("fcntl, F_SETFD", rv, opts);
return;
}
rv = ::fcntl(fd, F_SETFL, 0);
if (errno != ENOTSUP) {
output ("fcntl, F_SETFL", rv, opts);
return;
}
rv = 0;
errno = 0;
json out = syscall_coverage_output{(int) rv, errno};
fmt::print("{}\n", out.dump(2));
return;
}
void
syscall_coverage_init(CLI::App& app) {
// Create the option and subcommand objects
auto opts = std::make_shared<syscall_coverage_options>();
auto* cmd = app.add_subcommand(
"syscall_coverage",
"Execute severals syscalls");
// Add options to cmd, binding them to opts
cmd->add_flag("-v,--verbose", opts->verbose,
"Produce human writeable output");
cmd->add_option("pathname", opts->pathname, "File name")
->required()
->type_name("");
cmd->callback([opts]() { syscall_coverage_exec(*opts); });
}