1 /* Remote File-I/O communications
3 Copyright 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
28 #include "gdb/fileio.h"
35 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
44 #define FIO_FD_INVALID -1
45 #define FIO_FD_CONSOLE_IN -2
46 #define FIO_FD_CONSOLE_OUT -3
48 static int remote_fio_system_call_allowed = 0;
51 remote_fileio_init_fd_map (void)
55 if (!remote_fio_data.fd_map)
57 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
58 remote_fio_data.fd_map_size = 10;
59 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
60 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
61 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
62 for (i = 3; i < 10; ++i)
63 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
69 remote_fileio_resize_fd_map (void)
71 if (!remote_fio_data.fd_map)
72 return remote_fileio_init_fd_map ();
73 remote_fio_data.fd_map_size += 10;
74 remote_fio_data.fd_map =
75 (int *) xrealloc (remote_fio_data.fd_map,
76 remote_fio_data.fd_map_size * sizeof (int));
77 return remote_fio_data.fd_map_size - 10;
81 remote_fileio_next_free_fd (void)
85 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
86 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
88 return remote_fileio_resize_fd_map ();
92 remote_fileio_fd_to_targetfd (int fd)
94 int target_fd = remote_fileio_next_free_fd ();
95 remote_fio_data.fd_map[target_fd] = fd;
100 remote_fileio_map_fd (int target_fd)
102 remote_fileio_init_fd_map ();
103 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
104 return FIO_FD_INVALID;
105 return remote_fio_data.fd_map[target_fd];
109 remote_fileio_close_target_fd (int target_fd)
111 remote_fileio_init_fd_map ();
112 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
113 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
117 remote_fileio_oflags_to_host (long flags)
121 if (flags & FILEIO_O_CREAT)
123 if (flags & FILEIO_O_EXCL)
125 if (flags & FILEIO_O_TRUNC)
127 if (flags & FILEIO_O_APPEND)
129 if (flags & FILEIO_O_RDONLY)
131 if (flags & FILEIO_O_WRONLY)
133 if (flags & FILEIO_O_RDWR)
135 /* On systems supporting binary and text mode, always open files in
144 remote_fileio_mode_to_host (long mode, int open_call)
150 if (mode & FILEIO_S_IFREG)
152 if (mode & FILEIO_S_IFDIR)
154 if (mode & FILEIO_S_IFCHR)
157 if (mode & FILEIO_S_IRUSR)
159 if (mode & FILEIO_S_IWUSR)
161 if (mode & FILEIO_S_IXUSR)
163 if (mode & FILEIO_S_IRGRP)
165 if (mode & FILEIO_S_IWGRP)
167 if (mode & FILEIO_S_IXGRP)
169 if (mode & FILEIO_S_IROTH)
171 if (mode & FILEIO_S_IWOTH)
173 if (mode & FILEIO_S_IXOTH)
179 remote_fileio_mode_to_target (mode_t mode)
184 tmode |= FILEIO_S_IFREG;
186 tmode |= FILEIO_S_IFDIR;
188 tmode |= FILEIO_S_IFCHR;
190 tmode |= FILEIO_S_IRUSR;
192 tmode |= FILEIO_S_IWUSR;
194 tmode |= FILEIO_S_IXUSR;
196 tmode |= FILEIO_S_IRGRP;
198 tmode |= FILEIO_S_IWGRP;
200 tmode |= FILEIO_S_IXGRP;
202 tmode |= FILEIO_S_IROTH;
204 tmode |= FILEIO_S_IWOTH;
206 tmode |= FILEIO_S_IXOTH;
211 remote_fileio_errno_to_target (int error)
218 return FILEIO_ENOENT;
226 return FILEIO_EACCES;
228 return FILEIO_EFAULT;
232 return FILEIO_EEXIST;
234 return FILEIO_ENODEV;
236 return FILEIO_ENOTDIR;
238 return FILEIO_EISDIR;
240 return FILEIO_EINVAL;
242 return FILEIO_ENFILE;
244 return FILEIO_EMFILE;
248 return FILEIO_ENOSPC;
250 return FILEIO_ESPIPE;
254 return FILEIO_ENOSYS;
256 return FILEIO_ENAMETOOLONG;
258 return FILEIO_EUNKNOWN;
262 remote_fileio_seek_flag_to_host (long num, int *flag)
268 case FILEIO_SEEK_SET:
271 case FILEIO_SEEK_CUR:
274 case FILEIO_SEEK_END:
284 remote_fileio_extract_long (char **buf, LONGEST *retlong)
289 if (!buf || !*buf || !**buf || !retlong)
291 c = strchr (*buf, ',');
295 c = strchr (*buf, '\0');
296 while (strchr ("+-", **buf))
302 for (*retlong = 0; **buf; ++*buf)
305 if (**buf >= '0' && **buf <= '9')
306 *retlong += **buf - '0';
307 else if (**buf >= 'a' && **buf <= 'f')
308 *retlong += **buf - 'a' + 10;
309 else if (**buf >= 'A' && **buf <= 'F')
310 *retlong += **buf - 'A' + 10;
320 remote_fileio_extract_int (char **buf, long *retint)
327 ret = remote_fileio_extract_long (buf, &retlong);
329 *retint = (long) retlong;
334 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
339 if (!buf || !*buf || !**buf || !ptrval || !length)
341 c = strchr (*buf, '/');
345 if (remote_fileio_extract_long (buf, &retlong))
347 *ptrval = (CORE_ADDR) retlong;
349 if (remote_fileio_extract_long (buf, &retlong))
351 *length = (int) retlong;
355 /* Convert to big endian */
357 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
361 for (i = 0; i < bytes; ++i)
362 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
366 remote_fileio_to_fio_int (long num, fio_int_t fnum)
368 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
372 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
374 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
378 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
380 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
384 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
386 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
390 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
392 remote_fileio_to_be (num, (char *) fnum, 8);
396 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
398 remote_fileio_to_be (num, (char *) fnum, 8);
402 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
404 /* `st_dev' is set in the calling function */
405 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
406 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
407 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
408 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
409 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
410 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
411 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
412 remote_fileio_to_fio_ulong ((LONGEST) st->st_blksize, fst->fst_blksize);
413 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
414 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
415 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
416 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
420 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
422 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
423 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
426 static int remote_fio_ctrl_c_flag = 0;
427 static int remote_fio_no_longjmp = 0;
429 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
430 static struct sigaction remote_fio_sa;
431 static struct sigaction remote_fio_osa;
433 static void (*remote_fio_ofunc)(int);
437 remote_fileio_sig_init (void)
439 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
440 remote_fio_sa.sa_handler = SIG_IGN;
441 sigemptyset (&remote_fio_sa.sa_mask);
442 remote_fio_sa.sa_flags = 0;
443 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
445 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
450 remote_fileio_sig_set (void (*sigint_func)(int))
452 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
453 remote_fio_sa.sa_handler = sigint_func;
454 sigemptyset (&remote_fio_sa.sa_mask);
455 remote_fio_sa.sa_flags = 0;
456 sigaction (SIGINT, &remote_fio_sa, NULL);
458 signal (SIGINT, sigint_func);
463 remote_fileio_sig_exit (void)
465 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
466 sigaction (SIGINT, &remote_fio_osa, NULL);
468 signal (SIGINT, remote_fio_ofunc);
473 remote_fileio_ctrl_c_signal_handler (int signo)
475 remote_fileio_sig_set (SIG_IGN);
476 remote_fio_ctrl_c_flag = 1;
477 if (!remote_fio_no_longjmp)
478 throw_exception (RETURN_QUIT);
479 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
483 remote_fileio_reply (int retcode, int error)
487 remote_fileio_sig_set (SIG_IGN);
494 sprintf (buf + strlen (buf), "%x", retcode);
495 if (error || remote_fio_ctrl_c_flag)
497 if (error && remote_fio_ctrl_c_flag)
498 error = FILEIO_EINTR;
504 sprintf (buf + strlen (buf), ",%x", error);
505 if (remote_fio_ctrl_c_flag)
508 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
513 remote_fileio_ioerror (void)
515 remote_fileio_reply (-1, FILEIO_EIO);
519 remote_fileio_badfd (void)
521 remote_fileio_reply (-1, FILEIO_EBADF);
525 remote_fileio_return_errno (int retcode)
527 remote_fileio_reply (retcode,
528 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
532 remote_fileio_return_success (int retcode)
534 remote_fileio_reply (retcode, 0);
537 /* Wrapper function for remote_write_bytes() which has the disadvantage to
538 write only one packet, regardless of the requested number of bytes to
539 transfer. This wrapper calls remote_write_bytes() as often as needed. */
541 remote_fileio_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
543 int ret = 0, written;
545 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
556 remote_fileio_func_open (char *buf)
559 int length, retlength;
566 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
567 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
569 remote_fileio_ioerror ();
572 /* 2. Parameter: open flags */
573 if (remote_fileio_extract_int (&buf, &num))
575 remote_fileio_ioerror ();
578 flags = remote_fileio_oflags_to_host (num);
579 /* 3. Parameter: open mode */
580 if (remote_fileio_extract_int (&buf, &num))
582 remote_fileio_ioerror ();
585 mode = remote_fileio_mode_to_host (num, 1);
587 /* Request pathname using 'm' packet */
588 pathname = alloca (length);
589 retlength = remote_read_bytes (ptrval, pathname, length);
590 if (retlength != length)
592 remote_fileio_ioerror ();
596 /* Check if pathname exists and is not a regular file or directory. If so,
597 return an appropriate error code. Same for trying to open directories
599 if (!stat (pathname, &st))
601 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
603 remote_fileio_reply (-1, FILEIO_ENODEV);
606 if (S_ISDIR (st.st_mode)
607 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
609 remote_fileio_reply (-1, FILEIO_EISDIR);
614 remote_fio_no_longjmp = 1;
615 fd = open (pathname, flags, mode);
618 remote_fileio_return_errno (-1);
622 fd = remote_fileio_fd_to_targetfd (fd);
623 remote_fileio_return_success (fd);
627 remote_fileio_func_close (char *buf)
632 /* Parameter: file descriptor */
633 if (remote_fileio_extract_int (&buf, &num))
635 remote_fileio_ioerror ();
638 fd = remote_fileio_map_fd ((int) num);
639 if (fd == FIO_FD_INVALID)
641 remote_fileio_badfd ();
645 remote_fio_no_longjmp = 1;
646 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
647 remote_fileio_return_errno (-1);
648 remote_fileio_close_target_fd ((int) num);
649 remote_fileio_return_success (0);
653 remote_fileio_func_read (char *buf)
658 int fd, ret, retlength;
661 off_t old_offset, new_offset;
663 /* 1. Parameter: file descriptor */
664 if (remote_fileio_extract_int (&buf, &target_fd))
666 remote_fileio_ioerror ();
669 fd = remote_fileio_map_fd ((int) target_fd);
670 if (fd == FIO_FD_INVALID)
672 remote_fileio_badfd ();
675 /* 2. Parameter: buffer pointer */
676 if (remote_fileio_extract_long (&buf, &lnum))
678 remote_fileio_ioerror ();
681 ptrval = (CORE_ADDR) lnum;
682 /* 3. Parameter: buffer length */
683 if (remote_fileio_extract_int (&buf, &num))
685 remote_fileio_ioerror ();
688 length = (size_t) num;
692 case FIO_FD_CONSOLE_OUT:
693 remote_fileio_badfd ();
695 case FIO_FD_CONSOLE_IN:
697 static char *remaining_buf = NULL;
698 static int remaining_length = 0;
700 buffer = (char *) xmalloc (32768);
703 remote_fio_no_longjmp = 1;
704 if (remaining_length > length)
706 memcpy (buffer, remaining_buf, length);
707 memmove (remaining_buf, remaining_buf + length,
708 remaining_length - length);
709 remaining_length -= length;
714 memcpy (buffer, remaining_buf, remaining_length);
715 xfree (remaining_buf);
716 remaining_buf = NULL;
717 ret = remaining_length;
722 ret = ui_file_read (gdb_stdtargin, buffer, 32767);
723 remote_fio_no_longjmp = 1;
724 if (ret > 0 && (size_t)ret > length)
726 remaining_buf = (char *) xmalloc (ret - length);
727 remaining_length = ret - length;
728 memcpy (remaining_buf, buffer + length, remaining_length);
735 buffer = (char *) xmalloc (length);
736 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
737 for read() to return -1 even if "some" bytes have been read. It
738 has been corrected in SUSv2 but that doesn't help us much...
739 Therefore a complete solution must check how many bytes have been
740 read on EINTR to return a more reliable value to the target */
741 old_offset = lseek (fd, 0, SEEK_CUR);
742 remote_fio_no_longjmp = 1;
743 ret = read (fd, buffer, length);
744 if (ret < 0 && errno == EINTR)
746 new_offset = lseek (fd, 0, SEEK_CUR);
747 /* If some data has been read, return the number of bytes read.
748 The Ctrl-C flag is set in remote_fileio_reply() anyway */
749 if (old_offset != new_offset)
750 ret = new_offset - old_offset;
757 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
758 if (retlength != ret)
759 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
763 remote_fileio_return_errno (-1);
765 remote_fileio_return_success (ret);
771 remote_fileio_func_write (char *buf)
776 int fd, ret, retlength;
780 /* 1. Parameter: file descriptor */
781 if (remote_fileio_extract_int (&buf, &target_fd))
783 remote_fileio_ioerror ();
786 fd = remote_fileio_map_fd ((int) target_fd);
787 if (fd == FIO_FD_INVALID)
789 remote_fileio_badfd ();
792 /* 2. Parameter: buffer pointer */
793 if (remote_fileio_extract_long (&buf, &lnum))
795 remote_fileio_ioerror ();
798 ptrval = (CORE_ADDR) lnum;
799 /* 3. Parameter: buffer length */
800 if (remote_fileio_extract_int (&buf, &num))
802 remote_fileio_ioerror ();
805 length = (size_t) num;
807 buffer = (char *) xmalloc (length);
808 retlength = remote_read_bytes (ptrval, buffer, length);
809 if (retlength != length)
812 remote_fileio_ioerror ();
816 remote_fio_no_longjmp = 1;
819 case FIO_FD_CONSOLE_IN:
820 remote_fileio_badfd ();
822 case FIO_FD_CONSOLE_OUT:
823 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr, buffer,
825 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
829 ret = write (fd, buffer, length);
830 if (ret < 0 && errno == EACCES)
831 errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
836 remote_fileio_return_errno (-1);
838 remote_fileio_return_success (ret);
844 remote_fileio_func_lseek (char *buf)
851 /* 1. Parameter: file descriptor */
852 if (remote_fileio_extract_int (&buf, &num))
854 remote_fileio_ioerror ();
857 fd = remote_fileio_map_fd ((int) num);
858 if (fd == FIO_FD_INVALID)
860 remote_fileio_badfd ();
863 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
865 remote_fileio_reply (-1, FILEIO_ESPIPE);
869 /* 2. Parameter: offset */
870 if (remote_fileio_extract_long (&buf, &lnum))
872 remote_fileio_ioerror ();
875 offset = (off_t) lnum;
876 /* 3. Parameter: flag */
877 if (remote_fileio_extract_int (&buf, &num))
879 remote_fileio_ioerror ();
882 if (remote_fileio_seek_flag_to_host (num, &flag))
884 remote_fileio_reply (-1, FILEIO_EINVAL);
888 remote_fio_no_longjmp = 1;
889 ret = lseek (fd, offset, flag);
891 if (ret == (off_t) -1)
892 remote_fileio_return_errno (-1);
894 remote_fileio_return_success (ret);
898 remote_fileio_func_rename (char *buf)
901 int length, retlength;
902 char *oldpath, *newpath;
904 struct stat ost, nst;
906 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
907 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
909 remote_fileio_ioerror ();
912 /* Request oldpath using 'm' packet */
913 oldpath = alloca (length);
914 retlength = remote_read_bytes (ptrval, oldpath, length);
915 if (retlength != length)
917 remote_fileio_ioerror ();
920 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
921 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
923 remote_fileio_ioerror ();
926 /* Request newpath using 'm' packet */
927 newpath = alloca (length);
928 retlength = remote_read_bytes (ptrval, newpath, length);
929 if (retlength != length)
931 remote_fileio_ioerror ();
935 /* Only operate on regular files and directories */
936 of = stat (oldpath, &ost);
937 nf = stat (newpath, &nst);
938 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
939 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
941 remote_fileio_reply (-1, FILEIO_EACCES);
945 remote_fio_no_longjmp = 1;
946 ret = rename (oldpath, newpath);
950 /* Special case: newpath is a non-empty directory. Some systems
951 return ENOTEMPTY, some return EEXIST. We coerce that to be
953 if (errno == ENOTEMPTY)
956 /* Workaround some Cygwin problems with correct errnos. */
959 if (!of && !nf && S_ISDIR (nst.st_mode))
961 if (S_ISREG (ost.st_mode))
965 char oldfullpath[PATH_MAX + 1];
966 char newfullpath[PATH_MAX + 1];
969 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
970 cygwin_conv_to_full_posix_path (newpath, newfullpath);
971 len = strlen (oldfullpath);
972 if (newfullpath[len] == '/'
973 && !strncmp (oldfullpath, newfullpath, len))
982 remote_fileio_return_errno (-1);
985 remote_fileio_return_success (ret);
989 remote_fileio_func_unlink (char *buf)
992 int length, retlength;
997 /* Parameter: Ptr to pathname / length incl. trailing zero */
998 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1000 remote_fileio_ioerror ();
1003 /* Request pathname using 'm' packet */
1004 pathname = alloca (length);
1005 retlength = remote_read_bytes (ptrval, pathname, length);
1006 if (retlength != length)
1008 remote_fileio_ioerror ();
1012 /* Only operate on regular files (and directories, which allows to return
1013 the correct return code) */
1014 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1016 remote_fileio_reply (-1, FILEIO_ENODEV);
1020 remote_fio_no_longjmp = 1;
1021 ret = unlink (pathname);
1024 remote_fileio_return_errno (-1);
1026 remote_fileio_return_success (ret);
1030 remote_fileio_func_stat (char *buf)
1033 int ret, length, retlength;
1037 struct fio_stat fst;
1039 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1040 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1042 remote_fileio_ioerror ();
1045 /* Request pathname using 'm' packet */
1046 pathname = alloca (length);
1047 retlength = remote_read_bytes (ptrval, pathname, length);
1048 if (retlength != length)
1050 remote_fileio_ioerror ();
1054 /* 2. Parameter: Ptr to struct stat */
1055 if (remote_fileio_extract_long (&buf, &lnum))
1057 remote_fileio_ioerror ();
1060 ptrval = (CORE_ADDR) lnum;
1062 remote_fio_no_longjmp = 1;
1063 ret = stat (pathname, &st);
1067 remote_fileio_return_errno (-1);
1070 /* Only operate on regular files and directories */
1071 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1073 remote_fileio_reply (-1, FILEIO_EACCES);
1078 remote_fileio_to_fio_stat (&st, &fst);
1079 remote_fileio_to_fio_uint (0, fst.fst_dev);
1081 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1082 if (retlength != sizeof fst)
1084 remote_fileio_return_errno (-1);
1088 remote_fileio_return_success (ret);
1092 remote_fileio_func_fstat (char *buf)
1095 int fd, ret, retlength;
1099 struct fio_stat fst;
1102 /* 1. Parameter: file descriptor */
1103 if (remote_fileio_extract_int (&buf, &target_fd))
1105 remote_fileio_ioerror ();
1108 fd = remote_fileio_map_fd ((int) target_fd);
1109 if (fd == FIO_FD_INVALID)
1111 remote_fileio_badfd ();
1114 /* 2. Parameter: Ptr to struct stat */
1115 if (remote_fileio_extract_long (&buf, &lnum))
1117 remote_fileio_ioerror ();
1120 ptrval = (CORE_ADDR) lnum;
1122 remote_fio_no_longjmp = 1;
1123 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1125 remote_fileio_to_fio_uint (1, fst.fst_dev);
1126 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1128 st.st_uid = getuid ();
1129 st.st_gid = getgid ();
1132 st.st_blksize = 512;
1134 if (!gettimeofday (&tv, NULL))
1135 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1137 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1141 ret = fstat (fd, &st);
1145 remote_fileio_return_errno (-1);
1150 remote_fileio_to_fio_stat (&st, &fst);
1152 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1153 if (retlength != sizeof fst)
1155 remote_fileio_return_errno (-1);
1159 remote_fileio_return_success (ret);
1163 remote_fileio_func_gettimeofday (char *buf)
1169 struct fio_timeval ftv;
1171 /* 1. Parameter: struct timeval pointer */
1172 if (remote_fileio_extract_long (&buf, &lnum))
1174 remote_fileio_ioerror ();
1177 ptrval = (CORE_ADDR) lnum;
1178 /* 2. Parameter: some pointer value... */
1179 if (remote_fileio_extract_long (&buf, &lnum))
1181 remote_fileio_ioerror ();
1184 /* ...which has to be NULL */
1187 remote_fileio_reply (-1, FILEIO_EINVAL);
1191 remote_fio_no_longjmp = 1;
1192 ret = gettimeofday (&tv, NULL);
1196 remote_fileio_return_errno (-1);
1202 remote_fileio_to_fio_timeval (&tv, &ftv);
1204 retlength = remote_fileio_write_bytes (ptrval, (char *) &ftv, sizeof ftv);
1205 if (retlength != sizeof ftv)
1207 remote_fileio_return_errno (-1);
1211 remote_fileio_return_success (ret);
1215 remote_fileio_func_isatty (char *buf)
1220 /* Parameter: file descriptor */
1221 if (remote_fileio_extract_int (&buf, &target_fd))
1223 remote_fileio_ioerror ();
1226 remote_fio_no_longjmp = 1;
1227 fd = remote_fileio_map_fd ((int) target_fd);
1228 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1229 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1233 remote_fileio_func_system (char *buf)
1236 int ret, length, retlength;
1239 /* Check if system(3) has been explicitely allowed using the
1240 `set remote system-call-allowed 1' command. If not, return
1242 if (!remote_fio_system_call_allowed)
1244 remote_fileio_reply (-1, FILEIO_EPERM);
1248 /* Parameter: Ptr to commandline / length incl. trailing zero */
1249 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1251 remote_fileio_ioerror ();
1254 /* Request commandline using 'm' packet */
1255 cmdline = alloca (length);
1256 retlength = remote_read_bytes (ptrval, cmdline, length);
1257 if (retlength != length)
1259 remote_fileio_ioerror ();
1263 remote_fio_no_longjmp = 1;
1264 ret = system (cmdline);
1267 remote_fileio_return_errno (-1);
1269 remote_fileio_return_success (WEXITSTATUS (ret));
1274 void (*func)(char *);
1275 } remote_fio_func_map[] = {
1276 "open", remote_fileio_func_open,
1277 "close", remote_fileio_func_close,
1278 "read", remote_fileio_func_read,
1279 "write", remote_fileio_func_write,
1280 "lseek", remote_fileio_func_lseek,
1281 "rename", remote_fileio_func_rename,
1282 "unlink", remote_fileio_func_unlink,
1283 "stat", remote_fileio_func_stat,
1284 "fstat", remote_fileio_func_fstat,
1285 "gettimeofday", remote_fileio_func_gettimeofday,
1286 "isatty", remote_fileio_func_isatty,
1287 "system", remote_fileio_func_system,
1292 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1294 char *buf = buf_arg;
1298 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1300 c = strchr (++buf, ',');
1304 c = strchr (buf, '\0');
1305 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1306 if (!strcmp (remote_fio_func_map[idx].name, buf))
1308 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1309 return RETURN_ERROR;
1310 remote_fio_func_map[idx].func (c);
1315 remote_fileio_request (char *buf)
1319 remote_fileio_sig_init ();
1321 remote_fio_ctrl_c_flag = 0;
1322 remote_fio_no_longjmp = 0;
1324 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1325 NULL, RETURN_MASK_ALL);
1329 remote_fileio_reply (-1, FILEIO_ENOSYS);
1332 remote_fileio_reply (-1, FILEIO_EINTR);
1338 remote_fileio_sig_exit ();
1342 set_system_call_allowed (char *args, int from_tty)
1347 int val = strtoul (args, &arg_end, 10);
1348 if (*args && *arg_end == '\0')
1350 remote_fio_system_call_allowed = !!val;
1354 error ("Illegal argument for \"set remote system-call-allowed\" command");
1358 show_system_call_allowed (char *args, int from_tty)
1361 error ("Garbage after \"show remote system-call-allowed\" command: `%s'", args);
1362 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1363 remote_fio_system_call_allowed ? "" : "not ");
1367 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1368 struct cmd_list_element *remote_show_cmdlist)
1370 add_cmd ("system-call-allowed", no_class,
1371 set_system_call_allowed,
1372 "Set if the host system(3) call is allowed for the target.\n",
1373 &remote_set_cmdlist);
1374 add_cmd ("system-call-allowed", no_class,
1375 show_system_call_allowed,
1376 "Show if the host system(3) call is allowed for the target.\n",
1377 &remote_show_cmdlist);