~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/backend/storage/ipc/ipc.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * ipc.c
 
4
 *        POSTGRES inter-process communication definitions.
 
5
 *
 
6
 * This file is misnamed, as it no longer has much of anything directly
 
7
 * to do with IPC.      The functionality here is concerned with managing
 
8
 * exit-time cleanup for either a postmaster or a backend.
 
9
 *
 
10
 *
 
11
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
12
 * Portions Copyright (c) 1994, Regents of the University of California
 
13
 *
 
14
 *
 
15
 * IDENTIFICATION
 
16
 *        src/backend/storage/ipc/ipc.c
 
17
 *
 
18
 *-------------------------------------------------------------------------
 
19
 */
 
20
#include "postgres.h"
 
21
 
 
22
#include <signal.h>
 
23
#include <unistd.h>
 
24
#include <sys/stat.h>
 
25
 
 
26
#include "miscadmin.h"
 
27
#ifdef PROFILE_PID_DIR
 
28
#include "postmaster/autovacuum.h"
 
29
#endif
 
30
#include "storage/ipc.h"
 
31
#include "tcop/tcopprot.h"
 
32
 
 
33
 
 
34
/*
 
35
 * This flag is set during proc_exit() to change ereport()'s behavior,
 
36
 * so that an ereport() from an on_proc_exit routine cannot get us out
 
37
 * of the exit procedure.  We do NOT want to go back to the idle loop...
 
38
 */
 
39
bool            proc_exit_inprogress = false;
 
40
 
 
41
/*
 
42
 * This flag tracks whether we've called atexit(2) in the current process
 
43
 * (or in the parent postmaster).
 
44
 */
 
45
static bool atexit_callback_setup = false;
 
46
 
 
47
/* local functions */
 
48
static void proc_exit_prepare(int code);
 
49
 
 
50
 
 
51
/* ----------------------------------------------------------------
 
52
 *                                              exit() handling stuff
 
53
 *
 
54
 * These functions are in generally the same spirit as atexit(2),
 
55
 * but provide some additional features we need --- in particular,
 
56
 * we want to register callbacks to invoke when we are disconnecting
 
57
 * from a broken shared-memory context but not exiting the postmaster.
 
58
 *
 
59
 * Callback functions can take zero, one, or two args: the first passed
 
60
 * arg is the integer exitcode, the second is the Datum supplied when
 
61
 * the callback was registered.
 
62
 * ----------------------------------------------------------------
 
63
 */
 
64
 
 
65
#define MAX_ON_EXITS 20
 
66
 
 
67
static struct ONEXIT
 
68
{
 
69
        pg_on_exit_callback function;
 
70
        Datum           arg;
 
71
}       on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
 
72
 
 
73
static int      on_proc_exit_index,
 
74
                        on_shmem_exit_index;
 
75
 
 
76
 
 
77
/* ----------------------------------------------------------------
 
78
 *              proc_exit
 
79
 *
 
80
 *              this function calls all the callbacks registered
 
81
 *              for it (to free resources) and then calls exit.
 
82
 *
 
83
 *              This should be the only function to call exit().
 
84
 *              -cim 2/6/90
 
85
 *
 
86
 *              Unfortunately, we can't really guarantee that add-on code
 
87
 *              obeys the rule of not calling exit() directly.  So, while
 
88
 *              this is the preferred way out of the system, we also register
 
89
 *              an atexit callback that will make sure cleanup happens.
 
90
 * ----------------------------------------------------------------
 
91
 */
 
92
void
 
93
proc_exit(int code)
 
94
{
 
95
        /* Clean up everything that must be cleaned up */
 
96
        proc_exit_prepare(code);
 
97
 
 
98
#ifdef PROFILE_PID_DIR
 
99
        {
 
100
                /*
 
101
                 * If we are profiling ourself then gprof's mcleanup() is about to
 
102
                 * write out a profile to ./gmon.out.  Since mcleanup() always uses a
 
103
                 * fixed file name, each backend will overwrite earlier profiles. To
 
104
                 * fix that, we create a separate subdirectory for each backend
 
105
                 * (./gprof/pid) and 'cd' to that subdirectory before we exit() - that
 
106
                 * forces mcleanup() to write each profile into its own directory.      We
 
107
                 * end up with something like: $PGDATA/gprof/8829/gmon.out
 
108
                 * $PGDATA/gprof/8845/gmon.out ...
 
109
                 *
 
110
                 * To avoid undesirable disk space bloat, autovacuum workers are
 
111
                 * discriminated against: all their gmon.out files go into the same
 
112
                 * subdirectory.  Without this, an installation that is "just sitting
 
113
                 * there" nonetheless eats megabytes of disk space every few seconds.
 
114
                 *
 
115
                 * Note that we do this here instead of in an on_proc_exit() callback
 
116
                 * because we want to ensure that this code executes last - we don't
 
117
                 * want to interfere with any other on_proc_exit() callback.  For the
 
118
                 * same reason, we do not include it in proc_exit_prepare ... so if
 
119
                 * you are exiting in the "wrong way" you won't drop your profile in a
 
120
                 * nice place.
 
121
                 */
 
122
                char            gprofDirName[32];
 
123
 
 
124
                if (IsAutoVacuumWorkerProcess())
 
125
                        snprintf(gprofDirName, 32, "gprof/avworker");
 
126
                else
 
127
                        snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
 
128
 
 
129
                mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
 
130
                mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
 
131
                chdir(gprofDirName);
 
132
        }
 
133
#endif
 
134
 
 
135
        elog(DEBUG3, "exit(%d)", code);
 
136
 
 
137
        exit(code);
 
138
}
 
