Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Feb 2011 23:08:20 +0000 (UTC)
From:      Pawel Jakub Dawidek <pjd@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r218464 - head/sbin/hastd
Message-ID:  <201102082308.p18N8Kgj063051@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pjd
Date: Tue Feb  8 23:08:20 2011
New Revision: 218464
URL: http://svn.freebsd.org/changeset/base/218464

Log:
  Unlink UNIX domain socket file only if:
  1. The descriptor is the one we are listening on (not the one when we connect
     as a client and not the one which is created on accept(2)).
  2. Descriptor was created by us (PID matches with the PID stored on bind(2)).
  
  Reported by:	Mikolaj Golub <to.my.trociny@gmail.com>
  MFC after:	1 week

Modified:
  head/sbin/hastd/proto_uds.c

Modified: head/sbin/hastd/proto_uds.c
==============================================================================
--- head/sbin/hastd/proto_uds.c	Tue Feb  8 22:17:59 2011	(r218463)
+++ head/sbin/hastd/proto_uds.c	Tue Feb  8 23:08:20 2011	(r218464)
@@ -54,6 +54,7 @@ struct uds_ctx {
 #define	UDS_SIDE_CLIENT		0
 #define	UDS_SIDE_SERVER_LISTEN	1
 #define	UDS_SIDE_SERVER_WORK	2
+	pid_t			uc_owner;
 };
 
 static void uds_close(void *ctx);
@@ -109,6 +110,7 @@ uds_common_setup(const char *addr, void 
 	}
 
 	uctx->uc_side = side;
+	uctx->uc_owner = 0;
 	uctx->uc_magic = UDS_CTX_MAGIC;
 	*ctxp = uctx;
 
@@ -167,13 +169,14 @@ uds_server(const char *addr, void **ctxp
 
 	uctx = *ctxp;
 
-	unlink(uctx->uc_sun.sun_path);
+	(void)unlink(uctx->uc_sun.sun_path);
 	if (bind(uctx->uc_fd, (struct sockaddr *)&uctx->uc_sun,
 	    sizeof(uctx->uc_sun)) < 0) {
 		ret = errno;
 		uds_close(uctx);
 		return (ret);
 	}
+	uctx->uc_owner = getpid();
 	if (listen(uctx->uc_fd, 8) < 0) {
 		ret = errno;
 		uds_close(uctx);
@@ -200,9 +203,9 @@ uds_accept(void *ctx, void **newctxp)
 	if (newuctx == NULL)
 		return (errno);
 
-	fromlen = sizeof(uctx->uc_sun);
-	newuctx->uc_fd = accept(uctx->uc_fd, (struct sockaddr *)&uctx->uc_sun,
-	    &fromlen);
+	fromlen = sizeof(newuctx->uc_sun);
+	newuctx->uc_fd = accept(uctx->uc_fd,
+	    (struct sockaddr *)&newuctx->uc_sun, &fromlen);
 	if (newuctx->uc_fd < 0) {
 		ret = errno;
 		free(newuctx);
@@ -309,7 +312,15 @@ uds_close(void *ctx)
 
 	if (uctx->uc_fd >= 0)
 		close(uctx->uc_fd);
-	unlink(uctx->uc_sun.sun_path);
+	/*
+	 * Unlink the socket only if we are the owner and this is descriptor
+	 * we listen on.
+	 */
+	if (uctx->uc_side == UDS_SIDE_SERVER_LISTEN &&
+	    uctx->uc_owner == getpid()) {
+		(void)unlink(uctx->uc_sun.sun_path);
+	}
+	uctx->uc_owner = 0;
 	uctx->uc_magic = 0;
 	free(uctx);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201102082308.p18N8Kgj063051>