import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / socket / socketpair.c
blobda7443ed8217e937f4e34988f317210dac1cdbf5
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/stropts.h>
45 #include <sys/stream.h>
46 #include <sys/socketvar.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <stdlib.h>
51 extern int _so_socketpair(int*);
53 #pragma weak __xnet_socketpair = socketpair
55 int
56 socketpair(int family, int type, int protocol, int sv[2])
58 int res;
59 int fd1, fd2;
62 * Create the two sockets and pass them to _so_socketpair, which
63 * will connect them together.
65 fd1 = socket(family, type, protocol);
66 if (fd1 < 0)
67 return (-1);
68 fd2 = socket(family, type, protocol);
69 if (fd2 < 0) {
70 int error = errno;
72 (void) close(fd1);
73 errno = error;
74 return (-1);
76 sv[0] = fd1;
77 sv[1] = fd2;
78 res = _so_socketpair(sv);
79 if (res < 0) {
80 int error = errno;
82 (void) close(fd1);
83 (void) close(fd2);
84 errno = error;
85 return (-1);
88 * Check if kernel returned different fds in which case we close
89 * the original ones. This is the case for SOCK_STREAM where
90 * one of the original sockets is used as a listener and
91 * _so_socketpair passes out the newly accepted socket.
93 if (sv[0] != fd1)
94 (void) close(fd1);
95 if (sv[1] != fd2)
96 (void) close(fd2);
97 return (res);