20int main(
int argc,
char** argv)
24 if (strcmp(argv[1],
"all") == 0)
26 else if (strcmp(argv[1],
"named") == 0)
28 else if (strcmp(argv[1],
"unnamed") == 0)
31 printf(
"Usage : clear_basf2_ipc {all|named|unnamed} \n");
32 printf(
"all: all IPCs, named: named IPCs only, unnamed: basf2 internal IPCs only(default)\n");
34 printf(
"\n Cleans up inter-process communication (IPC) resources left behind if basf2 crashes.\n\n");
35 printf(
"Normally, this shouldn't be necessary, but resources may remain after a bad crash or basf2 is killed using SIGKILL.\n");
36 printf(
"Resources currently in use are not touched.\n");
37 printf(
"The return code is set to 1 when uncleaned resources are found, 0 if none are found, and -1 on error.\n");
43 if ((dir = opendir(std::filesystem::temp_directory_path().c_str())) !=
nullptr) {
46 while ((ent = readdir(dir)) !=
nullptr) {
47 if (strncmp(ent->d_name,
"SHM", 3) == 0) {
51 sscanf(ent->d_name,
"SHM%d-SEM%d-%255s", &shmid, &semid, name);
53 int unnamed = strcmp(name,
"UNNAMED");
57 if (mode == 0 || (unnamed == 0 && mode == 2) ||
58 (unnamed != 0 && mode == 1)) {
61 if (shmctl(shmid, IPC_STAT, &shmInfo) != 0) {
62 printf(
"Removing stale file %s\n", ent->d_name);
67 if (shmInfo.shm_nattch != 0) {
68 printf(
"%s/%s still has %ld processes attached, skipped.\n", std::filesystem::temp_directory_path().c_str(), ent->d_name,
73 if (shmctl(shmid, IPC_RMID, (
struct shmid_ds*)
nullptr) == 0) {
74 printf(
"SHM %d deleted. ", shmid);
81 if (mode == 0 || (unnamed == 0 && mode == 2) ||
82 (unnamed != 0 && mode == 1)) {
83 if (semctl(semid, 1, IPC_RMID) == 0) {
84 printf(
"SEM %d deleted. ", semid);
89 if (deleted or stale) {
93 snprintf(strbuf, 1024,
"%s/%s", std::filesystem::temp_directory_path().c_str(), ent->d_name);
96 snprintf(strbuf, 1024,
"%s/%s_%s", std::filesystem::temp_directory_path().c_str(), getenv(
"USER"), name);