1 /* This file is part of GNU Paxutils.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "system-ioctl.h"
19 #include <configmake.h>
21 #include <argp-version-etc.h>
23 #include <full-write.h>
24 #include <configmake.h>
29 #include <safe-read.h>
32 # define EXIT_FAILURE 1
35 # define EXIT_SUCCESS 0
42 #define DEBUG(lev,msg) \
43 do { if (dbgout && (lev) <= dbglev) fprintf (dbgout, "%s", msg); } while (0)
44 #define DEBUG1(lev, fmt, x) \
45 do { if (dbgout && (lev) <= dbglev) fprintf (dbgout, fmt, x); } while (0)
46 #define DEBUG2(lev, fmt, x1, x2) \
49 if (dbgout && (lev) <= dbglev) \
50 fprintf (dbgout, fmt, x1, x2); \
54 #define VDEBUG(lev, pfx, fmt, ap) \
57 if (dbgout && (lev) <= dbglev) \
59 fprintf (dbgout, "%s", pfx); \
60 vfprintf (dbgout, fmt, ap); \
72 size_t len = strlen (str);
73 if (len > 1 && str[len-1] == '\n')
80 char *input_buf_ptr = NULL;
81 size_t input_buf_size = 0;
86 ssize_t rc = getline (&input_buf_ptr, &input_buf_size, stdin);
89 DEBUG1 (10, "C: %s", input_buf_ptr);
90 trimnl (input_buf_ptr);
93 DEBUG (10, "reached EOF");
98 rmt_write (const char *fmt, ...)
102 vfprintf (stdout, fmt, ap);
103 VDEBUG (10, "S: ", fmt, ap);
107 rmt_reply (uintmax_t code)
109 char buf[UINTMAX_STRSIZE_BOUND];
110 rmt_write ("A%s\n", umaxtostr (code, buf));
114 rmt_error_message (int code, const char *msg)
116 DEBUG1 (10, "S: E%d\n", code);
117 DEBUG1 (10, "S: %s\n", msg);
118 DEBUG1 (1, "error: %s\n", msg);
119 fprintf (stdout, "E%d\n%s\n", code, msg);
125 rmt_error_message (code, strerror (code));
129 char *record_buffer_ptr;
130 size_t record_buffer_size;
133 prepare_record_buffer (size_t size)
135 if (size > record_buffer_size)
137 record_buffer_ptr = xrealloc (record_buffer_ptr, size);
138 record_buffer_size = size;
153 #define RMT_KW(s,v) { #s, sizeof (#s) - 1, v }
156 xlat_kw (const char *s, const char *pfx,
157 struct rmt_kw const *kw, int *valp, const char **endp)
159 size_t slen = strlen (s);
163 size_t pfxlen = strlen (pfx);
164 if (slen > pfxlen && memcmp (s, pfx, pfxlen) == 0)
171 for (; kw->name; kw++)
174 && memcmp (kw->name, s, kw->len) == 0
175 && !(s[kw->len] && c_isalnum (s[kw->len])))
186 skip_ws (const char *s)
188 while (*s && c_isblank (*s))
193 static struct rmt_kw const open_flag_kw[] =
196 RMT_KW(APPEND, O_APPEND),
198 RMT_KW(CREAT, O_CREAT),
200 RMT_KW(DSYNC, O_DSYNC),
202 RMT_KW(EXCL, O_EXCL),
204 RMT_KW(LARGEFILE, O_LARGEFILE),
207 RMT_KW(NOCTTY, O_NOCTTY),
210 RMT_KW(NONBLOCK, O_NONBLOCK),
212 RMT_KW(RDONLY, O_RDONLY),
213 RMT_KW(RDWR, O_RDWR),
215 RMT_KW(RSYNC, O_RSYNC),
218 RMT_KW(SYNC, O_SYNC),
220 RMT_KW(TRUNC, O_TRUNC),
221 RMT_KW(WRONLY, O_WRONLY),
226 decode_open_flag (const char *mstr, int *pmode)
235 mstr = skip_ws (mstr);
238 else if (c_isdigit (*mstr))
239 v = strtol (mstr, (char**) &p, 10);
240 else if (xlat_kw (mstr, "O_", open_flag_kw, &v, &p))
242 rmt_error_message (EINVAL, "invalid open mode");
248 if (*p && c_isblank (*p))
256 rmt_error_message (EINVAL, "invalid open mode");
262 rmt_error_message (EINVAL, "invalid open mode");
277 Opens the <device> with given <flags>. If a device had already been opened,
278 it is closed before opening the new one.
282 <device> - name of the device to open.
283 <flags> - flags for open(2): a decimal number, or any valid O_* constant
284 from fcntl.h (the initial O_ may be omitted), or a bitwise or (using '|')
285 of any number of these, e.g.:
293 A0\n on success, E<errno>\n<msg>\n on error.
297 The BSD version allows only decimal number as <flags>.
301 open_device (char *str)
303 char *device = xstrdup (str);
307 flag_str = rmt_read ();
310 DEBUG (1, "unexpected EOF");
313 if (decode_open_flag (flag_str, &flag) == 0)
318 device_fd = open (device, flag, MODE_RW);
333 Close the currently open device.
337 Any arguments are silently ignored.
341 A0\n on success, E<errno>\n<msg>\n on error.
346 if (close (device_fd) < 0)
357 L<whence>\n<offset>\n
361 Perform an lseek(2) on the currently open device with the specified
366 <whence> - Where to measure offset from. Valid values are:
367 0, SET, SEEK_SET to seek from the file beginning,
368 1, CUR, SEEK_CUR to seek from the current location in file,
369 2, END, SEEK_END to seek from the file end.
372 A<offset>\n on success. The <offset> is the new offset in file.
373 E<errno>\n<msg>\n on error.
377 The BSD version allows only 0,1,2 as <whence>.
380 static struct rmt_kw const seek_whence_kw[] =
382 RMT_KW(SET, SEEK_SET),
383 RMT_KW(CUR, SEEK_CUR),
384 RMT_KW(END, SEEK_END),
389 lseek_device (const char *str)
396 if (str[0] && str[1] == 0)
413 rmt_error_message (EINVAL, N_("Seek direction out of range"));
417 else if (xlat_kw (str, "SEEK_", seek_whence_kw, &whence, (const char **) &p))
419 rmt_error_message (EINVAL, N_("Invalid seek direction"));
424 n = off = strtoumax (str, &p, 10);
427 rmt_error_message (EINVAL, N_("Invalid seek offset"));
431 if (n != off || errno == ERANGE)
433 rmt_error_message (EINVAL, N_("Seek offset out of range"));
437 off = lseek (device_fd, off, whence);
450 Read <count> bytes of data from the current device.
454 <count> - number of bytes to read.
458 On success: A<rdcount>\n, followed by <rdcount> bytes of data read from
460 On error: E<errno>\n<msg>\n
464 read_device (const char *str)
471 n = size = strtoumax (str, &p, 10);
474 rmt_error_message (EINVAL, N_("Invalid byte count"));
478 if (n != size || errno == ERANGE)
480 rmt_error_message (EINVAL, N_("Byte count out of range"));
484 prepare_record_buffer (size);
485 status = safe_read (device_fd, record_buffer_ptr, size);
486 if (status == SAFE_READ_ERROR)
491 full_write (STDOUT_FILENO, record_buffer_ptr, status);
497 W<count>\n followed by <count> bytes of input data.
501 Write data onto the current device.
505 <count> - number of bytes.
509 On success: A<wrcount>\n, where <wrcount> is number of bytes actually
511 On error: E<errno>\n<msg>\n
515 write_device (const char *str)
522 n = size = strtoumax (str, &p, 10);
525 rmt_error_message (EINVAL, N_("Invalid byte count"));
529 if (n != size || errno == ERANGE)
531 rmt_error_message (EINVAL, N_("Byte count out of range"));
535 prepare_record_buffer (size);
536 if (fread (record_buffer_ptr, size, 1, stdin) != 1)
539 rmt_error_message (EIO, N_("Premature eof"));
545 status = full_write (device_fd, record_buffer_ptr, size);
558 Perform a MTIOCOP ioctl(2) command using the specified paramedters.
562 <opcode> - MTIOCOP operation code.
568 On error: E<errno>\n<msg>\n
572 iocop_device (const char *str)
579 opcode = strtol (str, &p, 10);
582 rmt_error_message (EINVAL, N_("Invalid operation code"));
586 n = count = strtoumax (str, &p, 10);
589 rmt_error_message (EINVAL, N_("Invalid byte count"));
593 if (n != count || errno == ERANGE)
595 rmt_error_message (EINVAL, N_("Byte count out of range"));
603 mtop.mt_count = count;
604 if (mtop.mt_count != count)
606 rmt_error_message (EINVAL, N_("Byte count out of range"));
611 if (ioctl (device_fd, MTIOCTOP, (char *) &mtop) < 0)
617 rmt_error_message (ENOSYS, N_("Operation not supported"));
627 Return the status of the open device, as obtained with a MTIOCGET
636 On success: A<count>\n followed by <count> bytes of data.
637 On error: E<errno>\n<msg>\n
641 status_device (const char *str)
645 rmt_error_message (EINVAL, N_("Unexpected arguments"));
652 if (ioctl (device_fd, MTIOCGET, (char *) &mtget) < 0)
656 rmt_reply (sizeof (mtget));
657 full_write (STDOUT_FILENO, (char *) &mtget, sizeof (mtget));
661 rmt_error_message (ENOSYS, N_("Operation not supported"));
667 const char *argp_program_version = "rmt (" PACKAGE_NAME ") " VERSION;
668 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
670 static char const doc[] = N_("Manipulate a tape drive, accepting commands from a remote process");
673 DEBUG_FILE_OPTION = 256
676 static struct argp_option options[] = {
677 { "debug", 'd', N_("NUMBER"), 0,
678 N_("set debug level"), 0 },
679 { "debug-file", DEBUG_FILE_OPTION, N_("FILE"), 0,
680 N_("set debug output file name"), 0 },
685 parse_opt (int key, char *arg, struct argp_state *state)
690 dbglev = strtol (arg, NULL, 0);
693 case DEBUG_FILE_OPTION:
694 dbgout = fopen (arg, "w");
696 error (EXIT_FAILURE, errno, _("cannot open %s"), arg);
710 return ARGP_ERR_UNKNOWN;
715 static struct argp argp = {
725 static const char *rmt_authors[] = {
740 main (int argc, char **argv)
746 set_program_name (argv[0]);
747 argp_version_setup ("rmt", rmt_authors);
749 if (isatty (STDOUT_FILENO))
751 setlocale (LC_ALL, "");
752 bindtextdomain (PACKAGE, LOCALEDIR);
753 textdomain (PACKAGE);
756 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &idx, NULL))
761 error (EXIT_FAILURE, 0, _("too many arguments"));
762 dbgout = fopen (argv[idx], "w");
764 error (EXIT_FAILURE, errno, _("cannot open %s"), argv[idx]);
768 while (!stop && (buf = rmt_read ()) != NULL)
778 iocop_device (buf + 1);
782 lseek_device (buf + 1);
786 open_device (buf + 1);
790 read_device (buf + 1);
794 status_device (buf + 1);
798 write_device (buf + 1);
802 DEBUG1 (1, "garbage input %s\n", buf);
803 rmt_error_message (EINVAL, N_("Garbage command"));
804 return EXIT_FAILURE; /* exit status used to be 3 */
809 free (input_buf_ptr);
810 free (record_buffer_ptr);