예제 #1
0
/* Write LENGTH bytes from BUFFER to remote tape connection HANDLE.
   Return the number of bytes written.  */
size_t
rmt_write__ (int handle, char *buffer, size_t length)
{
  char command_buffer[COMMAND_BUFFER_SIZE];
  RETSIGTYPE (*pipe_handler) (int);
  size_t written;

  sprintf (command_buffer, "W%lu\n", (unsigned long) length);
  if (do_command (handle, command_buffer) == -1)
    return 0;

  pipe_handler = signal (SIGPIPE, SIG_IGN);
  written = full_write (WRITE_SIDE (handle), buffer, length);
  signal (SIGPIPE, pipe_handler);
  if (written == length)
    {
      long int r = get_status (handle);
      if (r < 0)
	return 0;
      if (r == length)
	return length;
      written = r;
    }

  /* Write error.  */

  _rmt_shutdown (handle, EIO);
  return written;
}
예제 #2
0
/* Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.
   Return the number of bytes read on success, SAFE_READ_ERROR on error.  */
size_t
rmt_read__ (int handle, char *buffer, size_t length)
{
  char command_buffer[COMMAND_BUFFER_SIZE];
  size_t status;
  size_t rlen;
  size_t counter;

  sprintf (command_buffer, "R%lu\n", (unsigned long) length);
  if (do_command (handle, command_buffer) == -1
      || (status = get_status (handle)) == SAFE_READ_ERROR
      || status > length)
    return SAFE_READ_ERROR;

  for (counter = 0; counter < status; counter += rlen, buffer += rlen)
    {
      rlen = safe_read (READ_SIDE (handle), buffer, status - counter);
      if (rlen == SAFE_READ_ERROR || rlen == 0)
	{
	  _rmt_shutdown (handle, EIO);
	  return SAFE_READ_ERROR;
	}
    }

  return status;
}
예제 #3
0
/* Close remote tape connection HANDLE and shut down.  Return 0 if
   successful, -1 on error.  */
int
rmt_close__ (int handle)
{
  long int status;

  if (do_command (handle, "C\n") == -1)
    return -1;

  status = get_status (handle);
  _rmt_shutdown (handle, errno);
  return status;
}
예제 #4
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
int
__rmt_close (int file_descriptor)
{
  int status;

  if (do_command (file_descriptor, "C\n") == -1)
    return -1;

  status = get_status (file_descriptor);
  _rmt_shutdown (file_descriptor);
  return status;
}
예제 #5
0
/* Attempt to perform the remote tape command specified in BUFFER on
   remote tape connection HANDLE.  Return 0 if successful, -1 on
   error.  */
static int
do_command (int handle, const char *buffer)
{
  /* Save the current pipe handler and try to make the request.  */

  size_t length = strlen (buffer);
  RETSIGTYPE (*pipe_handler) (int) = signal (SIGPIPE, SIG_IGN);
  ssize_t written = full_write (WRITE_SIDE (handle), buffer, length);
  signal (SIGPIPE, pipe_handler);

  if (written == length)
    return 0;

  /* Something went wrong.  Close down and go home.  */

  _rmt_shutdown (handle, EIO);
  return -1;
}
예제 #6
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
static int
do_command (int file_descriptor, const char *buf)
{
  register int buflen;
  RETSIGTYPE (*pipe_handler) ();

  /* Save the current pipe handler and try to make the request.  */

  pipe_handler = signal (SIGPIPE, SIG_IGN);
  buflen = strlen (buf);
  if (write (WRITE (file_descriptor), buf, (size_t) buflen) == buflen)
    {
      signal (SIGPIPE, pipe_handler);
      return 0;
    }

  /* Something went wrong.  Close down and go home.  */

  signal (SIGPIPE, pipe_handler);
  _rmt_shutdown (file_descriptor);
  errno = EIO;
  return -1;
}
예제 #7
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
int
__rmt_write (int file_descriptor, char *buf, unsigned int nbyte)
{
  char command_buffer[CMDBUFSIZE];
  RETSIGTYPE (*pipe_handler) ();

  sprintf (command_buffer, "W%d\n", nbyte);
  if (do_command (file_descriptor, command_buffer) == -1)
    return -1;

  pipe_handler = signal (SIGPIPE, SIG_IGN);
  if (write (WRITE (file_descriptor), buf, nbyte) == nbyte)
    {
      signal (SIGPIPE, pipe_handler);
      return get_status (file_descriptor);
    }

  /* Write error.  */

  signal (SIGPIPE, pipe_handler);
  _rmt_shutdown (file_descriptor);
  errno = EIO;
  return -1;
}
예제 #8
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
int
__rmt_read (int file_descriptor, char *buf, unsigned int nbyte)
{
  int status, i;
  char command_buffer[CMDBUFSIZE];

  sprintf (command_buffer, "R%d\n", nbyte);
  if (do_command (file_descriptor, command_buffer) == -1
      || (status = get_status (file_descriptor)) == -1)
    return -1;

  for (i = 0; i < status; i += nbyte, buf += nbyte)
    {
      nbyte = read (READ (file_descriptor), buf, (size_t) (status - i));
      if (nbyte <= 0)
	{
	  _rmt_shutdown (file_descriptor);
	  errno = EIO;
	  return -1;
	}
    }

  return status;
}
예제 #9
0
/* Perform a raw tape operation on remote tape connection HANDLE.
   Return the results of the ioctl, or -1 on error.  */
