rmtlib.c revision 1.7 1 1.7 lukem /* $NetBSD: rmtlib.c,v 1.7 1997/10/09 11:58:19 lukem Exp $ */
2 1.1 jtc
3 1.1 jtc /*
4 1.1 jtc * rmt --- remote tape emulator subroutines
5 1.1 jtc *
6 1.1 jtc * Originally written by Jeff Lee, modified some by Arnold Robbins
7 1.1 jtc *
8 1.1 jtc * WARNING: The man page rmt(8) for /etc/rmt documents the remote mag
9 1.1 jtc * tape protocol which rdump and rrestore use. Unfortunately, the man
10 1.1 jtc * page is *WRONG*. The author of the routines I'm including originally
11 1.1 jtc * wrote his code just based on the man page, and it didn't work, so he
12 1.1 jtc * went to the rdump source to figure out why. The only thing he had to
13 1.1 jtc * change was to check for the 'F' return code in addition to the 'E',
14 1.1 jtc * and to separate the various arguments with \n instead of a space. I
15 1.1 jtc * personally don't think that this is much of a problem, but I wanted to
16 1.1 jtc * point it out.
17 1.1 jtc * -- Arnold Robbins
18 1.1 jtc *
19 1.1 jtc * Redone as a library that can replace open, read, write, etc, by
20 1.1 jtc * Fred Fish, with some additional work by Arnold Robbins.
21 1.1 jtc */
22 1.1 jtc
23 1.1 jtc /*
24 1.1 jtc * MAXUNIT --- Maximum number of remote tape file units
25 1.1 jtc *
26 1.1 jtc * READ --- Return the number of the read side file descriptor
27 1.1 jtc * WRITE --- Return the number of the write side file descriptor
28 1.1 jtc */
29 1.1 jtc
30 1.1 jtc #define RMTIOCTL 1
31 1.6 mikel /* #define USE_REXEC 1 */ /* rexec code courtesy of Dan Kegel, srs!dan */
32 1.1 jtc
33 1.1 jtc #include <stdio.h>
34 1.2 jtc #include <stdlib.h>
35 1.2 jtc #include <string.h>
36 1.1 jtc #include <signal.h>
37 1.1 jtc #include <sys/types.h>
38 1.1 jtc
39 1.1 jtc #ifdef RMTIOCTL
40 1.1 jtc #include <sys/ioctl.h>
41 1.1 jtc #include <sys/mtio.h>
42 1.1 jtc #endif
43 1.1 jtc
44 1.1 jtc #ifdef USE_REXEC
45 1.1 jtc #include <netdb.h>
46 1.1 jtc #endif
47 1.1 jtc
48 1.1 jtc #include <errno.h>
49 1.1 jtc #include <sys/stat.h>
50 1.1 jtc
51 1.2 jtc #include <fcntl.h>
52 1.2 jtc #include <unistd.h>
53 1.2 jtc
54 1.7 lukem static int _rmt_close __P((int));
55 1.7 lukem static int _rmt_ioctl __P((int, unsigned long, char *));
56 1.7 lukem static off_t _rmt_lseek __P((int, off_t, int));
57 1.7 lukem static int _rmt_open __P((const char *, int, int));
58 1.7 lukem static int _rmt_read __P((int, char *, unsigned int));
59 1.7 lukem static int _rmt_write __P((int, const void *, unsigned int));
60 1.7 lukem static int command __P((int, char *));
61 1.7 lukem static int remdev __P((const char *));
62 1.7 lukem static void rmtabort __P((int));
63 1.7 lukem static int status __P((int));
64 1.7 lukem
65 1.7 lukem int isrmt __P((int));
66 1.7 lukem int rmtaccess __P((const char *, int));
67 1.7 lukem int rmtclose __P((int));
68 1.7 lukem int rmtcreat __P((const char *, mode_t));
69 1.7 lukem int rmtdup __P((int));
70 1.7 lukem int rmtfcntl __P((int, int, int));
71 1.7 lukem int rmtfstat __P((int, struct stat *));
72 1.7 lukem int rmtioctl __P((int, unsigned long, char *));
73 1.7 lukem int rmtisatty __P((int));
74 1.7 lukem off_t rmtlseek __P((int, off_t, int));
75 1.7 lukem int rmtlstat __P((const char *, struct stat *));
76 1.7 lukem int rmtopen __P((const char *, int, mode_t));
77 1.7 lukem ssize_t rmtread __P((int, void *, size_t));
78 1.7 lukem int rmtstat __P((const char *, struct stat *));
79 1.7 lukem ssize_t rmtwrite __P((int, const void *, size_t));
80 1.7 lukem
81 1.7 lukem
82 1.1 jtc #define BUFMAGIC 64 /* a magic number for buffer sizes */
83 1.1 jtc #define MAXUNIT 4
84 1.1 jtc
85 1.1 jtc #define READ(fd) (Ctp[fd][0])
86 1.1 jtc #define WRITE(fd) (Ptc[fd][1])
87 1.1 jtc
88 1.6 mikel static int Ctp[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
89 1.6 mikel static int Ptc[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
90 1.1 jtc
91 1.1 jtc
92 1.1 jtc /*
93 1.2 jtc * rmtabort --- close off a remote tape connection
94 1.1 jtc */
95 1.1 jtc
96 1.7 lukem static void
97 1.7 lukem rmtabort(fildes)
98 1.7 lukem int fildes;
99 1.1 jtc {
100 1.1 jtc close(READ(fildes));
101 1.1 jtc close(WRITE(fildes));
102 1.1 jtc READ(fildes) = -1;
103 1.1 jtc WRITE(fildes) = -1;
104 1.1 jtc }
105 1.1 jtc
106 1.1 jtc
107 1.1 jtc
108 1.1 jtc /*
109 1.1 jtc * command --- attempt to perform a remote tape command
110 1.1 jtc */
111 1.1 jtc
112 1.7 lukem static int
113 1.7 lukem command(fildes, buf)
114 1.7 lukem int fildes;
115 1.7 lukem char *buf;
116 1.1 jtc {
117 1.7 lukem int blen;
118 1.7 lukem void (*pstat) __P((int));
119 1.1 jtc
120 1.1 jtc /*
121 1.1 jtc * save current pipe status and try to make the request
122 1.1 jtc */
123 1.1 jtc
124 1.1 jtc blen = strlen(buf);
125 1.1 jtc pstat = signal(SIGPIPE, SIG_IGN);
126 1.1 jtc if (write(WRITE(fildes), buf, blen) == blen)
127 1.1 jtc {
128 1.1 jtc signal(SIGPIPE, pstat);
129 1.1 jtc return(0);
130 1.1 jtc }
131 1.1 jtc
132 1.1 jtc /*
133 1.1 jtc * something went wrong. close down and go home
134 1.1 jtc */
135 1.1 jtc
136 1.1 jtc signal(SIGPIPE, pstat);
137 1.2 jtc rmtabort(fildes);
138 1.1 jtc
139 1.1 jtc errno = EIO;
140 1.1 jtc return(-1);
141 1.1 jtc }
142 1.1 jtc
143 1.1 jtc
144 1.1 jtc
145 1.1 jtc /*
146 1.1 jtc * status --- retrieve the status from the pipe
147 1.1 jtc */
148 1.1 jtc
149 1.7 lukem static int
150 1.7 lukem status(fildes)
151 1.7 lukem int fildes;
152 1.1 jtc {
153 1.1 jtc int i;
154 1.1 jtc char c, *cp;
155 1.1 jtc char buffer[BUFMAGIC];
156 1.1 jtc
157 1.1 jtc /*
158 1.1 jtc * read the reply command line
159 1.1 jtc */
160 1.1 jtc
161 1.1 jtc for (i = 0, cp = buffer; i < BUFMAGIC; i++, cp++)
162 1.1 jtc {
163 1.1 jtc if (read(READ(fildes), cp, 1) != 1)
164 1.1 jtc {
165 1.2 jtc rmtabort(fildes);
166 1.1 jtc errno = EIO;
167 1.1 jtc return(-1);
168 1.1 jtc }
169 1.1 jtc if (*cp == '\n')
170 1.1 jtc {
171 1.1 jtc *cp = 0;
172 1.1 jtc break;
173 1.1 jtc }
174 1.1 jtc }
175 1.1 jtc
176 1.1 jtc if (i == BUFMAGIC)
177 1.1 jtc {
178 1.2 jtc rmtabort(fildes);
179 1.1 jtc errno = EIO;
180 1.1 jtc return(-1);
181 1.1 jtc }
182 1.1 jtc
183 1.1 jtc /*
184 1.1 jtc * check the return status
185 1.1 jtc */
186 1.1 jtc
187 1.1 jtc for (cp = buffer; *cp; cp++)
188 1.1 jtc if (*cp != ' ')
189 1.1 jtc break;
190 1.1 jtc
191 1.1 jtc if (*cp == 'E' || *cp == 'F')
192 1.1 jtc {
193 1.1 jtc errno = atoi(cp + 1);
194 1.1 jtc while (read(READ(fildes), &c, 1) == 1)
195 1.1 jtc if (c == '\n')
196 1.1 jtc break;
197 1.1 jtc
198 1.1 jtc if (*cp == 'F')
199 1.2 jtc rmtabort(fildes);
200 1.1 jtc
201 1.1 jtc return(-1);
202 1.1 jtc }
203 1.1 jtc
204 1.1 jtc /*
205 1.1 jtc * check for mis-synced pipes
206 1.1 jtc */
207 1.1 jtc
208 1.1 jtc if (*cp != 'A')
209 1.1 jtc {
210 1.2 jtc rmtabort(fildes);
211 1.1 jtc errno = EIO;
212 1.1 jtc return(-1);
213 1.1 jtc }
214 1.1 jtc
215 1.1 jtc return(atoi(cp + 1));
216 1.1 jtc }
217 1.1 jtc
218 1.1 jtc #ifdef USE_REXEC
219 1.1 jtc
220 1.1 jtc /*
221 1.1 jtc * _rmt_rexec
222 1.1 jtc *
223 1.1 jtc * execute /etc/rmt on a remote system using rexec().
224 1.1 jtc * Return file descriptor of bidirectional socket for stdin and stdout
225 1.1 jtc * If username is NULL, or an empty string, uses current username.
226 1.1 jtc *
227 1.1 jtc * ADR: By default, this code is not used, since it requires that
228 1.1 jtc * the user have a .netrc file in his/her home directory, or that the
229 1.1 jtc * application designer be willing to have rexec prompt for login and
230 1.1 jtc * password info. This may be unacceptable, and .rhosts files for use
231 1.1 jtc * with rsh are much more common on BSD systems.
232 1.1 jtc */
233 1.1 jtc
234 1.7 lukem static int _rmt_rexec __P((const char *, const char *));
235 1.7 lukem
236 1.1 jtc static int
237 1.1 jtc _rmt_rexec(host, user)
238 1.7 lukem const char *host;
239 1.7 lukem const char *user; /* may be NULL */
240 1.1 jtc {
241 1.1 jtc struct servent *rexecserv;
242 1.1 jtc
243 1.1 jtc rexecserv = getservbyname("exec", "tcp");
244 1.1 jtc if (NULL == rexecserv) {
245 1.1 jtc fprintf (stderr, "? exec/tcp: service not available.");
246 1.1 jtc exit (-1);
247 1.1 jtc }
248 1.1 jtc if ((user != NULL) && *user == '\0')
249 1.1 jtc user = (char *) NULL;
250 1.1 jtc return rexec (&host, rexecserv->s_port, user, NULL,
251 1.1 jtc "/etc/rmt", (int *)NULL);
252 1.1 jtc }
253 1.1 jtc #endif /* USE_REXEC */
254 1.1 jtc
255 1.1 jtc /*
256 1.1 jtc * _rmt_open --- open a magtape device on system specified, as given user
257 1.1 jtc *
258 1.1 jtc * file name has the form [user@]system:/dev/????
259 1.1 jtc #ifdef COMPAT
260 1.1 jtc * file name has the form system[.user]:/dev/????
261 1.1 jtc #endif
262 1.1 jtc */
263 1.1 jtc
264 1.1 jtc #define MAXHOSTLEN 257 /* BSD allows very long host names... */
265 1.1 jtc
266 1.7 lukem static int
267 1.7 lukem _rmt_open(path, oflag, mode)
268 1.7 lukem const char *path;
269 1.7 lukem int oflag;
270 1.7 lukem int mode;
271 1.1 jtc {
272 1.1 jtc int i, rc;
273 1.1 jtc char buffer[BUFMAGIC];
274 1.1 jtc char system[MAXHOSTLEN];
275 1.1 jtc char device[BUFMAGIC];
276 1.1 jtc char login[BUFMAGIC];
277 1.1 jtc char *sys, *dev, *user;
278 1.1 jtc
279 1.1 jtc sys = system;
280 1.1 jtc dev = device;
281 1.1 jtc user = login;
282 1.1 jtc
283 1.1 jtc /*
284 1.1 jtc * first, find an open pair of file descriptors
285 1.1 jtc */
286 1.1 jtc
287 1.1 jtc for (i = 0; i < MAXUNIT; i++)
288 1.1 jtc if (READ(i) == -1 && WRITE(i) == -1)
289 1.1 jtc break;
290 1.1 jtc
291 1.1 jtc if (i == MAXUNIT)
292 1.1 jtc {
293 1.1 jtc errno = EMFILE;
294 1.1 jtc return(-1);
295 1.1 jtc }
296 1.1 jtc
297 1.1 jtc /*
298 1.1 jtc * pull apart system and device, and optional user
299 1.1 jtc * don't munge original string
300 1.1 jtc * if COMPAT is defined, also handle old (4.2) style person.site notation.
301 1.1 jtc */
302 1.1 jtc
303 1.1 jtc while (*path != '@'
304 1.1 jtc #ifdef COMPAT
305 1.1 jtc && *path != '.'
306 1.1 jtc #endif
307 1.1 jtc && *path != ':') {
308 1.1 jtc *sys++ = *path++;
309 1.1 jtc }
310 1.1 jtc *sys = '\0';
311 1.1 jtc path++;
312 1.1 jtc
313 1.1 jtc if (*(path - 1) == '@')
314 1.1 jtc {
315 1.5 mrg (void)strncpy(user, system, sizeof(login) - 1);
316 1.5 mrg /* saw user part of user@host */
317 1.1 jtc sys = system; /* start over */
318 1.1 jtc while (*path != ':') {
319 1.1 jtc *sys++ = *path++;
320 1.1 jtc }
321 1.1 jtc *sys = '\0';
322 1.1 jtc path++;
323 1.1 jtc }
324 1.1 jtc #ifdef COMPAT
325 1.1 jtc else if (*(path - 1) == '.')
326 1.1 jtc {
327 1.1 jtc while (*path != ':') {
328 1.1 jtc *user++ = *path++;
329 1.1 jtc }
330 1.1 jtc *user = '\0';
331 1.1 jtc path++;
332 1.1 jtc }
333 1.1 jtc #endif
334 1.1 jtc else
335 1.1 jtc *user = '\0';
336 1.1 jtc
337 1.1 jtc while (*path) {
338 1.1 jtc *dev++ = *path++;
339 1.1 jtc }
340 1.1 jtc *dev = '\0';
341 1.1 jtc
342 1.1 jtc #ifdef USE_REXEC
343 1.1 jtc /*
344 1.1 jtc * Execute the remote command using rexec
345 1.1 jtc */
346 1.1 jtc READ(i) = WRITE(i) = _rmt_rexec(system, login);
347 1.1 jtc if (READ(i) < 0)
348 1.1 jtc return -1;
349 1.1 jtc #else
350 1.1 jtc /*
351 1.1 jtc * setup the pipes for the 'rsh' command and fork
352 1.1 jtc */
353 1.1 jtc
354 1.1 jtc if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1)
355 1.1 jtc return(-1);
356 1.1 jtc
357 1.1 jtc if ((rc = fork()) == -1)
358 1.1 jtc return(-1);
359 1.1 jtc
360 1.1 jtc if (rc == 0)
361 1.1 jtc {
362 1.1 jtc close(0);
363 1.1 jtc dup(Ptc[i][0]);
364 1.1 jtc close(Ptc[i][0]); close(Ptc[i][1]);
365 1.1 jtc close(1);
366 1.1 jtc dup(Ctp[i][1]);
367 1.1 jtc close(Ctp[i][0]); close(Ctp[i][1]);
368 1.1 jtc (void) setuid (getuid ());
369 1.1 jtc (void) setgid (getgid ());
370 1.1 jtc if (*login)
371 1.1 jtc {
372 1.3 jtc execl("/usr/bin/rsh", "rsh", system, "-l", login,
373 1.1 jtc "/etc/rmt", (char *) 0);
374 1.1 jtc }
375 1.1 jtc else
376 1.1 jtc {
377 1.3 jtc execl("/usr/bin/rsh", "rsh", system,
378 1.1 jtc "/etc/rmt", (char *) 0);
379 1.1 jtc }
380 1.1 jtc
381 1.1 jtc /*
382 1.1 jtc * bad problems if we get here
383 1.1 jtc */
384 1.1 jtc
385 1.1 jtc perror("exec");
386 1.1 jtc exit(1);
387 1.1 jtc }
388 1.1 jtc
389 1.1 jtc close(Ptc[i][0]); close(Ctp[i][1]);
390 1.1 jtc #endif
391 1.1 jtc
392 1.1 jtc /*
393 1.1 jtc * now attempt to open the tape device
394 1.1 jtc */
395 1.1 jtc
396 1.5 mrg (void)snprintf(buffer, sizeof(buffer), "O%s\n%d\n", device, oflag);
397 1.1 jtc if (command(i, buffer) == -1 || status(i) == -1)
398 1.1 jtc return(-1);
399 1.1 jtc
400 1.1 jtc return(i);
401 1.1 jtc }
402 1.1 jtc
403 1.1 jtc
404 1.1 jtc
405 1.1 jtc /*
406 1.1 jtc * _rmt_close --- close a remote magtape unit and shut down
407 1.1 jtc */
408 1.1 jtc
409 1.7 lukem static int
410 1.7 lukem _rmt_close(fildes)
411 1.7 lukem int fildes;
412 1.1 jtc {
413 1.1 jtc int rc;
414 1.1 jtc
415 1.1 jtc if (command(fildes, "C\n") != -1)
416 1.1 jtc {
417 1.1 jtc rc = status(fildes);
418 1.1 jtc
419 1.2 jtc rmtabort(fildes);
420 1.1 jtc return(rc);
421 1.1 jtc }
422 1.1 jtc
423 1.1 jtc return(-1);
424 1.1 jtc }
425 1.1 jtc
426 1.1 jtc
427 1.1 jtc
428 1.1 jtc /*
429 1.1 jtc * _rmt_read --- read a buffer from a remote tape
430 1.1 jtc */
431 1.1 jtc
432 1.7 lukem static int
433 1.7 lukem _rmt_read(fildes, buf, nbyte)
434 1.7 lukem int fildes;
435 1.7 lukem char *buf;
436 1.7 lukem unsigned int nbyte;
437 1.1 jtc {
438 1.1 jtc int rc, i;
439 1.1 jtc char buffer[BUFMAGIC];
440 1.1 jtc
441 1.5 mrg (void)snprintf(buffer, sizeof buffer, "R%d\n", nbyte);
442 1.1 jtc if (command(fildes, buffer) == -1 || (rc = status(fildes)) == -1)
443 1.1 jtc return(-1);
444 1.1 jtc
445 1.1 jtc for (i = 0; i < rc; i += nbyte, buf += nbyte)
446 1.1 jtc {
447 1.1 jtc nbyte = read(READ(fildes), buf, rc);
448 1.1 jtc if (nbyte <= 0)
449 1.1 jtc {
450 1.2 jtc rmtabort(fildes);
451 1.1 jtc errno = EIO;
452 1.1 jtc return(-1);
453 1.1 jtc }
454 1.1 jtc }
455 1.1 jtc
456 1.1 jtc return(rc);
457 1.1 jtc }
458 1.1 jtc
459 1.1 jtc
460 1.1 jtc
461 1.1 jtc /*
462 1.1 jtc * _rmt_write --- write a buffer to the remote tape
463 1.1 jtc */
464 1.1 jtc
465 1.7 lukem static int
466 1.7 lukem _rmt_write(fildes, buf, nbyte)
467 1.7 lukem int fildes;
468 1.7 lukem const void *buf;
469 1.7 lukem unsigned int nbyte;
470 1.1 jtc {
471 1.1 jtc char buffer[BUFMAGIC];
472 1.7 lukem void (*pstat) __P((int));
473 1.1 jtc
474 1.5 mrg (void)snprintf(buffer, sizeof buffer, "W%d\n", nbyte);
475 1.1 jtc if (command(fildes, buffer) == -1)
476 1.1 jtc return(-1);
477 1.1 jtc
478 1.1 jtc pstat = signal(SIGPIPE, SIG_IGN);
479 1.1 jtc if (write(WRITE(fildes), buf, nbyte) == nbyte)
480 1.1 jtc {
481 1.1 jtc signal (SIGPIPE, pstat);
482 1.1 jtc return(status(fildes));
483 1.1 jtc }
484 1.1 jtc
485 1.1 jtc signal (SIGPIPE, pstat);
486 1.2 jtc rmtabort(fildes);
487 1.1 jtc errno = EIO;
488 1.1 jtc return(-1);
489 1.1 jtc }
490 1.1 jtc
491 1.1 jtc
492 1.1 jtc
493 1.1 jtc /*
494 1.1 jtc * _rmt_lseek --- perform an imitation lseek operation remotely
495 1.1 jtc */
496 1.1 jtc
497 1.7 lukem static off_t
498 1.7 lukem _rmt_lseek(fildes, offset, whence)
499 1.7 lukem int fildes;
500 1.7 lukem off_t offset;
501 1.7 lukem int whence;
502 1.1 jtc {
503 1.1 jtc char buffer[BUFMAGIC];
504 1.1 jtc
505 1.7 lukem (void)snprintf(buffer, sizeof buffer, "L%qd\n%d\n", offset, whence);
506 1.1 jtc if (command(fildes, buffer) == -1)
507 1.1 jtc return(-1);
508 1.1 jtc
509 1.1 jtc return(status(fildes));
510 1.1 jtc }
511 1.1 jtc
512 1.1 jtc
513 1.1 jtc /*
514 1.1 jtc * _rmt_ioctl --- perform raw tape operations remotely
515 1.1 jtc */
516 1.1 jtc
517 1.1 jtc #ifdef RMTIOCTL
518 1.7 lukem static int
519 1.7 lukem _rmt_ioctl(fildes, op, arg)
520 1.7 lukem int fildes;
521 1.7 lukem unsigned long op;
522 1.7 lukem char *arg;
523 1.1 jtc {
524 1.1 jtc char c;
525 1.1 jtc int rc, cnt;
526 1.1 jtc char buffer[BUFMAGIC];
527 1.1 jtc
528 1.1 jtc /*
529 1.1 jtc * MTIOCOP is the easy one. nothing is transfered in binary
530 1.1 jtc */
531 1.1 jtc
532 1.1 jtc if (op == MTIOCTOP)
533 1.1 jtc {
534 1.5 mrg (void)snprintf(buffer, sizeof buffer, "I%d\n%d\n",
535 1.5 mrg ((struct mtop *)arg)->mt_op,
536 1.5 mrg ((struct mtop *)arg)->mt_count);
537 1.1 jtc if (command(fildes, buffer) == -1)
538 1.1 jtc return(-1);
539 1.1 jtc return(status(fildes));
540 1.1 jtc }
541 1.1 jtc
542 1.1 jtc /*
543 1.1 jtc * we can only handle 2 ops, if not the other one, punt
544 1.1 jtc */
545 1.1 jtc
546 1.1 jtc if (op != MTIOCGET)
547 1.1 jtc {
548 1.1 jtc errno = EINVAL;
549 1.1 jtc return(-1);
550 1.1 jtc }
551 1.1 jtc
552 1.1 jtc /*
553 1.1 jtc * grab the status and read it directly into the structure
554 1.1 jtc * this assumes that the status buffer is (hopefully) not
555 1.1 jtc * padded and that 2 shorts fit in a long without any word
556 1.1 jtc * alignment problems, ie - the whole struct is contiguous
557 1.1 jtc * NOTE - this is probably NOT a good assumption.
558 1.1 jtc */
559 1.1 jtc
560 1.1 jtc if (command(fildes, "S") == -1 || (rc = status(fildes)) == -1)
561 1.1 jtc return(-1);
562 1.1 jtc
563 1.1 jtc for (; rc > 0; rc -= cnt, arg += cnt)
564 1.1 jtc {
565 1.1 jtc cnt = read(READ(fildes), arg, rc);
566 1.1 jtc if (cnt <= 0)
567 1.1 jtc {
568 1.2 jtc rmtabort(fildes);
569 1.1 jtc errno = EIO;
570 1.1 jtc return(-1);
571 1.1 jtc }
572 1.1 jtc }
573 1.1 jtc
574 1.1 jtc /*
575 1.1 jtc * now we check for byte position. mt_type is a small integer field
576 1.1 jtc * (normally) so we will check its magnitude. if it is larger than
577 1.1 jtc * 256, we will assume that the bytes are swapped and go through
578 1.1 jtc * and reverse all the bytes
579 1.1 jtc */
580 1.1 jtc
581 1.1 jtc if (((struct mtget *) arg)->mt_type < 256)
582 1.1 jtc return(0);
583 1.1 jtc
584 1.1 jtc for (cnt = 0; cnt < rc; cnt += 2)
585 1.1 jtc {
586 1.1 jtc c = arg[cnt];
587 1.1 jtc arg[cnt] = arg[cnt+1];
588 1.1 jtc arg[cnt+1] = c;
589 1.1 jtc }
590 1.1 jtc
591 1.1 jtc return(0);
592 1.1 jtc }
593 1.1 jtc #endif /* RMTIOCTL */
594 1.1 jtc
595 1.1 jtc /*
596 1.1 jtc * Added routines to replace open(), close(), lseek(), ioctl(), etc.
597 1.1 jtc * The preprocessor can be used to remap these the rmtopen(), etc
598 1.1 jtc * thus minimizing source changes:
599 1.1 jtc *
600 1.1 jtc * #ifdef <something>
601 1.1 jtc * # define access rmtaccess
602 1.1 jtc * # define close rmtclose
603 1.1 jtc * # define creat rmtcreat
604 1.1 jtc * # define dup rmtdup
605 1.1 jtc * # define fcntl rmtfcntl
606 1.1 jtc * # define fstat rmtfstat
607 1.1 jtc * # define ioctl rmtioctl
608 1.1 jtc * # define isatty rmtisatty
609 1.1 jtc * # define lseek rmtlseek
610 1.1 jtc * # define lstat rmtlstat
611 1.1 jtc * # define open rmtopen
612 1.1 jtc * # define read rmtread
613 1.1 jtc * # define stat rmtstat
614 1.1 jtc * # define write rmtwrite
615 1.1 jtc * #endif
616 1.1 jtc *
617 1.1 jtc * -- Fred Fish
618 1.1 jtc *
619 1.1 jtc * ADR --- I set up a <rmt.h> include file for this
620 1.1 jtc *
621 1.1 jtc */
622 1.1 jtc
623 1.1 jtc /*
624 1.1 jtc * Note that local vs remote file descriptors are distinquished
625 1.1 jtc * by adding a bias to the remote descriptors. This is a quick
626 1.1 jtc * and dirty trick that may not be portable to some systems.
627 1.1 jtc */
628 1.1 jtc
629 1.1 jtc #define REM_BIAS 128
630 1.1 jtc
631 1.1 jtc
632 1.1 jtc /*
633 1.1 jtc * Test pathname to see if it is local or remote. A remote device
634 1.1 jtc * is any string that contains ":/dev/". Returns 1 if remote,
635 1.1 jtc * 0 otherwise.
636 1.1 jtc */
637 1.1 jtc
638 1.7 lukem static int
639 1.7 lukem remdev(path)
640 1.7 lukem const char *path;
641 1.1 jtc {
642 1.1 jtc if ((path = strchr (path, ':')) != NULL)
643 1.1 jtc {
644 1.1 jtc if (strncmp (path + 1, "/dev/", 5) == 0)
645 1.1 jtc {
646 1.1 jtc return (1);
647 1.1 jtc }
648 1.1 jtc }
649 1.1 jtc return (0);
650 1.1 jtc }
651 1.1 jtc
652 1.1 jtc
653 1.1 jtc /*
654 1.1 jtc * Open a local or remote file. Looks just like open(2) to
655 1.1 jtc * caller.
656 1.1 jtc */
657 1.1 jtc
658 1.7 lukem int
659 1.7 lukem rmtopen(path, oflag, mode)
660 1.7 lukem const char *path;
661 1.7 lukem int oflag;
662 1.7 lukem mode_t mode;
663 1.1 jtc {
664 1.1 jtc int fd;
665 1.1 jtc
666 1.1 jtc if (remdev (path))
667 1.1 jtc {
668 1.1 jtc fd = _rmt_open (path, oflag, mode);
669 1.1 jtc
670 1.1 jtc return (fd == -1) ? -1 : (fd + REM_BIAS);
671 1.1 jtc }
672 1.1 jtc else
673 1.1 jtc {
674 1.1 jtc return (open (path, oflag, mode));
675 1.1 jtc }
676 1.1 jtc }
677 1.1 jtc
678 1.1 jtc /*
679 1.1 jtc * Test pathname for specified access. Looks just like access(2)
680 1.1 jtc * to caller.
681 1.1 jtc */
682 1.1 jtc
683 1.7 lukem int
684 1.7 lukem rmtaccess(path, amode)
685 1.7 lukem const char *path;
686 1.7 lukem int amode;
687 1.1 jtc {
688 1.1 jtc if (remdev (path))
689 1.1 jtc {
690 1.1 jtc return (0); /* Let /etc/rmt find out */
691 1.1 jtc }
692 1.1 jtc else
693 1.1 jtc {
694 1.1 jtc return (access (path, amode));
695 1.1 jtc }
696 1.1 jtc }
697 1.1 jtc
698 1.1 jtc
699 1.1 jtc /*
700 1.6 mikel * Isrmt. Let a programmer know he has a remote device.
701 1.6 mikel */
702 1.6 mikel
703 1.7 lukem int
704 1.7 lukem isrmt(fd)
705 1.7 lukem int fd;
706 1.6 mikel {
707 1.6 mikel return (fd >= REM_BIAS);
708 1.6 mikel }
709 1.6 mikel
710 1.6 mikel
711 1.6 mikel /*
712 1.1 jtc * Read from stream. Looks just like read(2) to caller.
713 1.1 jtc */
714 1.1 jtc
715 1.7 lukem ssize_t
716 1.7 lukem rmtread(fildes, buf, nbyte)
717 1.7 lukem int fildes;
718 1.7 lukem void *buf;
719 1.7 lukem size_t nbyte;
720 1.1 jtc {
721 1.1 jtc if (isrmt (fildes))
722 1.1 jtc {
723 1.1 jtc return (_rmt_read (fildes - REM_BIAS, buf, nbyte));
724 1.1 jtc }
725 1.1 jtc else
726 1.1 jtc {
727 1.1 jtc return (read (fildes, buf, nbyte));
728 1.1 jtc }
729 1.1 jtc }
730 1.1 jtc
731 1.1 jtc
732 1.1 jtc /*
733 1.1 jtc * Write to stream. Looks just like write(2) to caller.
734 1.1 jtc */
735 1.1 jtc
736 1.7 lukem ssize_t
737 1.7 lukem rmtwrite(fildes, buf, nbyte)
738 1.7 lukem int fildes;
739 1.7 lukem const void *buf;
740 1.7 lukem size_t nbyte;
741 1.1 jtc {
742 1.1 jtc if (isrmt (fildes))
743 1.1 jtc {
744 1.1 jtc return (_rmt_write (fildes - REM_BIAS, buf, nbyte));
745 1.1 jtc }
746 1.1 jtc else
747 1.1 jtc {
748 1.1 jtc return (write (fildes, buf, nbyte));
749 1.1 jtc }
750 1.1 jtc }
751 1.1 jtc
752 1.1 jtc /*
753 1.1 jtc * Perform lseek on file. Looks just like lseek(2) to caller.
754 1.1 jtc */
755 1.1 jtc
756 1.7 lukem off_t
757 1.7 lukem rmtlseek(fildes, offset, whence)
758 1.7 lukem int fildes;
759 1.7 lukem off_t offset;
760 1.7 lukem int whence;
761 1.1 jtc {
762 1.1 jtc if (isrmt (fildes))
763 1.1 jtc {
764 1.1 jtc return (_rmt_lseek (fildes - REM_BIAS, offset, whence));
765 1.1 jtc }
766 1.1 jtc else
767 1.1 jtc {
768 1.1 jtc return (lseek (fildes, offset, whence));
769 1.1 jtc }
770 1.1 jtc }
771 1.1 jtc
772 1.1 jtc
773 1.1 jtc /*
774 1.1 jtc * Close a file. Looks just like close(2) to caller.
775 1.1 jtc */
776 1.1 jtc
777 1.7 lukem int
778 1.7 lukem rmtclose(fildes)
779 1.7 lukem int fildes;
780 1.1 jtc {
781 1.1 jtc if (isrmt (fildes))
782 1.1 jtc {
783 1.1 jtc return (_rmt_close (fildes - REM_BIAS));
784 1.1 jtc }
785 1.1 jtc else
786 1.1 jtc {
787 1.1 jtc return (close (fildes));
788 1.1 jtc }
789 1.1 jtc }
790 1.1 jtc
791 1.1 jtc /*
792 1.1 jtc * Do ioctl on file. Looks just like ioctl(2) to caller.
793 1.1 jtc */
794 1.1 jtc
795 1.7 lukem int
796 1.7 lukem rmtioctl(fildes, request, arg)
797 1.7 lukem int fildes;
798 1.7 lukem unsigned long request;
799 1.7 lukem char *arg;
800 1.1 jtc {
801 1.1 jtc if (isrmt (fildes))
802 1.1 jtc {
803 1.1 jtc #ifdef RMTIOCTL
804 1.1 jtc return (_rmt_ioctl (fildes - REM_BIAS, request, arg));
805 1.1 jtc #else
806 1.1 jtc errno = EOPNOTSUPP;
807 1.1 jtc return (-1); /* For now (fnf) */
808 1.1 jtc #endif
809 1.1 jtc }
810 1.1 jtc else
811 1.1 jtc {
812 1.1 jtc return (ioctl (fildes, request, arg));
813 1.1 jtc }
814 1.1 jtc }
815 1.1 jtc
816 1.1 jtc
817 1.1 jtc /*
818 1.1 jtc * Duplicate an open file descriptor. Looks just like dup(2)
819 1.1 jtc * to caller.
820 1.1 jtc */
821 1.1 jtc
822 1.7 lukem int
823 1.7 lukem rmtdup(fildes)
824 1.7 lukem int fildes;
825 1.1 jtc {
826 1.1 jtc if (isrmt (fildes))
827 1.1 jtc {
828 1.1 jtc errno = EOPNOTSUPP;
829 1.1 jtc return (-1); /* For now (fnf) */
830 1.1 jtc }
831 1.1 jtc else
832 1.1 jtc {
833 1.1 jtc return (dup (fildes));
834 1.1 jtc }
835 1.1 jtc }
836 1.1 jtc
837 1.1 jtc /*
838 1.1 jtc * Get file status. Looks just like fstat(2) to caller.
839 1.1 jtc */
840 1.1 jtc
841 1.7 lukem int
842 1.7 lukem rmtfstat(fildes, buf)
843 1.7 lukem int fildes;
844 1.7 lukem struct stat *buf;
845 1.1 jtc {
846 1.1 jtc if (isrmt (fildes))
847 1.1 jtc {
848 1.1 jtc errno = EOPNOTSUPP;
849 1.1 jtc return (-1); /* For now (fnf) */
850 1.1 jtc }
851 1.1 jtc else
852 1.1 jtc {
853 1.1 jtc return (fstat (fildes, buf));
854 1.1 jtc }
855 1.1 jtc }
856 1.1 jtc
857 1.1 jtc
858 1.1 jtc /*
859 1.1 jtc * Get file status. Looks just like stat(2) to caller.
860 1.1 jtc */
861 1.1 jtc
862 1.7 lukem int
863 1.7 lukem rmtstat(path, buf)
864 1.7 lukem const char *path;
865 1.7 lukem struct stat *buf;
866 1.1 jtc {
867 1.1 jtc if (remdev (path))
868 1.1 jtc {
869 1.1 jtc errno = EOPNOTSUPP;
870 1.1 jtc return (-1); /* For now (fnf) */
871 1.1 jtc }
872 1.1 jtc else
873 1.1 jtc {
874 1.1 jtc return (stat (path, buf));
875 1.1 jtc }
876 1.1 jtc }
877 1.1 jtc
878 1.1 jtc
879 1.1 jtc
880 1.1 jtc /*
881 1.1 jtc * Create a file from scratch. Looks just like creat(2) to the caller.
882 1.1 jtc */
883 1.1 jtc
884 1.7 lukem int
885 1.7 lukem rmtcreat(path, mode)
886 1.7 lukem const char *path;
887 1.7 lukem mode_t mode;
888 1.1 jtc {
889 1.1 jtc if (remdev (path))
890 1.1 jtc {
891 1.1 jtc return (rmtopen (path, 1 | O_CREAT, mode));
892 1.1 jtc }
893 1.1 jtc else
894 1.1 jtc {
895 1.1 jtc return (creat (path, mode));
896 1.1 jtc }
897 1.1 jtc }
898 1.1 jtc
899 1.1 jtc /*
900 1.1 jtc * Rmtfcntl. Do a remote fcntl operation.
901 1.1 jtc */
902 1.1 jtc
903 1.7 lukem int
904 1.7 lukem rmtfcntl(fd, cmd, arg)
905 1.7 lukem int fd, cmd, arg;
906 1.1 jtc {
907 1.1 jtc if (isrmt (fd))
908 1.1 jtc {
909 1.1 jtc errno = EOPNOTSUPP;
910 1.1 jtc return (-1);
911 1.1 jtc }
912 1.1 jtc else
913 1.1 jtc {
914 1.1 jtc return (fcntl (fd, cmd, arg));
915 1.1 jtc }
916 1.1 jtc }
917 1.1 jtc
918 1.1 jtc /*
919 1.1 jtc * Rmtisatty. Do the isatty function.
920 1.1 jtc */
921 1.1 jtc
922 1.7 lukem int
923 1.7 lukem rmtisatty(fd)
924 1.7 lukem int fd;
925 1.1 jtc {
926 1.1 jtc if (isrmt (fd))
927 1.1 jtc return (0);
928 1.1 jtc else
929 1.1 jtc return (isatty (fd));
930 1.1 jtc }
931 1.1 jtc
932 1.1 jtc
933 1.1 jtc /*
934 1.1 jtc * Get file status, even if symlink. Looks just like lstat(2) to caller.
935 1.1 jtc */
936 1.1 jtc
937 1.7 lukem int
938 1.7 lukem rmtlstat(path, buf)
939 1.7 lukem const char *path;
940 1.7 lukem struct stat *buf;
941 1.1 jtc {
942 1.1 jtc if (remdev (path))
943 1.1 jtc {
944 1.1 jtc errno = EOPNOTSUPP;
945 1.1 jtc return (-1); /* For now (fnf) */
946 1.1 jtc }
947 1.1 jtc else
948 1.1 jtc {
949 1.1 jtc return (lstat (path, buf));
950 1.1 jtc }
951 1.1 jtc }
952