1 /* Remote File-I/O communications
3 Copyright (C) 2003-2013 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"
31 #include "event-loop.h"
33 #include "filenames.h"
34 #include "filestuff.h"
39 #include <sys/cygwin.h> /* For cygwin_conv_path. */
48 #define FIO_FD_INVALID -1
49 #define FIO_FD_CONSOLE_IN -2
50 #define FIO_FD_CONSOLE_OUT -3
52 static int remote_fio_system_call_allowed = 0;
54 static struct async_signal_handler *sigint_fileio_token;
57 remote_fileio_init_fd_map (void)
61 if (!remote_fio_data.fd_map)
63 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
64 remote_fio_data.fd_map_size = 10;
65 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
66 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
67 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
68 for (i = 3; i < 10; ++i)
69 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
75 remote_fileio_resize_fd_map (void)
77 int i = remote_fio_data.fd_map_size;
79 if (!remote_fio_data.fd_map)
80 return remote_fileio_init_fd_map ();
81 remote_fio_data.fd_map_size += 10;
82 remote_fio_data.fd_map =
83 (int *) xrealloc (remote_fio_data.fd_map,
84 remote_fio_data.fd_map_size * sizeof (int));
85 for (; i < remote_fio_data.fd_map_size; i++)
86 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
87 return remote_fio_data.fd_map_size - 10;
91 remote_fileio_next_free_fd (void)
95 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
96 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
98 return remote_fileio_resize_fd_map ();
102 remote_fileio_fd_to_targetfd (int fd)
104 int target_fd = remote_fileio_next_free_fd ();
106 remote_fio_data.fd_map[target_fd] = fd;
111 remote_fileio_map_fd (int target_fd)
113 remote_fileio_init_fd_map ();
114 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
115 return FIO_FD_INVALID;
116 return remote_fio_data.fd_map[target_fd];
120 remote_fileio_close_target_fd (int target_fd)
122 remote_fileio_init_fd_map ();
123 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
124 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
128 remote_fileio_oflags_to_host (long flags)
132 if (flags & FILEIO_O_CREAT)
134 if (flags & FILEIO_O_EXCL)
136 if (flags & FILEIO_O_TRUNC)
138 if (flags & FILEIO_O_APPEND)
140 if (flags & FILEIO_O_RDONLY)
142 if (flags & FILEIO_O_WRONLY)
144 if (flags & FILEIO_O_RDWR)
146 /* On systems supporting binary and text mode, always open files in
155 remote_fileio_mode_to_host (long mode, int open_call)
161 if (mode & FILEIO_S_IFREG)
163 if (mode & FILEIO_S_IFDIR)
165 if (mode & FILEIO_S_IFCHR)
168 if (mode & FILEIO_S_IRUSR)
170 if (mode & FILEIO_S_IWUSR)
172 if (mode & FILEIO_S_IXUSR)
175 if (mode & FILEIO_S_IRGRP)
179 if (mode & FILEIO_S_IWGRP)
183 if (mode & FILEIO_S_IXGRP)
186 if (mode & FILEIO_S_IROTH)
189 if (mode & FILEIO_S_IWOTH)
193 if (mode & FILEIO_S_IXOTH)
200 remote_fileio_mode_to_target (mode_t mode)
205 tmode |= FILEIO_S_IFREG;
207 tmode |= FILEIO_S_IFDIR;
209 tmode |= FILEIO_S_IFCHR;
211 tmode |= FILEIO_S_IRUSR;
213 tmode |= FILEIO_S_IWUSR;
215 tmode |= FILEIO_S_IXUSR;
218 tmode |= FILEIO_S_IRGRP;
222 tmode |= FILEIO_S_IWGRP;
226 tmode |= FILEIO_S_IXGRP;
229 tmode |= FILEIO_S_IROTH;
232 tmode |= FILEIO_S_IWOTH;
236 tmode |= FILEIO_S_IXOTH;
242 remote_fileio_errno_to_target (int error)
249 return FILEIO_ENOENT;
257 return FILEIO_EACCES;
259 return FILEIO_EFAULT;
263 return FILEIO_EEXIST;
265 return FILEIO_ENODEV;
267 return FILEIO_ENOTDIR;
269 return FILEIO_EISDIR;
271 return FILEIO_EINVAL;
273 return FILEIO_ENFILE;
275 return FILEIO_EMFILE;
279 return FILEIO_ENOSPC;
281 return FILEIO_ESPIPE;
285 return FILEIO_ENOSYS;
287 return FILEIO_ENAMETOOLONG;
289 return FILEIO_EUNKNOWN;
293 remote_fileio_seek_flag_to_host (long num, int *flag)
299 case FILEIO_SEEK_SET:
302 case FILEIO_SEEK_CUR:
305 case FILEIO_SEEK_END:
315 remote_fileio_extract_long (char **buf, LONGEST *retlong)
320 if (!buf || !*buf || !**buf || !retlong)
322 c = strchr (*buf, ',');
326 c = strchr (*buf, '\0');
327 while (strchr ("+-", **buf))
333 for (*retlong = 0; **buf; ++*buf)
336 if (**buf >= '0' && **buf <= '9')
337 *retlong += **buf - '0';
338 else if (**buf >= 'a' && **buf <= 'f')
339 *retlong += **buf - 'a' + 10;
340 else if (**buf >= 'A' && **buf <= 'F')
341 *retlong += **buf - 'A' + 10;
351 remote_fileio_extract_int (char **buf, long *retint)
358 ret = remote_fileio_extract_long (buf, &retlong);
360 *retint = (long) retlong;
365 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
370 if (!buf || !*buf || !**buf || !ptrval || !length)
372 c = strchr (*buf, '/');
376 if (remote_fileio_extract_long (buf, &retlong))
378 *ptrval = (CORE_ADDR) retlong;
380 if (remote_fileio_extract_long (buf, &retlong))
382 *length = (int) retlong;
386 /* Convert to big endian. */
388 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
392 for (i = 0; i < bytes; ++i)
393 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
397 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
399 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
403 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
405 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
409 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
411 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
415 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
417 remote_fileio_to_be (num, (char *) fnum, 8);
421 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
423 remote_fileio_to_be (num, (char *) fnum, 8);
427 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
431 /* `st_dev' is set in the calling function. */
432 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
433 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
434 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
435 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
436 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
437 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
438 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
439 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
440 blksize = st->st_blksize;
444 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
445 #if HAVE_STRUCT_STAT_ST_BLOCKS
446 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
448 /* FIXME: This is correct for DJGPP, but other systems that don't
449 have st_blocks, if any, might prefer 512 instead of st_blksize.
450 (eliz, 30-12-2003) */
451 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
455 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
456 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
457 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
461 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
463 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
464 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
467 static int remote_fio_ctrl_c_flag = 0;
468 static int remote_fio_no_longjmp = 0;
470 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
471 static struct sigaction remote_fio_sa;
472 static struct sigaction remote_fio_osa;
474 static void (*remote_fio_ofunc)(int);
478 remote_fileio_sig_init (void)
480 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
481 remote_fio_sa.sa_handler = SIG_IGN;
482 sigemptyset (&remote_fio_sa.sa_mask);
483 remote_fio_sa.sa_flags = 0;
484 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
486 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
491 remote_fileio_sig_set (void (*sigint_func)(int))
493 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
494 remote_fio_sa.sa_handler = sigint_func;
495 sigemptyset (&remote_fio_sa.sa_mask);
496 remote_fio_sa.sa_flags = 0;
497 sigaction (SIGINT, &remote_fio_sa, NULL);
499 signal (SIGINT, sigint_func);
504 remote_fileio_sig_exit (void)
506 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
507 sigaction (SIGINT, &remote_fio_osa, NULL);
509 signal (SIGINT, remote_fio_ofunc);
514 async_remote_fileio_interrupt (gdb_client_data arg)
516 deprecated_throw_reason (RETURN_QUIT);
520 remote_fileio_ctrl_c_signal_handler (int signo)
522 remote_fileio_sig_set (SIG_IGN);
523 remote_fio_ctrl_c_flag = 1;
524 if (!remote_fio_no_longjmp)
525 gdb_call_async_signal_handler (sigint_fileio_token, 1);
526 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
530 remote_fileio_reply (int retcode, int error)
534 remote_fileio_sig_set (SIG_IGN);
541 sprintf (buf + strlen (buf), "%x", retcode);
542 if (error || remote_fio_ctrl_c_flag)
544 if (error && remote_fio_ctrl_c_flag)
545 error = FILEIO_EINTR;
551 sprintf (buf + strlen (buf), ",%x", error);
552 if (remote_fio_ctrl_c_flag)
555 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
560 remote_fileio_ioerror (void)
562 remote_fileio_reply (-1, FILEIO_EIO);
566 remote_fileio_badfd (void)
568 remote_fileio_reply (-1, FILEIO_EBADF);
572 remote_fileio_return_errno (int retcode)
574 remote_fileio_reply (retcode, retcode < 0
575 ? remote_fileio_errno_to_target (errno) : 0);
579 remote_fileio_return_success (int retcode)
581 remote_fileio_reply (retcode, 0);
585 remote_fileio_func_open (char *buf)
595 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
596 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
598 remote_fileio_ioerror ();
601 /* 2. Parameter: open flags */
602 if (remote_fileio_extract_int (&buf, &num))
604 remote_fileio_ioerror ();
607 flags = remote_fileio_oflags_to_host (num);
608 /* 3. Parameter: open mode */
609 if (remote_fileio_extract_int (&buf, &num))
611 remote_fileio_ioerror ();
614 mode = remote_fileio_mode_to_host (num, 1);
616 /* Request pathname. */
617 pathname = alloca (length);
618 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
620 remote_fileio_ioerror ();
624 /* Check if pathname exists and is not a regular file or directory. If so,
625 return an appropriate error code. Same for trying to open directories
627 if (!stat (pathname, &st))
629 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
631 remote_fileio_reply (-1, FILEIO_ENODEV);
634 if (S_ISDIR (st.st_mode)
635 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
637 remote_fileio_reply (-1, FILEIO_EISDIR);
642 remote_fio_no_longjmp = 1;
643 fd = gdb_open_cloexec (pathname, flags, mode);
646 remote_fileio_return_errno (-1);
650 fd = remote_fileio_fd_to_targetfd (fd);
651 remote_fileio_return_success (fd);
655 remote_fileio_func_close (char *buf)
660 /* Parameter: file descriptor */
661 if (remote_fileio_extract_int (&buf, &num))
663 remote_fileio_ioerror ();
666 fd = remote_fileio_map_fd ((int) num);
667 if (fd == FIO_FD_INVALID)
669 remote_fileio_badfd ();
673 remote_fio_no_longjmp = 1;
674 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
675 remote_fileio_return_errno (-1);
676 remote_fileio_close_target_fd ((int) num);
677 remote_fileio_return_success (0);
681 remote_fileio_func_read (char *buf)
689 off_t old_offset, new_offset;
691 /* 1. Parameter: file descriptor */
692 if (remote_fileio_extract_int (&buf, &target_fd))
694 remote_fileio_ioerror ();
697 fd = remote_fileio_map_fd ((int) target_fd);
698 if (fd == FIO_FD_INVALID)
700 remote_fileio_badfd ();
703 /* 2. Parameter: buffer pointer */
704 if (remote_fileio_extract_long (&buf, &lnum))
706 remote_fileio_ioerror ();
709 ptrval = (CORE_ADDR) lnum;
710 /* 3. Parameter: buffer length */
711 if (remote_fileio_extract_int (&buf, &num))
713 remote_fileio_ioerror ();
716 length = (size_t) num;
720 case FIO_FD_CONSOLE_OUT:
721 remote_fileio_badfd ();
723 case FIO_FD_CONSOLE_IN:
725 static char *remaining_buf = NULL;
726 static int remaining_length = 0;
728 buffer = (gdb_byte *) xmalloc (16384);
731 remote_fio_no_longjmp = 1;
732 if (remaining_length > length)
734 memcpy (buffer, remaining_buf, length);
735 memmove (remaining_buf, remaining_buf + length,
736 remaining_length - length);
737 remaining_length -= length;
742 memcpy (buffer, remaining_buf, remaining_length);
743 xfree (remaining_buf);
744 remaining_buf = NULL;
745 ret = remaining_length;
750 /* Windows (at least XP and Server 2003) has difficulty
751 with large reads from consoles. If a handle is
752 backed by a real console device, overly large reads
753 from the handle will fail and set errno == ENOMEM.
754 On a Windows Server 2003 system where I tested,
755 reading 26608 bytes from the console was OK, but
756 anything above 26609 bytes would fail. The limit has
757 been observed to vary on different systems. So, we
758 limit this read to something smaller than that - by a
759 safe margin, in case the limit depends on system
760 resources or version. */
761 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
762 remote_fio_no_longjmp = 1;
763 if (ret > 0 && (size_t)ret > length)
765 remaining_buf = (char *) xmalloc (ret - length);
766 remaining_length = ret - length;
767 memcpy (remaining_buf, buffer + length, remaining_length);
774 buffer = (gdb_byte *) xmalloc (length);
775 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
776 for read() to return -1 even if "some" bytes have been read. It
777 has been corrected in SUSv2 but that doesn't help us much...
778 Therefore a complete solution must check how many bytes have been
779 read on EINTR to return a more reliable value to the target */
780 old_offset = lseek (fd, 0, SEEK_CUR);
781 remote_fio_no_longjmp = 1;
782 ret = read (fd, buffer, length);
783 if (ret < 0 && errno == EINTR)
785 new_offset = lseek (fd, 0, SEEK_CUR);
786 /* If some data has been read, return the number of bytes read.
787 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
788 if (old_offset != new_offset)
789 ret = new_offset - old_offset;
796 errno = target_write_memory (ptrval, buffer, ret);
802 remote_fileio_return_errno (-1);
804 remote_fileio_return_success (ret);
810 remote_fileio_func_write (char *buf)
819 /* 1. Parameter: file descriptor */
820 if (remote_fileio_extract_int (&buf, &target_fd))
822 remote_fileio_ioerror ();
825 fd = remote_fileio_map_fd ((int) target_fd);
826 if (fd == FIO_FD_INVALID)
828 remote_fileio_badfd ();
831 /* 2. Parameter: buffer pointer */
832 if (remote_fileio_extract_long (&buf, &lnum))
834 remote_fileio_ioerror ();
837 ptrval = (CORE_ADDR) lnum;
838 /* 3. Parameter: buffer length */
839 if (remote_fileio_extract_int (&buf, &num))
841 remote_fileio_ioerror ();
844 length = (size_t) num;
846 buffer = (gdb_byte *) xmalloc (length);
847 if (target_read_memory (ptrval, buffer, length) != 0)
850 remote_fileio_ioerror ();
854 remote_fio_no_longjmp = 1;
857 case FIO_FD_CONSOLE_IN:
858 remote_fileio_badfd ();
861 case FIO_FD_CONSOLE_OUT:
862 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
863 (char *) buffer, length);
864 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
868 ret = write (fd, buffer, length);
869 if (ret < 0 && errno == EACCES)
870 errno = EBADF; /* Cygwin returns EACCESS when writing to a
876 remote_fileio_return_errno (-1);
878 remote_fileio_return_success (ret);
884 remote_fileio_func_lseek (char *buf)
891 /* 1. Parameter: file descriptor */
892 if (remote_fileio_extract_int (&buf, &num))
894 remote_fileio_ioerror ();
897 fd = remote_fileio_map_fd ((int) num);
898 if (fd == FIO_FD_INVALID)
900 remote_fileio_badfd ();
903 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
905 remote_fileio_reply (-1, FILEIO_ESPIPE);
909 /* 2. Parameter: offset */
910 if (remote_fileio_extract_long (&buf, &lnum))
912 remote_fileio_ioerror ();
915 offset = (off_t) lnum;
916 /* 3. Parameter: flag */
917 if (remote_fileio_extract_int (&buf, &num))
919 remote_fileio_ioerror ();
922 if (remote_fileio_seek_flag_to_host (num, &flag))
924 remote_fileio_reply (-1, FILEIO_EINVAL);
928 remote_fio_no_longjmp = 1;
929 ret = lseek (fd, offset, flag);
931 if (ret == (off_t) -1)
932 remote_fileio_return_errno (-1);
934 remote_fileio_return_success (ret);
938 remote_fileio_func_rename (char *buf)
940 CORE_ADDR old_ptr, new_ptr;
941 int old_len, new_len;
942 char *oldpath, *newpath;
944 struct stat ost, nst;
946 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
947 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
949 remote_fileio_ioerror ();
953 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
954 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
956 remote_fileio_ioerror ();
960 /* Request oldpath using 'm' packet */
961 oldpath = alloca (old_len);
962 if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
964 remote_fileio_ioerror ();
968 /* Request newpath using 'm' packet */
969 newpath = alloca (new_len);
970 if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
972 remote_fileio_ioerror ();
976 /* Only operate on regular files and directories. */
977 of = stat (oldpath, &ost);
978 nf = stat (newpath, &nst);
979 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
980 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
982 remote_fileio_reply (-1, FILEIO_EACCES);
986 remote_fio_no_longjmp = 1;
987 ret = rename (oldpath, newpath);
991 /* Special case: newpath is a non-empty directory. Some systems
992 return ENOTEMPTY, some return EEXIST. We coerce that to be
994 if (errno == ENOTEMPTY)
997 /* Workaround some Cygwin problems with correct errnos. */
1000 if (!of && !nf && S_ISDIR (nst.st_mode))
1002 if (S_ISREG (ost.st_mode))
1006 char oldfullpath[PATH_MAX];
1007 char newfullpath[PATH_MAX];
1010 cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
1012 cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
1014 len = strlen (oldfullpath);
1015 if (IS_DIR_SEPARATOR (newfullpath[len])
1016 && !filename_ncmp (oldfullpath, newfullpath, len))
1025 remote_fileio_return_errno (-1);
1028 remote_fileio_return_success (ret);
1032 remote_fileio_func_unlink (char *buf)
1040 /* Parameter: Ptr to pathname / length incl. trailing zero */
1041 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1043 remote_fileio_ioerror ();
1046 /* Request pathname using 'm' packet */
1047 pathname = alloca (length);
1048 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
1050 remote_fileio_ioerror ();
1054 /* Only operate on regular files (and directories, which allows to return
1055 the correct return code). */
1056 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1058 remote_fileio_reply (-1, FILEIO_ENODEV);
1062 remote_fio_no_longjmp = 1;
1063 ret = unlink (pathname);
1066 remote_fileio_return_errno (-1);
1068 remote_fileio_return_success (ret);
1072 remote_fileio_func_stat (char *buf)
1074 CORE_ADDR statptr, nameptr;
1075 int ret, namelength;
1079 struct fio_stat fst;
1081 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1082 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1084 remote_fileio_ioerror ();
1088 /* 2. Parameter: Ptr to struct stat */
1089 if (remote_fileio_extract_long (&buf, &lnum))
1091 remote_fileio_ioerror ();
1094 statptr = (CORE_ADDR) lnum;
1096 /* Request pathname using 'm' packet */
1097 pathname = alloca (namelength);
1098 if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
1100 remote_fileio_ioerror ();
1104 remote_fio_no_longjmp = 1;
1105 ret = stat (pathname, &st);
1109 remote_fileio_return_errno (-1);
1112 /* Only operate on regular files and directories. */
1113 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1115 remote_fileio_reply (-1, FILEIO_EACCES);
1120 remote_fileio_to_fio_stat (&st, &fst);
1121 remote_fileio_to_fio_uint (0, fst.fst_dev);
1123 errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
1126 remote_fileio_return_errno (-1);
1130 remote_fileio_return_success (ret);
1134 remote_fileio_func_fstat (char *buf)
1141 struct fio_stat fst;
1144 /* 1. Parameter: file descriptor */
1145 if (remote_fileio_extract_int (&buf, &target_fd))
1147 remote_fileio_ioerror ();
1150 fd = remote_fileio_map_fd ((int) target_fd);
1151 if (fd == FIO_FD_INVALID)
1153 remote_fileio_badfd ();
1156 /* 2. Parameter: Ptr to struct stat */
1157 if (remote_fileio_extract_long (&buf, &lnum))
1159 remote_fileio_ioerror ();
1162 ptrval = (CORE_ADDR) lnum;
1164 remote_fio_no_longjmp = 1;
1165 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1167 remote_fileio_to_fio_uint (1, fst.fst_dev);
1168 memset (&st, 0, sizeof (st));
1169 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1172 st.st_uid = getuid ();
1175 st.st_gid = getgid ();
1177 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1178 st.st_blksize = 512;
1180 #if HAVE_STRUCT_STAT_ST_BLOCKS
1183 if (!gettimeofday (&tv, NULL))
1184 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1186 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1190 ret = fstat (fd, &st);
1194 remote_fileio_return_errno (-1);
1199 remote_fileio_to_fio_stat (&st, &fst);
1201 errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1204 remote_fileio_return_errno (-1);
1208 remote_fileio_return_success (ret);
1212 remote_fileio_func_gettimeofday (char *buf)
1218 struct fio_timeval ftv;
1220 /* 1. Parameter: struct timeval pointer */
1221 if (remote_fileio_extract_long (&buf, &lnum))
1223 remote_fileio_ioerror ();
1226 ptrval = (CORE_ADDR) lnum;
1227 /* 2. Parameter: some pointer value... */
1228 if (remote_fileio_extract_long (&buf, &lnum))
1230 remote_fileio_ioerror ();
1233 /* ...which has to be NULL. */
1236 remote_fileio_reply (-1, FILEIO_EINVAL);
1240 remote_fio_no_longjmp = 1;
1241 ret = gettimeofday (&tv, NULL);
1245 remote_fileio_return_errno (-1);
1251 remote_fileio_to_fio_timeval (&tv, &ftv);
1253 errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1256 remote_fileio_return_errno (-1);
1260 remote_fileio_return_success (ret);
1264 remote_fileio_func_isatty (char *buf)
1269 /* Parameter: file descriptor */
1270 if (remote_fileio_extract_int (&buf, &target_fd))
1272 remote_fileio_ioerror ();
1275 remote_fio_no_longjmp = 1;
1276 fd = remote_fileio_map_fd ((int) target_fd);
1277 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1278 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1282 remote_fileio_func_system (char *buf)
1286 char *cmdline = NULL;
1288 /* Parameter: Ptr to commandline / length incl. trailing zero */
1289 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1291 remote_fileio_ioerror ();
1297 /* Request commandline using 'm' packet */
1298 cmdline = alloca (length);
1299 if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
1301 remote_fileio_ioerror ();
1306 /* Check if system(3) has been explicitely allowed using the
1307 `set remote system-call-allowed 1' command. If length is 0,
1308 indicating a NULL parameter to the system call, return zero to
1309 indicate a shell is not available. Otherwise fail with EPERM. */
1310 if (!remote_fio_system_call_allowed)
1313 remote_fileio_return_success (0);
1315 remote_fileio_reply (-1, FILEIO_EPERM);
1319 remote_fio_no_longjmp = 1;
1320 ret = system (cmdline);
1323 remote_fileio_return_success (ret);
1325 remote_fileio_return_errno (-1);
1327 remote_fileio_return_success (WEXITSTATUS (ret));
1332 void (*func)(char *);
1333 } remote_fio_func_map[] = {
1334 { "open", remote_fileio_func_open },
1335 { "close", remote_fileio_func_close },
1336 { "read", remote_fileio_func_read },
1337 { "write", remote_fileio_func_write },
1338 { "lseek", remote_fileio_func_lseek },
1339 { "rename", remote_fileio_func_rename },
1340 { "unlink", remote_fileio_func_unlink },
1341 { "stat", remote_fileio_func_stat },
1342 { "fstat", remote_fileio_func_fstat },
1343 { "gettimeofday", remote_fileio_func_gettimeofday },
1344 { "isatty", remote_fileio_func_isatty },
1345 { "system", remote_fileio_func_system },
1350 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1352 char *buf = buf_arg;
1356 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1358 c = strchr (++buf, ',');
1362 c = strchr (buf, '\0');
1363 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1364 if (!strcmp (remote_fio_func_map[idx].name, buf))
1366 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1367 return RETURN_ERROR;
1368 remote_fio_func_map[idx].func (c);
1372 /* Close any open descriptors, and reinitialize the file mapping. */
1375 remote_fileio_reset (void)
1379 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1381 int fd = remote_fio_data.fd_map[ix];
1386 if (remote_fio_data.fd_map)
1388 xfree (remote_fio_data.fd_map);
1389 remote_fio_data.fd_map = NULL;
1390 remote_fio_data.fd_map_size = 0;
1394 /* Handle a file I/O request. BUF points to the packet containing the
1395 request. CTRLC_PENDING_P should be nonzero if the target has not
1396 acknowledged the Ctrl-C sent asynchronously earlier. */
1399 remote_fileio_request (char *buf, int ctrlc_pending_p)
1403 remote_fileio_sig_init ();
1405 if (ctrlc_pending_p)
1407 /* If the target hasn't responded to the Ctrl-C sent
1408 asynchronously earlier, take this opportunity to send the
1409 Ctrl-C synchronously. */
1410 remote_fio_ctrl_c_flag = 1;
1411 remote_fio_no_longjmp = 0;
1412 remote_fileio_reply (-1, FILEIO_EINTR);
1416 remote_fio_ctrl_c_flag = 0;
1417 remote_fio_no_longjmp = 0;
1419 ex = catch_exceptions (current_uiout,
1420 do_remote_fileio_request, (void *)buf,
1425 remote_fileio_reply (-1, FILEIO_ENOSYS);
1428 remote_fileio_reply (-1, FILEIO_EINTR);
1435 remote_fileio_sig_exit ();
1439 set_system_call_allowed (char *args, int from_tty)
1444 int val = strtoul (args, &arg_end, 10);
1446 if (*args && *arg_end == '\0')
1448 remote_fio_system_call_allowed = !!val;
1452 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1456 show_system_call_allowed (char *args, int from_tty)
1459 error (_("Garbage after \"show remote "
1460 "system-call-allowed\" command: `%s'"), args);
1461 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1462 remote_fio_system_call_allowed ? "" : "not ");
1466 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1467 struct cmd_list_element *remote_show_cmdlist)
1469 sigint_fileio_token =
1470 create_async_signal_handler (async_remote_fileio_interrupt, NULL);
1472 add_cmd ("system-call-allowed", no_class,
1473 set_system_call_allowed,
1474 _("Set if the host system(3) call is allowed for the target."),
1475 &remote_set_cmdlist);
1476 add_cmd ("system-call-allowed", no_class,
1477 show_system_call_allowed,
1478 _("Show if the host system(3) call is allowed for the target."),
1479 &remote_show_cmdlist);