int
rmt_ioctl__ (int handle, int operation, char *argument)
{
  switch (operation)
    {
    default:
      errno = EOPNOTSUPP;
      return -1;

#ifdef MTIOCTOP
    case MTIOCTOP:
      {
	char command_buffer[COMMAND_BUFFER_SIZE];
	char operand_buffer[UINTMAX_STRSIZE_BOUND];
	uintmax_t u = (((struct mtop *) argument)->mt_count < 0
		       ? - (uintmax_t) ((struct mtop *) argument)->mt_count
		       : (uintmax_t) ((struct mtop *) argument)->mt_count);
	char *p = operand_buffer + sizeof operand_buffer;

        *--p = 0;
	do
	  *--p = '0' + (int) (u % 10);
	while ((u /= 10) != 0);
	if (((struct mtop *) argument)->mt_count < 0)
	  *--p = '-';

	/* MTIOCTOP is the easy one.  Nothing is transferred in binary.  */

	sprintf (command_buffer, "I%d\n%s\n",
		 ((struct mtop *) argument)->mt_op, p);
	if (do_command (handle, command_buffer) == -1)
	  return -1;

	return get_status (handle);
      }
#endif /* MTIOCTOP */

#ifdef MTIOCGET
    case MTIOCGET:
      {
	ssize_t status;
	size_t counter;

	/* Grab the status and read it directly into the structure.  This
	   assumes that the status buffer is not padded and that 2 shorts
	   fit in a long without any word alignment problems; i.e., the
	   whole struct is contiguous.  NOTE - this is probably NOT a good
	   assumption.  */

	if (do_command (handle, "S") == -1
	    || (status = get_status (handle), status == -1))
	  return -1;

	if (status > sizeof (struct mtop))
	  {
	    errno = EOVERFLOW;
	    return -1;
	  }

	for (; status > 0; status -= counter, argument += counter)
	  {
	    counter = safe_read (READ_SIDE (handle), argument, status);
	    if (counter == SAFE_READ_ERROR || counter == 0)
	      {
		_rmt_shutdown (handle, EIO);
		return -1;
	      }
	  }

	/* Check for byte position.  mt_type (or mt_model) is a small integer
	   field (normally) so we will check its magnitude.  If it is larger
	   than 256, we will assume that the bytes are swapped and go through
	   and reverse all the bytes.  */

	if (((struct mtget *) argument)->MTIO_CHECK_FIELD < 256)
	  return 0;

	for (counter = 0; counter < status; counter += 2)
	  {
	    char copy = argument[counter];

	    argument[counter] = argument[counter + 1];
	    argument[counter + 1] = copy;
	  }

	return 0;
      }
#endif /* MTIOCGET */

    }
}
예제 #10
0
/* Open a file (a magnetic tape device?) on the system specified in
   FILE_NAME, as the given user. FILE_NAME has the form `[USER@]HOST:FILE'.
   OPEN_MODE is O_RDONLY, O_WRONLY, etc.  If successful, return the
   remote pipe number plus BIAS.  REMOTE_SHELL may be overridden.  On
   error, return -1.  */
