Index: head/share/man/man9/socket.9 =================================================================== --- head/share/man/man9/socket.9 +++ head/share/man/man9/socket.9 @@ -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 @@ .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 Index: head/sys/kern/uipc_socket.c =================================================================== --- head/sys/kern/uipc_socket.c +++ head/sys/kern/uipc_socket.c @@ -1101,6 +1101,8 @@ 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 @@ 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; } /* Index: head/sys/sys/socketvar.h =================================================================== --- head/sys/sys/socketvar.h +++ head/sys/sys/socketvar.h @@ -63,6 +63,7 @@ * 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 @@ /* 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 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);