5#include <cosmos/error/RuntimeError.hxx>
6#include <cosmos/formatting.hxx>
9#include <clues/Tracee.hxx>
10#include <clues/items/fcntl.hxx>
11#include <clues/macros.h>
12#include <clues/private/utils.hxx>
13#include <clues/syscalls/fs.hxx>
15namespace clues::item {
18 BITFLAGS_FORMAT_START(m_flags);
20 BITFLAGS_ADD(FD_CLOEXEC);
22 return BITFLAGS_STR();
26 m_flags = cosmos::FileDescriptor::DescFlags{
valueAs<int>()};
44#pragma push_macro("F_GETLK")
45#pragma push_macro("F_GETLK64")
46#pragma push_macro("F_SETLK")
47#pragma push_macro("F_SETLK64")
48#pragma push_macro("F_SETLKW")
49#pragma push_macro("F_SETLKW64")
57 constexpr int F_GETLK = cosmos::to_integral(Oper::GETLK);
58 constexpr int F_GETLK64 = cosmos::to_integral(Oper::GETLK64);
59 constexpr int F_SETLK = cosmos::to_integral(Oper::SETLK);
60 constexpr int F_SETLK64 = cosmos::to_integral(Oper::SETLK64);
61 constexpr int F_SETLKW = cosmos::to_integral(Oper::SETLKW);
62 constexpr int F_SETLKW64 = cosmos::to_integral(Oper::SETLKW64);
64 switch (cosmos::to_integral(m_op)) {
65 CASE_ENUM_TO_STR(F_DUPFD);
66 CASE_ENUM_TO_STR(F_DUPFD_CLOEXEC);
67 CASE_ENUM_TO_STR(F_GETFD);
68 CASE_ENUM_TO_STR(F_SETFD);
69 CASE_ENUM_TO_STR(F_GETFL);
70 CASE_ENUM_TO_STR(F_SETFL);
71 CASE_ENUM_TO_STR(F_GETLK);
72 CASE_ENUM_TO_STR(F_SETLK);
73 CASE_ENUM_TO_STR(F_SETLKW);
74 CASE_ENUM_TO_STR(F_GETLK64);
75 CASE_ENUM_TO_STR(F_SETLK64);
76 CASE_ENUM_TO_STR(F_SETLKW64);
77 CASE_ENUM_TO_STR(F_OFD_SETLK);
78 CASE_ENUM_TO_STR(F_OFD_SETLKW);
79 CASE_ENUM_TO_STR(F_OFD_GETLK);
80 CASE_ENUM_TO_STR(F_GETOWN);
81 CASE_ENUM_TO_STR(F_SETOWN);
82 CASE_ENUM_TO_STR(F_GETOWN_EX);
83 CASE_ENUM_TO_STR(F_SETOWN_EX);
84 CASE_ENUM_TO_STR(F_GETSIG);
85 CASE_ENUM_TO_STR(F_SETSIG);
86 CASE_ENUM_TO_STR(F_SETLEASE);
87 CASE_ENUM_TO_STR(F_GETLEASE);
88 CASE_ENUM_TO_STR(F_NOTIFY);
89 CASE_ENUM_TO_STR(F_SETPIPE_SZ);
90 CASE_ENUM_TO_STR(F_GETPIPE_SZ);
91 CASE_ENUM_TO_STR(F_ADD_SEALS);
92 CASE_ENUM_TO_STR(F_GET_SEALS);
93 CASE_ENUM_TO_STR(F_GET_RW_HINT);
94 CASE_ENUM_TO_STR(F_SET_RW_HINT);
95 CASE_ENUM_TO_STR(F_GET_FILE_RW_HINT);
96 CASE_ENUM_TO_STR(F_SET_FILE_RW_HINT);
97 default:
return "F_???";
99#pragma pop_macro("F_GETLK")
100#pragma pop_macro("F_GETLK64")
101#pragma pop_macro("F_SETLK")
102#pragma pop_macro("F_SETLK64")
103#pragma pop_macro("F_SETLKW")
104#pragma pop_macro("F_SETLKW64")
118template <
typename OFF_T>
125} __attribute__((packed));
193 cosmos::DeferGuard reset_lock{[
this]() { m_lock.reset(); }};
195 auto assign_data = [
this, &reset_lock](
const auto &
lock) {
197 m_lock = std::make_optional<cosmos::FileLock>(cosmos::FileLock::Type::READ_LOCK);
200 m_lock->l_type =
lock.l_type;
201 m_lock->l_whence =
lock.l_whence;
202 m_lock->l_start =
lock.l_start;
203 m_lock->l_len =
lock.l_len;
204 m_lock->l_pid =
lock.l_pid;
208 auto fetch_lock = [
this, assign_data, &proc]<
typename FLOCK>() {
215 if (
const auto abi =
m_call->abi(); abi != ABI::I386) {
226 if (
const auto sysnr =
m_call->callNr(); sysnr != SystemCallNr::FCNTL64) {
232 fetch_lock.operator()<kernel::flock32>();
247 if (fcntl_sys->operation.isLock64()) {
250 fetch_lock.operator()<kernel::flock64_i386>();
256 fetch_lock.operator()<kernel::flock32>();
262 if (!fcntl_sc || fcntl_sc->operation.operation() != item::FcntlOperation::Oper::GETLK) {
270template <
typename INT>
271static std::string_view lock_type_to_str(INT
type) {
273 CASE_ENUM_TO_STR(F_RDLCK);
274 CASE_ENUM_TO_STR(F_WRLCK);
275 CASE_ENUM_TO_STR(F_UNLCK);
276 default:
return "F_???";
285 auto whence_str = [](
short whence) {
287 CASE_ENUM_TO_STR(SEEK_SET);
288 CASE_ENUM_TO_STR(SEEK_CUR);
289 CASE_ENUM_TO_STR(SEEK_END);
290 default:
return "???";
294 return cosmos::sprintf(
"{l_type=%s, l_whence=%s, l_start=%jd, l_len=%jd, l_pid=%d}",
295 lock_type_to_str(cosmos::to_integral(m_lock->type())).data(),
296 whence_str(cosmos::to_integral(m_lock->whence())),
299 cosmos::to_integral(m_lock->pid())
306 if (pid_or_pgid >= 0) {
308 m_pid = cosmos::ProcessID{pid_or_pgid};
313 m_pgid = cosmos::ProcessGroupID{-pid_or_pgid};
321 return std::to_string(cosmos::to_integral(*m_pid));
323 return std::to_string(cosmos::to_integral(*m_pgid));
331 auto type_str = [](cosmos::FileDescriptor::Owner::Type type) {
332 switch (cosmos::to_integral(type)) {
333 CASE_ENUM_TO_STR(F_OWNER_TID);
334 CASE_ENUM_TO_STR(F_OWNER_PID);
335 CASE_ENUM_TO_STR(F_OWNER_PGRP);
336 default:
return "???";
340 std::stringstream ss;
341 ss <<
"{type=" << type_str(m_owner->type()) <<
", id=" << m_owner->raw()->pid <<
"}";
346 m_owner = cosmos::FileDescriptor::Owner{};
348 if (!proc.
readStruct(asPtr(), *m_owner->raw())) {
354 m_lease = cosmos::FileDescriptor::LeaseType{
valueAs<int>()};
358 return std::string{lock_type_to_str(cosmos::to_integral(m_lease))};
366 BITFLAGS_FORMAT_START(m_settings);
368 BITFLAGS_ADD(DN_ACCESS);
369 BITFLAGS_ADD(DN_MODIFY);
370 BITFLAGS_ADD(DN_CREATE);
371 BITFLAGS_ADD(DN_DELETE);
372 BITFLAGS_ADD(DN_RENAME);
373 BITFLAGS_ADD(DN_ATTRIB);
374 BITFLAGS_ADD(DN_MULTISHOT);
376 return BITFLAGS_STR();
384 BITFLAGS_FORMAT_START(m_flags);
386 BITFLAGS_ADD(F_SEAL_SEAL);
387 BITFLAGS_ADD(F_SEAL_SHRINK);
388 BITFLAGS_ADD(F_SEAL_GROW);
389 BITFLAGS_ADD(F_SEAL_WRITE);
390 BITFLAGS_ADD(F_SEAL_FUTURE_WRITE);
392 return BITFLAGS_STR();
399 uint64_t native_hint;
401 m_hint = Hint{native_hint};
403 m_hint = Hint::LIFE_NOT_SET;
408 switch (cosmos::to_integral(m_hint)) {
409 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_NOT_SET);
410 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_NONE);
411 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_SHORT);
412 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_MEDIUM);
413 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_LONG);
414 CASE_ENUM_TO_STR(RWH_WRITE_LIFE_EXTREME);
415 default:
return "???";
const SystemCall * m_call
The system call context this item part of.
std::string_view m_short_name
A human readable short name for the item, should be one word only.
OTHER valueAs() const
Helper to cast the strongly typed Word m_val to other strong enum types.
std::string_view m_long_name
A human readable longer name for the item.
Base class for traced processes.
bool readStruct(const ForeignPtr addr, T &out) const
Reads a system call struct from the tracee's address space into out.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
std::string str() const override
Returns a human readable string representation of the item.
const std::optional< cosmos::FileLock > & lock() const
Access to the extracted FileLock data.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
void updateData(const Tracee &proc) override
Called upon exit of the system call to update possible out parameters.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
std::string str() const override
Returns a human readable string representation of the item.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.
std::string str() const override
Returns a human readable string representation of the item.
std::string str() const override
Returns a human readable string representation of the item.
void processValue(const Tracee &proc) override
Processes the value stored in m_val acc. to the actual item type.