int
rmt_open__ (const char *file_name, int open_mode, int bias,
            const char *remote_shell)
{
  int remote_pipe_number;	/* pseudo, biased file descriptor */
  char *file_name_copy;		/* copy of file_name string */
  char *remote_host;		/* remote host name */
  char *remote_file;		/* remote file name (often a device) */
  char *remote_user;		/* remote user name */

  /* Find an unused pair of file descriptors.  */

  for (remote_pipe_number = 0;
       remote_pipe_number < MAXUNIT;
       remote_pipe_number++)
    if (READ_SIDE (remote_pipe_number) == -1
	&& WRITE_SIDE (remote_pipe_number) == -1)
      break;

  if (remote_pipe_number == MAXUNIT)
    {
      errno = EMFILE;
      return -1;
    }

  /* Pull apart the system and device, and optional user.  */

  {
    char *cursor;

    file_name_copy = xstrdup (file_name);
    remote_host = file_name_copy;
    remote_user = 0;
    remote_file = 0;

    for (cursor = file_name_copy; *cursor; cursor++)
      switch (*cursor)
	{
	default:
	  break;

	case '\n':
	  /* Do not allow newlines in the file_name, since the protocol
	     uses newline delimiters.  */
	  free (file_name_copy);
	  errno = ENOENT;
	  return -1;

	case '@':
	  if (!remote_user)
	    {
	      remote_user = remote_host;
	      *cursor = '\0';
	      remote_host = cursor + 1;
	    }
	  break;

	case ':':
	  if (!remote_file)
	    {
	      *cursor = '\0';
	      remote_file = cursor + 1;
	    }
	  break;
	}
  }

  /* FIXME: Should somewhat validate the decoding, here.  */
  if (gethostbyname (remote_host) == NULL)
    error (EXIT_ON_EXEC_ERROR, 0, _("Cannot connect to %s: resolve failed"),
	   remote_host);

  if (remote_user && *remote_user == '\0')
    remote_user = 0;

#if WITH_REXEC

  /* Execute the remote command using rexec.  */

  READ_SIDE (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
  if (READ_SIDE (remote_pipe_number) < 0)
    {
      int e = errno;
      free (file_name_copy);
      errno = e;
      return -1;
    }

  WRITE_SIDE (remote_pipe_number) = READ_SIDE (remote_pipe_number);

#else /* not WITH_REXEC */
  {
    const char *remote_shell_basename;
    pid_t status;

    /* Identify the remote command to be executed.  */

    if (!remote_shell)
      {
#ifdef REMOTE_SHELL
	remote_shell = REMOTE_SHELL;
#else
	free (file_name_copy);
	errno = EIO;
	return -1;
#endif
      }
    remote_shell_basename = last_component (remote_shell);

    /* Set up the pipes for the `rsh' command, and fork.  */

    if (pipe (to_remote[remote_pipe_number]) == -1
	|| pipe (from_remote[remote_pipe_number]) == -1)
      {
	int e = errno;
	free (file_name_copy);
	errno = e;
	return -1;
      }

    status = fork ();
    if (status == -1)
      {
	int e = errno;
	free (file_name_copy);
	errno = e;
	return -1;
      }

    if (status == 0)
      {
	/* Child.  */

	if (dup2 (to_remote[remote_pipe_number][PREAD], STDIN_FILENO) < 0
	    || (to_remote[remote_pipe_number][PREAD] != STDIN_FILENO
		&& close (to_remote[remote_pipe_number][PREAD]) != 0)
	    || (to_remote[remote_pipe_number][PWRITE] != STDIN_FILENO
		&& close (to_remote[remote_pipe_number][PWRITE]) != 0)
	    || dup2 (from_remote[remote_pipe_number][PWRITE], STDOUT_FILENO) < 0
	    || close (from_remote[remote_pipe_number][PREAD]) != 0
	    || close (from_remote[remote_pipe_number][PWRITE]) != 0)
	  error (EXIT_ON_EXEC_ERROR, errno,
		 _("Cannot redirect files for remote shell"));

	sys_reset_uid_gid ();

	if (remote_user)
	  execl (remote_shell, remote_shell_basename, remote_host,
		 "-l", remote_user, rmt_command, (char *) 0);
	else
	  execl (remote_shell, remote_shell_basename, remote_host,
		 rmt_command, (char *) 0);

	/* Bad problems if we get here.  */

	/* In a previous version, _exit was used here instead of exit.  */
	error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
      }

    /* Parent.  */

    close (from_remote[remote_pipe_number][PWRITE]);
    close (to_remote[remote_pipe_number][PREAD]);
  }
#endif /* not WITH_REXEC */

  /* Attempt to open the tape device.  */

  {
    size_t remote_file_len = strlen (remote_file);
    char *command_buffer = xmalloc (remote_file_len + 1000);
    sprintf (command_buffer, "O%s\n", remote_file);
    encode_oflag (command_buffer + remote_file_len + 2, open_mode);
    strcat (command_buffer, "\n");
    if (do_command (remote_pipe_number, command_buffer) == -1
	|| get_status (remote_pipe_number) == -1)
      {
	int e = errno;
	free (command_buffer);
	free (file_name_copy);
	_rmt_shutdown (remote_pipe_number, e);
	return -1;
      }
    free (command_buffer);
  }

  free (file_name_copy);
  return remote_pipe_number + bias;
}
예제 #11
0
static char *
get_status_string (int handle, char *command_buffer)
{
  char *cursor;
  int counter;

  /* Read the reply command line.  */

  for (counter = 0, cursor = command_buffer;
       counter < COMMAND_BUFFER_SIZE;
       counter++, cursor++)
    {
      if (safe_read (READ_SIDE (handle), cursor, 1) != 1)
	{
	  _rmt_shutdown (handle, EIO);
	  return 0;
	}
      if (*cursor == '\n')
	{
	  *cursor = '\0';
	  break;
	}
    }

  if (counter == COMMAND_BUFFER_SIZE)
    {
      _rmt_shutdown (handle, EIO);
      return 0;
    }

  /* Check the return status.  */

  for (cursor = command_buffer; *cursor; cursor++)
    if (*cursor != ' ')
      break;

  if (*cursor == 'E' || *cursor == 'F')
    {
      /* Skip the error message line.  */

      /* FIXME: there is better to do than merely ignoring error messages
	 coming from the remote end.  Translate them, too...  */

      {
	char character;

	while (safe_read (READ_SIDE (handle), &character, 1) == 1)
	  if (character == '\n')
	    break;
      }

      errno = atoi (cursor + 1);

      if (*cursor == 'F')
	_rmt_shutdown (handle, errno);

      return 0;
    }

  /* Check for mis-synced pipes.  */

  if (*cursor != 'A')
    {
      _rmt_shutdown (handle, EIO);
      return 0;
    }

  /* Got an `A' (success) response.  */

  return cursor + 1;
}
예제 #12
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
int
__rmt_ioctl (int file_descriptor, int op, char *arg)
{
  char c;
  int status, cnt;
  char command_buffer[CMDBUFSIZE];

  switch (op)
    {
    default:
      errno = EOPNOTSUPP;
      return -1;

#ifdef MTIOCTOP

    case MTIOCTOP:

      /* MTIOCTOP is the easy one.  Nothing is transfered in binary.  */

      sprintf (command_buffer, "I%d\n%d\n", ((struct mtop *) arg)->mt_op,
	       ((struct mtop *) arg)->mt_count);
      if (do_command (file_descriptor, command_buffer) == -1)
	return -1;

      /* Return the count.  */

      return get_status (file_descriptor);

#endif /* MTIOCTOP */

#ifdef MTIOCGET

    case MTIOCGET:

      /* Grab the status and read it directly into the structure.  This
	 assumes that the status buffer is not padded and that 2 shorts
	 fit in a long without any word alignment problems; i.e., the
	 whole struct is contiguous.  NOTE - this is probably NOT a good
	 assumption.  */

      if (do_command (file_descriptor, "S") == -1
	  || (status = get_status (file_descriptor), status == -1))
	return -1;

      for (; status > 0; status -= cnt, arg += cnt)
	{
	  cnt = read (READ (file_descriptor), arg, (size_t) status);
	  if (cnt <= 0)
	    {
	      _rmt_shutdown (file_descriptor);
	      errno = EIO;
	      return -1;
	    }
	}

      /* Check for byte position.  mt_type (or mt_model) is a small integer
	 field (normally) so we will check its magnitude.  If it is larger
	 than 256, we will assume that the bytes are swapped and go through
	 and reverse all the bytes.  */

      if (((struct mtget *) arg)->MTIO_CHECK_FIELD < 256)
	return 0;

      for (cnt = 0; cnt < status; cnt += 2)
	{
	  c = arg[cnt];
	  arg[cnt] = arg[cnt + 1];
	  arg[cnt + 1] = c;
	}

      return 0;

#endif /* MTIOCGET */

    }
}
예제 #13
0
파일: rtapelib.c 프로젝트: KennethWilke/zrt
static int
get_status (int file_descriptor)
{
  int i;
  char c, *cp;
  char command_buffer[CMDBUFSIZE];

  /* Read the reply command line.  */

  for (i = 0, cp = command_buffer; i < CMDBUFSIZE; i++, cp++)
    {
      if (read (READ (file_descriptor), cp, 1) != 1)
	{
	  _rmt_shutdown (file_descriptor);
	  errno = EIO;
	  return -1;
	}
      if (*cp == '\n')
	{
	  *cp = '\0';
	  break;
	}
    }

  if (i == CMDBUFSIZE)
    {
      _rmt_shutdown (file_descriptor);
      errno = EIO;
      return -1;
    }

  /* Check the return status.  */

  for (cp = command_buffer; *cp; cp++)
    if (*cp != ' ')
      break;

  if (*cp == 'E' || *cp == 'F')
    {
      errno = atoi (cp + 1);

      /* Skip the error message line.  */

      while (read (READ (file_descriptor), &c, 1) == 1)
	if (c == '\n')
	  break;

      if (*cp == 'F')
	_rmt_shutdown (file_descriptor);

      return -1;
    }

  /* Check for mis-synced pipes.  */

  if (*cp != 'A')
    {
      _rmt_shutdown (file_descriptor);
      errno = EIO;
      return -1;
    }

  /* Got an `A' (success) response.  */

  return atoi (cp + 1);
}