summaryrefslogtreecommitdiff
path: root/minix/lib/libc/sys/socketpair.c
blob: 1b1767dcd773a12bd1d8987b363fc832598c0e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <sys/cdefs.h>
#include "namespace.h"
#include <lib.h>

#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <net/netlib.h>
#include <sys/ioctl.h>
#include <sys/ioc_net.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>

#define DEBUG 0

static int _uds_socketpair(int type, int protocol, int sv[2]);

/*
 * Create a pair of connected sockets.
 */
static int
__socketpair(int domain, int type, int protocol, int sv[2])
{
	message m;

	memset(&m, 0, sizeof(m));
	m.m_lc_vfs_socket.domain = domain;
	m.m_lc_vfs_socket.type = type;
	m.m_lc_vfs_socket.protocol = protocol;

	if (_syscall(VFS_PROC_NR, VFS_SOCKETPAIR, &m) < 0)
		return -1;

	sv[0] = m.m_vfs_lc_fdpair.fd0;
	sv[1] = m.m_vfs_lc_fdpair.fd1;
	return 0;
}

int
socketpair(int domain, int type, int protocol, int sv[2])
{
	int r;

	r = __socketpair(domain, type, protocol, sv);
	if (r != -1 || (errno != EAFNOSUPPORT && errno != ENOSYS))
		return r;

#if DEBUG
	fprintf(stderr, "socketpair: domain %d, type %d, protocol %d\n",
		domain, type, protocol);
#endif

	if (domain == AF_UNIX)
		return _uds_socketpair(type, protocol, sv);

	errno = EAFNOSUPPORT;
	return -1;
}

static int _uds_socketpair(int type, int protocol, int sv[2])
{
	dev_t dev;
	int r, i;
	struct stat sbuf;

	if (type != SOCK_STREAM && type != SOCK_SEQPACKET) {
		errno = EPROTOTYPE;
		return -1;
	}

	if (protocol != 0)
	{
#if DEBUG
		fprintf(stderr, "socketpair(uds): bad protocol %d\n", protocol);
#endif
		errno= EPROTONOSUPPORT;
		return -1;
	}

	/* in this 'for' loop two unconnected sockets are created */
	for (i = 0; i < 2; i++) {
		sv[i]= open(UDS_DEVICE, O_RDWR);
		if (sv[i] == -1) {
			int open_errno = errno;

			if (i == 1) {
				/* if we failed to open() the 2nd 
				 * socket, we need to close the 1st
				 */
				close(sv[0]);
				errno = open_errno;
			}

			return -1;
		}

		/* set the type for the socket via ioctl
		 * (SOCK_STREAM, SOCK_SEQPACKET, etc)
		 */
		r= ioctl(sv[i], NWIOSUDSTYPE, &type);
		if (r == -1) {
			int ioctl_errno;

			/* if that failed rollback socket creation */
			ioctl_errno= errno;
			close(sv[i]);

			if (i == 1) {
				/* if we just closed the 2nd socket, we 
				 * need to close the 1st
				 */
				close(sv[0]);
			}

			/* return the error thrown by the call to ioctl */
			errno= ioctl_errno;
			return -1;
		}
	}

	r= fstat(sv[1], &sbuf);
	if (r == -1) {
		int fstat_errno;

		/* if that failed rollback socket creation */
		fstat_errno= errno;

		close(sv[0]);
		close(sv[1]);

		/* return the error thrown by the call to fstat */
		errno= fstat_errno;
		return -1;
	}

	dev = sbuf.st_rdev;

	/* connect the sockets sv[0] and sv[1] */
	r= ioctl(sv[0], NWIOSUDSPAIR, &dev);
	if (r == -1) {
		int ioctl_errno;

		/* if that failed rollback socket creation */
		ioctl_errno= errno;

		close(sv[0]);
		close(sv[1]);

		/* return the error thrown by the call to ioctl */
		errno= ioctl_errno;
		return -1;
	}


	return 0;
}