Re: [HACKERS] close() vs. closesocket() - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [HACKERS] close() vs. closesocket()
Date
Msg-id 200304250120.h3P1K3701678@candle.pha.pa.us
Whole thread Raw
List pgsql-patches
The following applied patch uses closesocket() for all socket closing
because Win32 requires it, and uses a #define to map that to close() on
Unix.

This patch removes several #ifdef WIN32 defines from libpq.  Yea!

---------------------------------------------------------------------------

Bruce Momjian wrote:
> Looking at libpq, you can see Win32 requires closesocket() while Unix
> uses just uses close().
>
> I have to add this type of change to the backend for Win32, so I am
> inclined to make all the socket close calls closesocket() and #define
> that as close() on Unix?  It would remove quite a few Win32 defs from
> libpq too.
>
> Comments?
>
> --
>   Bruce Momjian                        |  http://candle.pha.pa.us
>   pgman@candle.pha.pa.us               |  (610) 359-1001
>   +  If your life is a hard drive,     |  13 Roberts Road
>   +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/libpq/hba.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/libpq/hba.c,v
retrieving revision 1.99
diff -c -c -r1.99 hba.c
*** src/backend/libpq/hba.c    17 Apr 2003 22:26:01 -0000    1.99
--- src/backend/libpq/hba.c    25 Apr 2003 01:16:51 -0000
***************
*** 1212,1218 ****
                                                              ident_user);
                  }
              }
!             close(sock_fd);
          }
      }
      return ident_return;
--- 1212,1218 ----
                                                              ident_user);
                  }
              }
!             closesocket(sock_fd);
          }
      }
      return ident_return;
Index: src/backend/libpq/pqcomm.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/libpq/pqcomm.c,v
retrieving revision 1.151
diff -c -c -r1.151 pqcomm.c
*** src/backend/libpq/pqcomm.c    22 Apr 2003 00:08:06 -0000    1.151
--- src/backend/libpq/pqcomm.c    25 Apr 2003 01:16:52 -0000
***************
*** 150,156 ****
      if (MyProcPort != NULL)
      {
          secure_close(MyProcPort);
!         close(MyProcPort->sock);
          /* make sure any subsequent attempts to do I/O fail cleanly */
          MyProcPort->sock = -1;
      }
--- 150,156 ----
      if (MyProcPort != NULL)
      {
          secure_close(MyProcPort);
!         closesocket(MyProcPort->sock);
          /* make sure any subsequent attempts to do I/O fail cleanly */
          MyProcPort->sock = -1;
      }
***************
*** 228,234 ****
          snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
          service = portNumberStr;
      }
!
      ret = getaddrinfo2(hostName, service, &hint, &addrs);
      if (ret || addrs == NULL)
      {
--- 228,234 ----
          snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
          service = portNumberStr;
      }
!
      ret = getaddrinfo2(hostName, service, &hint, &addrs);
      if (ret || addrs == NULL)
      {
***************
*** 470,476 ****
  void
  StreamClose(int sock)
  {
!     close(sock);
  }

  /*
--- 470,476 ----
  void
  StreamClose(int sock)
  {
!     closesocket(sock);
  }

  /*
Index: src/backend/postmaster/pgstat.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/postmaster/pgstat.c,v
retrieving revision 1.32
diff -c -c -r1.32 pgstat.c
*** src/backend/postmaster/pgstat.c    20 Mar 2003 03:34:56 -0000    1.32
--- src/backend/postmaster/pgstat.c    25 Apr 2003 01:16:53 -0000
***************
*** 235,241 ****

  startup_failed:
      if (pgStatSock >= 0)
!         close(pgStatSock);
      pgStatSock = -1;

      /* Adjust GUC variables to suppress useless activity */
--- 235,241 ----

  startup_failed:
      if (pgStatSock >= 0)
!         closesocket(pgStatSock);
      pgStatSock = -1;

      /* Adjust GUC variables to suppress useless activity */
***************
*** 359,368 ****
  pgstat_close_sockets(void)
  {
      if (pgStatPmPipe[0] >= 0)
!         close(pgStatPmPipe[0]);
      pgStatPmPipe[0] = -1;
      if (pgStatPmPipe[1] >= 0)
!         close(pgStatPmPipe[1]);
      pgStatPmPipe[1] = -1;
  }

--- 359,368 ----
  pgstat_close_sockets(void)
  {
      if (pgStatPmPipe[0] >= 0)
!         closesocket(pgStatPmPipe[0]);
      pgStatPmPipe[0] = -1;
      if (pgStatPmPipe[1] >= 0)
!         closesocket(pgStatPmPipe[1]);
      pgStatPmPipe[1] = -1;
  }

***************
*** 1120,1126 ****
       * Close the writing end of the postmaster pipe, so we'll see it
       * closing when the postmaster terminates and can terminate as well.
       */
!     close(pgStatPmPipe[1]);
      pgStatPmPipe[1] = -1;

      /*
--- 1120,1126 ----
       * Close the writing end of the postmaster pipe, so we'll see it
       * closing when the postmaster terminates and can terminate as well.
       */
!     closesocket(pgStatPmPipe[1]);
      pgStatPmPipe[1] = -1;

      /*
***************
*** 1167,1179 ****

          case 0:
              /* child becomes collector process */
!             close(pgStatPipe[1]);
!             close(pgStatSock);
              break;

          default:
              /* parent becomes buffer process */
!             close(pgStatPipe[0]);
              pgstat_recvbuffer();
              exit(0);
      }
