-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuser.cpp
More file actions
102 lines (85 loc) · 3.2 KB
/
fuser.cpp
File metadata and controls
102 lines (85 loc) · 3.2 KB
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
#include <cstdio>
#include <filesystem>
#include <string>
#include <vector>
#include <sys/stat.h>
#include <signal.h>
#include <cfbox/applet.hpp>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/proc.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "fuser",
.version = CFBOX_VERSION_STRING,
.one_line = "identify processes using files or sockets",
.usage = "fuser [-k] FILE...",
.options = " -k kill processes accessing the file\n"
" -v verbose output",
.extra = "",
};
} // anonymous namespace
auto fuser_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'k', false, "kill"},
cfbox::args::OptSpec{'v', false, "verbose"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool do_kill = parsed.has('k') || parsed.has_long("kill");
bool verbose = parsed.has('v') || parsed.has_long("verbose");
const auto& targets = parsed.positional();
if (targets.empty()) {
CFBOX_ERR("fuser", "no file specified");
return 1;
}
int rc = 1;
for (const auto& target : targets) {
auto target_str = std::string(target);
struct stat target_stat {};
if (stat(target_str.c_str(), &target_stat) != 0) {
CFBOX_ERR("fuser", "cannot stat %s", target_str.c_str());
continue;
}
dev_t target_dev = target_stat.st_dev;
ino_t target_ino = target_stat.st_ino;
std::vector<pid_t> found_pids;
std::error_code ec;
for (const auto& proc_entry : std::filesystem::directory_iterator("/proc", ec)) {
if (!proc_entry.is_directory()) continue;
auto name = proc_entry.path().filename().string();
if (name.empty() || name[0] < '0' || name[0] > '9') continue;
pid_t pid = static_cast<pid_t>(std::strtol(name.c_str(), nullptr, 10));
auto fd_dir = proc_entry.path() / "fd";
for (const auto& fd_entry : std::filesystem::directory_iterator(fd_dir, ec)) {
auto link = std::filesystem::read_symlink(fd_entry.path(), ec);
if (ec) continue;
struct stat fd_stat {};
if (stat(fd_entry.path().c_str(), &fd_stat) != 0) continue;
if (fd_stat.st_dev == target_dev && fd_stat.st_ino == target_ino) {
found_pids.push_back(pid);
break;
}
}
}
if (found_pids.empty()) continue;
rc = 0;
if (verbose) {
std::printf("%s:", target_str.c_str());
for (auto p : found_pids) std::printf(" %d", p);
std::printf("\n");
}
if (do_kill) {
for (auto p : found_pids) {
if (::kill(p, SIGKILL) != 0) {
CFBOX_ERR("fuser", "cannot kill %d", p);
}
}
} else if (!verbose) {
for (auto p : found_pids) std::printf("%d ", p);
std::printf("\n");
}
}
return rc;
}