~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

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

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

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-2005, PostgreSQL Global Development Group
 
12
 * Portions Copyright (c) 1994, Regents of the University of California
 
13
 *
 
14
 *
 
15
 * IDENTIFICATION
 
16
 *        $PostgreSQL: pgsql/src/backend/storage/ipc/ipc.c,v 1.90 2004-12-31 22:00:56 pgsql Exp $
 
17
 *
 
18
 *-------------------------------------------------------------------------
 
19
 */
 
20
#include "postgres.h"
 
21
 
 
22
#include <errno.h>
 
23
#include <signal.h>
 
24
#include <unistd.h>
 
25
 
 
26
#include "miscadmin.h"
 
27
#include "storage/ipc.h"
 
28
 
 
29
 
 
30
/*
 
31
 * This flag is set during proc_exit() to change ereport()'s behavior,
 
32
 * so that an ereport() from an on_proc_exit routine cannot get us out
 
33
 * of the exit procedure.  We do NOT want to go back to the idle loop...
 
34
 */
 
35
bool            proc_exit_inprogress = false;
 
36
 
 
37
 
 
38
/* ----------------------------------------------------------------
 
39
 *                                              exit() handling stuff
 
40
 *
 
41
 * These functions are in generally the same spirit as atexit(2),
 
42
 * but provide some additional features we need --- in particular,
 
43
 * we want to register callbacks to invoke when we are disconnecting
 
44
 * from a broken shared-memory context but not exiting the postmaster.
 
45
 *
 
46
 * Callback functions can take zero, one, or two args: the first passed
 
47
 * arg is the integer exitcode, the second is the Datum supplied when
 
48
 * the callback was registered.
 
49
 * ----------------------------------------------------------------
 
50
 */
 
51
 
 
52
#define MAX_ON_EXITS 20
 
53
 
 
54
static struct ONEXIT
 
55
{
 
56
        void            (*function) (int code, Datum arg);
 
57
        Datum           arg;
 
58
}       on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
 
59
 
 
60
static int      on_proc_exit_index,
 
61
                        on_shmem_exit_index;
 
62
 
 
63
 
 
64
/* ----------------------------------------------------------------
 
65
 *              proc_exit
 
66
 *
 
67
 *              this function calls all the callbacks registered
 
68
 *              for it (to free resources) and then calls exit.
 
69
 *              This should be the only function to call exit().
 
70
 *              -cim 2/6/90
 
71
 * ----------------------------------------------------------------
 
72
 */
 
73
void
 
74
proc_exit(int code)
 
75
{
 
76
        /*
 
77
         * Once we set this flag, we are committed to exit.  Any ereport()
 
78
         * will NOT send control back to the main loop, but right back here.
 
79
         */
 
80
        proc_exit_inprogress = true;
 
81
 
 
82
        /*
 
83
         * Forget any pending cancel or die requests; we're doing our best to
 
84
         * close up shop already.  Note that the signal handlers will not set
 
85
         * these flags again, now that proc_exit_inprogress is set.
 
86
         */
 
87
        InterruptPending = false;
 
88
        ProcDiePending = false;
 
89
        QueryCancelPending = false;
 
90
        /* And let's just make *sure* we're not interrupted ... */
 
91
        ImmediateInterruptOK = false;
 
92
        InterruptHoldoffCount = 1;
 
93
        CritSectionCount = 0;
 
94
 
 
95
        elog(DEBUG3, "proc_exit(%d)", code);
 
96
 
 
97
        /* do our shared memory exits first */
 
98
        shmem_exit(code);
 
99
 
 
100
        /*
 
101
         * call all the callbacks registered before calling exit().
 
102
         *
 
103
         * Note that since we decrement on_proc_exit_index each time, if a
 
104
         * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
 
105
         * invoked again when control comes back here (nor will the
 
106
         * previously-completed callbacks).  So, an infinite loop should not
 
107
         * be possible.
 
108
         */
 
109
        while (--on_proc_exit_index >= 0)
 
110
                (*on_proc_exit_list[on_proc_exit_index].function) (code,
 
111
                                                          on_proc_exit_list[on_proc_exit_index].arg);
 
112
 
 
113
        elog(DEBUG3, "exit(%d)", code);
 
114
        exit(code);
 
115
}
 
116
 
 
117
/* ------------------
 
118
 * Run all of the on_shmem_exit routines --- but don't actually exit.
 
119
 * This is used by the postmaster to re-initialize shared memory and
 
120
 * semaphores after a backend dies horribly.
 
121
 * ------------------
 
122
 */
 
123
void
 
124
shmem_exit(int code)
 
125
{
 
126
        elog(DEBUG3, "shmem_exit(%d)", code);
 
127
 
 
128
        /*
 
129
         * call all the registered callbacks.
 
130
         *
 
131
         * As with proc_exit(), we remove each callback from the list before
 
132
         * calling it, to avoid infinite loop in case of error.
 
133
         */
 
134
        while (--on_shmem_exit_index >= 0)
 
135
                (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
 
136
                                                        on_shmem_exit_list[on_shmem_exit_index].arg);
 
137
 
 
138
        on_shmem_exit_index = 0;
 
139
}
 
140
 
 
141
/* ----------------------------------------------------------------
 
142
 *              on_proc_exit
 
143
 *
 
144
 *              this function adds a callback function to the list of
 
145
 *              functions invoked by proc_exit().       -cim 2/6/90
 
146
 * ----------------------------------------------------------------
 
147
 */
 
148
void
 
149
                        on_proc_exit(void (*function) (int code, Datum arg), Datum arg)
 
150
{
 
151
        if (on_proc_exit_index >= MAX_ON_EXITS)
 
152
                ereport(FATAL,
 
153
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 
154
                                 errmsg_internal("out of on_proc_exit slots")));
 
155
 
 
156
        on_proc_exit_list[on_proc_exit_index].function = function;
 
157
        on_proc_exit_list[on_proc_exit_index].arg = arg;
 
158
 
 
159
        ++on_proc_exit_index;
 
160
}
 
161
 
 
162
/* ----------------------------------------------------------------
 
163
 *              on_shmem_exit
 
164
 *
 
165
 *              this function adds a callback function to the list of
 
166
 *              functions invoked by shmem_exit().      -cim 2/6/90
 
167
 * ----------------------------------------------------------------
 
168
 */
 
169
void
 
170
                        on_shmem_exit(void (*function) (int code, Datum arg), Datum arg)
 
171
{
 
172
        if (on_shmem_exit_index >= MAX_ON_EXITS)
 
173
                ereport(FATAL,
 
174
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 
175
                                 errmsg_internal("out of on_shmem_exit slots")));
 
176
 
 
177
        on_shmem_exit_list[on_shmem_exit_index].function = function;
 
178
        on_shmem_exit_list[on_shmem_exit_index].arg = arg;
 
179
 
 
180
        ++on_shmem_exit_index;
 
181
}
 
182
 
 
183
/* ----------------------------------------------------------------
 
184
 *              on_exit_reset
 
185
 *
 
186
 *              this function clears all on_proc_exit() and on_shmem_exit()
 
187
 *              registered functions.  This is used just after forking a backend,
 
188
 *              so that the backend doesn't believe it should call the postmaster's
 
189
 *              on-exit routines when it exits...
 
190
 * ----------------------------------------------------------------
 
191
 */
 
192
void
 
193
on_exit_reset(void)
 
194
{
 
195
        on_shmem_exit_index = 0;
 
196
        on_proc_exit_index = 0;
 
197
}