Skip to content

Commit c672aa8

Browse files
committed
For application to HEAD, following community review.
* Changes incorrect CYGWIN defines to __CYGWIN__ * Some localtime returns NULL checks (when unchecked cause SEGVs under Win32 regression tests) * Rationalized CreateSharedMemoryAndSemaphores and AttachSharedMemoryAndSemaphores (Bruce, I finally remembered to do it); requires attention. Claudio Natoli
1 parent 5ada9ef commit c672aa8

File tree

11 files changed

+105
-107
lines changed

11 files changed

+105
-107
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.176 2004/02/10 01:55:24 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.177 2004/02/25 19:41:22 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -428,7 +428,7 @@ BootstrapMain(int argc, char *argv[])
428428

429429
#ifdef EXEC_BACKEND
430430
if (IsUnderPostmaster)
431-
AttachSharedMemoryAndSemaphores();
431+
CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
432432
#endif
433433
XLOGPathInit();
434434

src/backend/commands/user.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.137 2004/02/10 01:55:25 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.138 2004/02/25 19:41:22 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -140,11 +140,11 @@ write_group_file(Relation grel)
140140
bufsize = strlen(filename) + 12;
141141
tempname = (char *) palloc(bufsize);
142142
snprintf(tempname, bufsize, "%s.%d", filename, MyProcPid);
143-
#if defined(WIN32) || defined(CYGWIN)
143+
#if defined(WIN32) || defined(__CYGWIN__)
144144
filename = repalloc(filename, strlen(filename) + 1 + strlen(".new"));
145145
strcat(filename, ".new");
146146
#endif
147-
147+
148148
oumask = umask((mode_t) 077);
149149
fp = AllocateFile(tempname, "w");
150150
umask(oumask);
@@ -291,7 +291,7 @@ write_user_file(Relation urel)
291291
bufsize = strlen(filename) + 12;
292292
tempname = (char *) palloc(bufsize);
293293
snprintf(tempname, bufsize, "%s.%d", filename, MyProcPid);
294-
#if defined(WIN32) || defined(CYGWIN)
294+
#if defined(WIN32) || defined(__CYGWIN__)
295295
filename = repalloc(filename, strlen(filename) + 1 + strlen(".new"));
296296
strcat(filename, ".new");
297297
#endif
@@ -466,7 +466,7 @@ AtEOXact_UpdatePasswordFile(bool isCommit)
466466
user_file_update_needed = false;
467467
write_user_file(urel);
468468
heap_close(urel, NoLock);
469-
#if defined(WIN32) || defined(CYGWIN)
469+
#if defined(WIN32) || defined(__CYGWIN__)
470470
{
471471
/* Rename active file while not holding an exclusive lock */
472472
char *filename = user_getfilename(), *filename_new;
@@ -485,7 +485,7 @@ AtEOXact_UpdatePasswordFile(bool isCommit)
485485
group_file_update_needed = false;
486486
write_group_file(grel);
487487
heap_close(grel, NoLock);
488-
#if defined(WIN32) || defined(CYGWIN)
488+
#if defined(WIN32) || defined(__CYGWIN__)
489489
{
490490
/* Rename active file while not holding an exclusive lock */
491491
char *filename = group_getfilename(), *filename_new;

src/backend/port/sysv_shmem.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.31 2004/02/08 22:28:56 neilc Exp $
13+
* $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.32 2004/02/25 19:41:22 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -254,7 +254,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
254254
{
255255
void* origUsedShmemSegAddr = UsedShmemSegAddr;
256256

257-
#ifdef CYGWIN
257+
#ifdef __CYGWIN__
258258
/* cygipc (currently) appears to not detach on exec. */
259259
PGSharedMemoryDetach();
260260
UsedShmemSegAddr = origUsedShmemSegAddr;
@@ -373,7 +373,7 @@ PGSharedMemoryDetach(void)
373373
if (UsedShmemSegAddr != NULL)
374374
{
375375
if ((shmdt(UsedShmemSegAddr) < 0)
376-
#if (defined(EXEC_BACKEND) && defined(CYGWIN))
376+
#if (defined(EXEC_BACKEND) && defined(__CYGWIN__))
377377
/* Work-around for cygipc exec bug */
378378
&& shmdt(NULL) < 0
379379
#endif

src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.368 2004/02/23 20:45:59 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.369 2004/02/25 19:41:22 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -2727,7 +2727,7 @@ SubPostmasterMain(int argc, char* argv[])
27272727
load_group();
27282728

27292729
/* Attach process to shared segments */
2730-
AttachSharedMemoryAndSemaphores();
2730+
CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
27312731

27322732
/* Run backend */
27332733
proc_exit(BackendRun(&port));

src/backend/storage/ipc/ipci.c

Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.64 2004/02/10 01:55:25 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.65 2004/02/25 19:41:22 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -36,8 +36,10 @@
3636
* Creates and initializes shared memory and semaphores.
3737
*
3838
* This is called by the postmaster or by a standalone backend.
39-
* It is NEVER called by a backend forked from the postmaster;
40-
* for such a backend, the shared memory is already ready-to-go.
39+
* It is also called by a backend forked from the postmaster under
40+
* the EXEC_BACKEND case
41+
*
42+
* In the non EXEC_BACKEND case, backends already have shared memory ready-to-go.
4143
*
4244
* If "makePrivate" is true then we only need private memory, not shared
4345
* memory. This is true for a standalone backend, false for a postmaster.
@@ -47,53 +49,68 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
4749
int maxBackends,
4850
int port)
4951
{
50-
int size;
51-
int numSemas;
52-
PGShmemHeader *seghdr;
53-
54-
/*
55-
* Size of the Postgres shared-memory block is estimated via
56-
* moderately-accurate estimates for the big hogs, plus 100K for the
57-
* stuff that's too small to bother with estimating.
58-
*/
59-
size = BufferShmemSize();
60-
size += LockShmemSize(maxBackends);
61-
size += XLOGShmemSize();
62-
size += CLOGShmemSize();
63-
size += LWLockShmemSize();
64-
size += SInvalShmemSize(maxBackends);
65-
size += FreeSpaceShmemSize();
52+
PGShmemHeader *seghdr = NULL;
53+
if (!IsUnderPostmaster)
54+
{
55+
int size;
56+
int numSemas;
57+
58+
/*
59+
* Size of the Postgres shared-memory block is estimated via
60+
* moderately-accurate estimates for the big hogs, plus 100K for the
61+
* stuff that's too small to bother with estimating.
62+
*/
63+
size = BufferShmemSize();
64+
size += LockShmemSize(maxBackends);
65+
size += XLOGShmemSize();
66+
size += CLOGShmemSize();
67+
size += LWLockShmemSize();
68+
size += SInvalShmemSize(maxBackends);
69+
size += FreeSpaceShmemSize();
6670
#ifdef EXEC_BACKEND
67-
size += ShmemBackendArraySize();
71+
size += ShmemBackendArraySize();
6872
#endif
69-
size += 100000;
70-
/* might as well round it off to a multiple of a typical page size */
71-
size += 8192 - (size % 8192);
72-
73-
elog(DEBUG3, "invoking IpcMemoryCreate(size=%d)", size);
73+
size += 100000;
74+
/* might as well round it off to a multiple of a typical page size */
75+
size += 8192 - (size % 8192);
76+
77+
elog(DEBUG3, "invoking IpcMemoryCreate(size=%d)", size);
78+
79+
/*
80+
* Create the shmem segment
81+
*/
82+
seghdr = PGSharedMemoryCreate(size, makePrivate, port);
83+
84+
/*
85+
* Create semaphores
86+
*/
87+
numSemas = ProcGlobalSemas(maxBackends);
88+
numSemas += SpinlockSemas();
89+
PGReserveSemaphores(numSemas, port);
90+
}
91+
else
92+
{
93+
/*
94+
* Attach to the shmem segment.
95+
* (this should only ever be reached by EXEC_BACKEND code,
96+
* and only then with makePrivate == false)
97+
*/
98+
Assert(ExecBackend && !makePrivate);
99+
seghdr = PGSharedMemoryCreate(-1, makePrivate, 0);
100+
}
74101

75-
/*
76-
* Create the shmem segment
77-
*/
78-
seghdr = PGSharedMemoryCreate(size, makePrivate, port);
79-
80-
/*
81-
* Create semaphores
82-
*/
83-
numSemas = ProcGlobalSemas(maxBackends);
84-
numSemas += SpinlockSemas();
85-
PGReserveSemaphores(numSemas, port);
86102

87103
/*
88104
* Set up shared memory allocation mechanism
89105
*/
90-
InitShmemAllocation(seghdr, true);
106+
InitShmemAllocation(seghdr, !IsUnderPostmaster);
91107

92108
/*
93109
* Now initialize LWLocks, which do shared memory allocation and are
94110
* needed for InitShmemIndex.
95111
*/
96-
CreateLWLocks();
112+
if (!IsUnderPostmaster)
113+
CreateLWLocks();
97114

98115
/*
99116
* Set up shmem.c index hashtable
@@ -137,41 +154,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
137154
/*
138155
* Alloc the win32 shared backend array
139156
*/
140-
ShmemBackendArrayAllocation();
157+
if (!IsUnderPostmaster)
158+
ShmemBackendArrayAllocation();
141159
#endif
142160
}
143-
144-
145-
#ifdef EXEC_BACKEND
146-
/*
147-
* AttachSharedMemoryAndSemaphores
148-
* Attaches to the existing shared resources.
149-
*/
150-
151-
/* FIXME: [fork/exec] This function is starting to look pretty much like
152-
CreateSharedMemoryAndSemaphores. Refactor? */
153-
void
154-
AttachSharedMemoryAndSemaphores(void)
155-
{
156-
PGShmemHeader *seghdr = PGSharedMemoryCreate(-1,false,-1);
157-
158-
InitShmemAllocation(seghdr, false);
159-
160-
InitShmemIndex();
161-
162-
XLOGShmemInit();
163-
CLOGShmemInit();
164-
InitBufferPool();
165-
166-
InitLocks();
167-
InitLockTable(MaxBackends);
168-
169-
InitProcGlobal(MaxBackends);
170-
171-
CreateSharedInvalidationState(MaxBackends);
172-
173-
InitFreeSpaceMap();
174-
175-
PMSignalInit();
176-
}
177-
#endif

src/backend/utils/adt/datetime.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.124 2004/01/19 19:04:40 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.125 2004/02/25 19:41:23 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1611,6 +1611,10 @@ DetermineLocalTimeZone(struct tm * tm)
16111611
* and reassemble to get a representation of local time.
16121612
*/
16131613
tx = localtime(&mytime);
1614+
if (!tx)
1615+
ereport(ERROR,
1616+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1617+
errmsg("timestamp out of range")));
16141618
day = date2j(tx->tm_year + 1900, tx->tm_mon + 1, tx->tm_mday) -
16151619
UNIX_EPOCH_JDATE;
16161620
locsec = tx->tm_sec + (tx->tm_min + (day * 24 + tx->tm_hour) * 60) * 60;
@@ -1632,6 +1636,10 @@ DetermineLocalTimeZone(struct tm * tm)
16321636
mysec += delta1;
16331637
mytime = (time_t) mysec;
16341638
tx = localtime(&mytime);
1639+
if (!tx)
1640+
ereport(ERROR,
1641+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1642+
errmsg("timestamp out of range")));
16351643
day = date2j(tx->tm_year + 1900, tx->tm_mon + 1, tx->tm_mday) -
16361644
UNIX_EPOCH_JDATE;
16371645
locsec = tx->tm_sec + (tx->tm_min + (day * 24 + tx->tm_hour) * 60) * 60;
@@ -1653,6 +1661,10 @@ DetermineLocalTimeZone(struct tm * tm)
16531661
mysec += (delta2 - delta1);
16541662
mytime = (time_t) mysec;
16551663
tx = localtime(&mytime);
1664+
if (!tx)
1665+
ereport(ERROR,
1666+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1667+
errmsg("timestamp out of range")));
16561668
day = date2j(tx->tm_year + 1900, tx->tm_mon + 1, tx->tm_mday) -
16571669
UNIX_EPOCH_JDATE;
16581670
locsec = tx->tm_sec + (tx->tm_min + (day * 24 + tx->tm_hour) * 60) * 60;

src/backend/utils/cache/relcache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.197 2004/02/10 01:55:26 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.198 2004/02/25 19:41:23 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3266,13 +3266,13 @@ write_relcache_init_file(void)
32663266
* OK, rename the temp file to its final name, deleting any
32673267
* previously-existing init file.
32683268
*/
3269-
#if defined(WIN32) || defined(CYGWIN)
3269+
#if defined(WIN32) || defined(__CYGWIN__)
32703270
rename(tempfilename, finalfilename);
32713271
LWLockRelease(RelCacheInitLock);
32723272
#else
32733273
{
32743274
char finalfilename_new[MAXPGPATH];
3275-
3275+
32763276
snprintf(finalfilename_new, sizeof(finalfilename_new), "%s.new", finalfilename);
32773277
rename(tempfilename, finalfilename_new);
32783278
LWLockRelease(RelCacheInitLock);

src/include/port.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.19 2004/02/10 03:42:45 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.20 2004/02/25 19:41:23 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -34,7 +34,7 @@ extern int fseeko(FILE *stream, off_t offset, int whence);
3434
extern off_t ftello(FILE *stream);
3535
#endif
3636

37-
#if defined(WIN32) || defined(CYGWIN)
37+
#if defined(WIN32) || defined(__CYGWIN__)
3838
/*
3939
* Win32 doesn't have reliable rename/unlink during concurrent access
4040
*/

src/include/storage/ipc.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/include/storage/ipc.h,v 1.64 2003/12/20 17:31:21 momjian Exp $
14+
* $PostgreSQL: pgsql/src/include/storage/ipc.h,v 1.65 2004/02/25 19:41:23 momjian Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -30,10 +30,6 @@ extern void on_exit_reset(void);
3030

3131
/* ipci.c */
3232
extern void CreateSharedMemoryAndSemaphores(bool makePrivate,
33-
int maxBackends,
34-
int port);
35-
#ifdef EXEC_BACKEND
36-
extern void AttachSharedMemoryAndSemaphores(void);
37-
#endif
38-
33+
int maxBackends,
34+
int port);
3935
#endif /* IPC_H */

0 commit comments

Comments
 (0)