1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006, 2007 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 3 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, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
23 #include "gdb_string.h"
26 #include "gdb/fileio.h"
29 #include "exceptions.h"
30 #include "remote-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 int i = remote_fio_data.fd_map_size;
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 for (; i < remote_fio_data.fd_map_size; i++)
80 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
81 return remote_fio_data.fd_map_size - 10;
85 remote_fileio_next_free_fd (void)
89 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
90 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
92 return remote_fileio_resize_fd_map ();
96 remote_fileio_fd_to_targetfd (int fd)
98 int target_fd = remote_fileio_next_free_fd ();
99 remote_fio_data.fd_map[target_fd] = fd;
104 remote_fileio_map_fd (int target_fd)
106 remote_fileio_init_fd_map ();
107 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
108 return FIO_FD_INVALID;
109 return remote_fio_data.fd_map[target_fd];
113 remote_fileio_close_target_fd (int target_fd)
115 remote_fileio_init_fd_map ();
116 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
117 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
121 remote_fileio_oflags_to_host (long flags)
125 if (flags & FILEIO_O_CREAT)
127 if (flags & FILEIO_O_EXCL)
129 if (flags & FILEIO_O_TRUNC)
131 if (flags & FILEIO_O_APPEND)
133 if (flags & FILEIO_O_RDONLY)
135 if (flags & FILEIO_O_WRONLY)
137 if (flags & FILEIO_O_RDWR)
139 /* On systems supporting binary and text mode, always open files in
148 remote_fileio_mode_to_host (long mode, int open_call)
154 if (mode & FILEIO_S_IFREG)
156 if (mode & FILEIO_S_IFDIR)
158 if (mode & FILEIO_S_IFCHR)
161 if (mode & FILEIO_S_IRUSR)
163 if (mode & FILEIO_S_IWUSR)
165 if (mode & FILEIO_S_IXUSR)
168 if (mode & FILEIO_S_IRGRP)
172 if (mode & FILEIO_S_IWGRP)
176 if (mode & FILEIO_S_IXGRP)
179 if (mode & FILEIO_S_IROTH)
182 if (mode & FILEIO_S_IWOTH)
186 if (mode & FILEIO_S_IXOTH)
193 remote_fileio_mode_to_target (mode_t mode)
198 tmode |= FILEIO_S_IFREG;
200 tmode |= FILEIO_S_IFDIR;
202 tmode |= FILEIO_S_IFCHR;
204 tmode |= FILEIO_S_IRUSR;
206 tmode |= FILEIO_S_IWUSR;
208 tmode |= FILEIO_S_IXUSR;
211 tmode |= FILEIO_S_IRGRP;
215 tmode |= FILEIO_S_IWGRP;
219 tmode |= FILEIO_S_IXGRP;
222 tmode |= FILEIO_S_IROTH;
225 tmode |= FILEIO_S_IWOTH;
229 tmode |= FILEIO_S_IXOTH;
235 remote_fileio_errno_to_target (int error)
242 return FILEIO_ENOENT;
250 return FILEIO_EACCES;
252 return FILEIO_EFAULT;
256 return FILEIO_EEXIST;
258 return FILEIO_ENODEV;
260 return FILEIO_ENOTDIR;
262 return FILEIO_EISDIR;
264 return FILEIO_EINVAL;
266 return FILEIO_ENFILE;
268 return FILEIO_EMFILE;
272 return FILEIO_ENOSPC;
274 return FILEIO_ESPIPE;
278 return FILEIO_ENOSYS;
280 return FILEIO_ENAMETOOLONG;
282 return FILEIO_EUNKNOWN;
286 remote_fileio_seek_flag_to_host (long num, int *flag)
292 case FILEIO_SEEK_SET:
295 case FILEIO_SEEK_CUR:
298 case FILEIO_SEEK_END:
308 remote_fileio_extract_long (char **buf, LONGEST *retlong)
313 if (!buf || !*buf || !**buf || !retlong)
315 c = strchr (*buf, ',');
319 c = strchr (*buf, '\0');
320 while (strchr ("+-", **buf))
326 for (*retlong = 0; **buf; ++*buf)
329 if (**buf >= '0' && **buf <= '9')
330 *retlong += **buf - '0';
331 else if (**buf >= 'a' && **buf <= 'f')
332 *retlong += **buf - 'a' + 10;
333 else if (**buf >= 'A' && **buf <= 'F')
334 *retlong += **buf - 'A' + 10;
344 remote_fileio_extract_int (char **buf, long *retint)
351 ret = remote_fileio_extract_long (buf, &retlong);
353 *retint = (long) retlong;
358 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
363 if (!buf || !*buf || !**buf || !ptrval || !length)
365 c = strchr (*buf, '/');
369 if (remote_fileio_extract_long (buf, &retlong))
371 *ptrval = (CORE_ADDR) retlong;
373 if (remote_fileio_extract_long (buf, &retlong))
375 *length = (int) retlong;
379 /* Convert to big endian */
381 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
385 for (i = 0; i < bytes; ++i)
386 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
390 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
392 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
396 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
398 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
402 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
404 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
408 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
410 remote_fileio_to_be (num, (char *) fnum, 8);
414 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
416 remote_fileio_to_be (num, (char *) fnum, 8);
420 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
424 /* `st_dev' is set in the calling function */
425 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
426 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
427 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
428 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
429 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
430 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
431 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
432 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
433 blksize = st->st_blksize;
437 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
438 #if HAVE_STRUCT_STAT_ST_BLOCKS
439 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
441 /* FIXME: This is correct for DJGPP, but other systems that don't
442 have st_blocks, if any, might prefer 512 instead of st_blksize.
443 (eliz, 30-12-2003) */
444 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
448 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
449 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
450 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
454 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
456 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
457 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
460 static int remote_fio_ctrl_c_flag = 0;
461 static int remote_fio_no_longjmp = 0;
463 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
464 static struct sigaction remote_fio_sa;
465 static struct sigaction remote_fio_osa;
467 static void (*remote_fio_ofunc)(int);
471 remote_fileio_sig_init (void)
473 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
474 remote_fio_sa.sa_handler = SIG_IGN;
475 sigemptyset (&remote_fio_sa.sa_mask);
476 remote_fio_sa.sa_flags = 0;
477 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
479 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
484 remote_fileio_sig_set (void (*sigint_func)(int))
486 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
487 remote_fio_sa.sa_handler = sigint_func;
488 sigemptyset (&remote_fio_sa.sa_mask);
489 remote_fio_sa.sa_flags = 0;
490 sigaction (SIGINT, &remote_fio_sa, NULL);
492 signal (SIGINT, sigint_func);
497 remote_fileio_sig_exit (void)
499 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
500 sigaction (SIGINT, &remote_fio_osa, NULL);
502 signal (SIGINT, remote_fio_ofunc);
507 remote_fileio_ctrl_c_signal_handler (int signo)
509 remote_fileio_sig_set (SIG_IGN);
510 remote_fio_ctrl_c_flag = 1;
511 if (!remote_fio_no_longjmp)
512 deprecated_throw_reason (RETURN_QUIT);
513 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
517 remote_fileio_reply (int retcode, int error)
521 remote_fileio_sig_set (SIG_IGN);
528 sprintf (buf + strlen (buf), "%x", retcode);
529 if (error || remote_fio_ctrl_c_flag)
531 if (error && remote_fio_ctrl_c_flag)
532 error = FILEIO_EINTR;
538 sprintf (buf + strlen (buf), ",%x", error);
539 if (remote_fio_ctrl_c_flag)
542 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
547 remote_fileio_ioerror (void)
549 remote_fileio_reply (-1, FILEIO_EIO);
553 remote_fileio_badfd (void)
555 remote_fileio_reply (-1, FILEIO_EBADF);
559 remote_fileio_return_errno (int retcode)
561 remote_fileio_reply (retcode,
562 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
566 remote_fileio_return_success (int retcode)
568 remote_fileio_reply (retcode, 0);
571 /* Wrapper function for remote_write_bytes() which has the disadvantage to
572 write only one packet, regardless of the requested number of bytes to
573 transfer. This wrapper calls remote_write_bytes() as often as needed. */
575 remote_fileio_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
577 int ret = 0, written;
579 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
590 remote_fileio_func_open (char *buf)
593 int length, retlength;
600 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
601 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
603 remote_fileio_ioerror ();
606 /* 2. Parameter: open flags */
607 if (remote_fileio_extract_int (&buf, &num))
609 remote_fileio_ioerror ();
612 flags = remote_fileio_oflags_to_host (num);
613 /* 3. Parameter: open mode */
614 if (remote_fileio_extract_int (&buf, &num))
616 remote_fileio_ioerror ();
619 mode = remote_fileio_mode_to_host (num, 1);
621 /* Request pathname using 'm' packet */
622 pathname = alloca (length);
623 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
624 if (retlength != length)
626 remote_fileio_ioerror ();
630 /* Check if pathname exists and is not a regular file or directory. If so,
631 return an appropriate error code. Same for trying to open directories
633 if (!stat (pathname, &st))
635 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
637 remote_fileio_reply (-1, FILEIO_ENODEV);
640 if (S_ISDIR (st.st_mode)
641 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
643 remote_fileio_reply (-1, FILEIO_EISDIR);
648 remote_fio_no_longjmp = 1;
649 fd = open (pathname, flags, mode);
652 remote_fileio_return_errno (-1);
656 fd = remote_fileio_fd_to_targetfd (fd);
657 remote_fileio_return_success (fd);
661 remote_fileio_func_close (char *buf)
666 /* Parameter: file descriptor */
667 if (remote_fileio_extract_int (&buf, &num))
669 remote_fileio_ioerror ();
672 fd = remote_fileio_map_fd ((int) num);
673 if (fd == FIO_FD_INVALID)
675 remote_fileio_badfd ();
679 remote_fio_no_longjmp = 1;
680 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
681 remote_fileio_return_errno (-1);
682 remote_fileio_close_target_fd ((int) num);
683 remote_fileio_return_success (0);
687 remote_fileio_func_read (char *buf)
692 int fd, ret, retlength;
695 off_t old_offset, new_offset;
697 /* 1. Parameter: file descriptor */
698 if (remote_fileio_extract_int (&buf, &target_fd))
700 remote_fileio_ioerror ();
703 fd = remote_fileio_map_fd ((int) target_fd);
704 if (fd == FIO_FD_INVALID)
706 remote_fileio_badfd ();
709 /* 2. Parameter: buffer pointer */
710 if (remote_fileio_extract_long (&buf, &lnum))
712 remote_fileio_ioerror ();
715 ptrval = (CORE_ADDR) lnum;
716 /* 3. Parameter: buffer length */
717 if (remote_fileio_extract_int (&buf, &num))
719 remote_fileio_ioerror ();
722 length = (size_t) num;
726 case FIO_FD_CONSOLE_OUT:
727 remote_fileio_badfd ();
729 case FIO_FD_CONSOLE_IN:
731 static char *remaining_buf = NULL;
732 static int remaining_length = 0;
734 buffer = (gdb_byte *) xmalloc (32768);
737 remote_fio_no_longjmp = 1;
738 if (remaining_length > length)
740 memcpy (buffer, remaining_buf, length);
741 memmove (remaining_buf, remaining_buf + length,
742 remaining_length - length);
743 remaining_length -= length;
748 memcpy (buffer, remaining_buf, remaining_length);
749 xfree (remaining_buf);
750 remaining_buf = NULL;
751 ret = remaining_length;
756 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 32767);
757 remote_fio_no_longjmp = 1;
758 if (ret > 0 && (size_t)ret > length)
760 remaining_buf = (char *) xmalloc (ret - length);
761 remaining_length = ret - length;
762 memcpy (remaining_buf, buffer + length, remaining_length);
769 buffer = (gdb_byte *) xmalloc (length);
770 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
771 for read() to return -1 even if "some" bytes have been read. It
772 has been corrected in SUSv2 but that doesn't help us much...
773 Therefore a complete solution must check how many bytes have been
774 read on EINTR to return a more reliable value to the target */
775 old_offset = lseek (fd, 0, SEEK_CUR);
776 remote_fio_no_longjmp = 1;
777 ret = read (fd, buffer, length);
778 if (ret < 0 && errno == EINTR)
780 new_offset = lseek (fd, 0, SEEK_CUR);
781 /* If some data has been read, return the number of bytes read.
782 The Ctrl-C flag is set in remote_fileio_reply() anyway */
783 if (old_offset != new_offset)
784 ret = new_offset - old_offset;
791 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
792 if (retlength != ret)
793 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
797 remote_fileio_return_errno (-1);
799 remote_fileio_return_success (ret);
805 remote_fileio_func_write (char *buf)
810 int fd, ret, retlength;
814 /* 1. Parameter: file descriptor */
815 if (remote_fileio_extract_int (&buf, &target_fd))
817 remote_fileio_ioerror ();
820 fd = remote_fileio_map_fd ((int) target_fd);
821 if (fd == FIO_FD_INVALID)
823 remote_fileio_badfd ();
826 /* 2. Parameter: buffer pointer */
827 if (remote_fileio_extract_long (&buf, &lnum))
829 remote_fileio_ioerror ();
832 ptrval = (CORE_ADDR) lnum;
833 /* 3. Parameter: buffer length */
834 if (remote_fileio_extract_int (&buf, &num))
836 remote_fileio_ioerror ();
839 length = (size_t) num;
841 buffer = (gdb_byte *) xmalloc (length);
842 retlength = remote_read_bytes (ptrval, buffer, length);
843 if (retlength != length)
846 remote_fileio_ioerror ();
850 remote_fio_no_longjmp = 1;
853 case FIO_FD_CONSOLE_IN:
854 remote_fileio_badfd ();
857 case FIO_FD_CONSOLE_OUT:
858 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
859 (char *) buffer, length);
860 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
864 ret = write (fd, buffer, length);
865 if (ret < 0 && errno == EACCES)
866 errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
871 remote_fileio_return_errno (-1);
873 remote_fileio_return_success (ret);
879 remote_fileio_func_lseek (char *buf)
886 /* 1. Parameter: file descriptor */
887 if (remote_fileio_extract_int (&buf, &num))
889 remote_fileio_ioerror ();
892 fd = remote_fileio_map_fd ((int) num);
893 if (fd == FIO_FD_INVALID)
895 remote_fileio_badfd ();
898 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
900 remote_fileio_reply (-1, FILEIO_ESPIPE);
904 /* 2. Parameter: offset */
905 if (remote_fileio_extract_long (&buf, &lnum))
907 remote_fileio_ioerror ();
910 offset = (off_t) lnum;
911 /* 3. Parameter: flag */
912 if (remote_fileio_extract_int (&buf, &num))
914 remote_fileio_ioerror ();
917 if (remote_fileio_seek_flag_to_host (num, &flag))
919 remote_fileio_reply (-1, FILEIO_EINVAL);
923 remote_fio_no_longjmp = 1;
924 ret = lseek (fd, offset, flag);
926 if (ret == (off_t) -1)
927 remote_fileio_return_errno (-1);
929 remote_fileio_return_success (ret);
933 remote_fileio_func_rename (char *buf)
935 CORE_ADDR old_ptr, new_ptr;
936 int old_len, new_len, retlength;
937 char *oldpath, *newpath;
939 struct stat ost, nst;
941 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
942 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
944 remote_fileio_ioerror ();
948 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
949 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
951 remote_fileio_ioerror ();
955 /* Request oldpath using 'm' packet */
956 oldpath = alloca (old_len);
957 retlength = remote_read_bytes (old_ptr, (gdb_byte *) oldpath, old_len);
958 if (retlength != old_len)
960 remote_fileio_ioerror ();
964 /* Request newpath using 'm' packet */
965 newpath = alloca (new_len);
966 retlength = remote_read_bytes (new_ptr, (gdb_byte *) newpath, new_len);
967 if (retlength != new_len)
969 remote_fileio_ioerror ();
973 /* Only operate on regular files and directories */
974 of = stat (oldpath, &ost);
975 nf = stat (newpath, &nst);
976 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
977 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
979 remote_fileio_reply (-1, FILEIO_EACCES);
983 remote_fio_no_longjmp = 1;
984 ret = rename (oldpath, newpath);
988 /* Special case: newpath is a non-empty directory. Some systems
989 return ENOTEMPTY, some return EEXIST. We coerce that to be
991 if (errno == ENOTEMPTY)
994 /* Workaround some Cygwin problems with correct errnos. */
997 if (!of && !nf && S_ISDIR (nst.st_mode))
999 if (S_ISREG (ost.st_mode))
1003 char oldfullpath[PATH_MAX + 1];
1004 char newfullpath[PATH_MAX + 1];
1007 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
1008 cygwin_conv_to_full_posix_path (newpath, newfullpath);
1009 len = strlen (oldfullpath);
1010 if (newfullpath[len] == '/'
1011 && !strncmp (oldfullpath, newfullpath, len))
1020 remote_fileio_return_errno (-1);
1023 remote_fileio_return_success (ret);
1027 remote_fileio_func_unlink (char *buf)
1030 int length, retlength;
1035 /* Parameter: Ptr to pathname / length incl. trailing zero */
1036 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1038 remote_fileio_ioerror ();
1041 /* Request pathname using 'm' packet */
1042 pathname = alloca (length);
1043 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
1044 if (retlength != length)
1046 remote_fileio_ioerror ();
1050 /* Only operate on regular files (and directories, which allows to return
1051 the correct return code) */
1052 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1054 remote_fileio_reply (-1, FILEIO_ENODEV);
1058 remote_fio_no_longjmp = 1;
1059 ret = unlink (pathname);
1062 remote_fileio_return_errno (-1);
1064 remote_fileio_return_success (ret);
1068 remote_fileio_func_stat (char *buf)
1070 CORE_ADDR statptr, nameptr;
1071 int ret, namelength, retlength;
1075 struct fio_stat fst;
1077 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1078 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1080 remote_fileio_ioerror ();
1084 /* 2. Parameter: Ptr to struct stat */
1085 if (remote_fileio_extract_long (&buf, &lnum))
1087 remote_fileio_ioerror ();
1090 statptr = (CORE_ADDR) lnum;
1092 /* Request pathname using 'm' packet */
1093 pathname = alloca (namelength);
1094 retlength = remote_read_bytes (nameptr, (gdb_byte *) pathname, namelength);
1095 if (retlength != namelength)
1097 remote_fileio_ioerror ();
1101 remote_fio_no_longjmp = 1;
1102 ret = stat (pathname, &st);
1106 remote_fileio_return_errno (-1);
1109 /* Only operate on regular files and directories */
1110 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1112 remote_fileio_reply (-1, FILEIO_EACCES);
1117 remote_fileio_to_fio_stat (&st, &fst);
1118 remote_fileio_to_fio_uint (0, fst.fst_dev);
1120 retlength = remote_fileio_write_bytes (statptr,
1121 (gdb_byte *) &fst, sizeof fst);
1122 if (retlength != sizeof fst)
1124 remote_fileio_return_errno (-1);
1128 remote_fileio_return_success (ret);
1132 remote_fileio_func_fstat (char *buf)
1135 int fd, ret, retlength;
1139 struct fio_stat fst;
1142 /* 1. Parameter: file descriptor */
1143 if (remote_fileio_extract_int (&buf, &target_fd))
1145 remote_fileio_ioerror ();
1148 fd = remote_fileio_map_fd ((int) target_fd);
1149 if (fd == FIO_FD_INVALID)
1151 remote_fileio_badfd ();
1154 /* 2. Parameter: Ptr to struct stat */
1155 if (remote_fileio_extract_long (&buf, &lnum))
1157 remote_fileio_ioerror ();
1160 ptrval = (CORE_ADDR) lnum;
1162 remote_fio_no_longjmp = 1;
1163 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1165 remote_fileio_to_fio_uint (1, fst.fst_dev);
1166 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1169 st.st_uid = getuid ();
1174 st.st_gid = getgid ();
1180 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1181 st.st_blksize = 512;
1183 #if HAVE_STRUCT_STAT_ST_BLOCKS
1186 if (!gettimeofday (&tv, NULL))
1187 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1189 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1193 ret = fstat (fd, &st);
1197 remote_fileio_return_errno (-1);
1202 remote_fileio_to_fio_stat (&st, &fst);
1204 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &fst, sizeof fst);
1205 if (retlength != sizeof fst)
1207 remote_fileio_return_errno (-1);
1211 remote_fileio_return_success (ret);
1215 remote_fileio_func_gettimeofday (char *buf)
1221 struct fio_timeval ftv;
1223 /* 1. Parameter: struct timeval pointer */
1224 if (remote_fileio_extract_long (&buf, &lnum))
1226 remote_fileio_ioerror ();
1229 ptrval = (CORE_ADDR) lnum;
1230 /* 2. Parameter: some pointer value... */
1231 if (remote_fileio_extract_long (&buf, &lnum))
1233 remote_fileio_ioerror ();
1236 /* ...which has to be NULL */
1239 remote_fileio_reply (-1, FILEIO_EINVAL);
1243 remote_fio_no_longjmp = 1;
1244 ret = gettimeofday (&tv, NULL);
1248 remote_fileio_return_errno (-1);
1254 remote_fileio_to_fio_timeval (&tv, &ftv);
1256 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1257 if (retlength != sizeof ftv)
1259 remote_fileio_return_errno (-1);
1263 remote_fileio_return_success (ret);
1267 remote_fileio_func_isatty (char *buf)
1272 /* Parameter: file descriptor */
1273 if (remote_fileio_extract_int (&buf, &target_fd))
1275 remote_fileio_ioerror ();
1278 remote_fio_no_longjmp = 1;
1279 fd = remote_fileio_map_fd ((int) target_fd);
1280 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1281 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1285 remote_fileio_func_system (char *buf)
1288 int ret, length, retlength;
1289 char *cmdline = NULL;
1291 /* Parameter: Ptr to commandline / length incl. trailing zero */
1292 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1294 remote_fileio_ioerror ();
1300 /* Request commandline using 'm' packet */
1301 cmdline = alloca (length);
1302 retlength = remote_read_bytes (ptrval, (gdb_byte *) cmdline, length);
1303 if (retlength != length)
1305 remote_fileio_ioerror ();
1310 /* Check if system(3) has been explicitely allowed using the
1311 `set remote system-call-allowed 1' command. If length is 0,
1312 indicating a NULL parameter to the system call, return zero to
1313 indicate a shell is not available. Otherwise fail with EPERM. */
1314 if (!remote_fio_system_call_allowed)
1317 remote_fileio_return_success (0);
1319 remote_fileio_reply (-1, FILEIO_EPERM);
1323 remote_fio_no_longjmp = 1;
1324 ret = system (cmdline);
1327 remote_fileio_return_success (ret);
1329 remote_fileio_return_errno (-1);
1331 remote_fileio_return_success (WEXITSTATUS (ret));
1336 void (*func)(char *);
1337 } remote_fio_func_map[] = {
1338 { "open", remote_fileio_func_open },
1339 { "close", remote_fileio_func_close },
1340 { "read", remote_fileio_func_read },
1341 { "write", remote_fileio_func_write },
1342 { "lseek", remote_fileio_func_lseek },
1343 { "rename", remote_fileio_func_rename },
1344 { "unlink", remote_fileio_func_unlink },
1345 { "stat", remote_fileio_func_stat },
1346 { "fstat", remote_fileio_func_fstat },
1347 { "gettimeofday", remote_fileio_func_gettimeofday },
1348 { "isatty", remote_fileio_func_isatty },
1349 { "system", remote_fileio_func_system },
1354 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1356 char *buf = buf_arg;
1360 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1362 c = strchr (++buf, ',');
1366 c = strchr (buf, '\0');
1367 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1368 if (!strcmp (remote_fio_func_map[idx].name, buf))
1370 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1371 return RETURN_ERROR;
1372 remote_fio_func_map[idx].func (c);
1376 /* Close any open descriptors, and reinitialize the file mapping. */
1379 remote_fileio_reset (void)
1383 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1385 int fd = remote_fio_data.fd_map[ix];
1390 if (remote_fio_data.fd_map)
1392 free (remote_fio_data.fd_map);
1393 remote_fio_data.fd_map = NULL;
1394 remote_fio_data.fd_map_size = 0;
1399 remote_fileio_request (char *buf)
1403 remote_fileio_sig_init ();
1405 remote_fio_ctrl_c_flag = 0;
1406 remote_fio_no_longjmp = 0;
1408 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1413 remote_fileio_reply (-1, FILEIO_ENOSYS);
1416 remote_fileio_reply (-1, FILEIO_EINTR);
1422 remote_fileio_sig_exit ();
1426 set_system_call_allowed (char *args, int from_tty)
1431 int val = strtoul (args, &arg_end, 10);
1432 if (*args && *arg_end == '\0')
1434 remote_fio_system_call_allowed = !!val;
1438 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1442 show_system_call_allowed (char *args, int from_tty)
1445 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1446 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1447 remote_fio_system_call_allowed ? "" : "not ");
1451 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1452 struct cmd_list_element *remote_show_cmdlist)
1454 add_cmd ("system-call-allowed", no_class,
1455 set_system_call_allowed,
1456 _("Set if the host system(3) call is allowed for the target."),
1457 &remote_set_cmdlist);
1458 add_cmd ("system-call-allowed", no_class,
1459 show_system_call_allowed,
1460 _("Show if the host system(3) call is allowed for the target."),
1461 &remote_show_cmdlist);