Navigation Menu

Skip to content

Commit

Permalink
Added atexit handler to clean up Console.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Apr 23, 2015
1 parent 76701eb commit 0a24c5b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions vm/environment.cpp
Expand Up @@ -59,6 +59,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <dirent.h>

#include "missing/setproctitle.h"

Expand Down Expand Up @@ -543,7 +544,31 @@ namespace rubinius {
halt_lock_.init();
}

static char fsapi_path[MAXPATHLEN];

// Last resort cleanup function if normal cleanup does not occur.
static void remove_fsapi_atexit() {
char path[MAXPATHLEN];
struct dirent* entry;
DIR* dir = opendir(fsapi_path);

if(dir != NULL) {
while((entry = readdir(dir)) != NULL) {
if(entry->d_name[0] == '.') continue;

snprintf(path, MAXPATHLEN, "%s/%s", fsapi_path, entry->d_name);
unlink(path);
}

(void)closedir(dir);
}

rmdir(fsapi_path);
}

void Environment::create_fsapi(STATE) {
strncpy(fsapi_path, shared->fsapi_path.c_str(), MAXPATHLEN);

if(mkdir(shared->fsapi_path.c_str(), shared->config.system_fsapi_access) < 0) {
utilities::logger::error("%s: unable to create FSAPI path", strerror(errno));
}
Expand All @@ -560,6 +585,10 @@ namespace rubinius {
}
}

void Environment::atexit() {
remove_fsapi_atexit();
}

void Environment::halt_and_exit(STATE) {
halt(state);
exit(exit_code(state));
Expand Down Expand Up @@ -821,6 +850,9 @@ namespace rubinius {

load_platform_conf(runtime);
boot_vm();

::atexit(remove_fsapi_atexit);

start_finalizer(state);

load_argv(argc_, argv_);
Expand Down
1 change: 1 addition & 0 deletions vm/environment.hpp
Expand Up @@ -119,6 +119,7 @@ namespace rubinius {
void halt(STATE);
void halt_and_exit(STATE);
int exit_code(STATE);
void atexit();

void create_fsapi(STATE);
void remove_fsapi(STATE);
Expand Down
2 changes: 2 additions & 0 deletions vm/signal.cpp
Expand Up @@ -335,6 +335,8 @@ namespace rubinius {
sleep(timeout);
}

signal_thread_->shared().env()->atexit();

#define RBX_ABORT_CALLSTACK_SIZE 128
void* callstack[RBX_ABORT_CALLSTACK_SIZE];
int i, frames = backtrace(callstack, RBX_ABORT_CALLSTACK_SIZE);
Expand Down

0 comments on commit 0a24c5b

Please sign in to comment.