/* ---------------------------------------------------------------- * atexit_callback * * Backstop to ensure that direct calls of exit() don't mess us up. * * Somebody who was being really uncooperative could call _exit(), * but for that case we have a "dead man switch" that will make the * postmaster treat it as a crash --- see pmsignal.c. * ---------------------------------------------------------------- */ static void atexit_callback(void) { /* Clean up everything that must be cleaned up */ /* ... too bad we don't know the real exit code ... */ proc_exit_prepare(-1); }
/* ---------------------------------------------------------------- * proc_exit * * this function calls all the callbacks registered * for it (to free resources) and then calls exit. * * This should be the only function to call exit(). * -cim 2/6/90 * * Unfortunately, we can't really guarantee that add-on code * obeys the rule of not calling exit() directly. So, while * this is the preferred way out of the system, we also register * an atexit callback that will make sure cleanup happens. * ---------------------------------------------------------------- */ void proc_exit(int code) { pqsignal(SIGALRM, SIG_IGN); /* Clean up everything that must be cleaned up */ proc_exit_prepare(code); #ifdef PROFILE_PID_DIR { /* * If we are profiling ourself then gprof's mcleanup() is about to * write out a profile to ./gmon.out. Since mcleanup() always uses a * fixed file name, each backend will overwrite earlier profiles. To * fix that, we create a separate subdirectory for each backend * (./gprof/pid) and 'cd' to that subdirectory before we exit() - that * forces mcleanup() to write each profile into its own directory. We * end up with something like: $PGDATA/gprof/8829/gmon.out * $PGDATA/gprof/8845/gmon.out ... * * To avoid undesirable disk space bloat, autovacuum workers are * discriminated against: all their gmon.out files go into the same * subdirectory. Without this, an installation that is "just sitting * there" nonetheless eats megabytes of disk space every few seconds. * * Note that we do this here instead of in an on_proc_exit() callback * because we want to ensure that this code executes last - we don't * want to interfere with any other on_proc_exit() callback. For the * same reason, we do not include it in proc_exit_prepare ... so if * you are exiting in the "wrong way" you won't drop your profile in a * nice place. */ char gprofDirName[32]; if (IsAutoVacuumWorkerProcess()) snprintf(gprofDirName, 32, "gprof/avworker"); else snprintf(gprofDirName, 32, "gprof/%d", (int) getpid()); mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO); mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO); chdir(gprofDirName); } #endif elog(DEBUG3, "exit(%d)", code); exit(code); }
static void atexit_callback(int exitstatus, void *arg) { /* Clean up everything that must be cleaned up */ proc_exit_prepare(exitstatus); }