--- 1167,1179 ----

          case 0:
              /* child becomes collector process */
!             closesocket(pgStatPipe[1]);
!             closesocket(pgStatSock);
              break;

          default:
              /* parent becomes buffer process */
!             closesocket(pgStatPipe[0]);
              pgstat_recvbuffer();
              exit(0);
      }
Index: src/include/c.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/include/c.h,v
retrieving revision 1.139
diff -c -c -r1.139 c.h
*** src/include/c.h    22 Apr 2003 02:18:09 -0000    1.139
--- src/include/c.h    25 Apr 2003 01:16:55 -0000
***************
*** 721,726 ****
--- 721,733 ----
  #define unlink(from, to)    pgunlink(from, to)
  #endif

+ /*
+  *    Win32 requires a special close for sockets and pipes, while on Unix
+  *    close() does them all.
+  */
+ #ifndef WIN32
+ #define    closesocket close
+ #endif

  /* These are for things that are one way on Unix and another on NT */
  #define NULL_DEV        "/dev/null"
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.234
diff -c -c -r1.234 fe-connect.c
*** src/interfaces/libpq/fe-connect.c    22 Apr 2003 00:08:07 -0000    1.234
--- src/interfaces/libpq/fe-connect.c    25 Apr 2003 01:16:58 -0000
***************
*** 956,962 ****
          /* ignore connect() failure if we have more addrs to try */
          if (addr_cur->ai_next != NULL)
          {
!             close(conn->sock);
              conn->sock = -1;
              continue;
          }
--- 956,962 ----
          /* ignore connect() failure if we have more addrs to try */
          if (addr_cur->ai_next != NULL)
          {
!             closesocket(conn->sock);
              conn->sock = -1;
              continue;
          }
***************
*** 1015,1025 ****
              if (conn->Pfdebug)
                  fprintf(conn->Pfdebug, "Postmaster reports error, attempting fallback to pre-7.0.\n");
              pqsecure_close(conn);
- #ifdef WIN32
              closesocket(conn->sock);
- #else
-             close(conn->sock);
- #endif
              conn->sock = -1;
              conn->allow_ssl_try = FALSE;
              return connectDBStart(conn);
--- 1015,1021 ----
***************
*** 1056,1066 ****
      if (conn->sock >= 0)
      {
          pqsecure_close(conn);
- #ifdef WIN32
          closesocket(conn->sock);
- #else
-         close(conn->sock);
- #endif
          conn->sock = -1;
      }
      conn->status = CONNECTION_BAD;
--- 1052,1058 ----
***************
*** 1935,1945 ****
      if (conn->sock >= 0)
      {
          pqsecure_close(conn);
- #ifdef WIN32
          closesocket(conn->sock);
- #else
-         close(conn->sock);
- #endif
      }
      if (conn->pghost)
          free(conn->pghost);
--- 1927,1933 ----
***************
*** 2010,2020 ****
      if (conn->sock >= 0)
      {
          pqsecure_close(conn);
- #ifdef WIN32
          closesocket(conn->sock);
- #else
-         close(conn->sock);
- #endif
      }
      conn->sock = -1;
      conn->status = CONNECTION_BAD;        /* Well, not really _bad_ - just
--- 1998,2004 ----
***************
*** 2194,2204 ****
      }

      /* Sent it, done */
- #ifdef WIN32
      closesocket(tmpsock);
      WSASetLastError(save_errno);
  #else
-     close(tmpsock);
      errno = save_errno;
  #endif

--- 2178,2187 ----
      }

      /* Sent it, done */
      closesocket(tmpsock);
+ #ifdef WIN32
      WSASetLastError(save_errno);
  #else
      errno = save_errno;
  #endif

