Header And Logo

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

ipc.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * ipc.c
00004  *    POSTGRES inter-process communication definitions.
00005  *
00006  * This file is misnamed, as it no longer has much of anything directly
00007  * to do with IPC.  The functionality here is concerned with managing
00008  * exit-time cleanup for either a postmaster or a backend.
00009  *
00010  *
00011  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00012  * Portions Copyright (c) 1994, Regents of the University of California
00013  *
00014  *
00015  * IDENTIFICATION
00016  *    src/backend/storage/ipc/ipc.c
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 #include "postgres.h"
00021 
00022 #include <signal.h>
00023 #include <unistd.h>
00024 #include <sys/stat.h>
00025 
00026 #include "miscadmin.h"
00027 #ifdef PROFILE_PID_DIR
00028 #include "postmaster/autovacuum.h"
00029 #endif
00030 #include "storage/ipc.h"
00031 #include "tcop/tcopprot.h"
00032 
00033 
00034 /*
00035  * This flag is set during proc_exit() to change ereport()'s behavior,
00036  * so that an ereport() from an on_proc_exit routine cannot get us out
00037  * of the exit procedure.  We do NOT want to go back to the idle loop...
00038  */
00039 bool        proc_exit_inprogress = false;
00040 
00041 /*
00042  * This flag tracks whether we've called atexit() in the current process
00043  * (or in the parent postmaster).
00044  */
00045 static bool atexit_callback_setup = false;
00046 
00047 /* local functions */
00048 static void proc_exit_prepare(int code);
00049 
00050 
00051 /* ----------------------------------------------------------------
00052  *                      exit() handling stuff
00053  *
00054  * These functions are in generally the same spirit as atexit(),
00055  * but provide some additional features we need --- in particular,
00056  * we want to register callbacks to invoke when we are disconnecting
00057  * from a broken shared-memory context but not exiting the postmaster.
00058  *
00059  * Callback functions can take zero, one, or two args: the first passed
00060  * arg is the integer exitcode, the second is the Datum supplied when
00061  * the callback was registered.
00062  * ----------------------------------------------------------------
00063  */
00064 
00065 #define MAX_ON_EXITS 20
00066 
00067 static struct ONEXIT
00068 {
00069     pg_on_exit_callback function;
00070     Datum       arg;
00071 }   on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
00072 
00073 static int  on_proc_exit_index,
00074             on_shmem_exit_index;
00075 
00076 
00077 /* ----------------------------------------------------------------
00078  *      proc_exit
00079  *
00080  *      this function calls all the callbacks registered
00081  *      for it (to free resources) and then calls exit.
00082  *
00083  *      This should be the only function to call exit().
00084  *      -cim 2/6/90
00085  *
00086  *      Unfortunately, we can't really guarantee that add-on code
00087  *      obeys the rule of not calling exit() directly.  So, while
00088  *      this is the preferred way out of the system, we also register
00089  *      an atexit callback that will make sure cleanup happens.
00090  * ----------------------------------------------------------------
00091  */
00092 void
00093 proc_exit(int code)
00094 {
00095     /* Clean up everything that must be cleaned up */
00096     proc_exit_prepare(code);
00097 
00098 #ifdef PROFILE_PID_DIR
00099     {
00100         /*
00101          * If we are profiling ourself then gprof's mcleanup() is about to
00102          * write out a profile to ./gmon.out.  Since mcleanup() always uses a
00103          * fixed file name, each backend will overwrite earlier profiles. To
00104          * fix that, we create a separate subdirectory for each backend
00105          * (./gprof/pid) and 'cd' to that subdirectory before we exit() - that
00106          * forces mcleanup() to write each profile into its own directory.  We
00107          * end up with something like: $PGDATA/gprof/8829/gmon.out
00108          * $PGDATA/gprof/8845/gmon.out ...
00109          *
00110          * To avoid undesirable disk space bloat, autovacuum workers are
00111          * discriminated against: all their gmon.out files go into the same
00112          * subdirectory.  Without this, an installation that is "just sitting
00113          * there" nonetheless eats megabytes of disk space every few seconds.
00114          *
00115          * Note that we do this here instead of in an on_proc_exit() callback
00116          * because we want to ensure that this code executes last - we don't
00117          * want to interfere with any other on_proc_exit() callback.  For the
00118          * same reason, we do not include it in proc_exit_prepare ... so if
00119          * you are exiting in the "wrong way" you won't drop your profile in a
00120          * nice place.
00121          */
00122         char        gprofDirName[32];
00123 
00124         if (IsAutoVacuumWorkerProcess())
00125             snprintf(gprofDirName, 32, "gprof/avworker");
00126         else
00127             snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
00128 
00129         mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
00130         mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
00131         chdir(gprofDirName);
00132     }
00133 #endif
00134 
00135     elog(DEBUG3, "exit(%d)", code);
00136 
00137     exit(code);
00138 }
00139 
00140 /*
00141  * Code shared between proc_exit and the atexit handler.  Note that in
00142  * normal exit through proc_exit, this will actually be called twice ...
00143  * but the second call will have nothing to do.
00144  */
00145 static void
00146 proc_exit_prepare(int code)
00147 {
00148     /*
00149      * Once we set this flag, we are committed to exit.  Any ereport() will
00150      * NOT send control back to the main loop, but right back here.
00151      */
00152     proc_exit_inprogress = true;
00153 
00154     /*
00155      * Forget any pending cancel or die requests; we're doing our best to
00156      * close up shop already.  Note that the signal handlers will not set
00157      * these flags again, now that proc_exit_inprogress is set.
00158      */
00159     InterruptPending = false;
00160     ProcDiePending = false;
00161     QueryCancelPending = false;
00162     /* And let's just make *sure* we're not interrupted ... */
00163     ImmediateInterruptOK = false;
00164     InterruptHoldoffCount = 1;
00165     CritSectionCount = 0;
00166 
00167     /*
00168      * Also clear the error context stack, to prevent error callbacks from
00169      * being invoked by any elog/ereport calls made during proc_exit. Whatever
00170      * context they might want to offer is probably not relevant, and in any
00171      * case they are likely to fail outright after we've done things like
00172      * aborting any open transaction.  (In normal exit scenarios the context
00173      * stack should be empty anyway, but it might not be in the case of
00174      * elog(FATAL) for example.)
00175      */
00176     error_context_stack = NULL;
00177     /* For the same reason, reset debug_query_string before it's clobbered */
00178     debug_query_string = NULL;
00179 
00180     /* do our shared memory exits first */
00181     shmem_exit(code);
00182 
00183     elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
00184          code, on_proc_exit_index);
00185 
00186     /*
00187      * call all the registered callbacks.
00188      *
00189      * Note that since we decrement on_proc_exit_index each time, if a
00190      * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
00191      * invoked again when control comes back here (nor will the
00192      * previously-completed callbacks).  So, an infinite loop should not be
00193      * possible.
00194      */
00195     while (--on_proc_exit_index >= 0)
00196         (*on_proc_exit_list[on_proc_exit_index].function) (code,
00197                                   on_proc_exit_list[on_proc_exit_index].arg);
00198 
00199     on_proc_exit_index = 0;
00200 }
00201 
00202 /* ------------------
00203  * Run all of the on_shmem_exit routines --- but don't actually exit.
00204  * This is used by the postmaster to re-initialize shared memory and
00205  * semaphores after a backend dies horribly.
00206  * ------------------
00207  */
00208 void
00209 shmem_exit(int code)
00210 {
00211     elog(DEBUG3, "shmem_exit(%d): %d callbacks to make",
00212          code, on_shmem_exit_index);
00213 
00214     /*
00215      * call all the registered callbacks.
00216      *
00217      * As with proc_exit(), we remove each callback from the list before
00218      * calling it, to avoid infinite loop in case of error.
00219      */
00220     while (--on_shmem_exit_index >= 0)
00221         (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
00222                                 on_shmem_exit_list[on_shmem_exit_index].arg);
00223 
00224     on_shmem_exit_index = 0;
00225 }
00226 
00227 /* ----------------------------------------------------------------
00228  *      atexit_callback
00229  *
00230  *      Backstop to ensure that direct calls of exit() don't mess us up.
00231  *
00232  * Somebody who was being really uncooperative could call _exit(),
00233  * but for that case we have a "dead man switch" that will make the
00234  * postmaster treat it as a crash --- see pmsignal.c.
00235  * ----------------------------------------------------------------
00236  */
00237 static void
00238 atexit_callback(void)
00239 {
00240     /* Clean up everything that must be cleaned up */
00241     /* ... too bad we don't know the real exit code ... */
00242     proc_exit_prepare(-1);
00243 }
00244 
00245 /* ----------------------------------------------------------------
00246  *      on_proc_exit
00247  *
00248  *      this function adds a callback function to the list of
00249  *      functions invoked by proc_exit().   -cim 2/6/90
00250  * ----------------------------------------------------------------
00251  */
00252 void
00253 on_proc_exit(pg_on_exit_callback function, Datum arg)
00254 {
00255     if (on_proc_exit_index >= MAX_ON_EXITS)
00256         ereport(FATAL,
00257                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
00258                  errmsg_internal("out of on_proc_exit slots")));
00259 
00260     on_proc_exit_list[on_proc_exit_index].function = function;
00261     on_proc_exit_list[on_proc_exit_index].arg = arg;
00262 
00263     ++on_proc_exit_index;
00264 
00265     if (!atexit_callback_setup)
00266     {
00267         atexit(atexit_callback);
00268         atexit_callback_setup = true;
00269     }
00270 }
00271 
00272 /* ----------------------------------------------------------------
00273  *      on_shmem_exit
00274  *
00275  *      this function adds a callback function to the list of
00276  *      functions invoked by shmem_exit().  -cim 2/6/90
00277  * ----------------------------------------------------------------
00278  */
00279 void
00280 on_shmem_exit(pg_on_exit_callback function, Datum arg)
00281 {
00282     if (on_shmem_exit_index >= MAX_ON_EXITS)
00283         ereport(FATAL,
00284                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
00285                  errmsg_internal("out of on_shmem_exit slots")));
00286 
00287     on_shmem_exit_list[on_shmem_exit_index].function = function;
00288     on_shmem_exit_list[on_shmem_exit_index].arg = arg;
00289 
00290     ++on_shmem_exit_index;
00291 
00292     if (!atexit_callback_setup)
00293     {
00294         atexit(atexit_callback);
00295         atexit_callback_setup = true;
00296     }
00297 }
00298 
00299 /* ----------------------------------------------------------------
00300  *      cancel_shmem_exit
00301  *
00302  *      this function removes an entry, if present, from the list of
00303  *      functions to be invoked by shmem_exit().  For simplicity,
00304  *      only the latest entry can be removed.  (We could work harder
00305  *      but there is no need for current uses.)
00306  * ----------------------------------------------------------------
00307  */
00308 void
00309 cancel_shmem_exit(pg_on_exit_callback function, Datum arg)
00310 {
00311     if (on_shmem_exit_index > 0 &&
00312         on_shmem_exit_list[on_shmem_exit_index - 1].function == function &&
00313         on_shmem_exit_list[on_shmem_exit_index - 1].arg == arg)
00314         --on_shmem_exit_index;
00315 }
00316 
00317 /* ----------------------------------------------------------------
00318  *      on_exit_reset
00319  *
00320  *      this function clears all on_proc_exit() and on_shmem_exit()
00321  *      registered functions.  This is used just after forking a backend,
00322  *      so that the backend doesn't believe it should call the postmaster's
00323  *      on-exit routines when it exits...
00324  * ----------------------------------------------------------------
00325  */
00326 void
00327 on_exit_reset(void)
00328 {
00329     on_shmem_exit_index = 0;
00330     on_proc_exit_index = 0;
00331 }