1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

Use /proc for finding fd if available

This commit is contained in:
Matthew Bauer 2020-07-11 19:34:57 +00:00
parent 5501732453
commit 5ebab9c133

View file

@ -44,10 +44,23 @@ struct CmdProcesses : StoreCommand
return std::nullopt;
}
// fuser just checks /proc on Linux, so we could just do that instead of calling an external program
// TODO: do this in C++ on Linux
static int fuser(Path path)
{
if (pathExists("/proc")) {
for (auto & entry : readDirectory("/proc")) {
if (entry.name[0] < '0' || entry.name[0] > '9')
continue;
int pid = std::stoi(entry.name);
if (pathExists(fmt("/proc/%d/fd", pid))) {
for (auto & fd : readDirectory(fmt("/proc/%d/fd", pid))) {
auto path2 = readLink(fmt("/proc/%d/fd/%s", pid, fd.name));
if (path == path2)
return pid;
}
}
}
return -1;
} else {
int fds[2];
if (pipe(fds) == -1)
throw Error("failed to make fuser pipe");
@ -72,6 +85,7 @@ struct CmdProcesses : StoreCommand
ssize_t size = read(fds[0], &buffer, sizeof(buffer));
return std::stoi(std::string(buffer, size));
}
}
void run(ref<Store> store) override
{
@ -98,6 +112,9 @@ struct CmdProcesses : StoreCommand
int pid = fuser(uidPath);
if (pid == -1)
continue;
if (i != dirs.begin())
std::cout << std::endl;