From owner-svn-src-head@freebsd.org Fri Jun 8 19:35:26 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2A163FD3BFD; Fri, 8 Jun 2018 19:35:26 +0000 (UTC) (envelope-from jtl@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id CAEB370A4F; Fri, 8 Jun 2018 19:35:25 +0000 (UTC) (envelope-from jtl@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ADB591366F; Fri, 8 Jun 2018 19:35:25 +0000 (UTC) (envelope-from jtl@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w58JZPCH055586; Fri, 8 Jun 2018 19:35:25 GMT (envelope-from jtl@FreeBSD.org) Received: (from jtl@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w58JZPLM055582; Fri, 8 Jun 2018 19:35:25 GMT (envelope-from jtl@FreeBSD.org) Message-Id: <201806081935.w58JZPLM055582@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jtl set sender to jtl@FreeBSD.org using -f From: "Jonathan T. Looney" Date: Fri, 8 Jun 2018 19:35:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334853 - in head: share/man/man9 sys/kern sys/sys X-SVN-Group: head X-SVN-Commit-Author: jtl X-SVN-Commit-Paths: in head: share/man/man9 sys/kern sys/sys X-SVN-Commit-Revision: 334853 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jun 2018 19:35:26 -0000 Author: jtl Date: Fri Jun 8 19:35:24 2018 New Revision: 334853 URL: https://svnweb.freebsd.org/changeset/base/334853 Log: Add a socket destructor callback. This allows kernel providers to set callbacks to perform additional cleanup actions at the time a socket is closed. Michio Honda presented a use for this at BSDCan 2018. (See https://www.bsdcan.org/2018/schedule/events/965.en.html .) Submitted by: Michio Honda (previous version) Reviewed by: lstewart (previous version) Differential Revision: https://reviews.freebsd.org/D15706 Modified: head/share/man/man9/socket.9 head/sys/kern/uipc_socket.c head/sys/sys/socketvar.h Modified: head/share/man/man9/socket.9 ============================================================================== --- head/share/man/man9/socket.9 Fri Jun 8 18:54:47 2018 (r334852) +++ head/share/man/man9/socket.9 Fri Jun 8 19:35:24 2018 (r334853) @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 26, 2014 +.Dd June 8, 2018 .Dt SOCKET 9 .Os .Sh NAME @@ -54,6 +54,11 @@ .Fc .Ft int .Fn sodisconnect "struct socket *so" +.Ft void +.Fo sodtor_set +.Fa "struct socket *so" +.Fa "void (*func)(struct socket *)" +.Fc .Ft struct sockaddr * .Fn sodupsockaddr "const struct sockaddr *sa" "int mflags" .Ft void @@ -369,6 +374,13 @@ be cleared, with .Dv SO_RCV or .Dv SO_SND . +.Ss Socket Destructor Callback +A kernel system can use the +.Fn sodtor_set +function to set a destructor for a socket. +The destructor is called when the socket is closed. +The destructor is called after the protocol close routine has completed. +The destructor can serve as a callback to initiate additional cleanup actions. .Ss Socket I/O The .Fn soreceive Modified: head/sys/kern/uipc_socket.c ============================================================================== --- head/sys/kern/uipc_socket.c Fri Jun 8 18:54:47 2018 (r334852) +++ head/sys/kern/uipc_socket.c Fri Jun 8 19:35:24 2018 (r334853) @@ -1101,6 +1101,8 @@ soclose(struct socket *so) drop: if (so->so_proto->pr_usrreqs->pru_close != NULL) (*so->so_proto->pr_usrreqs->pru_close)(so); + if (so->so_dtor != NULL) + so->so_dtor(so); SOCK_LOCK(so); if ((listening = (so->so_options & SO_ACCEPTCONN))) { @@ -3810,6 +3812,17 @@ sodupsockaddr(const struct sockaddr *sa, int mflags) if (sa2) bcopy(sa, sa2, sa->sa_len); return sa2; +} + +/* + * Register per-socket destructor. + */ +void +sodtor_set(struct socket *so, so_dtor_t *func) +{ + + SOCK_LOCK_ASSERT(so); + so->so_dtor = func; } /* Modified: head/sys/sys/socketvar.h ============================================================================== --- head/sys/sys/socketvar.h Fri Jun 8 18:54:47 2018 (r334852) +++ head/sys/sys/socketvar.h Fri Jun 8 19:35:24 2018 (r334853) @@ -63,6 +63,7 @@ struct vnet; * private data and error information. */ typedef int so_upcall_t(struct socket *, void *, int); +typedef void so_dtor_t(struct socket *); struct socket; @@ -99,6 +100,7 @@ struct socket { /* NB: generation count must not be first. */ so_gen_t so_gencnt; /* (h) generation count */ void *so_emuldata; /* (b) private data for emulators */ + so_dtor_t *so_dtor; /* (b) optional destructor */ struct osd osd; /* Object Specific extensions */ /* * so_fibnum, so_user_cookie and friends can be used to attach @@ -397,6 +399,7 @@ int soconnect2(struct socket *so1, struct socket *so2) int socreate(int dom, struct socket **aso, int type, int proto, struct ucred *cred, struct thread *td); int sodisconnect(struct socket *so); +void sodtor_set(struct socket *, so_dtor_t *); struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); void sofree(struct socket *so); void sohasoutofband(struct socket *so);