8 #include <net/netlib.h>
9 #include <sys/ioc_net.h>
10 #include <sys/socket.h>
15 __weak_alias(socketpair
, _socketpair
)
20 static int _uds_socketpair(int type
, int protocol
, int sv
[2]);
23 * Create a pair of connected sockets
25 int socketpair(int domain
, int type
, int protocol
, int sv
[2]) {
28 fprintf(stderr
, "socketpair: domain %d, type %d, protocol %d\n",
29 domain
, type
, protocol
);
32 if (domain
!= AF_UNIX
)
38 if (domain
== AF_UNIX
&&
39 (type
== SOCK_STREAM
|| type
== SOCK_SEQPACKET
))
40 return _uds_socketpair(type
, protocol
, sv
);
44 "socketpair: nothing for domain %d, type %d, protocol %d\n",
45 domain
, type
, protocol
);
52 static int _uds_socketpair(int type
, int protocol
, int sv
[2])
61 fprintf(stderr
, "socketpair(uds): bad protocol %d\n", protocol
);
63 errno
= EPROTONOSUPPORT
;
67 /* in this 'for' loop two unconnected sockets are created */
68 for (i
= 0; i
< 2; i
++) {
69 sv
[i
]= open(UDS_DEVICE
, O_RDWR
);
71 int open_errno
= errno
;
74 /* if we failed to open() the 2nd
75 * socket, we need to close the 1st
84 /* set the type for the socket via ioctl
85 * (SOCK_STREAM, SOCK_SEQPACKET, etc)
87 r
= ioctl(sv
[i
], NWIOSUDSTYPE
, &type
);
91 /* if that failed rollback socket creation */
96 /* if we just closed the 2nd socket, we
97 * need to close the 1st
102 /* return the error thrown by the call to ioctl */
108 r
= fstat(sv
[1], &sbuf
);
112 /* if that failed rollback socket creation */
118 /* return the error thrown by the call to fstat */
125 /* connect the sockets sv[0] and sv[1] */
126 r
= ioctl(sv
[0], NWIOSUDSPAIR
, &dev
);
130 /* if that failed rollback socket creation */
136 /* return the error thrown by the call to ioctl */