Debian Codegraph @

libsrc/os/unix/iodevr.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2011 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .NAME iodevr
31 .LANGUAGE C
32 .AUTHOR IPG-ESO Garching
33 .CATEGORY Host operating system interfaces. Remote tape management.
34 .ENVIRONMENT UNIX
35 .COMMENTS Remote tape management.
36
37 The functions of this module provides remote connection with a
38 server in a remote host which executes basic i/o on devices like
39 magnetic tapes, optical disks on UNIX enviroments.
40
41 This interface is called by "osu" when detecting that i/o device
42 is not located in a local but in a remote host. It will try to
43 establish connection with a tapeserver in the remote host.
44
45 A new INET service is expected to be defined for both
46 client and server in the system file /etc/services: mtape
47 Port numbers for private services are typically in the range 1024-2048.
48
49 .REMARKS The tapeserver must be running in the remote host before
50 trying to stablish connection with remote devices. (See
51 $MIDASHOME/$MIDVERS/system/tapeserv/inmtaped.doc file
52 for more details)
53
54 .VERSION 1.1 900124 Implementation C. Guirao
55
56 110525 last modif
57
58 ------------------------------------------------------------*/
59
60 /*
61 * Define _POSIX_SOURCE to indicate
62 * that this is a POSIX program
63 * Except for SUN & GNU-SUN & HP-UX & OSF & AIX & ULTRIX & SGI
64 * because the POSIX definition
65 * causes undefines (eg. u_long) in socket includes.
66 */
67
68 #if !defined(__APPLE__) && !defined(__hpux) && !defined(sun) && !defined(__sun) && !defined(__osf__) && !defined(_AIX) && !defined(ultrix) && !defined(__sgi) && !defined(__linux__) &&!defined(__FreeBSD__)
69 #define _POSIX_SOURCE 1
70 #endif
71
72
73 #ifndef Cygwin
74
75 /* Cygwin has no XDR structures... */
76
77 #define next_iodev iodevg /* Next iodev in chain: generic */
78 static char class_name[] = "remote";
79
80 #include <unistd.h>
81 #include <sys/types.h>
82 #include <proto_os.h> /* ANSI-C prototyping */
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <rpc/rpc.h> /* XDR definitions */
86 #include <sys/stat.h>
87 #include <fcntl.h>
88 #include <pwd.h>
89 #include <errno.h>
90 #include <oserror.h>
91 #include <string.h>
92
93 #include <osudef.h>
94 #include <rtape.h>
95 #include <osparms.h>
96
97 #define NULL_PTR(x) (x *)0
98 #define RET_ERROR(a,b) {osxclose(skfd); oserror=(a); oserrmsg=(b); return(-1);}
99 #define TAPE_SERVICE "mtape" /* must be defined in /etc/services */
100
101
102 #define ERR_SYNC msg0
103 #define ERR_XDR msg1
104 static char msg0[] = "Out of synchronism with tape server.";
105 static char msg1[] = "XDR library error";
106 static char errmsg[MAX_MSG];
107
108 char *osmsg();
109 /* FILE *fdopen(); */
110
111 static int skfd; /* Socket file descriptor */
112 FILE *fp_in, *fp_out;
113
114 struct info {
115 struct osustat stat;
116 int filenum;
117 long blkno;
118 };
119
120 /*
121 * XDR routines
122 */
123 static XDR xdrs_in, xdrs_out;
124 bool_t xdr_command(xdrs, cmd)
125 XDR *xdrs;
126 struct command *cmd;
127 {
128 return(xdr_int(xdrs, &cmd->cmd) &&
129 xdr_int(xdrs, &cmd->param[0]) &&
130 xdr_int(xdrs, &cmd->param[1]));
131 }
132
133 bool_t xdr_osustat(xdrs, stat)
134 XDR *xdrs;
135 struct osustat *stat;
136 {
137 #if defined(__APPLE__)
138 int for_stat_usize;
139
140 return (xdr_long(xdrs, &for_stat_usize) &&
141 xdr_int(xdrs, &stat->blocksize) &&
142 xdr_int(xdrs, &stat->density) &&
143 xdr_int(xdrs, &stat->isda) &&
144 xdr_int(xdrs, &stat->istm));
145 #else
146 return(xdr_long(xdrs, &stat->usize) &&
147 xdr_int(xdrs, &stat->blocksize) &&
148 xdr_int(xdrs, &stat->density) &&
149 xdr_int(xdrs, &stat->isda) &&
150 xdr_int(xdrs, &stat->istm));
151 #endif
152 }
153
154 bool_t xdr_result(xdrs, res)
155 XDR *xdrs;
156 struct result *res;
157 {
158 return(xdr_int(xdrs, &res->ret) &&
159 xdr_int(xdrs, &res->oserror) &&
160 xdr_int(xdrs, &res->nobyt));
161 }
162
163
164 static int send_op(op)
165 struct command *op;
166 {
167 oserror = 0;
168
169 /* Send the command to the server */
170 if (!xdr_command(&xdrs_out, op)) RET_ERROR(-1,ERR_XDR);
171 fflush(fp_out);
172
173 return(0);
174 }
175
176 static int get_res(res)
177 struct result *res;
178 {
179 char *msg;
180
181 oserror = 0;
182 msg = errmsg;
183 /* Wait and get results from the server */
184 if (!xdr_result(&xdrs_in, res)) RET_ERROR(-1,ERR_XDR);
185
186 /* If any error, then get error message */
187 oserror = res->oserror;
188 if ( res->ret < 0 && res->nobyt > 0)
189 if (!xdr_string(&xdrs_in, &msg,MAX_MSG)) RET_ERROR(-1,ERR_XDR);
190 return(0);
191 }
192
193 static int ioctop(op)
194 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
195 .PURPOSE Interface to remote ioctl routine.
196 The structure command is already completed by the caller.
197 .RETURNS 0 (OK) / -1 (error)
198 ------------------------------------------------------------*/
199 struct command *op; /* IN: Command to be executed */
200 {
201 struct result res;
202
203 /* Send command and get results */
204 if (send_op(op) < 0) return(-1);
205 if (get_res(&res) < 0) return(-1);
206
207 /* Returns the remote-return-code */
208 return(res.ret);
209 }
210
211 static int ioinfo(fd, s, filenum, blkno)
212 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
213 .PURPOSE Retrieve Info concerning an opened device
214 .RETURNS 0 (success) / -1 (error)
215 ------------------------------------------------------------*/
216 int fd; /* IN: File Descriptor */
217 struct osustat *s; /* OUT: The filled components */
218 int *filenum; /* OUT: Where we are */
219 #if defined(__APPLE__)
220 int *blkno; /* OUT: Where we are */
221 #else
222 long *blkno; /* OUT: Where we are */
223 #endif
224 {
225 struct command op;
226 struct result res;
227
228
229 /* Prepare command structure to tapeserver */
230 op.cmd = READ_INFO;
231 op.param[0] = fd;
232 op.param[1] = 0;
233
234 /* Send the command to the server */
235
236 /* Send command and get results */
237 if (send_op(&op) < 0) return(-1);
238 if (get_res(&res) < 0) return(-1);
239
240 /* If no error, more data is expected from the server */
241 if ( res.ret == 0) {
242 if (!xdr_osustat(&xdrs_in, s)) RET_ERROR(-1,ERR_XDR);
243 if (!xdr_int(&xdrs_in, filenum)) RET_ERROR(-1,ERR_XDR);
244 if (!xdr_long(&xdrs_in, blkno)) RET_ERROR(-1,ERR_XDR);
245 }
246
247 /* It returns the remote return code */
248 return(res.ret);
249 }
250
251
252 static int ioopen(name,mode,den)
253 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
254 .PURPOSE Open a tape device
255 .RETURNS File descriptor / -1 (error)
256 ------------------------------------------------------------*/
257 char *name; /* IN: Physical name of tape device */
258 int mode; /* IN: Open mode */
259 int den; /* IN: Density. Not used */
260 {
261 static char devname[MAX_DEVICE_NAME], remotehost[MAX_HOST_NAME];
262 static char *channame[2];
263 register char *p, *q;
264 int uid;
265 struct command op;
266 struct result res;
267 struct passwd *passwd;
268 char null;
269 char *pname, *puser;
270
271 oserror = 0; /* Clear error from getdev */
272
273 /* Check that name comes in the format <remotehost!device. */
274 for (q = remotehost , p = name ; (*p) && (*p != '!') && (*p != ':') ; p++, q++) *q = *p;
275 *q = '\0';
276 if (*p == '!' || *p == ':' ) p++;
277 else {
278 oserror = -1;
279 oserrmsg = "Name not in <remotehost:device> format";
280 return(-1);
281 }
282 for (q = devname; *p; p++, q++) *q = *p;
283 *q = '\0';
284
285 channame[0] = TAPE_SERVICE;
286 channame[1] = remotehost;
287 /* Open socket in WRITE mode to establish connection with remote host */
288 channame[0] = TAPE_SERVICE;
289 if ( (skfd=osxopen(channame,NETW|IPC_WRITE)) < 0 ) {
290 (void)strcpy(errmsg,"OPEN SOCKET WRITE_MODE: ");
291 strcat(errmsg,osmsg());
292 oserrmsg=errmsg;
293 oserror = -1;
294 return(-1);
295 }
296
297 /*
298 * Empty the input buffer. It could contains error messages from the
299 * the inetd itself (eg. ld.so: warning: /usr/lib/libc.so.1.8 ....
300 * NOTE: the timeout is set to 1 sec. this might need to be increased.
301 */
302 while ( osxinfo(skfd, 1, 0) == DATARDY ) osxread(skfd, &null, 1);
303
304 fp_in = fdopen(skfd,"r");
305 fp_out = fdopen(skfd,"w");
306 xdrstdio_create(&xdrs_in, fp_in, XDR_DECODE);
307 xdrstdio_create(&xdrs_out, fp_out, XDR_ENCODE);
308
309 /*
310 * Prepares command structure for OPEN device.
311 * In this command is indicated length of a second buffer to be
312 * sent to the server with the device name, and the name of local
313 * user.
314 */
315 uid=getuid();
316 passwd=getpwuid(uid);
317 op.cmd = OPEN_DEVICE;
318 op.param[0] = mode;
319 op.param[1] = 0;
320
321 /* Send OPEN command to server */
322 if (send_op(&op) < 0) return(-1);
323
324 /* Prepare a buffer with the second part of OPEN command */
325 pname = name;
326 puser = passwd->pw_name;
327
328 /* Send devicename and username to server */
329 if (!xdr_string(&xdrs_out, &pname, MAX_HOST_NAME+MAX_DEVICE_NAME))
330 RET_ERROR(-1,ERR_XDR);
331 if (!xdr_string(&xdrs_out, &puser, MAX_USER_NAME))
332 RET_ERROR(-1,ERR_XDR);
333 fflush(fp_out);
334
335 /* Read result of OPEN command from server */
336 if (get_res(&res) < 0) return(-1);
337
338 /* Returns return_code from remote open */
339 return(res.ret);
340 }
341
342 static int ioclose(fd/*,option*/)
343 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
344 .PURPOSE Close the opened device
345 .RETURNS 0/-1 if error
346 ------------------------------------------------------------*/
347 int fd; /* IN: Tape file descriptor */
348 /*int option;*/ /* IN: Option OPU_DISMOUNT */
349 {
350 struct command op;
351 struct result res;
352
353 /* Prepare CLOSE command to be sent to server */
354 op.cmd = CLOSE_DEVICE;
355 op.param[0] = fd;
356 op.param[1] = 0;
357
358 /* Send CLOSE command */
359 if (send_op(&op) < 0) return(-1);
360
361 /* Wait for the server to return result */
362 if (get_res(&res) < 0) return(-1);
363
364 /* Close socket */
365 if ( osxclose(skfd) < 0) return(-1);
366
367 /* Close XDR streams */
368 xdr_destroy(&xdrs_in);
369 xdr_destroy(&xdrs_out);
370
371 /* Returns the return code from server */
372 return(res.ret);
373 }
374
375
376 static int ioread(fd,buffer,size)
377 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
378 .PURPOSE Read a block from a magnetic tape.
379 .RETURNS Bytes read / -1 if error
380 .REMARKS 0 Bytes read, means a File Mark was detected.
381 .REMARKS oserror set to -2 if buffer too small
382 ------------------------------------------------------------*/
383 int fd; /* IN: Tape device file descriptor */
384 char *buffer; /* IN: Buffer for reading */
385 int size; /* IN: Length of bytes to be read */
386 {
387 int ret;
388 struct command op;
389 struct result res;
390
391 /* Prepares READ command */
392 op.cmd = READ_DATA;
393 op.param[0] = fd;
394 op.param[1] = size;
395
396 /* Send READ command to server */
397 if (send_op(&op) < 0) return(-1);
398
399 /* Wait for results of READ command */
400 if (get_res(&res) < 0) return(-1);
401
402 /* READ OK?, then get the data from the server */
403 if ( res.ret > 0 ) {
404 if (!xdr_bytes(&xdrs_in, &buffer,(u_int *)&ret,(u_int)size))
405 RET_ERROR(-1,ERR_XDR);
406 if ( ret != res.ret ) RET_ERROR(-1,ERR_SYNC);
407 }
408
409 /* Returns the return code from server */
410 return(res.ret);
411 }
412
413 static int iowrite(fd,buffer,size)
414 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
415 .PURPOSE Write a block on a magnetic tape.
416 .RETURNS Bytes written / -1 (error)
417 ------------------------------------------------------------*/
418 int fd; /* IN: Tape device file descriptor */
419 char *buffer; /* IN: Buffer for reading */
420 int size; /* IN: Length of bytes to be read */
421 {
422 struct result res;
423 struct command op;
424
425 /* Prepares WRITE command */
426 op.cmd = WRITE_DATA;
427 op.param[0] = fd;
428 op.param[1] = size;
429
430 /* Send WRITE command to server */
431 if (send_op(&op) < 0 ) return(-1);
432
433 /* Send data to be written in device */
434 if (!xdr_bytes(&xdrs_out, &buffer,(u_int *)&size,(u_int)size)) RET_ERROR(-1,ERR_XDR);
435 fflush(fp_out);
436
437 /* Wait for results of WRITE command */
438 if (get_res(&res) < 0) return(-1);
439
440 /* Return code from server */
441 return(res.ret);
442 }
443
444 static int ioweof(fd,ntm)
445 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
446 .PURPOSE Write end-of-file record (tape_mark) on the tape.
447 .RETURNS 0 / -1 (error)
448 ------------------------------------------------------------*/
449 int fd; /* IN: Tape device file descriptor */
450 int ntm; /* IN: Number of tape marks */
451 {
452 struct command op;
453
454 /* Prepare WRITE TAPE MARK command */
455 op.cmd = WRITE_EOF;
456 op.param[0] = fd;
457 op.param[1] = ntm;
458
459 /* Executes command in server and returns the remote-return-code */
460 return(ioctop(&op));
461 }
462
463 static int iofsf(fd,ntm)
464 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
465 .PURPOSE Skip forward space file on a tape.
466 .RETURNS Tape marks skipped/ -1 (error)
467 ------------------------------------------------------------*/
468 int fd; /* IN: Tape device file descriptor */
469 int ntm; /* IN: Number of tape marks */
470 {
471 struct command op;
472
473 /* Prepare command */
474 op.cmd = SKIP_FSF;
475 op.param[0] = fd;
476 op.param[1] = ntm;
477
478 /* Executes command in server and returns the remote-return-code */
479 return(ioctop(&op));
480 }
481
482
483 static int iobsf(fd,ntm)
484 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
485 .PURPOSE Skip backward space file on a tape.
486 .RETURNS Tape marks skipped/ -1 (error)
487 ------------------------------------------------------------*/
488 int fd; /* IN: Tape device file descriptor */
489 int ntm; /* IN: Number of tape marks */
490 {
491 struct command op;
492
493 /* Prepare command */
494 op.cmd = SKIP_BSF;
495 op.param[0] = fd;
496 op.param[1] = ntm;
497
498 /* Executes command in server and returns the remote-return-code */
499 return(ioctop(&op));
500 }
501
502
503 static int iorew(fd)
504 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
505 .PURPOSE Rewind tape
506 .RETURNS 0 / -1 (error)
507 ------------------------------------------------------------*/
508 int fd; /* IN: Tape device file descriptor */
509 {
510 struct command op;
511
512 /* Prepare command */
513 op.cmd = REWIND;
514 op.param[0] = fd;
515 op.param[1] = 0;
516
517 /* Executes command in server and returns the remote-return-code */
518 return(ioctop(&op));
519 }
520
521 static int ioeom(fd)
522 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
523 .PURPOSE Move to EOMedia
524 .RETURNS 0 (OK) / -1 (not done, error)
525 ------------------------------------------------------------*/
526 int fd; /* IN: Tape device file descriptor */
527 {
528 struct command op;
529
530 /* Prepare command */
531 op.cmd = SKIP_EOM;
532 op.param[0] = fd;
533 op.param[1] = 0;
534
535 /* Executes command in server and returns the remote-return-code */
536 return(ioctop(&op));
537 }
538
539 /*=====================================================================
540 * Definition of the structure returned to osu
541 *=====================================================================*/
542 #ifdef next_iodev
543 struct iolist *next_iodev();
544 #else
545 #define next_iodev (IODEV)0
546 #endif
547
548 static OPITEM list_of_functions[] = {
549 { U_INFO, ioinfo},
550 { U_OPEN, ioopen},
551 { U_CLOSE, ioclose},
552 { U_READ, ioread},
553 { U_WRITE, iowrite},
554 { U_REWIND, iorew},
555 { U_WEOF, ioweof},
556 { U_FMF, iofsf}, /* File Move Forward */
557 { U_FMB, iobsf}, /* File Move Backward */
558 { U_EOM, ioeom} /* Move to EOMedia */
559 };
560
561 static struct iolist this_dev = {
562 next_iodev, /* Next iodev in List */
563 class_name, /* How it's written in DEVCAPFILE */
564 sizeof(list_of_functions)/sizeof(list_of_functions[0]),
565 list_of_functions
566 };
567
568 struct iolist *iodevr()
569 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
570 .PURPOSE Get All definitions concerning this class of Devices
571 .RETURNS The iolist
572 .REMARKS Simply returns the local iolist address...
573 ------------------------------------------------------------*/
574 {
575 return(&this_dev);
576 }
577
578
579
580 #else
581
582
583 static int iobsf(fd,ntm)
584 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
585 .PURPOSE Skip backward space file on a tape.
586 .RETURNS Tape marks skipped/ -1 (error)
587 ------------------------------------------------------------*/
588 int fd; /* IN: Tape device file descriptor */
589 int ntm; /* IN: Number of tape marks */
590 {
591 return 0;
592 }
593
594 #endif
595