driverd.c
5.17 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <string.h>
#include <ctype.h>
/* cannot include rpc/pmap_clnt.h XXX hal what is this for? */
extern u_short pmap_getport(struct sockaddr_in *, u_long,u_long,u_int);
#ifdef __linux__
#define setoserror(x) (errno=x)
#define oserror() (errno)
#endif
#include "driverd.h"
#include "sys/u64gio.h"
int MakeDriverDConnection (
char *phostname,
int iserver,
int retries,
int *familyp) /* RETURN */
{
char hostnamebuf[256]; /* tmp space */
unsigned long hostinetaddr; /* result of inet_addr of arpa addr */
struct sockaddr_in inaddr; /* IP socket */
struct sockaddr *addr; /* generic socket pointer */
int addrlen; /* length of addr */
struct hostent *hp; /* entry in hosts table */
int fd; /* file descriptor to return */
#define INVALID_INETADDR ((unsigned long) -1)
if (!phostname) {
hostnamebuf[0] = '\0';
gethostname(hostnamebuf, sizeof hostnamebuf);
phostname = hostnamebuf;
}
/*
* if numeric host name then try to parse it as such; do the number
* first because some systems return garbage instead of INVALID_INETADDR
*/
if (isascii(phostname[0]) && isdigit(phostname[0])) {
hostinetaddr = inet_addr (phostname);
} else {
hostinetaddr = INVALID_INETADDR;
}
/*
* try numeric
*/
if (hostinetaddr == INVALID_INETADDR) {
if ((hp = gethostbyname(phostname)) == NULL) {
/* No such host! */
return -1;
}
if (hp->h_addrtype != AF_INET) { /* is IP host? */
/* Not an Internet host! */
return -1;
}
/* Set up the socket data. */
inaddr.sin_family = hp->h_addrtype;
bcopy ((char *)hp->h_addr, (char *)&inaddr.sin_addr,
sizeof(inaddr.sin_addr));
} else {
inaddr.sin_addr.s_addr = hostinetaddr;
inaddr.sin_family = AF_INET;
}
addr = (struct sockaddr *) &inaddr;
addrlen = sizeof (struct sockaddr_in);
inaddr.sin_port = pmap_getport(&inaddr, DRIVERD_RPC_PORT, 1, IPPROTO_TCP);
if (inaddr.sin_port == 0) {
inaddr.sin_port = DRIVERD_TCP_PORT + iserver;
inaddr.sin_port = htons (inaddr.sin_port); /* may be funky macro */
}
/*
* Open the network connection.
*/
do {
if ((fd = socket ((int) addr->sa_family, SOCK_STREAM, 0)) < 0) {
return -1;
}
/*
* turn off TCP coalescence
*/
#ifdef TCP_NODELAY
{
int tmp = 1;
setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &tmp, sizeof (int));
}
#endif
/*
* connect to the socket; if there is no driver daemon server or
* if the backlog has been reached, then ECONNREFUSED will be returned.
*/
if (connect (fd, addr, addrlen) < 0) {
int olderrno = oserror();
(void) close (fd);
if (olderrno != ECONNREFUSED || retries <= 0) {
setoserror(olderrno);
return -1;
}
sleep (1);
} else {
break;
}
} while (retries-- > 0);
return fd;
}
int
readn(int fd, void *buf, int nbytes)
{
unsigned char *bp;
int n, ret;
ret = nbytes;
while (nbytes) {
n = read(fd, buf, nbytes);
if (n < 0) {
printf("readn failure fd %d, n is %d\n", fd, n);
perror("XXX");
return n;
}
buf = (void *)((int)buf + n);
nbytes -= n;
}
return ret;
}
/*
* Fake open code (user code side)
* Translate open into a socket connection to driverd
*/
emopen(char *file)
{
int family;
return (MakeDriverDConnection(NULL, 0, 10, &family));
}
/*
* Fake ioctl code (user code side)
* Translate ioctl into data packet to send over the socket connection
* to driverd
*/
int
emioctl(int fd, int request, long arg)
{
IoctlRequest req;
int data;
req.dd_header = DRIVERD_IOCTL_REQUEST;
req.request = request;
write(fd, &req, sizeof(req));
switch (request) {
case U64_RESET:
fprintf(stderr, "Reset not implemented\n");
break;
case U64_WRITE:
case U64_SAFE_WRITE:
write(fd, (void *)arg, sizeof(u64_write_arg_t));
write(fd, ((struct u64_write_arg *)arg)->buffer,
((struct u64_write_arg *)arg)->nbytes);
read(fd, &data, sizeof(data)); /* response */
break;
case U64_READ:
write(fd, (void *)arg, sizeof(u64_read_arg_t));
read(fd, &data, sizeof(data)); /* response is count*/
/* now read data */
return (readn(fd, ((struct u64_read_arg *)arg)->buffer, data));
break;
case U64_LISTEN:
data = arg;
write(fd, &data, sizeof(data));
}
}