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);
104 VDEBUG (10, "S: ", fmt, ap);
108 rmt_reply (uintmax_t code)
110 char buf[UINTMAX_STRSIZE_BOUND];
111 rmt_write ("A%s\n", umaxtostr (code, buf));
115 rmt_error_message (int code, const char *msg)
117 DEBUG1 (10, "S: E%d\n", code);
118 DEBUG1 (10, "S: %s\n", msg);
119 DEBUG1 (1, "error: %s\n", msg);
120 fprintf (stdout, "E%d\n%s\n", code, msg);
127 rmt_error_message (code, strerror (code));
131 char *record_buffer_ptr;
132 size_t record_buffer_size;
135 prepare_record_buffer (size_t size)
137 if (size > record_buffer_size)
139 record_buffer_ptr = xrealloc (record_buffer_ptr, size);
140 record_buffer_size = size;
155 #define RMT_KW(s,v) { #s, sizeof (#s) - 1, v }
158 xlat_kw (const char *s, const char *pfx,
159 struct rmt_kw const *kw, int *valp, const char **endp)
161 size_t slen = strlen (s);
165 size_t pfxlen = strlen (pfx);
166 if (slen > pfxlen && memcmp (s, pfx, pfxlen) == 0)
173 for (; kw->name; kw++)
176 && memcmp (kw->name, s, kw->len) == 0
177 && !(s[kw->len] && c_isalnum (s[kw->len])))
188 skip_ws (const char *s)
190 while (*s && c_isblank (*s))
195 static struct rmt_kw const open_flag_kw[] =
198 RMT_KW(APPEND, O_APPEND),
200 RMT_KW(CREAT, O_CREAT),
202 RMT_KW(DSYNC, O_DSYNC),
204 RMT_KW(EXCL, O_EXCL),
206 RMT_KW(LARGEFILE, O_LARGEFILE),
209 RMT_KW(NOCTTY, O_NOCTTY),
212 RMT_KW(NONBLOCK, O_NONBLOCK),
214 RMT_KW(RDONLY, O_RDONLY),
215 RMT_KW(RDWR, O_RDWR),
217 RMT_KW(RSYNC, O_RSYNC),
220 RMT_KW(SYNC, O_SYNC),
222 RMT_KW(TRUNC, O_TRUNC),
223 RMT_KW(WRONLY, O_WRONLY),
228 decode_open_flag (const char *mstr, int *pmode)
230 int numeric_mode = 0;
234 mstr = skip_ws (mstr);
235 if (c_isdigit (*mstr))
237 numeric_mode = strtol (mstr, (char**) &p, 10);
247 mstr = skip_ws (mstr);
250 else if (c_isdigit (*mstr))
251 v = strtol (mstr, (char**) &p, 10);
252 else if (xlat_kw (mstr, "O_", open_flag_kw, &v, &p))
254 rmt_error_message (EINVAL, "invalid open mode");
260 if (*p && c_isblank (*p))
268 rmt_error_message (EINVAL, "invalid open mode");
274 rmt_error_message (EINVAL, "invalid open mode");
292 Opens the <device> with given <flags>. If a device had already been opened,
293 it is closed before opening the new one.
297 <device> - name of the device to open.
298 <flags> - flags for open(2): a decimal number, or any valid O_* constant
299 from fcntl.h (the initial O_ may be omitted), or a bitwise or (using '|')
300 of any number of these, e.g.:
306 In addition, a compined form is also allowed, i.e. a decimal mode followed
307 by its symbolic representation. In this case the symbolic representation
312 A0\n on success, E0\n<msg>\n on error.
316 BSD version allows only decimal number as <flags>
320 open_device (char *str)
322 char *device = xstrdup (str);
326 flag_str = rmt_read ();
329 DEBUG (1, "unexpected EOF");
332 if (decode_open_flag (flag_str, &flag) == 0)
337 device_fd = open (device, flag, MODE_RW);
352 Close the currently open device.
356 Any arguments are silently ignored.
360 A0\n on success, E0\n<msg>\n on error.
365 if (close (device_fd) < 0)
376 L<whence>\n<offset>\n
380 Perform an lseek(2) on the currently open device with the specified
385 <whence> - Where to measure offset from. Valid values are:
386 0, SET, SEEK_SET to seek from the file beginning,
387 1, CUR, SEEK_CUR to seek from the current location in file,
388 2, END, SEEK_END to seek from the file end.
391 A<offset>\n on success. The <offset> is the new offset in file.
392 E0\n<msg>\n on error.
396 BSD version allows only 0,1,2 as <whence>.
399 static struct rmt_kw const seek_whence_kw[] =
401 RMT_KW(SET, SEEK_SET),
402 RMT_KW(CUR, SEEK_CUR),
403 RMT_KW(END, SEEK_END),
408 lseek_device (const char *str)
415 if (str[0] && str[1] == 0)
432 rmt_error_message (EINVAL, N_("Seek direction out of range"));
436 else if (xlat_kw (str, "SEEK_", seek_whence_kw, &whence, (const char **) &p))
438 rmt_error_message (EINVAL, N_("Invalid seek direction"));
443 n = off = strtoumax (str, &p, 10);
446 rmt_error_message (EINVAL, N_("Invalid seek offset"));
450 if (n != off || errno == ERANGE)
452 rmt_error_message (EINVAL, N_("Seek offset out of range"));
456 off = lseek (device_fd, off, whence);
469 Read <count> bytes of data from the current device.
473 <count> - number of bytes to read.
477 On success: A<rdcount>\n, followed by <rdcount> bytes of data read from
479 On error: E0\n<msg>\n
483 read_device (const char *str)
490 n = size = strtoumax (str, &p, 10);
493 rmt_error_message (EINVAL, N_("Invalid byte count"));
497 if (n != size || errno == ERANGE)
499 rmt_error_message (EINVAL, N_("Byte count out of range"));
503 prepare_record_buffer (size);
504 status = safe_read (device_fd, record_buffer_ptr, size);
505 if (status == SAFE_READ_ERROR)
510 full_write (STDOUT_FILENO, record_buffer_ptr, status);
516 W<count>\n followed by <count> bytes of input data.
520 Write data onto the current device.
524 <count> - number of bytes.
528 On success: A<wrcount>\n, where <wrcount> is number of bytes actually
530 On error: E0\n<msg>\n
534 write_device (const char *str)
541 n = size = strtoumax (str, &p, 10);
544 rmt_error_message (EINVAL, N_("Invalid byte count"));
548 if (n != size || errno == ERANGE)
550 rmt_error_message (EINVAL, N_("Byte count out of range"));
554 prepare_record_buffer (size);
555 if (fread (record_buffer_ptr, size, 1, stdin) != 1)
558 rmt_error_message (EIO, N_("Premature eof"));
564 status = full_write (device_fd, record_buffer_ptr, size);
577 Perform a MTIOCOP ioctl(2) command using the specified paramedters.
581 <opcode> - MTIOCOP operation code.
587 On error: E0\n<msg>\n
591 iocop_device (const char *str)
598 opcode = strtol (str, &p, 10);
601 rmt_error_message (EINVAL, N_("Invalid operation code"));
605 n = count = strtoumax (str, &p, 10);
608 rmt_error_message (EINVAL, N_("Invalid byte count"));
612 if (n != count || errno == ERANGE)
614 rmt_error_message (EINVAL, N_("Byte count out of range"));
622 mtop.mt_count = count;
623 if (mtop.mt_count != count)
625 rmt_error_message (EINVAL, N_("Byte count out of range"));
630 if (ioctl (device_fd, MTIOCTOP, (char *) &mtop) < 0)
636 rmt_error_message (ENOSYS, N_("Operation not supported"));
646 Return the status of the open device, as obtained with a MTIOCGET
655 On success: A<count>\n followed by <count> bytes of data.
656 On error: E0\n<msg>\n
660 status_device (const char *str)
664 rmt_error_message (EINVAL, N_("Unexpected arguments"));
671 if (ioctl (device_fd, MTIOCGET, (char *) &mtget) < 0)
675 rmt_reply (sizeof (mtget));
676 full_write (STDOUT_FILENO, (char *) &mtget, sizeof (mtget));
680 rmt_error_message (ENOSYS, N_("Operation not supported"));
686 const char *argp_program_version = "rmt (" PACKAGE_NAME ") " VERSION;
687 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
689 static char const doc[] = N_("Manipulate a tape drive, accepting commands from a remote process");
692 DEBUG_FILE_OPTION = 256
695 static struct argp_option options[] = {
696 { "debug", 'd', N_("NUMBER"), 0,
697 N_("set debug level"), 0 },
698 { "debug-file", DEBUG_FILE_OPTION, N_("FILE"), 0,
699 N_("set debug output file name"), 0 },
704 parse_opt (int key, char *arg, struct argp_state *state)
709 dbglev = strtol (arg, NULL, 0);
712 case DEBUG_FILE_OPTION:
713 dbgout = fopen (arg, "w");
715 error (EXIT_FAILURE, errno, _("cannot open %s"), arg);
729 return ARGP_ERR_UNKNOWN;
734 static struct argp argp = {
744 static const char *rmt_authors[] = {
759 main (int argc, char **argv)
765 set_program_name (argv[0]);
766 argp_version_setup ("rmt", rmt_authors);
768 if (isatty (STDOUT_FILENO))
770 setlocale (LC_ALL, "");
771 bindtextdomain (PACKAGE, LOCALEDIR);
772 textdomain (PACKAGE);
775 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &idx, NULL))
780 error (EXIT_FAILURE, 0, _("too many arguments"));
781 dbgout = fopen (argv[idx], "w");
783 error (EXIT_FAILURE, errno, _("cannot open %s"), argv[idx]);
787 while (!stop && (buf = rmt_read ()) != NULL)
797 iocop_device (buf + 1);
801 lseek_device (buf + 1);
805 open_device (buf + 1);
809 read_device (buf + 1);
813 status_device (buf + 1);
817 write_device (buf + 1);
821 DEBUG1 (1, "garbage input %s\n", buf);
822 rmt_error_message (EINVAL, N_("Garbage command"));
823 return EXIT_FAILURE; /* exit status used to be 3 */
828 free (input_buf_ptr);
829 free (record_buffer_ptr);