Skip to content

Commit

Permalink
Only when running in Cygwin, auto-wipe shared memory files when the c…
Browse files Browse the repository at this point in the history
…ontroller does its first boot, but skip on reboots and updates
  • Loading branch information
Thulinma committed Jul 29, 2024
1 parent d5aac19 commit 7a3fd0c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ static bool checkSerial(const std::string &ser){
#endif
#endif

void Util::Config::wipeShm(){
DIR *d = opendir("/dev/shm");
char fileName[300];
struct dirent *dp;
uint64_t deleted = 0;
if (d){
do{
errno = 0;
if ((dp = readdir(d))){
if (strstr(dp->d_name, "Mst")){
snprintf(fileName, sizeof(fileName), "/dev/shm/%s", dp->d_name);
unlink(fileName);
++deleted;
}
}
}while (dp != NULL);
closedir(d);
}
if (deleted){WARN_MSG("Wiped %" PRIu64 " shared memory file(s)", deleted);}
}

Util::Config::Config(){
// global options here
vals["debug"]["long"] = "debug";
Expand Down
1 change: 1 addition & 0 deletions lib/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Util{

public:
static void setMutexAborter(void * mutex);
static void wipeShm();
// variables
static bool is_active; ///< Set to true by activate(), set to false by the signal handler.
static bool is_restarting; ///< Set to true when restarting, set to false on boot.
Expand Down
11 changes: 11 additions & 0 deletions src/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ int main_loop(int argc, char **argv){
}
setenv("MIST_CONTROL", "1", 0); // Signal in the environment that the controller handles all children
}

#ifdef __CYGWIN__
// Wipe shared memory, unless NO_WIPE_SHM is set
if (!getenv("NO_WIPE_SHM")){
Util::Config::wipeShm();
setenv("NO_WIPE_SHM", "1", 1);
}
#endif

Controller::readConfigFromDisk();
Controller::writeConfig();
Expand Down Expand Up @@ -642,6 +650,9 @@ int main(int argc, char **argv){
return main_loop(argc, argv);
}
Util::Procs::fork_complete();
#ifdef __CYGWIN__
setenv("NO_WIPE_SHM", "1", 1);
#endif
if (pid == -1){
FAIL_MSG("Unable to spawn controller process!");
return 2;
Expand Down

0 comments on commit 7a3fd0c

Please sign in to comment.