import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / socket / socket.c
blob3f8166ed26d92f7c78b008dc4820e30c50589d69
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
31 * All Rights Reserved
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
35 * contributors.
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/stropts.h>
41 #include <sys/stream.h>
42 #include <sys/socketvar.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <stdlib.h>
47 extern int _so_socket();
48 extern int _s_netconfig_path();
50 #pragma weak __xnet_socket = socket
52 int
53 socket(int family, int type, int protocol)
55 int fd;
58 * Try creating without knowing the device assuming that
59 * the transport provider is registered in /etc/sock2path.d.
60 * If none found fall back to using /etc/netconfig to look
61 * up the name of the transport device name. This provides
62 * backwards compatibility for transport providers that have not
63 * yet been converted to using /etc/sock2path.d.
64 * XXX When all transport providers use /etc/sock2path.d. this
65 * part of the code can be removed.
67 fd = _so_socket(family, type, protocol, NULL);
68 if (fd == -1) {
69 char *devpath;
70 int saved_errno = errno;
71 int prototype = 0;
73 switch (saved_errno) {
74 case EAFNOSUPPORT:
75 case EPROTOTYPE:
76 break;
77 case EPROTONOSUPPORT:
78 break;
80 default:
81 errno = saved_errno;
82 return (-1);
84 if (_s_netconfig_path(family, type, protocol,
85 &devpath, &prototype) == -1) {
86 errno = saved_errno;
87 return (-1);
89 fd = _so_socket(family, type, protocol, devpath);
90 free(devpath);
91 if (fd == -1) {
92 errno = saved_errno;
93 return (-1);
95 if (prototype != 0) {
96 if (setsockopt(fd, SOL_SOCKET, SO_PROTOTYPE,
97 (caddr_t)&prototype, (int)sizeof (prototype)) < 0) {
98 (void) close(fd);
100 * setsockopt often fails with ENOPROTOOPT
101 * but socket() should fail with
102 * EPROTONOSUPPORT.
104 errno = EPROTONOSUPPORT;
105 return (-1);
109 return (fd);