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