Files
FEX-Emu--FEX/unittests/Utilities/DeleteOldSHMRegions.cpp
T
Paulo Matos 905aa935f5 Reformat until fixed-point
Followup to 2b4ec88dae.
Some files needed a couple of calls to clang-format 16.0.6 to
reach a fixed point.
2024-04-15 09:40:00 +02:00

45 lines
1.4 KiB
C++

#include <cstdint>
#include <fstream>
#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>
static bool ctime_is_old(uint64_t ctime) {
time_t curtime;
time(&curtime);
// If it is older than ten minutes.
return (curtime - ctime) > 600;
}
int main() {
// clang-format off
// key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap
// 0 360448 777 4096 165187 165187 0 1002 1002 1002 1002 1679659857 1679659857 1679659857 4096 0
// 0 32769 777 4096 153814 153814 0 1002 1002 1002 1002 1676490841 1676490841 1676490841 0 4096
// clang-format on
std::ifstream fs {"/proc/sysvipc/shm", std::fstream::binary};
std::string Line;
// Remove first line
std::getline(fs, Line);
const auto current_uid = getuid();
while (std::getline(fs, Line)) {
if (fs.eof()) {
break;
}
int shmid;
int uid;
int attach;
uint64_t ctime;
if (sscanf(Line.c_str(), "%*d %d %*d %*d %*d %*d %d %d %*d %*d %*d %*d %*d %ld", &shmid, &attach, &uid, &ctime) == 4) {
// If the UID matches and nothing is attached AND it is old then delete it.
if (uid == current_uid && attach == 0 && ctime_is_old(ctime)) {
shmctl(shmid, IPC_RMID, nullptr);
}
}
}
return 0;
}