1 /* Remote File-I/O communications
3 Copyright (C) 2003-2014 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. */
25 #include "gdb/fileio.h"
28 #include "remote-fileio.h"
29 #include "event-loop.h"
31 #include "filenames.h"
32 #include "filestuff.h"
37 #include <sys/cygwin.h> /* For cygwin_conv_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;
52 static struct async_signal_handler *sigint_fileio_token;
55 remote_fileio_init_fd_map (void)
59 if (!remote_fio_data.fd_map)
61 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
62 remote_fio_data.fd_map_size = 10;
63 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
64 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
65 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
66 for (i = 3; i < 10; ++i)
67 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
73 remote_fileio_resize_fd_map (void)
75 int i = remote_fio_data.fd_map_size;
77 if (!remote_fio_data.fd_map)
78 return remote_fileio_init_fd_map ();
79 remote_fio_data.fd_map_size += 10;
80 remote_fio_data.fd_map =
81 (int *) xrealloc (remote_fio_data.fd_map,
82 remote_fio_data.fd_map_size * sizeof (int));
83 for (; i < remote_fio_data.fd_map_size; i++)
84 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
85 return remote_fio_data.fd_map_size - 10;
89 remote_fileio_next_free_fd (void)
93 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
94 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
96 return remote_fileio_resize_fd_map ();
100 remote_fileio_fd_to_targetfd (int fd)
102 int target_fd = remote_fileio_next_free_fd ();
104 remote_fio_data.fd_map[target_fd] = fd;
109 remote_fileio_map_fd (int target_fd)
111 remote_fileio_init_fd_map ();
112 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
113 return FIO_FD_INVALID;
114 return remote_fio_data.fd_map[target_fd];
118 remote_fileio_close_target_fd (int target_fd)
120 remote_fileio_init_fd_map ();
121 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
122 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
126 remote_fileio_oflags_to_host (long flags)
130 if (flags & FILEIO_O_CREAT)
132 if (flags & FILEIO_O_EXCL)
134 if (flags & FILEIO_O_TRUNC)
136 if (flags & FILEIO_O_APPEND)
138 if (flags & FILEIO_O_RDONLY)
140 if (flags & FILEIO_O_WRONLY)
142 if (flags & FILEIO_O_RDWR)
144 /* On systems supporting binary and text mode, always open files in
153 remote_fileio_mode_to_host (long mode, int open_call)
159 if (mode & FILEIO_S_IFREG)
161 if (mode & FILEIO_S_IFDIR)
163 if (mode & FILEIO_S_IFCHR)
166 if (mode & FILEIO_S_IRUSR)
168 if (mode & FILEIO_S_IWUSR)
170 if (mode & FILEIO_S_IXUSR)
173 if (mode & FILEIO_S_IRGRP)
177 if (mode & FILEIO_S_IWGRP)
181 if (mode & FILEIO_S_IXGRP)
184 if (mode & FILEIO_S_IROTH)
187 if (mode & FILEIO_S_IWOTH)
191 if (mode & FILEIO_S_IXOTH)
198 remote_fileio_mode_to_target (mode_t mode)
203 tmode |= FILEIO_S_IFREG;
205 tmode |= FILEIO_S_IFDIR;
207 tmode |= FILEIO_S_IFCHR;
209 tmode |= FILEIO_S_IRUSR;
211 tmode |= FILEIO_S_IWUSR;
213 tmode |= FILEIO_S_IXUSR;
216 tmode |= FILEIO_S_IRGRP;
220 tmode |= FILEIO_S_IWGRP;
224 tmode |= FILEIO_S_IXGRP;
227 tmode |= FILEIO_S_IROTH;
230 tmode |= FILEIO_S_IWOTH;
234 tmode |= FILEIO_S_IXOTH;
240 remote_fileio_errno_to_target (int error)
247 return FILEIO_ENOENT;
255 return FILEIO_EACCES;
257 return FILEIO_EFAULT;
261 return FILEIO_EEXIST;
263 return FILEIO_ENODEV;
265 return FILEIO_ENOTDIR;
267 return FILEIO_EISDIR;
269 return FILEIO_EINVAL;
271 return FILEIO_ENFILE;
273 return FILEIO_EMFILE;
277 return FILEIO_ENOSPC;
279 return FILEIO_ESPIPE;
283 return FILEIO_ENOSYS;
285 return FILEIO_ENAMETOOLONG;
287 return FILEIO_EUNKNOWN;
291 remote_fileio_seek_flag_to_host (long num, int *flag)
297 case FILEIO_SEEK_SET:
300 case FILEIO_SEEK_CUR:
303 case FILEIO_SEEK_END:
313 remote_fileio_extract_long (char **buf, LONGEST *retlong)
318 if (!buf || !*buf || !**buf || !retlong)
320 c = strchr (*buf, ',');
324 c = strchr (*buf, '\0');
325 while (strchr ("+-", **buf))
331 for (*retlong = 0; **buf; ++*buf)
334 if (**buf >= '0' && **buf <= '9')
335 *retlong += **buf - '0';
336 else if (**buf >= 'a' && **buf <= 'f')
337 *retlong += **buf - 'a' + 10;
338 else if (**buf >= 'A' && **buf <= 'F')
339 *retlong += **buf - 'A' + 10;
349 remote_fileio_extract_int (char **buf, long *retint)
356 ret = remote_fileio_extract_long (buf, &retlong);
358 *retint = (long) retlong;
363 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
368 if (!buf || !*buf || !**buf || !ptrval || !length)
370 c = strchr (*buf, '/');
374 if (remote_fileio_extract_long (buf, &retlong))
376 *ptrval = (CORE_ADDR) retlong;
378 if (remote_fileio_extract_long (buf, &retlong))
380 *length = (int) retlong;
384 /* Convert to big endian. */
386 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
390 for (i = 0; i < bytes; ++i)
391 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
395 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
397 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
401 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
403 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
407 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
409 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
413 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
415 remote_fileio_to_be (num, (char *) fnum, 8);
419 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
421 remote_fileio_to_be (num, (char *) fnum, 8);
425 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
429 /* `st_dev' is set in the calling function. */
430 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
431 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
432 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
433 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
434 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
435 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
436 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
437 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
438 blksize = st->st_blksize;
442 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
443 #if HAVE_STRUCT_STAT_ST_BLOCKS
444 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
446 /* FIXME: This is correct for DJGPP, but other systems that don't
447 have st_blocks, if any, might prefer 512 instead of st_blksize.
448 (eliz, 30-12-2003) */
449 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
453 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
454 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
455 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
459 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
461 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
462 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
465 static int remote_fio_ctrl_c_flag = 0;
466 static int remote_fio_no_longjmp = 0;
468 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
469 static struct sigaction remote_fio_sa;
470 static struct sigaction remote_fio_osa;
472 static void (*remote_fio_ofunc)(int);
476 remote_fileio_sig_init (void)
478 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
479 remote_fio_sa.sa_handler = SIG_IGN;
480 sigemptyset (&remote_fio_sa.sa_mask);
481 remote_fio_sa.sa_flags = 0;
482 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
484 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
489 remote_fileio_sig_set (void (*sigint_func)(int))
491 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
492 remote_fio_sa.sa_handler = sigint_func;
493 sigemptyset (&remote_fio_sa.sa_mask);
494 remote_fio_sa.sa_flags = 0;
495 sigaction (SIGINT, &remote_fio_sa, NULL);
497 signal (SIGINT, sigint_func);
502 remote_fileio_sig_exit (void)
504 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
505 sigaction (SIGINT, &remote_fio_osa, NULL);
507 signal (SIGINT, remote_fio_ofunc);
512 async_remote_fileio_interrupt (gdb_client_data arg)
518 remote_fileio_ctrl_c_signal_handler (int signo)
520 remote_fileio_sig_set (SIG_IGN);
521 remote_fio_ctrl_c_flag = 1;
522 if (!remote_fio_no_longjmp)
523 gdb_call_async_signal_handler (sigint_fileio_token, 1);
524 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
528 remote_fileio_reply (int retcode, int error)
532 remote_fileio_sig_set (SIG_IGN);
539 sprintf (buf + strlen (buf), "%x", retcode);
540 if (error || remote_fio_ctrl_c_flag)
542 if (error && remote_fio_ctrl_c_flag)
543 error = FILEIO_EINTR;
549 sprintf (buf + strlen (buf), ",%x", error);
550 if (remote_fio_ctrl_c_flag)
553 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
558 remote_fileio_ioerror (void)
560 remote_fileio_reply (-1, FILEIO_EIO);
564 remote_fileio_badfd (void)
566 remote_fileio_reply (-1, FILEIO_EBADF);
570 remote_fileio_return_errno (int retcode)
572 remote_fileio_reply (retcode, retcode < 0
573 ? remote_fileio_errno_to_target (errno) : 0);
577 remote_fileio_return_success (int retcode)
579 remote_fileio_reply (retcode, 0);
583 remote_fileio_func_open (char *buf)
593 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
594 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
596 remote_fileio_ioerror ();
599 /* 2. Parameter: open flags */
600 if (remote_fileio_extract_int (&buf, &num))
602 remote_fileio_ioerror ();
605 flags = remote_fileio_oflags_to_host (num);
606 /* 3. Parameter: open mode */
607 if (remote_fileio_extract_int (&buf, &num))
609 remote_fileio_ioerror ();
612 mode = remote_fileio_mode_to_host (num, 1);
614 /* Request pathname. */
615 pathname = alloca (length);
616 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
618 remote_fileio_ioerror ();
622 /* Check if pathname exists and is not a regular file or directory. If so,
623 return an appropriate error code. Same for trying to open directories
625 if (!stat (pathname, &st))
627 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
629 remote_fileio_reply (-1, FILEIO_ENODEV);
632 if (S_ISDIR (st.st_mode)
633 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
635 remote_fileio_reply (-1, FILEIO_EISDIR);
640 remote_fio_no_longjmp = 1;
641 fd = gdb_open_cloexec (pathname, flags, mode);
644 remote_fileio_return_errno (-1);
648 fd = remote_fileio_fd_to_targetfd (fd);
649 remote_fileio_return_success (fd);
653 remote_fileio_func_close (char *buf)
658 /* Parameter: file descriptor */
659 if (remote_fileio_extract_int (&buf, &num))
661 remote_fileio_ioerror ();
664 fd = remote_fileio_map_fd ((int) num);
665 if (fd == FIO_FD_INVALID)
667 remote_fileio_badfd ();
671 remote_fio_no_longjmp = 1;
672 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
673 remote_fileio_return_errno (-1);
674 remote_fileio_close_target_fd ((int) num);
675 remote_fileio_return_success (0);
679 remote_fileio_func_read (char *buf)
687 off_t old_offset, new_offset;
689 /* 1. Parameter: file descriptor */
690 if (remote_fileio_extract_int (&buf, &target_fd))
692 remote_fileio_ioerror ();
695 fd = remote_fileio_map_fd ((int) target_fd);
696 if (fd == FIO_FD_INVALID)
698 remote_fileio_badfd ();
701 /* 2. Parameter: buffer pointer */
702 if (remote_fileio_extract_long (&buf, &lnum))
704 remote_fileio_ioerror ();
707 ptrval = (CORE_ADDR) lnum;
708 /* 3. Parameter: buffer length */
709 if (remote_fileio_extract_int (&buf, &num))
711 remote_fileio_ioerror ();
714 length = (size_t) num;
718 case FIO_FD_CONSOLE_OUT:
719 remote_fileio_badfd ();
721 case FIO_FD_CONSOLE_IN:
723 static char *remaining_buf = NULL;
724 static int remaining_length = 0;
726 buffer = (gdb_byte *) xmalloc (16384);
729 remote_fio_no_longjmp = 1;
730 if (remaining_length > length)
732 memcpy (buffer, remaining_buf, length);
733 memmove (remaining_buf, remaining_buf + length,
734 remaining_length - length);
735 remaining_length -= length;
740 memcpy (buffer, remaining_buf, remaining_length);
741 xfree (remaining_buf);
742 remaining_buf = NULL;
743 ret = remaining_length;
748 /* Windows (at least XP and Server 2003) has difficulty
749 with large reads from consoles. If a handle is
750 backed by a real console device, overly large reads
751 from the handle will fail and set errno == ENOMEM.
752 On a Windows Server 2003 system where I tested,
753 reading 26608 bytes from the console was OK, but
754 anything above 26609 bytes would fail. The limit has
755 been observed to vary on different systems. So, we
756 limit this read to something smaller than that - by a
757 safe margin, in case the limit depends on system
758 resources or version. */
759 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
760 remote_fio_no_longjmp = 1;
761 if (ret > 0 && (size_t)ret > length)
763 remaining_buf = (char *) xmalloc (ret - length);
764 remaining_length = ret - length;
765 memcpy (remaining_buf, buffer + length, remaining_length);
772 buffer = (gdb_byte *) xmalloc (length);
773 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
774 for read() to return -1 even if "some" bytes have been read. It
775 has been corrected in SUSv2 but that doesn't help us much...
776 Therefore a complete solution must check how many bytes have been
777 read on EINTR to return a more reliable value to the target */
778 old_offset = lseek (fd, 0, SEEK_CUR);
779 remote_fio_no_longjmp = 1;
780 ret = read (fd, buffer, length);
781 if (ret < 0 && errno == EINTR)
783 new_offset = lseek (fd, 0, SEEK_CUR);
784 /* If some data has been read, return the number of bytes read.
785 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
786 if (old_offset != new_offset)
787 ret = new_offset - old_offset;
794 errno = target_write_memory (ptrval, buffer, ret);
800 remote_fileio_return_errno (-1);
802 remote_fileio_return_success (ret);
808 remote_fileio_func_write (char *buf)
817 /* 1. Parameter: file descriptor */
818 if (remote_fileio_extract_int (&buf, &target_fd))
820 remote_fileio_ioerror ();
823 fd = remote_fileio_map_fd ((int) target_fd);
824 if (fd == FIO_FD_INVALID)
826 remote_fileio_badfd ();
829 /* 2. Parameter: buffer pointer */
830 if (remote_fileio_extract_long (&buf, &lnum))
832 remote_fileio_ioerror ();
835 ptrval = (CORE_ADDR) lnum;
836 /* 3. Parameter: buffer length */
837 if (remote_fileio_extract_int (&buf, &num))
839 remote_fileio_ioerror ();
842 length = (size_t) num;
844 buffer = (gdb_byte *) xmalloc (length);
845 if (target_read_memory (ptrval, buffer, length) != 0)
848 remote_fileio_ioerror ();
852 remote_fio_no_longjmp = 1;
855 case FIO_FD_CONSOLE_IN:
856 remote_fileio_badfd ();
859 case FIO_FD_CONSOLE_OUT:
860 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
861 (char *) buffer, length);
862 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
866 ret = write (fd, buffer, length);
867 if (ret < 0 && errno == EACCES)
868 errno = EBADF; /* Cygwin returns EACCESS when writing to a
874 remote_fileio_return_errno (-1);
876 remote_fileio_return_success (ret);
882 remote_fileio_func_lseek (char *buf)
889 /* 1. Parameter: file descriptor */
890 if (remote_fileio_extract_int (&buf, &num))
892 remote_fileio_ioerror ();
895 fd = remote_fileio_map_fd ((int) num);
896 if (fd == FIO_FD_INVALID)
898 remote_fileio_badfd ();
901 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
903 remote_fileio_reply (-1, FILEIO_ESPIPE);
907 /* 2. Parameter: offset */
908 if (remote_fileio_extract_long (&buf, &lnum))
910 remote_fileio_ioerror ();
913 offset = (off_t) lnum;
914 /* 3. Parameter: flag */
915 if (remote_fileio_extract_int (&buf, &num))
917 remote_fileio_ioerror ();
920 if (remote_fileio_seek_flag_to_host (num, &flag))
922 remote_fileio_reply (-1, FILEIO_EINVAL);
926 remote_fio_no_longjmp = 1;
927 ret = lseek (fd, offset, flag);
929 if (ret == (off_t) -1)
930 remote_fileio_return_errno (-1);
932 remote_fileio_return_success (ret);
936 remote_fileio_func_rename (char *buf)
938 CORE_ADDR old_ptr, new_ptr;
939 int old_len, new_len;
940 char *oldpath, *newpath;
942 struct stat ost, nst;
944 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
945 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
947 remote_fileio_ioerror ();
951 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
952 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
954 remote_fileio_ioerror ();
958 /* Request oldpath using 'm' packet */
959 oldpath = alloca (old_len);
960 if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
962 remote_fileio_ioerror ();
966 /* Request newpath using 'm' packet */
967 newpath = alloca (new_len);
968 if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
970 remote_fileio_ioerror ();
974 /* Only operate on regular files and directories. */
975 of = stat (oldpath, &ost);
976 nf = stat (newpath, &nst);
977 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
978 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
980 remote_fileio_reply (-1, FILEIO_EACCES);
984 remote_fio_no_longjmp = 1;
985 ret = rename (oldpath, newpath);
989 /* Special case: newpath is a non-empty directory. Some systems
990 return ENOTEMPTY, some return EEXIST. We coerce that to be
992 if (errno == ENOTEMPTY)
995 /* Workaround some Cygwin problems with correct errnos. */
998 if (!of && !nf && S_ISDIR (nst.st_mode))
1000 if (S_ISREG (ost.st_mode))
1004 char oldfullpath[PATH_MAX];
1005 char newfullpath[PATH_MAX];
1008 cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
1010 cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
1012 len = strlen (oldfullpath);
1013 if (IS_DIR_SEPARATOR (newfullpath[len])
1014 && !filename_ncmp (oldfullpath, newfullpath, len))
1023 remote_fileio_return_errno (-1);
1026 remote_fileio_return_success (ret);
1030 remote_fileio_func_unlink (char *buf)
1038 /* Parameter: Ptr to pathname / length incl. trailing zero */
1039 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1041 remote_fileio_ioerror ();
1044 /* Request pathname using 'm' packet */
1045 pathname = alloca (length);
1046 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
1048 remote_fileio_ioerror ();
1052 /* Only operate on regular files (and directories, which allows to return
1053 the correct return code). */
1054 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1056 remote_fileio_reply (-1, FILEIO_ENODEV);
1060 remote_fio_no_longjmp = 1;
1061 ret = unlink (pathname);
1064 remote_fileio_return_errno (-1);
1066 remote_fileio_return_success (ret);
1070 remote_fileio_func_stat (char *buf)
1072 CORE_ADDR statptr, nameptr;
1073 int ret, namelength;
1077 struct fio_stat fst;
1079 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1080 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1082 remote_fileio_ioerror ();
1086 /* 2. Parameter: Ptr to struct stat */
1087 if (remote_fileio_extract_long (&buf, &lnum))
1089 remote_fileio_ioerror ();
1092 statptr = (CORE_ADDR) lnum;
1094 /* Request pathname using 'm' packet */
1095 pathname = alloca (namelength);
1096 if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
1098 remote_fileio_ioerror ();
1102 remote_fio_no_longjmp = 1;
1103 ret = stat (pathname, &st);
1107 remote_fileio_return_errno (-1);
1110 /* Only operate on regular files and directories. */
1111 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1113 remote_fileio_reply (-1, FILEIO_EACCES);
1118 remote_fileio_to_fio_stat (&st, &fst);
1119 remote_fileio_to_fio_uint (0, fst.fst_dev);
1121 errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
1124 remote_fileio_return_errno (-1);
1128 remote_fileio_return_success (ret);
1132 remote_fileio_func_fstat (char *buf)
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 memset (&st, 0, sizeof (st));
1167 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1170 st.st_uid = getuid ();
1173 st.st_gid = getgid ();
1175 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1176 st.st_blksize = 512;
1178 #if HAVE_STRUCT_STAT_ST_BLOCKS
1181 if (!gettimeofday (&tv, NULL))
1182 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1184 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1188 ret = fstat (fd, &st);
1192 remote_fileio_return_errno (-1);
1197 remote_fileio_to_fio_stat (&st, &fst);
1199 errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1202 remote_fileio_return_errno (-1);
1206 remote_fileio_return_success (ret);
1210 remote_fileio_func_gettimeofday (char *buf)
1216 struct fio_timeval ftv;
1218 /* 1. Parameter: struct timeval pointer */
1219 if (remote_fileio_extract_long (&buf, &lnum))
1221 remote_fileio_ioerror ();
1224 ptrval = (CORE_ADDR) lnum;
1225 /* 2. Parameter: some pointer value... */
1226 if (remote_fileio_extract_long (&buf, &lnum))
1228 remote_fileio_ioerror ();
1231 /* ...which has to be NULL. */
1234 remote_fileio_reply (-1, FILEIO_EINVAL);
1238 remote_fio_no_longjmp = 1;
1239 ret = gettimeofday (&tv, NULL);
1243 remote_fileio_return_errno (-1);
1249 remote_fileio_to_fio_timeval (&tv, &ftv);
1251 errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1254 remote_fileio_return_errno (-1);
1258 remote_fileio_return_success (ret);
1262 remote_fileio_func_isatty (char *buf)
1267 /* Parameter: file descriptor */
1268 if (remote_fileio_extract_int (&buf, &target_fd))
1270 remote_fileio_ioerror ();
1273 remote_fio_no_longjmp = 1;
1274 fd = remote_fileio_map_fd ((int) target_fd);
1275 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1276 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1280 remote_fileio_func_system (char *buf)
1284 char *cmdline = NULL;
1286 /* Parameter: Ptr to commandline / length incl. trailing zero */
1287 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1289 remote_fileio_ioerror ();
1295 /* Request commandline using 'm' packet */
1296 cmdline = alloca (length);
1297 if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
1299 remote_fileio_ioerror ();
1304 /* Check if system(3) has been explicitely allowed using the
1305 `set remote system-call-allowed 1' command. If length is 0,
1306 indicating a NULL parameter to the system call, return zero to
1307 indicate a shell is not available. Otherwise fail with EPERM. */
1308 if (!remote_fio_system_call_allowed)
1311 remote_fileio_return_success (0);
1313 remote_fileio_reply (-1, FILEIO_EPERM);
1317 remote_fio_no_longjmp = 1;
1318 ret = system (cmdline);
1321 remote_fileio_return_success (ret);
1323 remote_fileio_return_errno (-1);
1325 remote_fileio_return_success (WEXITSTATUS (ret));
1330 void (*func)(char *);
1331 } remote_fio_func_map[] = {
1332 { "open", remote_fileio_func_open },
1333 { "close", remote_fileio_func_close },
1334 { "read", remote_fileio_func_read },
1335 { "write", remote_fileio_func_write },
1336 { "lseek", remote_fileio_func_lseek },
1337 { "rename", remote_fileio_func_rename },
1338 { "unlink", remote_fileio_func_unlink },
1339 { "stat", remote_fileio_func_stat },
1340 { "fstat", remote_fileio_func_fstat },
1341 { "gettimeofday", remote_fileio_func_gettimeofday },
1342 { "isatty", remote_fileio_func_isatty },
1343 { "system", remote_fileio_func_system },
1348 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1350 char *buf = buf_arg;
1354 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1356 c = strchr (++buf, ',');
1360 c = strchr (buf, '\0');
1361 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1362 if (!strcmp (remote_fio_func_map[idx].name, buf))
1364 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1365 return RETURN_ERROR;
1366 remote_fio_func_map[idx].func (c);
1370 /* Close any open descriptors, and reinitialize the file mapping. */
1373 remote_fileio_reset (void)
1377 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1379 int fd = remote_fio_data.fd_map[ix];
1384 if (remote_fio_data.fd_map)
1386 xfree (remote_fio_data.fd_map);
1387 remote_fio_data.fd_map = NULL;
1388 remote_fio_data.fd_map_size = 0;
1392 /* Handle a file I/O request. BUF points to the packet containing the
1393 request. CTRLC_PENDING_P should be nonzero if the target has not
1394 acknowledged the Ctrl-C sent asynchronously earlier. */
1397 remote_fileio_request (char *buf, int ctrlc_pending_p)
1401 remote_fileio_sig_init ();
1403 if (ctrlc_pending_p)
1405 /* If the target hasn't responded to the Ctrl-C sent
1406 asynchronously earlier, take this opportunity to send the
1407 Ctrl-C synchronously. */
1408 remote_fio_ctrl_c_flag = 1;
1409 remote_fio_no_longjmp = 0;
1410 remote_fileio_reply (-1, FILEIO_EINTR);
1414 remote_fio_ctrl_c_flag = 0;
1415 remote_fio_no_longjmp = 0;
1417 ex = catch_exceptions (current_uiout,
1418 do_remote_fileio_request, (void *)buf,
1423 remote_fileio_reply (-1, FILEIO_ENOSYS);
1426 remote_fileio_reply (-1, FILEIO_EINTR);
1433 remote_fileio_sig_exit ();
1437 set_system_call_allowed (char *args, int from_tty)
1442 int val = strtoul (args, &arg_end, 10);
1444 if (*args && *arg_end == '\0')
1446 remote_fio_system_call_allowed = !!val;
1450 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1454 show_system_call_allowed (char *args, int from_tty)
1457 error (_("Garbage after \"show remote "
1458 "system-call-allowed\" command: `%s'"), args);
1459 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1460 remote_fio_system_call_allowed ? "" : "not ");
1464 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1465 struct cmd_list_element *remote_show_cmdlist)
1467 sigint_fileio_token =
1468 create_async_signal_handler (async_remote_fileio_interrupt, NULL);
1470 add_cmd ("system-call-allowed", no_class,
1471 set_system_call_allowed,
1472 _("Set if the host system(3) call is allowed for the target."),
1473 &remote_set_cmdlist);
1474 add_cmd ("system-call-allowed", no_class,
1475 show_system_call_allowed,
1476 _("Show if the host system(3) call is allowed for the target."),
1477 &remote_show_cmdlist);