changeset 14013:1a1202688ff3

3666 Implement SOCK_CLOEXEC flag to socket() Reviewed by Dan McDonald <danmcd@nexenta.com> Reviewed by Robert Mustacchi <rm@joyent.com> Approved by Garrett D'Amore <garrett@damore.org>
author Theo Schlossnagle <jesus@omniti.com>
date Fri, 12 Apr 2013 15:07:53 -0400
parents c1f1ea4feeb1
children 626936c65627
files usr/src/cmd/truss/print.c usr/src/man/man3socket/socket.3socket usr/src/uts/common/fs/sockfs/socksyscalls.c usr/src/uts/common/sys/socket.h
diffstat 4 files changed, 46 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/truss/print.c	Tue Apr 02 13:52:19 2013 -0700
+++ b/usr/src/cmd/truss/print.c	Fri Apr 12 15:07:53 2013 -0400
@@ -26,6 +26,8 @@
 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
 /*	  All Rights Reserved  	*/
 
+/* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
+
 #define	_SYSCALL32	/* make 32-bit compat headers visible */
 
 #include <stdio.h>
@@ -1684,11 +1686,17 @@
 prt_skt(private_t *pri, int raw, long val)
 {
 	const char *s;
+	long type = val & SOCK_TYPE_MASK;
 
-	if ((ulong_t)val <= MAX_SOCKTYPES && (s = socktype_codes[val]) != NULL)
+	if ((ulong_t)type <= MAX_SOCKTYPES &&
+	    (s = socktype_codes[type]) != NULL) {
 		outstring(pri, s);
-	else
+		if ((val & SOCK_CLOEXEC) != 0) {
+			outstring(pri, "|SOCK_CLOEXEC");
+		}
+	} else {
 		prt_dec(pri, 0, val);
+	}
 }
 
 
--- a/usr/src/man/man3socket/socket.3socket	Tue Apr 02 13:52:19 2013 -0700
+++ b/usr/src/man/man3socket/socket.3socket	Fri Apr 12 15:07:53 2013 -0400
@@ -1,6 +1,7 @@
 '\" te
 .\" Copyright (C) 2009, Sun Microsystems, Inc. All Rights Reserved.
 .\" Copyright 1989 AT&T
+.\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All Rights Reserved.
 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
@@ -84,6 +85,22 @@
 
 .sp
 .LP
+The \fItype\fR may be augmented by a bitwise-inclusive-OR of flags from the
+following list, defined in <sys/socket.h>.
+
+.sp
+.ne 2
+.na
+\fB\fBSOCK_CLOEXEC\fR\fR
+.ad
+.RS 12n
+Creates the socket with the \fBFD_CLOEXEC\fR flag set, causing the underlying
+file descriptor to be closed prior to any future calls to \fBexec\fR(2). This
+is similar in purpose to the \fBO_CLOEXEC\fR flag to \fBopen\fR(2).
+.RE
+
+.sp
+.LP
 There must be an entry in the \fBnetconfig\fR(4) file for at least each
 protocol family and type required. If  a non-zero protocol has been specified
 but no exact match for the protocol family, type, and protocol is found, then
@@ -267,9 +284,9 @@
 .sp
 .LP
 \fBnca\fR(1), \fBclose\fR(2), \fBfcntl\fR(2), \fBioctl\fR(2), \fBread\fR(2),
-\fBwrite\fR(2), \fBaccept\fR(3SOCKET), \fBbind\fR(3SOCKET),
+\fBwrite\fR(2), \fBaccept\fR(3SOCKET), \fBbind\fR(3SOCKET), \fBexec\fR(2),
 \fBconnect\fR(3SOCKET), \fBgetsockname\fR(3SOCKET), \fBgetsockopt\fR(3SOCKET),
-\fBin.h\fR(3HEAD),\fBlisten\fR(3SOCKET), \fBrecv\fR(3SOCKET),
+\fBin.h\fR(3HEAD),\fBlisten\fR(3SOCKET), \fBrecv\fR(3SOCKET), \fBopen\fR(2),
 \fBsetsockopt\fR(3SOCKET), \fBsend\fR(3SOCKET), \fBshutdown\fR(3SOCKET),
 \fBsocket.h\fR(3HEAD), \fBsocketpair\fR(3SOCKET), \fBattributes\fR(5)
 .SH NOTES
--- a/usr/src/uts/common/fs/sockfs/socksyscalls.c	Tue Apr 02 13:52:19 2013 -0700
+++ b/usr/src/uts/common/fs/sockfs/socksyscalls.c	Fri Apr 12 15:07:53 2013 -0400
@@ -23,6 +23,8 @@
  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
  */
 
+/* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
+
 #include <sys/types.h>
 #include <sys/t_lock.h>
 #include <sys/param.h>
@@ -96,14 +98,17 @@
  * devpath for the kernel to use.
  */
 int
-so_socket(int family, int type, int protocol, char *devpath, int version)
+so_socket(int family, int type_w_flags, int protocol, char *devpath,
+    int version)
 {
 	struct sonode *so;
 	vnode_t *vp;
 	struct file *fp;
 	int fd;
 	int error;
+	int type;
 
+	type = type_w_flags & SOCK_TYPE_MASK;
 	if (devpath != NULL) {
 		char *buf;
 		size_t kdevpathlen = 0;
@@ -137,6 +142,9 @@
 	 */
 	mutex_exit(&fp->f_tlock);
 	setf(fd, fp);
+	if ((type_w_flags & SOCK_CLOEXEC) != 0) {
+		f_setfd(fd, FD_CLOEXEC);
+	}
 
 	return (fd);
 }
--- a/usr/src/uts/common/sys/socket.h	Tue Apr 02 13:52:19 2013 -0700
+++ b/usr/src/uts/common/sys/socket.h	Fri Apr 12 15:07:53 2013 -0400
@@ -35,6 +35,8 @@
  * contributors.
  */
 
+/* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
+
 #ifndef	_SYS_SOCKET_H
 #define	_SYS_SOCKET_H
 
@@ -101,6 +103,12 @@
 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
 #define	SOCK_RDM	5		/* reliably-delivered message */
 #define	SOCK_SEQPACKET	6		/* sequenced packet stream */
+#define	SOCK_TYPE_MASK	0xffff		/* type reside in these bits only */
+
+/*
+ * Flags for socket()
+ */
+#define	SOCK_CLOEXEC	0x80000		/* like open(2) O_CLOEXEC for socket */
 
 /*
  * Option flags per-socket.