139
 
 
140
/*
 
141
 * Code shared between proc_exit and the atexit handler.  Note that in
 
142
 * normal exit through proc_exit, this will actually be called twice ...
 
143
 * but the second call will have nothing to do.
 
144
 */
 
145
static void
 
146
proc_exit_prepare(int code)
 
147
{
 
148
        /*
 
149
         * Once we set this flag, we are committed to exit.  Any ereport() will
 
150
         * NOT send control back to the main loop, but right back here.
 
151
         */
 
152
        proc_exit_inprogress = true;
 
153
 
 
154
        /*
 
155
         * Forget any pending cancel or die requests; we're doing our best to
 
156
         * close up shop already.  Note that the signal handlers will not set
 
157
         * these flags again, now that proc_exit_inprogress is set.
 
158
         */
 
159
        InterruptPending = false;
 
160
        ProcDiePending = false;
 
161
        QueryCancelPending = false;
 
162
        /* And let's just make *sure* we're not interrupted ... */
 
163
        ImmediateInterruptOK = false;
 
164
        InterruptHoldoffCount = 1;
 
165
        CritSectionCount = 0;
 
166
 
 
167
        /*
 
168
         * Also clear the error context stack, to prevent error callbacks from
 
169
         * being invoked by any elog/ereport calls made during proc_exit. Whatever
 
170
         * context they might want to offer is probably not relevant, and in any
 
171
         * case they are likely to fail outright after we've done things like
 
172
         * aborting any open transaction.  (In normal exit scenarios the context
 
173
         * stack should be empty anyway, but it might not be in the case of
 
174
         * elog(FATAL) for example.)
 
175
         */
 
176
        error_context_stack = NULL;
 
177
        /* For the same reason, reset debug_query_string before it's clobbered */
 
178
        debug_query_string = NULL;
 
179
 
 
180
        /* do our shared memory exits first */
 
181
        shmem_exit(code);
 
182
 
 
183
        elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
 
184
                 code, on_proc_exit_index);
 
185
 
 
186
        /*
 
187
         * call all the registered callbacks.
 
188
         *
 
189
         * Note that since we decrement on_proc_exit_index each time, if a
 
190
         * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
 
191
         * invoked again when control comes back here (nor will the
 
192
         * previously-completed callbacks).  So, an infinite loop should not be
 
193
         * possible.
 
194
         */
 
195
        while (--on_proc_exit_index >= 0)
 
196
                (*on_proc_exit_list[on_proc_exit_index].function) (code,
 
197
                                                                  on_proc_exit_list[on_proc_exit_index].arg);
 
198
 
 
199
        on_proc_exit_index = 0;
 
200
}
 
201
 
 
202
/* ------------------
 
203
 * Run all of the on_shmem_exit routines --- but don't actually exit.
 
204
 * This is used by the postmaster to re-initialize shared memory and
 
205
 * semaphores after a backend dies horribly.
 
206
 * ------------------
 
207
 */
 
208
void
 
209
shmem_exit(int code)
 
210
{
 
211
        elog(DEBUG3, "shmem_exit(%d): %d callbacks to make",
 
212
                 code, on_shmem_exit_index);
 
213
 
 
214
        /*
 
215
         * call all the registered callbacks.
 
216
         *
 
217
         * As with proc_exit(), we remove each callback from the list before
 
218
         * calling it, to avoid infinite loop in case of error.
 
219
         */
 
220
        while (--on_shmem_exit_index >= 0)
 
221
                (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
 
222
                                                                on_shmem_exit_list[on_shmem_exit_index].arg);
 
223
 
 
224
        on_shmem_exit_index = 0;
 
225
}
 
226
 
 
227
/* ----------------------------------------------------------------
 
228
 *              atexit_callback
 
229
 *
 
230
 *              Backstop to ensure that direct calls of exit() don't mess us up.
 
231
 *
 
232
 * Somebody who was being really uncooperative could call _exit(),
 
233
 * but for that case we have a "dead man switch" that will make the
 
234
 * postmaster treat it as a crash --- see pmsignal.c.
 
235
 * ----------------------------------------------------------------
 
236
 */
 
