remote_access.c
10.8 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*
* remote_access.c - Routines to provide remote access to SimOS files.
*/
#include "sim.h"
#include <stdio.h>
#include <sys/types.h>
#ifndef __alpha
#ifndef i386
#include <sys/unistd.h>
#include <sys/ioccom.h>
#include <sys/filio.h>
#endif
#endif
#include <sys/mman.h>
#include <sys/file.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <unistd.h>
#ifndef linux
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <sys/param.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include "remote_access.h"
#include "sim_error.h"
#include "simutil.h"
#include "rmtaccess.h"
extern char *MemFileDir;
#define RMT_MAXHOSTNAMELEN 64
#define RMT_MAXCONN 64
enum {RMT_DISK = 0, RMT_CPT, RMT_END};
static char server[RMT_END][RMT_MAXHOSTNAMELEN];
static struct sockaddr_in rmt_addr[RMT_END];
static char cpt_rootname[MAX_PATH_LENGTH];
/* --------------------------------------- */
static int StartRequest(struct sockaddr_in *rmtsrv, int cmd, char *arg, int argsize);
static int ReadForSure(int fd, byte *buf, int size);
static int CopyFile(int infd, int outfd);
/*
* Simrmt_init -Initialized access to a remote checkpoint.
* returns < 0 on error, 1 if sucessful.
*/
int
Simrmt_cptinit(char *fullname, char *rootname)
{
int port;
struct hostent *host;
/*
* We assume that the root pathname encodes the host and port as follows:
* hostname:port:masterlog_root
*/
if (sscanf(fullname, "%[^:]:%d:%s", server[RMT_CPT], &port, rootname) != 3) {
Sim_Warning("Simrmt_init: Bad remote checkpoint name <%s>\n", fullname);
return -1;
}
/*
* Fill in the internet address to be used in later calls.
*/
strcpy(cpt_rootname, rootname);
host = gethostbyname(server[RMT_CPT]);
if (!host) {
perror( "Simrmt_init:gethostbyname bad host");
Sim_Warning("Bad hostname %s\n", server[RMT_CPT]);
return -1;
}
rmt_addr[RMT_CPT].sin_family = host->h_addrtype;
bcopy(host->h_addr, (char *) &rmt_addr[RMT_CPT].sin_addr, host->h_length);
rmt_addr[RMT_CPT].sin_port = htons((short)port);
return 1;
}
/*
* Simrmt_diskinit - Initialize remote disk for access.
* Return the open file descriptor.
*/
int
Simrmt_diskinit(char *remoteName)
{
int port;
struct hostent *host;
/* If no remote name is given, then we are using the remote
checkpoint server as a remote disk server as well */
if (remoteName == NULL) {
return StartRequest(&(rmt_addr[RMT_CPT]), SIMRMT_DISKINIT, (char *) NULL, 0);
}
else {
/*
* We assume that the remote name encodes the host and port as follows:
* $hostname:port
*/
if (sscanf(remoteName, "$%[^:]:%d", server[RMT_DISK], &port) != 2) {
Sim_Warning("Simrmt_diskinit: Bad remote disk name <%s>\n", remoteName);
return -1;
}
/*
* Fill in the internet address to be used in later calls.
*/
host = gethostbyname(server[RMT_DISK]);
if (!host) {
perror( "Simrmt_diskinit:gethostbyname bad host");
Sim_Warning("Bad hostname %s\n", server[RMT_DISK]);
return -1;
}
rmt_addr[RMT_DISK].sin_family = host->h_addrtype;
bcopy(host->h_addr, (char *) &rmt_addr[RMT_DISK].sin_addr, host->h_length);
rmt_addr[RMT_DISK].sin_port = htons((short)port);
return StartRequest(&(rmt_addr[RMT_DISK]), SIMRMT_DISKINIT, (char *) NULL, 0);
}
}
/*
* Simrmt_access - See if a remote file is readable
* Returns: TRUE if file exists and is readable, FALSE otherwise
*/
bool
Simrmt_access(char *pathname)
{
int fd = StartRequest(&(rmt_addr[RMT_CPT]), SIMRMT_ACCESS, pathname, strlen(pathname));
if (fd == -1) return FALSE;
close(fd);
return TRUE;
}
/*
* Simrmt_fopen - Open a remote file.
* Returns: NULL on error, otherwise the open file pointer
*/
FILE *
Simrmt_fopen(char *pathname)
{
int fd = StartRequest(&(rmt_addr[RMT_CPT]), SIMRMT_OPEN, pathname, strlen(pathname));
if (fd < 0) return NULL;
else return fdopen(fd, "r");
}
/*
* Simrmt_diskcmd - Send a remote disk command.
*/
int64
Simrmt_diskcmd(int diskfd, int op,
int node, int ctrl, int unit,
int64 sectorNum,int64 sizeInBytes,
byte *buffer)
{
NetDiskHdr hdr, rhdr;
int size;
static int reqnum;
reqnum++;
bzero((char *) &hdr, sizeof(hdr));
hdr.op = (DiskOp)op;
hdr.node = node;
hdr.ctrl = ctrl;
hdr.unit = unit;
hdr.sectorNum = sectorNum;
hdr.sizeInBytes = sizeInBytes;
hdr.reqnum = reqnum;
if( write(diskfd, &hdr,sizeof(hdr))!=sizeof(hdr) ) {
CPUError("write to rmtaccess server failed\n");
};
if( (op == NETDISK_WRITE) || (op == NETDISK_ATTACH) ) {
if( write(diskfd,buffer,sizeInBytes)!=sizeInBytes) {
CPUError("write to rmtaccess server failed\n");
}
}
size = ReadForSure(diskfd, (byte *)&rhdr, sizeof(rhdr));
if (size != sizeof(rhdr)) {
perror("simckpt_netdisk:read");
rhdr.retVal = EIO;
return rhdr.retVal;
}
if (rhdr.reqnum != hdr.reqnum) {
Sim_Warning("Simnetdisk: Bad reqnum (%d)\n", rhdr.reqnum);
}
if (((op == NETDISK_READ) || (op == NETDISK_ATTACH)) && (rhdr.retVal > 0)) {
size = ReadForSure(diskfd, buffer, rhdr.retVal);
if (size != rhdr.retVal) {
Sim_Warning("Simnetdisk: Reply packet too small (%d)\n", size);
rhdr.retVal = EIO;
}
}
return rhdr.retVal;
}
/*
* ReadForSure - just like the read() system call execpt it keeps trying
* if not enough data was returned by the read or it was interrupted
* by a signal.
*/
static int
ReadForSure(int fd, byte *buf, int size)
{
int a;
for(a = 0; a < size;) {
int ret = read(fd, buf + a, size-a);
if (ret < 0) {
if (errno == EINTR)
continue;
return -1;
}
a += ret;
}
return size;
}
/*
* StartRequest -
* Establish a connection with the remote server and exchange the specified command
* with it.
* Returns the open connection to the server. < 0 if the connection
* fails or the command encountes an errors.
*/
static int
StartRequest(struct sockaddr_in *rmtsrv, int cmd, char *arg, int argsize)
{
int fd, err;
RmtAccessStartRequest req;
byte ack[8];
/*
* Create the TCP socket and send command, returning socket
* blocks to and from the net disk server.
*/
fd = socket(rmtsrv->sin_family, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
perror("Simrmt:StartRequest:socket");
return -1;
}
if (connect(fd, (struct sockaddr *)rmtsrv, sizeof (*rmtsrv)) < 0) {
perror("Simrmt:StartRequest:connect");
close(fd);
return -1;
}
/*
* format and send the request over the connection.
*/
req.magic = RMTACCESS_MAGIC;
req.cmd = (Rmtaccess_op)cmd;
req.arglen = argsize;
err = write(fd, (char *) &req, sizeof(req));
if (err != sizeof(req)) {
perror("Simrmt:StartRequest:write");
close(fd);
goto error;
}
if (req.arglen > 0) {
err = write(fd, arg, req.arglen);
if (err != req.arglen) {
perror("Simrmt:StartRequest:write2");
goto error;
}
}
/*
* Get error code for command: OK\n or Enn
*/
err = ReadForSure(fd, ack, 3);
if (err < 0) {
perror("Simrmt:StartRequest:ReadForSure ack");
goto error;
}
if ((ack[0] == 'O') && (ack[1] == 'K')) {
return fd;
}
error:
close(fd);
return -1;
}
static int
CopyFile(int infd, int outfd)
{
char buf[8192];
int cnt, cnt2;
while(1) {
cnt = read(infd, buf, sizeof(buf));
if (cnt == 0)
break;
if (cnt < 0) {
perror("CopyFile -- read");
exit(1);
}
cnt2 = write(outfd, buf, cnt);
if (cnt2 != cnt) {
perror("CopyFile -- write");
exit(1);
}
}
return 0;
}
/*
* Open a file across a remote server and return the FILE ptr
* The client must close the FILE ptr when done.
*/
FILE*
Simrmt_RemoteFileOpen(char *pathName)
{
struct hostent *host;
char hostName[RMT_MAXHOSTNAMELEN];
int port;
char fileName[RMT_MAXHOSTNAMELEN];
int fd;
struct sockaddr_in rmtsrv;
if (sscanf(pathName, "%[^:]:%d:%s", hostName, &port, fileName) != 3) {
Sim_Warning("Simrmt_RemoteFileOpen: Bad remote file name <%s>",
pathName);
return NULL;
}
host = gethostbyname(hostName);
if (!host) {
perror( "Simrmt_RemoteFileOpen: gethostbyname bad host");
Sim_Warning("Bad host name %s\n", hostName);
return NULL;
}
bzero((char *) &rmtsrv, sizeof(rmtsrv));
rmtsrv.sin_family = host->h_addrtype;
bcopy(host->h_addr, (char *) &(rmtsrv.sin_addr), host->h_length);
rmtsrv.sin_port = htons((short) port);
fd = StartRequest(&rmtsrv, SIMRMT_OPEN, fileName, strlen(fileName));
if (fd < 0) {
return NULL;
} else {
return fdopen(fd, "r");
}
}
/*
* This routine implements an mmap of a remote file. Currently we do the
* dumb thing, which is to copy the file to a temporary local file
* and then mmaps it. The parameters to the call are exactly the same as
* for the mmap call, except that you pass a remoteserver filepath instead of
* of a file descriptor.
*/
void *
Simrmt_mmap(void *addr, size_t len, int prot, int flags, char* pathName, off_t off)
{
struct stat statbuf;
struct hostent *host;
char hostName[RMT_MAXHOSTNAMELEN];
int port;
char fileName[RMT_MAXHOSTNAMELEN];
int fd, tmpfd;
struct sockaddr_in rmtsrv;
void *maddr;
if (sscanf(pathName, "%[^:]:%d:%s", hostName, &port, fileName) != 3) {
Sim_Warning("Simrmt_mmap: Bad remote file name <%s>",
pathName);
return NULL;
}
host = gethostbyname(hostName);
if (!host) {
perror( "Simrmt_mmap: gethostbyname bad host");
Sim_Warning("Bad host name %s\n", hostName);
return NULL;
}
rmtsrv.sin_family = host->h_addrtype;
bcopy(host->h_addr, (char *) &(rmtsrv.sin_addr), host->h_length);
rmtsrv.sin_port = htons((short) port);
fd = StartRequest(&rmtsrv, SIMRMT_OPEN, fileName, strlen(fileName));
if (fd < 0) {
return NULL;
}
tmpfd = MakeFile(MemFileDir, NULL, 0, 1);
CopyFile(fd, tmpfd);
close(fd);
if (len == 0) {
/* this means we want to map to the end of file */
if (fstat(tmpfd, &statbuf) < 0) {
perror("Simrmt_mmap fstat");
exit(1);
}
len = statbuf.st_size - off;
}
if((maddr = mmap(addr, len, prot, flags, tmpfd, off)) == (void *)-1) {
perror("Simrmt_mmap");
exit(1);
}
close(tmpfd);
return maddr;
}