2929 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3030 * Portions Copyright (c) 1994, Regents of the University of California
3131 *
32- *$Id: pqcomm.c,v 1.146 2003/01/14 22:52:57 momjian Exp $
32+ *$Id: pqcomm.c,v 1.147 2003/01/25 05:19:46 tgl Exp $
3333 *
3434 *-------------------------------------------------------------------------
3535 */
4141 *StreamServerPort- Open postmaster's server port
4242 *StreamConnection- Create new connection with client
4343 *StreamClose- Close a client/backend connection
44+ *TouchSocketFile- Protect socket file against /tmp cleaners
4445 *pq_init- initialize libpq at backend startup
4546 *pq_close- shutdown libpq at backend exit
4647 *
6667#include <fcntl.h>
6768#include <grp.h>
6869#include <unistd.h>
69- #include <sys/stat .h>
70+ #include <sys/file .h>
7071#include <sys/socket.h>
72+ #include <sys/stat.h>
73+ #include <sys/time.h>
7174#include <netdb.h>
7275#include <netinet/in.h>
7376#ifdef HAVE_NETINET_TCP_H
7477#include <netinet/tcp.h>
7578#endif
7679#include <arpa/inet.h>
77- #include <sys/file.h>
80+ #ifdef HAVE_UTIME_H
81+ #include <utime.h>
82+ #endif
7883
7984#include "libpq/libpq.h"
8085#include "miscadmin.h"
@@ -87,8 +92,8 @@ extern ssize_t secure_write(Port *, const void *, size_t);
8792static void pq_close (void );
8893
8994#ifdef HAVE_UNIX_SOCKETS
90- int Lock_AF_UNIX (unsigned short portNumber ,char * unixSocketName );
91- int Setup_AF_UNIX (void );
95+ static int Lock_AF_UNIX (unsigned short portNumber ,char * unixSocketName );
96+ static int Setup_AF_UNIX (void );
9297#endif /* HAVE_UNIX_SOCKETS */
9398
9499#ifdef HAVE_IPV6
@@ -175,12 +180,14 @@ static char sock_path[MAXPGPATH];
175180 * Shutdown routine for backend connection
176181 * If a Unix socket is used for communication, explicitly close it.
177182 */
183+ #ifdef HAVE_UNIX_SOCKETS
178184static void
179185StreamDoUnlink (void )
180186{
181187Assert (sock_path [0 ]);
182188unlink (sock_path );
183189}
190+ #endif /* HAVE_UNIX_SOCKETS */
184191
185192/*
186193 * StreamServerPort -- open a sock stream "listening" port.
@@ -345,12 +352,13 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
345352
346353}
347354
355+
356+ #ifdef HAVE_UNIX_SOCKETS
357+
348358/*
349359 * Lock_AF_UNIX -- configure unix socket file path
350360 */
351-
352- #ifdef HAVE_UNIX_SOCKETS
353- int
361+ static int
354362Lock_AF_UNIX (unsigned short portNumber ,char * unixSocketName )
355363{
356364SockAddr saddr ;/* just used to get socket path */
@@ -377,7 +385,7 @@ Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
377385/*
378386 * Setup_AF_UNIX -- configure unix socket permissions
379387 */
380- int
388+ static int
381389Setup_AF_UNIX (void )
382390{
383391/* Arrange to unlink the socket file at exit */
@@ -429,6 +437,7 @@ Setup_AF_UNIX(void)
429437}
430438return STATUS_OK ;
431439}
440+
432441#endif /* HAVE_UNIX_SOCKETS */
433442
434443
@@ -506,6 +515,38 @@ StreamClose(int sock)
506515close (sock );
507516}
508517
518+ /*
519+ * TouchSocketFile -- mark socket file as recently accessed
520+ *
521+ * This routine should be called every so often to ensure that the socket
522+ * file has a recent mod date (ordinary operations on sockets usually won't
523+ * change the mod date). That saves it from being removed by
524+ * overenthusiastic /tmp-directory-cleaner daemons. (Another reason we should
525+ * never have put the socket file in /tmp...)
526+ */
527+ void
528+ TouchSocketFile (void )
529+ {
530+ /* Do nothing if we did not create a socket... */
531+ if (sock_path [0 ]!= '\0' )
532+ {
533+ /*
534+ * utime() is POSIX standard, utimes() is a common alternative.
535+ * If we have neither, there's no way to affect the mod or access
536+ * time of the socket :-(
537+ *
538+ * In either path, we ignore errors; there's no point in complaining.
539+ */
540+ #ifdef HAVE_UTIME
541+ utime (sock_path ,NULL );
542+ #else /* !HAVE_UTIME */
543+ #ifdef HAVE_UTIMES
544+ utimes (sock_path ,NULL );
545+ #endif /* HAVE_UTIMES */
546+ #endif /* HAVE_UTIME */
547+ }
548+ }
549+
509550
510551/* --------------------------------
511552 * Low-level I/O routines begin here.