237
#ifdef HAVE_ATEXIT
 
238
 
 
239
static void
 
240
atexit_callback(void)
 
241
{
 
242
        /* Clean up everything that must be cleaned up */
 
243
        /* ... too bad we don't know the real exit code ... */
 
244
        proc_exit_prepare(-1);
 
245
}
 
246
#else                                                   /* assume we have on_exit instead */
 
247
 
 
248
static void
 
249
atexit_callback(int exitstatus, void *arg)
 
250
{
 
251
        /* Clean up everything that must be cleaned up */
 
252
        proc_exit_prepare(exitstatus);
 
253
}
 
254
#endif   /* HAVE_ATEXIT */
 
255
 
 
256
/* ----------------------------------------------------------------
 
257
 *              on_proc_exit
 
258
 *
 
259
 *              this function adds a callback function to the list of
 
260
 *              functions invoked by proc_exit().       -cim 2/6/90
 
261
 * ----------------------------------------------------------------
 
262
 */
 
263
void
 
264
on_proc_exit(pg_on_exit_callback function, Datum arg)
 
265
{
 
266
        if (on_proc_exit_index >= MAX_ON_EXITS)
 
267
                ereport(FATAL,
 
268
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 
269
                                 errmsg_internal("out of on_proc_exit slots")));
 
270
 
 
271
        on_proc_exit_list[on_proc_exit_index].function = function;
 
272
        on_proc_exit_list[on_proc_exit_index].arg = arg;
 
273
 
 
274
        ++on_proc_exit_index;
 
275
 
 
276
        if (!atexit_callback_setup)
 
277
        {
 
278
#ifdef HAVE_ATEXIT
 
279
                atexit(atexit_callback);
 
280
#else
 
281
                on_exit(atexit_callback, NULL);
 
282
#endif
 
283
                atexit_callback_setup = true;
 
284
        }
 
285
}
 
286
 
 
287
/* ----------------------------------------------------------------
 
288
 *              on_shmem_exit
 
289
 *
 
290
 *              this function adds a callback function to the list of
 
291
 *              functions invoked by shmem_exit().      -cim 2/6/90
 
292
 * ----------------------------------------------------------------
 
293
 */
 
294
void
 
295
on_shmem_exit(pg_on_exit_callback function, Datum arg)
 
296
{
 
297
        if (on_shmem_exit_index >= MAX_ON_EXITS)
 
298
                ereport(FATAL,
 
299
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 
300
                                 errmsg_internal("out of on_shmem_exit slots")));
 
301
 
 
302
        on_shmem_exit_list[on_shmem_exit_index].function = function;
 
303
        on_shmem_exit_list[on_shmem_exit_index].arg = arg;
 
304
 
 
305
        ++on_shmem_exit_index;
 
306
 
 
307
        if (!atexit_callback_setup)
 
308
        {
 
309
#ifdef HAVE_ATEXIT
 
310
                atexit(atexit_callback);
 
311
#else
 
312
                on_exit(atexit_callback, NULL);
 
313
#endif
 
314
                atexit_callback_setup = true;
 
315
        }
 
316
}
 
317
 
 
318
/* ----------------------------------------------------------------
 
319
 *              cancel_shmem_exit
 
320
 *
 
321
 *              this function removes an entry, if present, from the list of
 
322
 *              functions to be invoked by shmem_exit().  For simplicity,
 
323
 *              only the latest entry can be removed.  (We could work harder
 
324
 *              but there is no need for current uses.)
 
325
 * ----------------------------------------------------------------
 
326
 */
 
327
void
 
328
cancel_shmem_exit(pg_on_exit_callback function, Datum arg)
 
329
{
 
330
        if (on_shmem_exit_index > 0 &&
 
331
                on_shmem_exit_list[on_shmem_exit_index - 1].function == function &&
 
332
                on_shmem_exit_list[on_shmem_exit_index - 1].arg == arg)
 
333
                --on_shmem_exit_index;
 
334
}
 
335
 
 
336
/* ----------------------------------------------------------------
 
337
 *              on_exit_reset
 
338
 *
 
339
 *              this function clears all on_proc_exit() and on_shmem_exit()
 
340
 *              registered functions.  This is used just after forking a backend,
 
341
 *              so that the backend doesn't believe it should call the postmaster's
 
342
 *              on-exit routines when it exits...
 
343
 * ----------------------------------------------------------------
 
344
 */
 
345
void
 
346
on_exit_reset(void)
 
347
{
 
348
        on_shmem_exit_index = 0;
 
349
        on_proc_exit_index = 0;
 
350
}