Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions | Variables

ipc.c File Reference

#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include "miscadmin.h"
#include "storage/ipc.h"
#include "tcop/tcopprot.h"
Include dependency graph for ipc.c:

Go to the source code of this file.

Data Structures

struct  ONEXIT

Defines

#define MAX_ON_EXITS   20

Functions

static void proc_exit_prepare (int code)
void proc_exit (int code)
void shmem_exit (int code)
static void atexit_callback (void)
void on_proc_exit (pg_on_exit_callback function, Datum arg)
void on_shmem_exit (pg_on_exit_callback function, Datum arg)
void cancel_shmem_exit (pg_on_exit_callback function, Datum arg)
void on_exit_reset (void)

Variables

bool proc_exit_inprogress = false
static bool atexit_callback_setup = false
static struct ONEXIT on_proc_exit_list [MAX_ON_EXITS]
static struct ONEXIT on_shmem_exit_list [MAX_ON_EXITS]
static int on_proc_exit_index
static int on_shmem_exit_index

Define Documentation

#define MAX_ON_EXITS   20

Definition at line 65 of file ipc.c.

Referenced by on_proc_exit(), and on_shmem_exit().


Function Documentation

static void atexit_callback ( void   )  [static]

Definition at line 238 of file ipc.c.

References proc_exit_prepare().

Referenced by on_proc_exit(), and on_shmem_exit().

{
    /* Clean up everything that must be cleaned up */
    /* ... too bad we don't know the real exit code ... */
    proc_exit_prepare(-1);
}

void cancel_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)
void on_exit_reset ( void   ) 

Definition at line 327 of file ipc.c.

References on_proc_exit_index, and on_shmem_exit_index.

void on_proc_exit ( pg_on_exit_callback  function,
Datum  arg 
)
void on_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)
void proc_exit ( int  code  ) 

Definition at line 93 of file ipc.c.

References arg, DEBUG3, elog, ONEXIT::function, IsAutoVacuumWorkerProcess(), mkdir, on_proc_exit_index, on_proc_exit_list, proc_exit_prepare(), S_IRWXG, S_IRWXO, shmem_exit(), and snprintf().

{
    /* 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 proc_exit_prepare ( int  code  )  [static]

Definition at line 146 of file ipc.c.

References arg, CritSectionCount, DEBUG3, debug_query_string, elog, error_context_stack, ONEXIT::function, ImmediateInterruptOK, InterruptHoldoffCount, InterruptPending, on_proc_exit_index, on_proc_exit_list, proc_exit_inprogress, ProcDiePending, QueryCancelPending, and shmem_exit().

Referenced by atexit_callback(), and proc_exit().

{
    /*
     * Once we set this flag, we are committed to exit.  Any ereport() will
     * NOT send control back to the main loop, but right back here.
     */
    proc_exit_inprogress = true;

    /*
     * Forget any pending cancel or die requests; we're doing our best to
     * close up shop already.  Note that the signal handlers will not set
     * these flags again, now that proc_exit_inprogress is set.
     */
    InterruptPending = false;
    ProcDiePending = false;
    QueryCancelPending = false;
    /* And let's just make *sure* we're not interrupted ... */
    ImmediateInterruptOK = false;
    InterruptHoldoffCount = 1;
    CritSectionCount = 0;

    /*
     * Also clear the error context stack, to prevent error callbacks from
     * being invoked by any elog/ereport calls made during proc_exit. Whatever
     * context they might want to offer is probably not relevant, and in any
     * case they are likely to fail outright after we've done things like
     * aborting any open transaction.  (In normal exit scenarios the context
     * stack should be empty anyway, but it might not be in the case of
     * elog(FATAL) for example.)
     */
    error_context_stack = NULL;
    /* For the same reason, reset debug_query_string before it's clobbered */
    debug_query_string = NULL;

    /* do our shared memory exits first */
    shmem_exit(code);

    elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
         code, on_proc_exit_index);

    /*
     * call all the registered callbacks.
     *
     * Note that since we decrement on_proc_exit_index each time, if a
     * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
     * invoked again when control comes back here (nor will the
     * previously-completed callbacks).  So, an infinite loop should not be
     * possible.
     */
    while (--on_proc_exit_index >= 0)
        (*on_proc_exit_list[on_proc_exit_index].function) (code,
                                  on_proc_exit_list[on_proc_exit_index].arg);

    on_proc_exit_index = 0;
}

void shmem_exit ( int  code  ) 

Definition at line 209 of file ipc.c.

References arg, DEBUG3, elog, ONEXIT::function, on_shmem_exit_index, and on_shmem_exit_list.

{
    elog(DEBUG3, "shmem_exit(%d): %d callbacks to make",
         code, on_shmem_exit_index);

    /*
     * call all the registered callbacks.
     *
     * As with proc_exit(), we remove each callback from the list before
     * calling it, to avoid infinite loop in case of error.
     */
    while (--on_shmem_exit_index >= 0)
        (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
                                on_shmem_exit_list[on_shmem_exit_index].arg);

    on_shmem_exit_index = 0;
}


Variable Documentation

bool atexit_callback_setup = false [static]

Definition at line 45 of file ipc.c.

Referenced by on_proc_exit(), and on_shmem_exit().

int on_proc_exit_index [static]

Definition at line 73 of file ipc.c.

Referenced by on_exit_reset(), on_proc_exit(), and proc_exit_prepare().

struct ONEXIT on_proc_exit_list[MAX_ON_EXITS] [static]

Referenced by on_proc_exit(), and proc_exit_prepare().

int on_shmem_exit_index [static]

Definition at line 73 of file ipc.c.

Referenced by cancel_shmem_exit(), on_exit_reset(), on_shmem_exit(), and shmem_exit().

struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS] [static]