***************
*** 2210,2220 ****
      conn->errorMessage.len = strlen(conn->errorMessage.data);
      if (tmpsock >= 0)
      {
- #ifdef WIN32
          closesocket(tmpsock);
          WSASetLastError(save_errno);
  #else
-         close(tmpsock);
          errno = save_errno;
  #endif
      }
--- 2193,2202 ----
      conn->errorMessage.len = strlen(conn->errorMessage.data);
      if (tmpsock >= 0)
      {
          closesocket(tmpsock);
+ #ifdef WIN32
          WSASetLastError(save_errno);
  #else
          errno = save_errno;
  #endif
      }
Index: src/interfaces/libpq/fe-exec.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-exec.c,v
retrieving revision 1.130
diff -c -c -r1.130 fe-exec.c
*** src/interfaces/libpq/fe-exec.c    22 Apr 2003 00:08:07 -0000    1.130
--- src/interfaces/libpq/fe-exec.c    25 Apr 2003 01:17:00 -0000
***************
*** 1154,1164 ****
                        id, msgLength);
      conn->status = CONNECTION_BAD;        /* No more connection to backend */
      pqsecure_close(conn);
- #ifdef WIN32
      closesocket(conn->sock);
- #else
-     close(conn->sock);
- #endif
      conn->sock = -1;
  }

--- 1154,1160 ----
Index: src/interfaces/libpq/fe-misc.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-misc.c,v
retrieving revision 1.90
diff -c -c -r1.90 fe-misc.c
*** src/interfaces/libpq/fe-misc.c    22 Apr 2003 00:08:07 -0000    1.90
--- src/interfaces/libpq/fe-misc.c    25 Apr 2003 01:17:01 -0000
***************
*** 681,691 ****
                           "\tbefore or while processing the request.\n"));
      conn->status = CONNECTION_BAD;        /* No more connection to backend */
      pqsecure_close(conn);
- #ifdef WIN32
      closesocket(conn->sock);
- #else
-     close(conn->sock);
- #endif
      conn->sock = -1;

      return -1;
--- 681,687 ----
Index: src/interfaces/python/pgmodule.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/python/pgmodule.c,v
retrieving revision 1.45
diff -c -c -r1.45 pgmodule.c
*** src/interfaces/python/pgmodule.c    25 Mar 2003 02:44:36 -0000    1.45
--- src/interfaces/python/pgmodule.c    25 Apr 2003 01:17:03 -0000
***************
*** 2405,2414 ****
                  n = j;        /* never used before this assignment */
          }
          if (n)
!         {
              /* allocate buffer */
              if (!(buffer = malloc(MAX_BUFFER_SIZE)))
!             {
                  PyErr_SetString(PyExc_MemoryError,
                      "can't allocate insert buffer.");
                  return NULL;
--- 2405,2414 ----
                  n = j;        /* never used before this assignment */
          }
          if (n)
!         {
              /* allocate buffer */
              if (!(buffer = malloc(MAX_BUFFER_SIZE)))
!             {
                  PyErr_SetString(PyExc_MemoryError,
                      "can't allocate insert buffer.");
                  return NULL;
***************
*** 2438,2444 ****
                      getsubitem = PyTuple_GetItem;
                  else
                      getsubitem = PyList_GetItem;
!
                  /* builds insert line */
                  bufpt=buffer;
                  bufsiz = MAX_BUFFER_SIZE - 1;
--- 2438,2444 ----
                      getsubitem = PyTuple_GetItem;
                  else
                      getsubitem = PyList_GetItem;
!
                  /* builds insert line */
                  bufpt=buffer;
                  bufsiz = MAX_BUFFER_SIZE - 1;
***************
*** 2527,2533 ****
                      {
                          *bufpt++ = '\t'; --bufsiz;
                      }
!
                      if (bufsiz <= 0)
                      {
                          free(buffer);
--- 2527,2533 ----
                      {
                          *bufpt++ = '\t'; --bufsiz;
                      }
!
                      if (bufsiz <= 0)
                      {
                          free(buffer);
***************
*** 2543,2549 ****
                  /* sends data */
                  PQputline(self->cnx, buffer);
              }
!
              /* ends query */
              PQputline(self->cnx, "\\.\n");
              PQendcopy(self->cnx);
--- 2543,2549 ----
                  /* sends data */
                  PQputline(self->cnx, buffer);
              }
!
              /* ends query */
              PQputline(self->cnx, "\\.\n");
              PQendcopy(self->cnx);

pgsql-patches by date:

Previous
From: "Christopher Kings-Lynne"
Date:
Subject: Re: log_min_duration_statement #2
Next
From: Alvaro Herrera
Date:
Subject: 1st nested trasanctions supporting patch