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"
31 #include "exceptions.h"
32 #include "remote-fileio.h"
37 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
46 #define FIO_FD_INVALID -1
47 #define FIO_FD_CONSOLE_IN -2
48 #define FIO_FD_CONSOLE_OUT -3
50 static int remote_fio_system_call_allowed = 0;
53 remote_fileio_init_fd_map (void)
57 if (!remote_fio_data.fd_map)
59 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
60 remote_fio_data.fd_map_size = 10;
61 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
62 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
63 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
64 for (i = 3; i < 10; ++i)
65 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
71 remote_fileio_resize_fd_map (void)
73 if (!remote_fio_data.fd_map)
74 return remote_fileio_init_fd_map ();
75 remote_fio_data.fd_map_size += 10;
76 remote_fio_data.fd_map =
77 (int *) xrealloc (remote_fio_data.fd_map,
78 remote_fio_data.fd_map_size * sizeof (int));
79 return remote_fio_data.fd_map_size - 10;
83 remote_fileio_next_free_fd (void)
87 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
88 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
90 return remote_fileio_resize_fd_map ();
94 remote_fileio_fd_to_targetfd (int fd)
96 int target_fd = remote_fileio_next_free_fd ();
97 remote_fio_data.fd_map[target_fd] = fd;
102 remote_fileio_map_fd (int target_fd)
104 remote_fileio_init_fd_map ();
105 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
106 return FIO_FD_INVALID;
107 return remote_fio_data.fd_map[target_fd];
111 remote_fileio_close_target_fd (int target_fd)
113 remote_fileio_init_fd_map ();
114 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
115 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
119 remote_fileio_oflags_to_host (long flags)
123 if (flags & FILEIO_O_CREAT)
125 if (flags & FILEIO_O_EXCL)
127 if (flags & FILEIO_O_TRUNC)
129 if (flags & FILEIO_O_APPEND)
131 if (flags & FILEIO_O_RDONLY)
133 if (flags & FILEIO_O_WRONLY)
135 if (flags & FILEIO_O_RDWR)
137 /* On systems supporting binary and text mode, always open files in
146 remote_fileio_mode_to_host (long mode, int open_call)
152 if (mode & FILEIO_S_IFREG)
154 if (mode & FILEIO_S_IFDIR)
156 if (mode & FILEIO_S_IFCHR)
159 if (mode & FILEIO_S_IRUSR)
161 if (mode & FILEIO_S_IWUSR)
163 if (mode & FILEIO_S_IXUSR)
165 if (mode & FILEIO_S_IRGRP)
167 if (mode & FILEIO_S_IWGRP)
169 if (mode & FILEIO_S_IXGRP)
171 if (mode & FILEIO_S_IROTH)
173 if (mode & FILEIO_S_IWOTH)
175 if (mode & FILEIO_S_IXOTH)
181 remote_fileio_mode_to_target (mode_t mode)
186 tmode |= FILEIO_S_IFREG;
188 tmode |= FILEIO_S_IFDIR;
190 tmode |= FILEIO_S_IFCHR;
192 tmode |= FILEIO_S_IRUSR;
194 tmode |= FILEIO_S_IWUSR;
196 tmode |= FILEIO_S_IXUSR;
198 tmode |= FILEIO_S_IRGRP;
200 tmode |= FILEIO_S_IWGRP;
202 tmode |= FILEIO_S_IXGRP;
204 tmode |= FILEIO_S_IROTH;
206 tmode |= FILEIO_S_IWOTH;
208 tmode |= FILEIO_S_IXOTH;
213 remote_fileio_errno_to_target (int error)
220 return FILEIO_ENOENT;
228 return FILEIO_EACCES;
230 return FILEIO_EFAULT;
234 return FILEIO_EEXIST;
236 return FILEIO_ENODEV;
238 return FILEIO_ENOTDIR;
240 return FILEIO_EISDIR;
242 return FILEIO_EINVAL;
244 return FILEIO_ENFILE;
246 return FILEIO_EMFILE;
250 return FILEIO_ENOSPC;
252 return FILEIO_ESPIPE;
256 return FILEIO_ENOSYS;
258 return FILEIO_ENAMETOOLONG;
260 return FILEIO_EUNKNOWN;
264 remote_fileio_seek_flag_to_host (long num, int *flag)
270 case FILEIO_SEEK_SET:
273 case FILEIO_SEEK_CUR:
276 case FILEIO_SEEK_END:
286 remote_fileio_extract_long (char **buf, LONGEST *retlong)
291 if (!buf || !*buf || !**buf || !retlong)
293 c = strchr (*buf, ',');
297 c = strchr (*buf, '\0');
298 while (strchr ("+-", **buf))
304 for (*retlong = 0; **buf; ++*buf)
307 if (**buf >= '0' && **buf <= '9')
308 *retlong += **buf - '0';
309 else if (**buf >= 'a' && **buf <= 'f')
310 *retlong += **buf - 'a' + 10;
311 else if (**buf >= 'A' && **buf <= 'F')
312 *retlong += **buf - 'A' + 10;
322 remote_fileio_extract_int (char **buf, long *retint)
329 ret = remote_fileio_extract_long (buf, &retlong);
331 *retint = (long) retlong;
336 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
341 if (!buf || !*buf || !**buf || !ptrval || !length)
343 c = strchr (*buf, '/');
347 if (remote_fileio_extract_long (buf, &retlong))
349 *ptrval = (CORE_ADDR) retlong;
351 if (remote_fileio_extract_long (buf, &retlong))
353 *length = (int) retlong;
357 /* Convert to big endian */
359 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
363 for (i = 0; i < bytes; ++i)
364 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
368 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
370 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
374 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
376 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
380 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
382 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
386 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
388 remote_fileio_to_be (num, (char *) fnum, 8);
392 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
394 remote_fileio_to_be (num, (char *) fnum, 8);
398 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
400 /* `st_dev' is set in the calling function */
401 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
402 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
403 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
404 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
405 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
406 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
407 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
408 remote_fileio_to_fio_ulong ((LONGEST) st->st_blksize, fst->fst_blksize);
409 #if HAVE_STRUCT_STAT_ST_BLOCKS
410 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
412 /* FIXME: This is correct for DJGPP, but other systems that don't
413 have st_blocks, if any, might prefer 512 instead of st_blksize.
414 (eliz, 30-12-2003) */
415 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + st->st_blksize - 1)
416 / (LONGEST) st->st_blksize,
419 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
420 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
421 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
425 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
427 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
428 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
431 static int remote_fio_ctrl_c_flag = 0;
432 static int remote_fio_no_longjmp = 0;
434 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
435 static struct sigaction remote_fio_sa;
436 static struct sigaction remote_fio_osa;
438 static void (*remote_fio_ofunc)(int);
442 remote_fileio_sig_init (void)
444 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
445 remote_fio_sa.sa_handler = SIG_IGN;
446 sigemptyset (&remote_fio_sa.sa_mask);
447 remote_fio_sa.sa_flags = 0;
448 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
450 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
455 remote_fileio_sig_set (void (*sigint_func)(int))
457 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
458 remote_fio_sa.sa_handler = sigint_func;
459 sigemptyset (&remote_fio_sa.sa_mask);
460 remote_fio_sa.sa_flags = 0;
461 sigaction (SIGINT, &remote_fio_sa, NULL);
463 signal (SIGINT, sigint_func);
468 remote_fileio_sig_exit (void)
470 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
471 sigaction (SIGINT, &remote_fio_osa, NULL);
473 signal (SIGINT, remote_fio_ofunc);
478 remote_fileio_ctrl_c_signal_handler (int signo)
480 remote_fileio_sig_set (SIG_IGN);
481 remote_fio_ctrl_c_flag = 1;
482 if (!remote_fio_no_longjmp)
483 deprecated_throw_reason (RETURN_QUIT);
484 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
488 remote_fileio_reply (int retcode, int error)
492 remote_fileio_sig_set (SIG_IGN);
499 sprintf (buf + strlen (buf), "%x", retcode);
500 if (error || remote_fio_ctrl_c_flag)
502 if (error && remote_fio_ctrl_c_flag)
503 error = FILEIO_EINTR;
509 sprintf (buf + strlen (buf), ",%x", error);
510 if (remote_fio_ctrl_c_flag)
513 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
518 remote_fileio_ioerror (void)
520 remote_fileio_reply (-1, FILEIO_EIO);
524 remote_fileio_badfd (void)
526 remote_fileio_reply (-1, FILEIO_EBADF);
530 remote_fileio_return_errno (int retcode)
532 remote_fileio_reply (retcode,
533 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
537 remote_fileio_return_success (int retcode)
539 remote_fileio_reply (retcode, 0);
542 /* Wrapper function for remote_write_bytes() which has the disadvantage to
543 write only one packet, regardless of the requested number of bytes to
544 transfer. This wrapper calls remote_write_bytes() as often as needed. */
546 remote_fileio_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
548 int ret = 0, written;
550 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
561 remote_fileio_func_open (char *buf)
564 int length, retlength;
571 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
572 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
574 remote_fileio_ioerror ();
577 /* 2. Parameter: open flags */
578 if (remote_fileio_extract_int (&buf, &num))
580 remote_fileio_ioerror ();
583 flags = remote_fileio_oflags_to_host (num);
584 /* 3. Parameter: open mode */
585 if (remote_fileio_extract_int (&buf, &num))
587 remote_fileio_ioerror ();
590 mode = remote_fileio_mode_to_host (num, 1);
592 /* Request pathname using 'm' packet */
593 pathname = alloca (length);
594 retlength = remote_read_bytes (ptrval, pathname, length);
595 if (retlength != length)
597 remote_fileio_ioerror ();
601 /* Check if pathname exists and is not a regular file or directory. If so,
602 return an appropriate error code. Same for trying to open directories
604 if (!stat (pathname, &st))
606 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
608 remote_fileio_reply (-1, FILEIO_ENODEV);
611 if (S_ISDIR (st.st_mode)
612 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
614 remote_fileio_reply (-1, FILEIO_EISDIR);
619 remote_fio_no_longjmp = 1;
620 fd = open (pathname, flags, mode);
623 remote_fileio_return_errno (-1);
627 fd = remote_fileio_fd_to_targetfd (fd);
628 remote_fileio_return_success (fd);
632 remote_fileio_func_close (char *buf)
637 /* Parameter: file descriptor */
638 if (remote_fileio_extract_int (&buf, &num))
640 remote_fileio_ioerror ();
643 fd = remote_fileio_map_fd ((int) num);
644 if (fd == FIO_FD_INVALID)
646 remote_fileio_badfd ();
650 remote_fio_no_longjmp = 1;
651 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
652 remote_fileio_return_errno (-1);
653 remote_fileio_close_target_fd ((int) num);
654 remote_fileio_return_success (0);
658 remote_fileio_func_read (char *buf)
663 int fd, ret, retlength;
666 off_t old_offset, new_offset;
668 /* 1. Parameter: file descriptor */
669 if (remote_fileio_extract_int (&buf, &target_fd))
671 remote_fileio_ioerror ();
674 fd = remote_fileio_map_fd ((int) target_fd);
675 if (fd == FIO_FD_INVALID)
677 remote_fileio_badfd ();
680 /* 2. Parameter: buffer pointer */
681 if (remote_fileio_extract_long (&buf, &lnum))
683 remote_fileio_ioerror ();
686 ptrval = (CORE_ADDR) lnum;
687 /* 3. Parameter: buffer length */
688 if (remote_fileio_extract_int (&buf, &num))
690 remote_fileio_ioerror ();
693 length = (size_t) num;
697 case FIO_FD_CONSOLE_OUT:
698 remote_fileio_badfd ();
700 case FIO_FD_CONSOLE_IN:
702 static char *remaining_buf = NULL;
703 static int remaining_length = 0;
705 buffer = (char *) xmalloc (32768);
708 remote_fio_no_longjmp = 1;
709 if (remaining_length > length)
711 memcpy (buffer, remaining_buf, length);
712 memmove (remaining_buf, remaining_buf + length,
713 remaining_length - length);
714 remaining_length -= length;
719 memcpy (buffer, remaining_buf, remaining_length);
720 xfree (remaining_buf);
721 remaining_buf = NULL;
722 ret = remaining_length;
727 ret = ui_file_read (gdb_stdtargin, buffer, 32767);
728 remote_fio_no_longjmp = 1;
729 if (ret > 0 && (size_t)ret > length)
731 remaining_buf = (char *) xmalloc (ret - length);
732 remaining_length = ret - length;
733 memcpy (remaining_buf, buffer + length, remaining_length);
740 buffer = (char *) xmalloc (length);
741 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
742 for read() to return -1 even if "some" bytes have been read. It
743 has been corrected in SUSv2 but that doesn't help us much...
744 Therefore a complete solution must check how many bytes have been
745 read on EINTR to return a more reliable value to the target */
746 old_offset = lseek (fd, 0, SEEK_CUR);
747 remote_fio_no_longjmp = 1;
748 ret = read (fd, buffer, length);
749 if (ret < 0 && errno == EINTR)
751 new_offset = lseek (fd, 0, SEEK_CUR);
752 /* If some data has been read, return the number of bytes read.
753 The Ctrl-C flag is set in remote_fileio_reply() anyway */
754 if (old_offset != new_offset)
755 ret = new_offset - old_offset;
762 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
763 if (retlength != ret)
764 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
768 remote_fileio_return_errno (-1);
770 remote_fileio_return_success (ret);
776 remote_fileio_func_write (char *buf)
781 int fd, ret, retlength;
785 /* 1. Parameter: file descriptor */
786 if (remote_fileio_extract_int (&buf, &target_fd))
788 remote_fileio_ioerror ();
791 fd = remote_fileio_map_fd ((int) target_fd);
792 if (fd == FIO_FD_INVALID)
794 remote_fileio_badfd ();
797 /* 2. Parameter: buffer pointer */
798 if (remote_fileio_extract_long (&buf, &lnum))
800 remote_fileio_ioerror ();
803 ptrval = (CORE_ADDR) lnum;
804 /* 3. Parameter: buffer length */
805 if (remote_fileio_extract_int (&buf, &num))
807 remote_fileio_ioerror ();
810 length = (size_t) num;
812 buffer = (char *) xmalloc (length);
813 retlength = remote_read_bytes (ptrval, buffer, length);
814 if (retlength != length)
817 remote_fileio_ioerror ();
821 remote_fio_no_longjmp = 1;
824 case FIO_FD_CONSOLE_IN:
825 remote_fileio_badfd ();
827 case FIO_FD_CONSOLE_OUT:
828 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr, buffer,
830 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
834 ret = write (fd, buffer, length);
835 if (ret < 0 && errno == EACCES)
836 errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
841 remote_fileio_return_errno (-1);
843 remote_fileio_return_success (ret);
849 remote_fileio_func_lseek (char *buf)
856 /* 1. Parameter: file descriptor */
857 if (remote_fileio_extract_int (&buf, &num))
859 remote_fileio_ioerror ();
862 fd = remote_fileio_map_fd ((int) num);
863 if (fd == FIO_FD_INVALID)
865 remote_fileio_badfd ();
868 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
870 remote_fileio_reply (-1, FILEIO_ESPIPE);
874 /* 2. Parameter: offset */
875 if (remote_fileio_extract_long (&buf, &lnum))
877 remote_fileio_ioerror ();
880 offset = (off_t) lnum;
881 /* 3. Parameter: flag */
882 if (remote_fileio_extract_int (&buf, &num))
884 remote_fileio_ioerror ();
887 if (remote_fileio_seek_flag_to_host (num, &flag))
889 remote_fileio_reply (-1, FILEIO_EINVAL);
893 remote_fio_no_longjmp = 1;
894 ret = lseek (fd, offset, flag);
896 if (ret == (off_t) -1)
897 remote_fileio_return_errno (-1);
899 remote_fileio_return_success (ret);
903 remote_fileio_func_rename (char *buf)
906 int length, retlength;
907 char *oldpath, *newpath;
909 struct stat ost, nst;
911 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
912 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
914 remote_fileio_ioerror ();
917 /* Request oldpath using 'm' packet */
918 oldpath = alloca (length);
919 retlength = remote_read_bytes (ptrval, oldpath, length);
920 if (retlength != length)
922 remote_fileio_ioerror ();
925 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
926 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
928 remote_fileio_ioerror ();
931 /* Request newpath using 'm' packet */
932 newpath = alloca (length);
933 retlength = remote_read_bytes (ptrval, newpath, length);
934 if (retlength != length)
936 remote_fileio_ioerror ();
940 /* Only operate on regular files and directories */
941 of = stat (oldpath, &ost);
942 nf = stat (newpath, &nst);
943 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
944 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
946 remote_fileio_reply (-1, FILEIO_EACCES);
950 remote_fio_no_longjmp = 1;
951 ret = rename (oldpath, newpath);
955 /* Special case: newpath is a non-empty directory. Some systems
956 return ENOTEMPTY, some return EEXIST. We coerce that to be
958 if (errno == ENOTEMPTY)
961 /* Workaround some Cygwin problems with correct errnos. */
964 if (!of && !nf && S_ISDIR (nst.st_mode))
966 if (S_ISREG (ost.st_mode))
970 char oldfullpath[PATH_MAX + 1];
971 char newfullpath[PATH_MAX + 1];
974 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
975 cygwin_conv_to_full_posix_path (newpath, newfullpath);
976 len = strlen (oldfullpath);
977 if (newfullpath[len] == '/'
978 && !strncmp (oldfullpath, newfullpath, len))
987 remote_fileio_return_errno (-1);
990 remote_fileio_return_success (ret);
994 remote_fileio_func_unlink (char *buf)
997 int length, retlength;
1002 /* Parameter: Ptr to pathname / length incl. trailing zero */
1003 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1005 remote_fileio_ioerror ();
1008 /* Request pathname using 'm' packet */
1009 pathname = alloca (length);
1010 retlength = remote_read_bytes (ptrval, pathname, length);
1011 if (retlength != length)
1013 remote_fileio_ioerror ();
1017 /* Only operate on regular files (and directories, which allows to return
1018 the correct return code) */
1019 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1021 remote_fileio_reply (-1, FILEIO_ENODEV);
1025 remote_fio_no_longjmp = 1;
1026 ret = unlink (pathname);
1029 remote_fileio_return_errno (-1);
1031 remote_fileio_return_success (ret);
1035 remote_fileio_func_stat (char *buf)
1038 int ret, length, retlength;
1042 struct fio_stat fst;
1044 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1045 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1047 remote_fileio_ioerror ();
1050 /* Request pathname using 'm' packet */
1051 pathname = alloca (length);
1052 retlength = remote_read_bytes (ptrval, pathname, length);
1053 if (retlength != length)
1055 remote_fileio_ioerror ();
1059 /* 2. Parameter: Ptr to struct stat */
1060 if (remote_fileio_extract_long (&buf, &lnum))
1062 remote_fileio_ioerror ();
1065 ptrval = (CORE_ADDR) lnum;
1067 remote_fio_no_longjmp = 1;
1068 ret = stat (pathname, &st);
1072 remote_fileio_return_errno (-1);
1075 /* Only operate on regular files and directories */
1076 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1078 remote_fileio_reply (-1, FILEIO_EACCES);
1083 remote_fileio_to_fio_stat (&st, &fst);
1084 remote_fileio_to_fio_uint (0, fst.fst_dev);
1086 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1087 if (retlength != sizeof fst)
1089 remote_fileio_return_errno (-1);
1093 remote_fileio_return_success (ret);
1097 remote_fileio_func_fstat (char *buf)
1100 int fd, ret, retlength;
1104 struct fio_stat fst;
1107 /* 1. Parameter: file descriptor */
1108 if (remote_fileio_extract_int (&buf, &target_fd))
1110 remote_fileio_ioerror ();
1113 fd = remote_fileio_map_fd ((int) target_fd);
1114 if (fd == FIO_FD_INVALID)
1116 remote_fileio_badfd ();
1119 /* 2. Parameter: Ptr to struct stat */
1120 if (remote_fileio_extract_long (&buf, &lnum))
1122 remote_fileio_ioerror ();
1125 ptrval = (CORE_ADDR) lnum;
1127 remote_fio_no_longjmp = 1;
1128 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1130 remote_fileio_to_fio_uint (1, fst.fst_dev);
1131 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1133 st.st_uid = getuid ();
1134 st.st_gid = getgid ();
1137 st.st_blksize = 512;
1138 #if HAVE_STRUCT_STAT_ST_BLOCKS
1141 if (!gettimeofday (&tv, NULL))
1142 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1144 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1148 ret = fstat (fd, &st);
1152 remote_fileio_return_errno (-1);
1157 remote_fileio_to_fio_stat (&st, &fst);
1159 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1160 if (retlength != sizeof fst)
1162 remote_fileio_return_errno (-1);
1166 remote_fileio_return_success (ret);
1170 remote_fileio_func_gettimeofday (char *buf)
1176 struct fio_timeval ftv;
1178 /* 1. Parameter: struct timeval pointer */
1179 if (remote_fileio_extract_long (&buf, &lnum))
1181 remote_fileio_ioerror ();
1184 ptrval = (CORE_ADDR) lnum;
1185 /* 2. Parameter: some pointer value... */
1186 if (remote_fileio_extract_long (&buf, &lnum))
1188 remote_fileio_ioerror ();
1191 /* ...which has to be NULL */
1194 remote_fileio_reply (-1, FILEIO_EINVAL);
1198 remote_fio_no_longjmp = 1;
1199 ret = gettimeofday (&tv, NULL);
1203 remote_fileio_return_errno (-1);
1209 remote_fileio_to_fio_timeval (&tv, &ftv);
1211 retlength = remote_fileio_write_bytes (ptrval, (char *) &ftv, sizeof ftv);
1212 if (retlength != sizeof ftv)
1214 remote_fileio_return_errno (-1);
1218 remote_fileio_return_success (ret);
1222 remote_fileio_func_isatty (char *buf)
1227 /* Parameter: file descriptor */
1228 if (remote_fileio_extract_int (&buf, &target_fd))
1230 remote_fileio_ioerror ();
1233 remote_fio_no_longjmp = 1;
1234 fd = remote_fileio_map_fd ((int) target_fd);
1235 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1236 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1240 remote_fileio_func_system (char *buf)
1243 int ret, length, retlength;
1246 /* Check if system(3) has been explicitely allowed using the
1247 `set remote system-call-allowed 1' command. If not, return
1249 if (!remote_fio_system_call_allowed)
1251 remote_fileio_reply (-1, FILEIO_EPERM);
1255 /* Parameter: Ptr to commandline / length incl. trailing zero */
1256 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1258 remote_fileio_ioerror ();
1261 /* Request commandline using 'm' packet */
1262 cmdline = alloca (length);
1263 retlength = remote_read_bytes (ptrval, cmdline, length);
1264 if (retlength != length)
1266 remote_fileio_ioerror ();
1270 remote_fio_no_longjmp = 1;
1271 ret = system (cmdline);
1274 remote_fileio_return_errno (-1);
1276 remote_fileio_return_success (WEXITSTATUS (ret));
1281 void (*func)(char *);
1282 } remote_fio_func_map[] = {
1283 "open", remote_fileio_func_open,
1284 "close", remote_fileio_func_close,
1285 "read", remote_fileio_func_read,
1286 "write", remote_fileio_func_write,
1287 "lseek", remote_fileio_func_lseek,
1288 "rename", remote_fileio_func_rename,
1289 "unlink", remote_fileio_func_unlink,
1290 "stat", remote_fileio_func_stat,
1291 "fstat", remote_fileio_func_fstat,
1292 "gettimeofday", remote_fileio_func_gettimeofday,
1293 "isatty", remote_fileio_func_isatty,
1294 "system", remote_fileio_func_system,
1299 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1301 char *buf = buf_arg;
1305 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1307 c = strchr (++buf, ',');
1311 c = strchr (buf, '\0');
1312 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1313 if (!strcmp (remote_fio_func_map[idx].name, buf))
1315 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1316 return RETURN_ERROR;
1317 remote_fio_func_map[idx].func (c);
1322 remote_fileio_request (char *buf)
1326 remote_fileio_sig_init ();
1328 remote_fio_ctrl_c_flag = 0;
1329 remote_fio_no_longjmp = 0;
1331 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1336 remote_fileio_reply (-1, FILEIO_ENOSYS);
1339 remote_fileio_reply (-1, FILEIO_EINTR);
1345 remote_fileio_sig_exit ();
1349 set_system_call_allowed (char *args, int from_tty)
1354 int val = strtoul (args, &arg_end, 10);
1355 if (*args && *arg_end == '\0')
1357 remote_fio_system_call_allowed = !!val;
1361 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1365 show_system_call_allowed (char *args, int from_tty)
1368 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1369 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1370 remote_fio_system_call_allowed ? "" : "not ");
1374 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1375 struct cmd_list_element *remote_show_cmdlist)
1377 add_cmd ("system-call-allowed", no_class,
1378 set_system_call_allowed,
1379 "Set if the host system(3) call is allowed for the target.\n",
1380 &remote_set_cmdlist);
1381 add_cmd ("system-call-allowed", no_class,
1382 show_system_call_allowed,
1383 "Show if the host system(3) call is allowed for the target.\n",
1384 &remote_show_cmdlist);