From 60a068b3897a27cbb1a13a6b050d05a0ca479055 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 10 Mar 2004 21:12:49 +0000 Subject: [PATCH] Move non-blocking code into its own /port file, for code clarity. --- src/Makefile.global.in | 4 ++-- src/backend/postmaster/pgstat.c | 6 ++--- src/backend/postmaster/postmaster.c | 9 ++------ src/include/c.h | 16 +------------ src/include/port.h | 5 ++++- src/interfaces/libpq/Makefile | 8 +++---- src/interfaces/libpq/fe-connect.c | 9 ++------ src/port/noblock.c | 35 +++++++++++++++++++++++++++++ 8 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 src/port/noblock.c diff --git a/src/Makefile.global.in b/src/Makefile.global.in index e0d0bbfaac..11edf55210 100644 --- a/src/Makefile.global.in +++ b/src/Makefile.global.in @@ -1,5 +1,5 @@ # -*-makefile-*- -# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.175 2004/02/10 03:42:42 tgl Exp $ +# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.176 2004/03/10 21:12:46 momjian Exp $ #------------------------------------------------------------------------------ # All PostgreSQL makefiles include this file and use the variables it sets, @@ -339,7 +339,7 @@ endif # # substitute implementations of the C library -LIBOBJS = @LIBOBJS@ path.o pgsleep.o sprompt.o thread.o +LIBOBJS = @LIBOBJS@ noblock.o path.o pgsleep.o sprompt.o thread.o ifneq (,$(LIBOBJS)) LIBS += -lpgport diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index c59cc1d120..95cd52e644 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -13,7 +13,7 @@ * * Copyright (c) 2001-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.59 2004/03/09 05:11:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.60 2004/03/10 21:12:46 momjian Exp $ * ---------- */ #include "postgres.h" @@ -327,7 +327,7 @@ pgstat_init(void) * messages will be discarded; backends won't block waiting to send * messages to the collector. */ - if (FCNTL_NONBLOCK(pgStatSock) < 0) + if (!set_noblock(pgStatSock)) { ereport(LOG, (errcode_for_socket_access(), @@ -1819,7 +1819,7 @@ pgstat_recvbuffer(void) * Set the write pipe to nonblock mode, so that we cannot block when * the collector falls behind. */ - if (FCNTL_NONBLOCK(writePipe) < 0) + if (!set_noblock(writePipe)) { ereport(LOG, (errcode_for_socket_access(), diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 1bd80e1611..2a420bfdbf 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.372 2004/03/09 05:11:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.373 2004/03/10 21:12:46 momjian Exp $ * * NOTES * @@ -219,11 +219,6 @@ bool Db_user_namespace = false; char *rendezvous_name; -/* For FNCTL_NONBLOCK */ -#if defined(WIN32) || defined(__BEOS__) -long ioctlsocket_ret=1; -#endif - /* list of library:init-function to be preloaded */ char *preload_libraries_string = NULL; @@ -2365,7 +2360,7 @@ report_fork_failure_to_client(Port *port, int errnum) strerror(errnum)); /* Set port to non-blocking. Don't do send() if this fails */ - if (FCNTL_NONBLOCK(port->sock) < 0) + if (!set_noblock(port->sock)) return; send(port->sock, buffer, strlen(buffer) + 1, 0); diff --git a/src/include/c.h b/src/include/c.h index 0562157333..325cfc217d 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -12,7 +12,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/c.h,v 1.159 2004/01/10 23:39:51 neilc Exp $ + * $PostgreSQL: pgsql/src/include/c.h,v 1.160 2004/03/10 21:12:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -689,20 +689,6 @@ typedef NameData *Name; #define PG_BINARY_W "w" #endif -#if !defined(WIN32) && !defined(__BEOS__) -#define FCNTL_NONBLOCK(sock) fcntl(sock, F_SETFL, O_NONBLOCK) -#else -extern long ioctlsocket_ret; - -/* Returns non-0 on failure, while fcntl() returns -1 on failure */ -#ifdef WIN32 -#define FCNTL_NONBLOCK(sock) ((ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0) ? 0 : -1) -#endif -#ifdef __BEOS__ -#define FCNTL_NONBLOCK(sock) ((ioctl(sock, FIONBIO, &ioctlsocket_ret) == 0) ? 0 : -1) -#endif -#endif - #if defined(sun) && defined(__sparc__) && !defined(__SVR4) #include #endif diff --git a/src/include/port.h b/src/include/port.h index fefdff117e..4f52075308 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/port.h,v 1.21 2004/03/09 04:49:02 momjian Exp $ + * $PostgreSQL: pgsql/src/include/port.h,v 1.22 2004/03/10 21:12:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -17,6 +17,9 @@ #include #endif +/* non-blocking */ +bool set_noblock(int sock); + /* Portable path handling for Unix/Win32 */ extern bool is_absolute_path(const char *filename); extern char *first_path_separator(const char *filename); diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile index 978aa24b13..a76281c0e8 100644 --- a/src/interfaces/libpq/Makefile +++ b/src/interfaces/libpq/Makefile @@ -4,7 +4,7 @@ # # Copyright (c) 1994, Regents of the University of California # -# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.97 2004/02/02 00:11:31 momjian Exp $ +# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.98 2004/03/10 21:12:46 momjian Exp $ # #------------------------------------------------------------------------- @@ -23,7 +23,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) $(THREAD_CPPFLAGS) -DFRONTEND -DSYS OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \ fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \ dllist.o md5.o ip.o wchar.o encnames.o \ - $(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o thread.o, $(LIBOBJS)) + $(filter crypt.o getaddrinfo.o inet_aton.o nonblock.o snprintf.o strerror.o path.o thread.o, $(LIBOBJS)) ifeq ($(PORTNAME), win32) OBJS+=win32.o endif @@ -52,7 +52,7 @@ backend_src = $(top_srcdir)/src/backend # For port modules, this only happens if configure decides the module # is needed (see filter hack in OBJS, above). -crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c thread.c: % : $(top_srcdir)/src/port/% +crypt.c getaddrinfo.c inet_aton.c nonblock.c snprintf.c strerror.c path.c thread.c: % : $(top_srcdir)/src/port/% rm -f $@ && $(LN_S) $< . md5.c ip.c: % : $(backend_src)/libpq/% @@ -78,4 +78,4 @@ uninstall: uninstall-lib rm -f $(DESTDIR)$(includedir)/libpq-fe.h $(DESTDIR)$(includedir_internal)/libpq-int.h $(DESTDIR)$(includedir_internal)/pqexpbuffer.h clean distclean maintainer-clean: clean-lib - rm -f $(OBJS) crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c thread.c dllist.c md5.c ip.c encnames.c wchar.c + rm -f $(OBJS) crypt.c getaddrinfo.c inet_aton.c nonblock.c snprintf.c strerror.c path.c thread.c dllist.c md5.c ip.c encnames.c wchar.c diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 1086fd7632..6bf07e1e20 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.267 2004/01/09 02:02:43 momjian Exp $ + * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.268 2004/03/10 21:12:47 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -50,11 +50,6 @@ #include "libpq/ip.h" #include "mb/pg_wchar.h" -/* For FNCTL_NONBLOCK */ -#if defined(WIN32) || defined(__BEOS__) -long ioctlsocket_ret=1; -#endif - #define PGPASSFILE ".pgpass" /* fall back options if they are not specified by arguments or defined @@ -779,7 +774,7 @@ update_db_info(PGconn *conn) static int connectMakeNonblocking(PGconn *conn) { - if (FCNTL_NONBLOCK(conn->sock) < 0) + if (!set_noblock(conn->sock)) { char sebuf[256]; diff --git a/src/port/noblock.c b/src/port/noblock.c new file mode 100644 index 0000000000..b7067549cd --- /dev/null +++ b/src/port/noblock.c @@ -0,0 +1,35 @@ +/*------------------------------------------------------------------------- + * + * noblock.c + * set a file descriptor as non-blocking + * + * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * $PostgreSQL: pgsql/src/port/noblock.c,v 1.1 2004/03/10 21:12:49 momjian Exp $ + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include +#include + +bool set_noblock(int sock) +{ +#if !defined(WIN32) && !defined(__BEOS__) + return (fcntl(sock, F_SETFL, O_NONBLOCK) != -1); +#else + long ioctlsocket_ret = 1; + + /* Returns non-0 on failure, while fcntl() returns -1 on failure */ +#ifdef WIN32 + return (ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0); +#endif +#ifdef __BEOS__ + return (ioctl(sock, FIONBIO, &ioctlsocket_ret) == 0); +#endif +#endif +}