1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Where available, use utime() or utimes() to update the file mod time

of the socket file and socket lock file; this should prevent both of them
from being removed by even the stupidest varieties of /tmp-cleaning
script.  Per suggestion from Giles Lean.
This commit is contained in:
Tom Lane
2003-01-25 05:19:47 +00:00
parent 80727ce14f
commit 15ab7a8720
7 changed files with 99 additions and 25 deletions

View File

@ -29,7 +29,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pqcomm.c,v 1.146 2003/01/14 22:52:57 momjian Exp $
* $Id: pqcomm.c,v 1.147 2003/01/25 05:19:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -41,6 +41,7 @@
* StreamServerPort - Open postmaster's server port
* StreamConnection - Create new connection with client
* StreamClose - Close a client/backend connection
* TouchSocketFile - Protect socket file against /tmp cleaners
* pq_init - initialize libpq at backend startup
* pq_close - shutdown libpq at backend exit
*
@ -66,15 +67,19 @@
#include <fcntl.h>
#include <grp.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#include <arpa/inet.h>
#include <sys/file.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#include "libpq/libpq.h"
#include "miscadmin.h"
@ -87,8 +92,8 @@ extern ssize_t secure_write(Port *, const void *, size_t);
static void pq_close(void);
#ifdef HAVE_UNIX_SOCKETS
int Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
int Setup_AF_UNIX(void);
static int Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
static int Setup_AF_UNIX(void);
#endif /* HAVE_UNIX_SOCKETS */
#ifdef HAVE_IPV6
@ -175,12 +180,14 @@ static char sock_path[MAXPGPATH];
* Shutdown routine for backend connection
* If a Unix socket is used for communication, explicitly close it.
*/
#ifdef HAVE_UNIX_SOCKETS
static void
StreamDoUnlink(void)
{
Assert(sock_path[0]);
unlink(sock_path);
}
#endif /* HAVE_UNIX_SOCKETS */
/*
* StreamServerPort -- open a sock stream "listening" port.
@ -345,12 +352,13 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
}
#ifdef HAVE_UNIX_SOCKETS
/*
* Lock_AF_UNIX -- configure unix socket file path
*/
#ifdef HAVE_UNIX_SOCKETS
int
static int
Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
{
SockAddr saddr; /* just used to get socket path */
@ -377,7 +385,7 @@ Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
/*
* Setup_AF_UNIX -- configure unix socket permissions
*/
int
static int
Setup_AF_UNIX(void)
{
/* Arrange to unlink the socket file at exit */
@ -429,6 +437,7 @@ Setup_AF_UNIX(void)
}
return STATUS_OK;
}
#endif /* HAVE_UNIX_SOCKETS */
@ -506,6 +515,38 @@ StreamClose(int sock)
close(sock);
}
/*
* TouchSocketFile -- mark socket file as recently accessed
*
* This routine should be called every so often to ensure that the socket
* file has a recent mod date (ordinary operations on sockets usually won't
* change the mod date). That saves it from being removed by
* overenthusiastic /tmp-directory-cleaner daemons. (Another reason we should
* never have put the socket file in /tmp...)
*/
void
TouchSocketFile(void)
{
/* Do nothing if we did not create a socket... */
if (sock_path[0] != '\0')
{
/*
* utime() is POSIX standard, utimes() is a common alternative.
* If we have neither, there's no way to affect the mod or access
* time of the socket :-(
*
* In either path, we ignore errors; there's no point in complaining.
*/
#ifdef HAVE_UTIME
utime(sock_path, NULL);
#else /* !HAVE_UTIME */
#ifdef HAVE_UTIMES
utimes(sock_path, NULL);
#endif /* HAVE_UTIMES */
#endif /* HAVE_UTIME */
}
}
/* --------------------------------
* Low-level I/O routines begin here.

View File

@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.305 2003/01/16 00:26:44 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.306 2003/01/25 05:19:46 tgl Exp $
*
* NOTES
*
@ -2678,6 +2678,7 @@ SSDataBase(int xlop)
* do other actions that should happen every now and then on no
* particular schedule. Such as...
*/
TouchSocketFile();
TouchSocketLockFile();
}

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.98 2002/12/05 04:04:46 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.99 2003/01/25 05:19:46 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -16,8 +16,9 @@
#include <sys/param.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <grp.h>
@ -25,6 +26,9 @@
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#include "catalog/catname.h"
#include "catalog/pg_shadow.h"
@ -872,27 +876,42 @@ CreateSocketLockFile(const char *socketfile, bool amPostmaster)
}
/*
* Re-read the socket lock file. This should be called every so often
* to ensure that the lock file has a recent access date. That saves it
* TouchSocketLockFile -- mark socket lock file as recently accessed
*
* This routine should be called every so often to ensure that the lock file
* has a recent mod or access date. That saves it
* from being removed by overenthusiastic /tmp-directory-cleaner daemons.
* (Another reason we should never have put the socket file in /tmp...)
*/
void
TouchSocketLockFile(void)
{
int fd;
char buffer[1];
/* Do nothing if we did not create a socket... */
if (socketLockFile[0] != '\0')
{
/* XXX any need to complain about errors here? */
/*
* utime() is POSIX standard, utimes() is a common alternative;
* if we have neither, fall back to actually reading the file
* (which only sets the access time not mod time, but that should
* be enough in most cases). In all paths, we ignore errors.
*/
#ifdef HAVE_UTIME
utime(socketLockFile, NULL);
#else /* !HAVE_UTIME */
#ifdef HAVE_UTIMES
utimes(socketLockFile, NULL);
#else /* !HAVE_UTIMES */
int fd;
char buffer[1];
fd = open(socketLockFile, O_RDONLY | PG_BINARY, 0);
if (fd >= 0)
{
read(fd, buffer, sizeof(buffer));
close(fd);
}
#endif /* HAVE_UTIMES */
#endif /* HAVE_UTIME */
}
}