]>
Dogcows Code - chaz/tar/blob - src/rmt.c
1 /* Remote connection server.
2 Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any later
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Copyright (C) 1983 Regents of the University of California.
21 Redistribution and use in source and binary forms are permitted provided
22 that the above copyright notice and this paragraph are duplicated in all
23 such forms and that any documentation, advertising materials, and other
24 materials related to such distribution and use acknowledge that the
25 software was developed by the University of California, Berkeley. The
26 name of the University may not be used to endorse or promote products
27 derived from this software without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
29 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
30 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
34 #include <sys/socket.h>
37 # define EXIT_FAILURE 1
40 # define EXIT_SUCCESS 0
43 /* Maximum size of a string from the requesting program. */
44 #define STRING_SIZE 64
46 /* Name of executing program. */
47 const char *program_name
;
49 /* File descriptor of the tape device, or negative if none open. */
52 /* Buffer containing transferred data, and its allocated size. */
53 static char *record_buffer
= NULL
;
54 static int allocated_size
= -1;
56 /* Buffer for constructing the reply. */
57 static char reply_buffer
[BUFSIZ
];
59 /* Debugging tools. */
61 static FILE *debug_file
= NULL
;
64 if (debug_file) fprintf(debug_file, File)
66 #define DEBUG1(File, Arg) \
67 if (debug_file) fprintf(debug_file, File, Arg)
69 #define DEBUG2(File, Arg1, Arg2) \
70 if (debug_file) fprintf(debug_file, File, Arg1, Arg2)
72 /*------------------------------------------------.
73 | Return an error string, given an error number. |
74 `------------------------------------------------*/
82 private_strerror (int errnum
)
84 extern const char *const sys_errlist
[];
87 if (errnum
> 0 && errnum
<= sys_nerr
)
88 return sys_errlist
[errnum
];
89 return N_("Unknown system error");
91 # define strerror private_strerror
99 report_error_message (const char *string
)
101 DEBUG1 ("rmtd: E 0 (%s)\n", string
);
103 sprintf (reply_buffer
, "E0\n%s\n", string
);
104 write (1, reply_buffer
, strlen (reply_buffer
));
112 report_numbered_error (int num
)
114 DEBUG2 ("rmtd: E %d (%s)\n", num
, strerror (num
));
116 sprintf (reply_buffer
, "E%d\n%s\n", num
, strerror (num
));
117 write (1, reply_buffer
, strlen (reply_buffer
));
125 get_string (char *string
)
129 for (counter
= 0; counter
< STRING_SIZE
; counter
++)
131 if (read (0, string
+ counter
, 1) != 1)
134 if (string
[counter
] == '\n')
137 string
[counter
] = '\0';
145 prepare_record_buffer (int size
)
147 if (size
<= allocated_size
)
151 free (record_buffer
);
153 record_buffer
= malloc ((size_t) size
);
155 if (record_buffer
== NULL
)
157 DEBUG (_("rmtd: Cannot allocate buffer space\n"));
159 report_error_message (N_("Cannot allocate buffer space"));
160 exit (EXIT_FAILURE
); /* exit status used to be 4 */
163 allocated_size
= size
;
166 while (size
> 1024 &&
167 setsockopt (0, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof (size
)) < 0)
170 /* FIXME: I do not see any purpose to the following line... Sigh! */
171 size
= 1 + ((size
- 1) % 1024);
180 main (int argc
, char *const *argv
)
185 /* FIXME: Localisation is meaningless, unless --help and --version are
186 locally used. Localisation would be best accomplished by the calling
187 tar, on messages found within error packets. */
189 program_name
= argv
[0];
190 setlocale (LC_ALL
, "");
191 bindtextdomain (PACKAGE
, LOCALEDIR
);
192 textdomain (PACKAGE
);
194 /* FIXME: Implement --help and --version as for any other GNU program. */
199 debug_file
= fopen (*argv
, "w");
202 report_numbered_error (errno
);
205 setbuf (debug_file
, NULL
);
209 errno
= 0; /* FIXME: errno should be read-only */
211 if (read (0, &command
, 1) != 1)
216 /* FIXME: Maybe 'H' and 'V' for --help and --version output? */
220 char device_string
[STRING_SIZE
];
221 char mode_string
[STRING_SIZE
];
223 get_string (device_string
);
224 get_string (mode_string
);
225 DEBUG2 ("rmtd: O %s %s\n", device_string
, mode_string
);
230 #if defined (i386) && defined (AIX)
232 /* This is alleged to fix a byte ordering problem. I'm quite
233 suspicious if it's right. -- mib. */
236 int old_mode
= atoi (mode_string
);
239 if ((old_mode
& 3) == 0)
240 new_mode
|= O_RDONLY
;
242 new_mode
|= O_WRONLY
;
245 if (old_mode
& 0x0008)
246 new_mode
|= O_APPEND
;
247 if (old_mode
& 0x0200)
249 if (old_mode
& 0x0400)
251 if (old_mode
& 0x0800)
253 tape
= open (device_string
, new_mode
, 0666);
256 tape
= open (device_string
, atoi (mode_string
), 0666);
265 char device_string
[STRING_SIZE
];
267 get_string (device_string
); /* discard */
270 if (close (tape
) < 0)
278 char count_string
[STRING_SIZE
];
279 char position_string
[STRING_SIZE
];
281 get_string (count_string
);
282 get_string (position_string
);
283 DEBUG2 ("rmtd: L %s %s\n", count_string
, position_string
);
286 = lseek (tape
, (off_t
) atol (count_string
), atoi (position_string
));
294 char count_string
[STRING_SIZE
];
298 get_string (count_string
);
299 size
= atoi (count_string
);
300 DEBUG1 ("rmtd: W %s\n", count_string
);
302 prepare_record_buffer (size
);
303 for (counter
= 0; counter
< size
; counter
+= status
)
305 status
= read (0, &record_buffer
[counter
], size
- counter
);
308 DEBUG (_("rmtd: Premature eof\n"));
310 report_error_message (N_("Premature end of file"));
311 exit (EXIT_FAILURE
); /* exit status used to be 2 */
314 status
= write (tape
, record_buffer
, size
);
322 char count_string
[STRING_SIZE
];
325 get_string (count_string
);
326 DEBUG1 ("rmtd: R %s\n", count_string
);
328 size
= atoi (count_string
);
329 prepare_record_buffer (size
);
330 status
= read (tape
, record_buffer
, size
);
333 sprintf (reply_buffer
, "A%d\n", status
);
334 write (1, reply_buffer
, strlen (reply_buffer
));
335 write (1, record_buffer
, status
);
341 char operation_string
[STRING_SIZE
];
342 char count_string
[STRING_SIZE
];
344 get_string (operation_string
);
345 get_string (count_string
);
346 DEBUG2 ("rmtd: I %s %s\n", operation_string
, count_string
);
352 mtop
.mt_op
= atoi (operation_string
);
353 mtop
.mt_count
= atoi (count_string
);
354 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0)
356 status
= mtop
.mt_count
;
362 case 'S': /* status */
368 struct mtget operation
;
370 if (ioctl (tape
, MTIOCGET
, (char *) &operation
) < 0)
372 status
= sizeof (operation
);
373 sprintf (reply_buffer
, "A%d\n", status
);
374 write (1, reply_buffer
, strlen (reply_buffer
));
375 write (1, (char *) &operation
, sizeof (operation
));
382 DEBUG1 (_("rmtd: Garbage command %c\n"), command
);
384 report_error_message (N_("Garbage command"));
385 exit (EXIT_FAILURE
); /* exit status used to be 3 */
389 DEBUG1 ("rmtd: A %d\n", status
);
391 sprintf (reply_buffer
, "A%d\n", status
);
392 write (1, reply_buffer
, strlen (reply_buffer
));
396 report_numbered_error (errno
);
This page took 0.052283 seconds and 5 git commands to generate.