/
my-unixdom.c
197 lines (168 loc) · 4.11 KB
/
my-unixdom.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <my-unixdom.h>
void
null(void)
{
printf("Hello, world! This is null()\r\n");
}
#define IS_SERVER 1
#define IS_ABSTRACT 2
#define IS_NULLTERM 4
int
my_open(char *path, int flags)
{
struct stat sb;
struct sockaddr_un sin;
size_t sin_len;
int s;
int res;
int is_server = !!(flags & IS_SERVER);
int is_abstract = !!(flags & IS_ABSTRACT);
int is_nullterm = !!(flags & IS_NULLTERM);
/* Race condition exists, but is better than nothing */
if (is_server && !is_abstract && stat(path, &sb) == 0) {
errno = EEXIST;
return -1;
}
if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sun_family = AF_UNIX;
/* Path to abstract unix domain contains \0 in first position. */
/* TODO abstract path doesn't need to be null terminated! */
if (is_abstract) {
sin.sun_path[0] = '\0';
strncpy(sin.sun_path+1, path, sizeof(sin.sun_path)-1);
} else {
strncpy(sin.sun_path, path, sizeof(sin.sun_path));
}
if (is_nullterm) {
if (is_abstract)
sin_len = sizeof(sin) - sizeof(sin.sun_path)
+ strlen(sin.sun_path+1) + 1;
else
sin_len = sizeof(sin) - sizeof(sin.sun_path)
+ strlen(sin.sun_path);
} else
sin_len = sizeof(sin);
#if defined(SUN_LEN) && (! (defined(solaris) || defined(linux)))
sin.sun_len = SUN_LEN(&sin);
#endif /* SUN_LEN */
if (is_server) {
res = bind(s, (struct sockaddr *) &sin, sin_len);
} else {
res = connect(s, (struct sockaddr *) &sin, sin_len);
}
if (res < 0) {
return -1;
} else {
return s;
}
}
int
my_getfd(int fd)
{
return fd;
}
int
my_sendfd(int fd, int wfd)
{
if (writefd(fd, wfd) >= 0) {
return 0;
} else {
return -1;
}
}
int
my_receivefd(int fd)
{
int passed_fd = -42;
if (readfd(fd, &passed_fd) < 0) {
return -1;
} else {
return passed_fd;
}
}
/*
** My interpretation of Steven's file descriptor passing code.
*/
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <netinet/in.h> /* htonl() et al */
#ifndef TCP_NODELAY
#include <netinet/tcp.h>
#endif /* TCP_NODELAY */
#include <sys/uio.h>
ssize_t
writevfd(int fd, const struct iovec *iov, int iovcnt, int wfd)
{
struct msghdr mh;
char cmhbuf[sizeof(struct cmsghdr) + sizeof(int)];
struct cmsghdr *const cmh = (struct cmsghdr *) cmhbuf;
int *const fdp = (int *) (cmhbuf + sizeof(struct cmsghdr));
*fdp = wfd;
memset(&mh, 0, sizeof(mh));
mh.msg_iov = (struct iovec *) iov;
mh.msg_iovlen = iovcnt;
mh.msg_control = (char *) cmh;
mh.msg_controllen = cmh->cmsg_len = sizeof(cmhbuf) /* + sizeof(int) */;
cmh->cmsg_level = SOL_SOCKET;
cmh->cmsg_type = SCM_RIGHTS;
return sendmsg(fd, &mh, 0);
}
ssize_t
writefd(int fd, int wfd)
{
void *buf = "b"; /* Gotta send at least 1 byte along with msg */
size_t len = 1;
struct iovec iov[1];
iov->iov_base = (void *) buf;
iov->iov_len = len;
return writevfd(fd, iov, 1, wfd);
}
ssize_t
readvfd(int fd, const struct iovec *iov, int iovcnt, int *rfdp)
{
struct msghdr mh;
char cmhbuf[sizeof(struct cmsghdr) + sizeof(int)];
struct cmsghdr *const cmh = (struct cmsghdr *) cmhbuf;
int *const fdp = (int *) (cmhbuf + sizeof(struct cmsghdr));
int n;
*fdp = -1;
memset(&mh, 0, sizeof(mh));
mh.msg_iov = (struct iovec *) iov;
mh.msg_iovlen = iovcnt;
mh.msg_control = (char *) cmh;
mh.msg_controllen = cmh->cmsg_len = sizeof(cmhbuf) /* + sizeof(int) */;
cmh->cmsg_level = SOL_SOCKET;
cmh->cmsg_type = SCM_RIGHTS;
n = recvmsg(fd, &mh, 0);
*rfdp = *fdp;
return n;
}
ssize_t
readfd(int fd, int *rfdp)
{
char readbuf[128];
size_t len = sizeof(readbuf);
struct iovec iov[1];
iov->iov_base = readbuf;
iov->iov_len = len;
return readvfd(fd, iov, 1, rfdp);
}