[Postgres-xl-developers] [FreeBSD] fix pool send and recv fds

Jov amutu at amutu.com
Tue Dec 16 20:08:46 PST 2014


This fix the runtime error when running pgxl on FreeBSD.
Pgxl backend use ancillary data send and recv connection fds from pooler
process.But it use ancillary api with a dangerous way.It assume the send
and recved ancillary data memory is aligned and use explicit pointer
instead of the CMSG_* macros.For linux,it is lucky and just work,but for
FreeBSD,backend receive wrong fds and cause 'bus error'.The fix is to
change code to use CMSG_* macros.

The patch is test on FreeBSD10_amd64 and CentOS_6.5 x86_64.
Some one use the patch on FreeBSD 8 last week and say it work fine.

diff --git a/src/backend/pgxc/pool/poolcomm.c
b/src/backend/pgxc/pool/poolcomm.c
index d512471..591aff0 100644
--- a/src/backend/pgxc/pool/poolcomm.c
+++ b/src/backend/pgxc/pool/poolcomm.c
@@ -503,7 +503,7 @@ pool_sendfds(PoolPort *port, int *fds, int count)
  struct msghdr msg;
  char buf[SEND_MSG_BUFFER_SIZE];
  uint n32;
- int controllen = sizeof(struct cmsghdr) + count * sizeof(int);
+ int                     controllen =  CMSG_LEN(count * sizeof(int));
  struct cmsghdr *cmptr = NULL;

  buf[0] = 'f';
@@ -525,7 +525,7 @@ pool_sendfds(PoolPort *port, int *fds, int count)
  }
  else
  {
- if ((cmptr = malloc(controllen)) == NULL)
+ if ((cmptr = malloc(CMSG_SPACE(count * sizeof(int)))) == NULL)
  return EOF;
  cmptr->cmsg_level = SOL_SOCKET;
  cmptr->cmsg_type = SCM_RIGHTS;
@@ -533,7 +533,7 @@ pool_sendfds(PoolPort *port, int *fds, int count)
  msg.msg_control = (caddr_t) cmptr;
  msg.msg_controllen = controllen;
  /* the fd to pass */
- memcpy(CMSG_DATA(cmptr), fds, count * sizeof(int));
+ memcpy(CMSG_DATA(CMSG_FIRSTHDR(&msg)), fds, count * sizeof(int));
  }

  if (sendmsg(Socket(*port), &msg, 0) != SEND_MSG_BUFFER_SIZE)
@@ -561,8 +561,8 @@ pool_recvfds(PoolPort *port, int *fds, int count)
  char buf[SEND_MSG_BUFFER_SIZE];
  struct iovec iov[1];
  struct msghdr msg;
- int controllen = sizeof(struct cmsghdr) + count * sizeof(int);
- struct cmsghdr *cmptr = malloc(controllen);
+ int                     controllen = CMSG_LEN(count * sizeof(int));
+ struct cmsghdr *cmptr = malloc(CMSG_SPACE(count * sizeof(int)));

  if (cmptr == NULL)
  return EOF;
@@ -642,7 +642,7 @@ pool_recvfds(PoolPort *port, int *fds, int count)
  goto failure;
  }

- memcpy(fds, CMSG_DATA(cmptr), count * sizeof(int));
+ memcpy(fds, CMSG_DATA(CMSG_FIRSTHDR(&msg)), count * sizeof(int));
  free(cmptr);
  return 0;
 failure:

ref:
https://books.google.com.hk/books?id=ptSC4LpwGA0C&pg=PA397&lpg=PA397&dq=cmsghdr&source=bl&ots=Ks1FSohnOv&sig=2FQVbvORzOeEIE6b8t0lXBSXQ50&hl=zh-CN&sa=X&ei=0_qQVPqKNIr58QWvu4CYBA&ved=0CCoQ6AEwAjgK#v=onepage&q=cmsghdr&f=false
Jov
blog: http:amutu.com/blog <http://amutu.com/blog>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.postgres-xl.org/private.cgi/postgres-xl-developers-postgres-xl.org/attachments/20141217/a64c3da3/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fix_pool_send_recv_fds.patch
Type: application/octet-stream
Size: 1846 bytes
Desc: not available
URL: <http://lists.postgres-xl.org/private.cgi/postgres-xl-developers-postgres-xl.org/attachments/20141217/a64c3da3/attachment.obj>


More information about the Postgres-xl-developers mailing list