From owner-svn-src-all@FreeBSD.ORG Tue Feb 8 23:08:20 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D1CA9106564A; Tue, 8 Feb 2011 23:08:20 +0000 (UTC) (envelope-from pjd@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id A6B0C8FC1B; Tue, 8 Feb 2011 23:08:20 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p18N8K0Q063053; Tue, 8 Feb 2011 23:08:20 GMT (envelope-from pjd@svn.freebsd.org) Received: (from pjd@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p18N8Kgj063051; Tue, 8 Feb 2011 23:08:20 GMT (envelope-from pjd@svn.freebsd.org) Message-Id: <201102082308.p18N8Kgj063051@svn.freebsd.org> From: Pawel Jakub Dawidek Date: Tue, 8 Feb 2011 23:08:20 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r218464 - head/sbin/hastd X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Feb 2011 23:08:20 -0000 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 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); }