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 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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)
166 if (mode & FILEIO_S_IRGRP)
170 if (mode & FILEIO_S_IWGRP)
174 if (mode & FILEIO_S_IXGRP)
177 if (mode & FILEIO_S_IROTH)
180 if (mode & FILEIO_S_IWOTH)
184 if (mode & FILEIO_S_IXOTH)
191 remote_fileio_mode_to_target (mode_t mode)
196 tmode |= FILEIO_S_IFREG;
198 tmode |= FILEIO_S_IFDIR;
200 tmode |= FILEIO_S_IFCHR;
202 tmode |= FILEIO_S_IRUSR;
204 tmode |= FILEIO_S_IWUSR;
206 tmode |= FILEIO_S_IXUSR;
209 tmode |= FILEIO_S_IRGRP;
213 tmode |= FILEIO_S_IWGRP;
217 tmode |= FILEIO_S_IXGRP;
220 tmode |= FILEIO_S_IROTH;
223 tmode |= FILEIO_S_IWOTH;
227 tmode |= FILEIO_S_IXOTH;
233 remote_fileio_errno_to_target (int error)
240 return FILEIO_ENOENT;
248 return FILEIO_EACCES;
250 return FILEIO_EFAULT;
254 return FILEIO_EEXIST;
256 return FILEIO_ENODEV;
258 return FILEIO_ENOTDIR;
260 return FILEIO_EISDIR;
262 return FILEIO_EINVAL;
264 return FILEIO_ENFILE;
266 return FILEIO_EMFILE;
270 return FILEIO_ENOSPC;
272 return FILEIO_ESPIPE;
276 return FILEIO_ENOSYS;
278 return FILEIO_ENAMETOOLONG;
280 return FILEIO_EUNKNOWN;
284 remote_fileio_seek_flag_to_host (long num, int *flag)
290 case FILEIO_SEEK_SET:
293 case FILEIO_SEEK_CUR:
296 case FILEIO_SEEK_END:
306 remote_fileio_extract_long (char **buf, LONGEST *retlong)
311 if (!buf || !*buf || !**buf || !retlong)
313 c = strchr (*buf, ',');
317 c = strchr (*buf, '\0');
318 while (strchr ("+-", **buf))
324 for (*retlong = 0; **buf; ++*buf)
327 if (**buf >= '0' && **buf <= '9')
328 *retlong += **buf - '0';
329 else if (**buf >= 'a' && **buf <= 'f')
330 *retlong += **buf - 'a' + 10;
331 else if (**buf >= 'A' && **buf <= 'F')
332 *retlong += **buf - 'A' + 10;
342 remote_fileio_extract_int (char **buf, long *retint)
349 ret = remote_fileio_extract_long (buf, &retlong);
351 *retint = (long) retlong;
356 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
361 if (!buf || !*buf || !**buf || !ptrval || !length)
363 c = strchr (*buf, '/');
367 if (remote_fileio_extract_long (buf, &retlong))
369 *ptrval = (CORE_ADDR) retlong;
371 if (remote_fileio_extract_long (buf, &retlong))
373 *length = (int) retlong;
377 /* Convert to big endian */
379 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
383 for (i = 0; i < bytes; ++i)
384 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
388 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
390 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
394 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
396 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
400 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
402 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
406 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
408 remote_fileio_to_be (num, (char *) fnum, 8);
412 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
414 remote_fileio_to_be (num, (char *) fnum, 8);
418 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
422 /* `st_dev' is set in the calling function */
423 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
424 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
425 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
426 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
427 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
428 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
429 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
430 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
431 blksize = st->st_blksize;
435 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
436 #if HAVE_STRUCT_STAT_ST_BLOCKS
437 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
439 /* FIXME: This is correct for DJGPP, but other systems that don't
440 have st_blocks, if any, might prefer 512 instead of st_blksize.
441 (eliz, 30-12-2003) */
442 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
446 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
447 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
448 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
452 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
454 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
455 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
458 static int remote_fio_ctrl_c_flag = 0;
459 static int remote_fio_no_longjmp = 0;
461 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
462 static struct sigaction remote_fio_sa;
463 static struct sigaction remote_fio_osa;
465 static void (*remote_fio_ofunc)(int);
469 remote_fileio_sig_init (void)
471 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
472 remote_fio_sa.sa_handler = SIG_IGN;
473 sigemptyset (&remote_fio_sa.sa_mask);
474 remote_fio_sa.sa_flags = 0;
475 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
477 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
482 remote_fileio_sig_set (void (*sigint_func)(int))
484 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
485 remote_fio_sa.sa_handler = sigint_func;
486 sigemptyset (&remote_fio_sa.sa_mask);
487 remote_fio_sa.sa_flags = 0;
488 sigaction (SIGINT, &remote_fio_sa, NULL);
490 signal (SIGINT, sigint_func);
495 remote_fileio_sig_exit (void)
497 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
498 sigaction (SIGINT, &remote_fio_osa, NULL);
500 signal (SIGINT, remote_fio_ofunc);
505 remote_fileio_ctrl_c_signal_handler (int signo)
507 remote_fileio_sig_set (SIG_IGN);
508 remote_fio_ctrl_c_flag = 1;
509 if (!remote_fio_no_longjmp)
510 deprecated_throw_reason (RETURN_QUIT);
511 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
515 remote_fileio_reply (int retcode, int error)
519 remote_fileio_sig_set (SIG_IGN);
526 sprintf (buf + strlen (buf), "%x", retcode);
527 if (error || remote_fio_ctrl_c_flag)
529 if (error && remote_fio_ctrl_c_flag)
530 error = FILEIO_EINTR;
536 sprintf (buf + strlen (buf), ",%x", error);
537 if (remote_fio_ctrl_c_flag)
540 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
545 remote_fileio_ioerror (void)
547 remote_fileio_reply (-1, FILEIO_EIO);
551 remote_fileio_badfd (void)
553 remote_fileio_reply (-1, FILEIO_EBADF);
557 remote_fileio_return_errno (int retcode)
559 remote_fileio_reply (retcode,
560 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
564 remote_fileio_return_success (int retcode)
566 remote_fileio_reply (retcode, 0);
569 /* Wrapper function for remote_write_bytes() which has the disadvantage to
570 write only one packet, regardless of the requested number of bytes to
571 transfer. This wrapper calls remote_write_bytes() as often as needed. */
573 remote_fileio_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
575 int ret = 0, written;
577 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
588 remote_fileio_func_open (char *buf)
591 int length, retlength;
598 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
599 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
601 remote_fileio_ioerror ();
604 /* 2. Parameter: open flags */
605 if (remote_fileio_extract_int (&buf, &num))
607 remote_fileio_ioerror ();
610 flags = remote_fileio_oflags_to_host (num);
611 /* 3. Parameter: open mode */
612 if (remote_fileio_extract_int (&buf, &num))
614 remote_fileio_ioerror ();
617 mode = remote_fileio_mode_to_host (num, 1);
619 /* Request pathname using 'm' packet */
620 pathname = alloca (length);
621 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
622 if (retlength != length)
624 remote_fileio_ioerror ();
628 /* Check if pathname exists and is not a regular file or directory. If so,
629 return an appropriate error code. Same for trying to open directories
631 if (!stat (pathname, &st))
633 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
635 remote_fileio_reply (-1, FILEIO_ENODEV);
638 if (S_ISDIR (st.st_mode)
639 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
641 remote_fileio_reply (-1, FILEIO_EISDIR);
646 remote_fio_no_longjmp = 1;
647 fd = open (pathname, flags, mode);
650 remote_fileio_return_errno (-1);
654 fd = remote_fileio_fd_to_targetfd (fd);
655 remote_fileio_return_success (fd);
659 remote_fileio_func_close (char *buf)
664 /* Parameter: file descriptor */
665 if (remote_fileio_extract_int (&buf, &num))
667 remote_fileio_ioerror ();
670 fd = remote_fileio_map_fd ((int) num);
671 if (fd == FIO_FD_INVALID)
673 remote_fileio_badfd ();
677 remote_fio_no_longjmp = 1;
678 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
679 remote_fileio_return_errno (-1);
680 remote_fileio_close_target_fd ((int) num);
681 remote_fileio_return_success (0);
685 remote_fileio_func_read (char *buf)
690 int fd, ret, retlength;
693 off_t old_offset, new_offset;
695 /* 1. Parameter: file descriptor */
696 if (remote_fileio_extract_int (&buf, &target_fd))
698 remote_fileio_ioerror ();
701 fd = remote_fileio_map_fd ((int) target_fd);
702 if (fd == FIO_FD_INVALID)
704 remote_fileio_badfd ();
707 /* 2. Parameter: buffer pointer */
708 if (remote_fileio_extract_long (&buf, &lnum))
710 remote_fileio_ioerror ();
713 ptrval = (CORE_ADDR) lnum;
714 /* 3. Parameter: buffer length */
715 if (remote_fileio_extract_int (&buf, &num))
717 remote_fileio_ioerror ();
720 length = (size_t) num;
724 case FIO_FD_CONSOLE_OUT:
725 remote_fileio_badfd ();
727 case FIO_FD_CONSOLE_IN:
729 static char *remaining_buf = NULL;
730 static int remaining_length = 0;
732 buffer = (gdb_byte *) xmalloc (32768);
735 remote_fio_no_longjmp = 1;
736 if (remaining_length > length)
738 memcpy (buffer, remaining_buf, length);
739 memmove (remaining_buf, remaining_buf + length,
740 remaining_length - length);
741 remaining_length -= length;
746 memcpy (buffer, remaining_buf, remaining_length);
747 xfree (remaining_buf);
748 remaining_buf = NULL;
749 ret = remaining_length;
754 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 32767);
755 remote_fio_no_longjmp = 1;
756 if (ret > 0 && (size_t)ret > length)
758 remaining_buf = (char *) xmalloc (ret - length);
759 remaining_length = ret - length;
760 memcpy (remaining_buf, buffer + length, remaining_length);
767 buffer = (gdb_byte *) xmalloc (length);
768 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
769 for read() to return -1 even if "some" bytes have been read. It
770 has been corrected in SUSv2 but that doesn't help us much...
771 Therefore a complete solution must check how many bytes have been
772 read on EINTR to return a more reliable value to the target */
773 old_offset = lseek (fd, 0, SEEK_CUR);
774 remote_fio_no_longjmp = 1;
775 ret = read (fd, buffer, length);
776 if (ret < 0 && errno == EINTR)
778 new_offset = lseek (fd, 0, SEEK_CUR);
779 /* If some data has been read, return the number of bytes read.
780 The Ctrl-C flag is set in remote_fileio_reply() anyway */
781 if (old_offset != new_offset)
782 ret = new_offset - old_offset;
789 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
790 if (retlength != ret)
791 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
795 remote_fileio_return_errno (-1);
797 remote_fileio_return_success (ret);
803 remote_fileio_func_write (char *buf)
808 int fd, ret, retlength;
812 /* 1. Parameter: file descriptor */
813 if (remote_fileio_extract_int (&buf, &target_fd))
815 remote_fileio_ioerror ();
818 fd = remote_fileio_map_fd ((int) target_fd);
819 if (fd == FIO_FD_INVALID)
821 remote_fileio_badfd ();
824 /* 2. Parameter: buffer pointer */
825 if (remote_fileio_extract_long (&buf, &lnum))
827 remote_fileio_ioerror ();
830 ptrval = (CORE_ADDR) lnum;
831 /* 3. Parameter: buffer length */
832 if (remote_fileio_extract_int (&buf, &num))
834 remote_fileio_ioerror ();
837 length = (size_t) num;
839 buffer = (gdb_byte *) xmalloc (length);
840 retlength = remote_read_bytes (ptrval, buffer, length);
841 if (retlength != length)
844 remote_fileio_ioerror ();
848 remote_fio_no_longjmp = 1;
851 case FIO_FD_CONSOLE_IN:
852 remote_fileio_badfd ();
855 case FIO_FD_CONSOLE_OUT:
856 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
857 (char *) buffer, length);
858 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
862 ret = write (fd, buffer, length);
863 if (ret < 0 && errno == EACCES)
864 errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
869 remote_fileio_return_errno (-1);
871 remote_fileio_return_success (ret);
877 remote_fileio_func_lseek (char *buf)
884 /* 1. Parameter: file descriptor */
885 if (remote_fileio_extract_int (&buf, &num))
887 remote_fileio_ioerror ();
890 fd = remote_fileio_map_fd ((int) num);
891 if (fd == FIO_FD_INVALID)
893 remote_fileio_badfd ();
896 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
898 remote_fileio_reply (-1, FILEIO_ESPIPE);
902 /* 2. Parameter: offset */
903 if (remote_fileio_extract_long (&buf, &lnum))
905 remote_fileio_ioerror ();
908 offset = (off_t) lnum;
909 /* 3. Parameter: flag */
910 if (remote_fileio_extract_int (&buf, &num))
912 remote_fileio_ioerror ();
915 if (remote_fileio_seek_flag_to_host (num, &flag))
917 remote_fileio_reply (-1, FILEIO_EINVAL);
921 remote_fio_no_longjmp = 1;
922 ret = lseek (fd, offset, flag);
924 if (ret == (off_t) -1)
925 remote_fileio_return_errno (-1);
927 remote_fileio_return_success (ret);
931 remote_fileio_func_rename (char *buf)
933 CORE_ADDR old_ptr, new_ptr;
934 int old_len, new_len, retlength;
935 char *oldpath, *newpath;
937 struct stat ost, nst;
939 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
940 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
942 remote_fileio_ioerror ();
946 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
947 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
949 remote_fileio_ioerror ();
953 /* Request oldpath using 'm' packet */
954 oldpath = alloca (old_len);
955 retlength = remote_read_bytes (old_ptr, (gdb_byte *) oldpath, old_len);
956 if (retlength != old_len)
958 remote_fileio_ioerror ();
962 /* Request newpath using 'm' packet */
963 newpath = alloca (new_len);
964 retlength = remote_read_bytes (new_ptr, (gdb_byte *) newpath, new_len);
965 if (retlength != new_len)
967 remote_fileio_ioerror ();
971 /* Only operate on regular files and directories */
972 of = stat (oldpath, &ost);
973 nf = stat (newpath, &nst);
974 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
975 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
977 remote_fileio_reply (-1, FILEIO_EACCES);
981 remote_fio_no_longjmp = 1;
982 ret = rename (oldpath, newpath);
986 /* Special case: newpath is a non-empty directory. Some systems
987 return ENOTEMPTY, some return EEXIST. We coerce that to be
989 if (errno == ENOTEMPTY)
992 /* Workaround some Cygwin problems with correct errnos. */
995 if (!of && !nf && S_ISDIR (nst.st_mode))
997 if (S_ISREG (ost.st_mode))
1001 char oldfullpath[PATH_MAX + 1];
1002 char newfullpath[PATH_MAX + 1];
1005 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
1006 cygwin_conv_to_full_posix_path (newpath, newfullpath);
1007 len = strlen (oldfullpath);
1008 if (newfullpath[len] == '/'
1009 && !strncmp (oldfullpath, newfullpath, len))
1018 remote_fileio_return_errno (-1);
1021 remote_fileio_return_success (ret);
1025 remote_fileio_func_unlink (char *buf)
1028 int length, retlength;
1033 /* Parameter: Ptr to pathname / length incl. trailing zero */
1034 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1036 remote_fileio_ioerror ();
1039 /* Request pathname using 'm' packet */
1040 pathname = alloca (length);
1041 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
1042 if (retlength != length)
1044 remote_fileio_ioerror ();
1048 /* Only operate on regular files (and directories, which allows to return
1049 the correct return code) */
1050 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1052 remote_fileio_reply (-1, FILEIO_ENODEV);
1056 remote_fio_no_longjmp = 1;
1057 ret = unlink (pathname);
1060 remote_fileio_return_errno (-1);
1062 remote_fileio_return_success (ret);
1066 remote_fileio_func_stat (char *buf)
1068 CORE_ADDR statptr, nameptr;
1069 int ret, namelength, retlength;
1073 struct fio_stat fst;
1075 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1076 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1078 remote_fileio_ioerror ();
1082 /* 2. Parameter: Ptr to struct stat */
1083 if (remote_fileio_extract_long (&buf, &lnum))
1085 remote_fileio_ioerror ();
1088 statptr = (CORE_ADDR) lnum;
1090 /* Request pathname using 'm' packet */
1091 pathname = alloca (namelength);
1092 retlength = remote_read_bytes (nameptr, (gdb_byte *) pathname, namelength);
1093 if (retlength != namelength)
1095 remote_fileio_ioerror ();
1099 remote_fio_no_longjmp = 1;
1100 ret = stat (pathname, &st);
1104 remote_fileio_return_errno (-1);
1107 /* Only operate on regular files and directories */
1108 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1110 remote_fileio_reply (-1, FILEIO_EACCES);
1115 remote_fileio_to_fio_stat (&st, &fst);
1116 remote_fileio_to_fio_uint (0, fst.fst_dev);
1118 retlength = remote_fileio_write_bytes (statptr,
1119 (gdb_byte *) &fst, sizeof fst);
1120 if (retlength != sizeof fst)
1122 remote_fileio_return_errno (-1);
1126 remote_fileio_return_success (ret);
1130 remote_fileio_func_fstat (char *buf)
1133 int fd, ret, retlength;
1137 struct fio_stat fst;
1140 /* 1. Parameter: file descriptor */
1141 if (remote_fileio_extract_int (&buf, &target_fd))
1143 remote_fileio_ioerror ();
1146 fd = remote_fileio_map_fd ((int) target_fd);
1147 if (fd == FIO_FD_INVALID)
1149 remote_fileio_badfd ();
1152 /* 2. Parameter: Ptr to struct stat */
1153 if (remote_fileio_extract_long (&buf, &lnum))
1155 remote_fileio_ioerror ();
1158 ptrval = (CORE_ADDR) lnum;
1160 remote_fio_no_longjmp = 1;
1161 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1163 remote_fileio_to_fio_uint (1, fst.fst_dev);
1164 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1167 st.st_uid = getuid ();
1172 st.st_gid = getgid ();
1178 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1179 st.st_blksize = 512;
1181 #if HAVE_STRUCT_STAT_ST_BLOCKS
1184 if (!gettimeofday (&tv, NULL))
1185 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1187 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1191 ret = fstat (fd, &st);
1195 remote_fileio_return_errno (-1);
1200 remote_fileio_to_fio_stat (&st, &fst);
1202 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &fst, sizeof fst);
1203 if (retlength != sizeof fst)
1205 remote_fileio_return_errno (-1);
1209 remote_fileio_return_success (ret);
1213 remote_fileio_func_gettimeofday (char *buf)
1219 struct fio_timeval ftv;
1221 /* 1. Parameter: struct timeval pointer */
1222 if (remote_fileio_extract_long (&buf, &lnum))
1224 remote_fileio_ioerror ();
1227 ptrval = (CORE_ADDR) lnum;
1228 /* 2. Parameter: some pointer value... */
1229 if (remote_fileio_extract_long (&buf, &lnum))
1231 remote_fileio_ioerror ();
1234 /* ...which has to be NULL */
1237 remote_fileio_reply (-1, FILEIO_EINVAL);
1241 remote_fio_no_longjmp = 1;
1242 ret = gettimeofday (&tv, NULL);
1246 remote_fileio_return_errno (-1);
1252 remote_fileio_to_fio_timeval (&tv, &ftv);
1254 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1255 if (retlength != sizeof ftv)
1257 remote_fileio_return_errno (-1);
1261 remote_fileio_return_success (ret);
1265 remote_fileio_func_isatty (char *buf)
1270 /* Parameter: file descriptor */
1271 if (remote_fileio_extract_int (&buf, &target_fd))
1273 remote_fileio_ioerror ();
1276 remote_fio_no_longjmp = 1;
1277 fd = remote_fileio_map_fd ((int) target_fd);
1278 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1279 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1283 remote_fileio_func_system (char *buf)
1286 int ret, length, retlength;
1287 char *cmdline = NULL;
1289 /* Parameter: Ptr to commandline / length incl. trailing zero */
1290 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1292 remote_fileio_ioerror ();
1298 /* Request commandline using 'm' packet */
1299 cmdline = alloca (length);
1300 retlength = remote_read_bytes (ptrval, (gdb_byte *) cmdline, length);
1301 if (retlength != length)
1303 remote_fileio_ioerror ();
1308 /* Check if system(3) has been explicitely allowed using the
1309 `set remote system-call-allowed 1' command. If length is 0,
1310 indicating a NULL parameter to the system call, return zero to
1311 indicate a shell is not available. Otherwise fail with EPERM. */
1312 if (!remote_fio_system_call_allowed)
1315 remote_fileio_return_success (0);
1317 remote_fileio_reply (-1, FILEIO_EPERM);
1321 remote_fio_no_longjmp = 1;
1322 ret = system (cmdline);
1325 remote_fileio_return_success (ret);
1327 remote_fileio_return_errno (-1);
1329 remote_fileio_return_success (WEXITSTATUS (ret));
1334 void (*func)(char *);
1335 } remote_fio_func_map[] = {
1336 { "open", remote_fileio_func_open },
1337 { "close", remote_fileio_func_close },
1338 { "read", remote_fileio_func_read },
1339 { "write", remote_fileio_func_write },
1340 { "lseek", remote_fileio_func_lseek },
1341 { "rename", remote_fileio_func_rename },
1342 { "unlink", remote_fileio_func_unlink },
1343 { "stat", remote_fileio_func_stat },
1344 { "fstat", remote_fileio_func_fstat },
1345 { "gettimeofday", remote_fileio_func_gettimeofday },
1346 { "isatty", remote_fileio_func_isatty },
1347 { "system", remote_fileio_func_system },
1352 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1354 char *buf = buf_arg;
1358 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1360 c = strchr (++buf, ',');
1364 c = strchr (buf, '\0');
1365 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1366 if (!strcmp (remote_fio_func_map[idx].name, buf))
1368 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1369 return RETURN_ERROR;
1370 remote_fio_func_map[idx].func (c);
1374 /* Close any open descriptors, and reinitialize the file mapping. */
1377 remote_fileio_reset (void)
1381 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1383 int fd = remote_fio_data.fd_map[ix];
1388 if (remote_fio_data.fd_map)
1390 free (remote_fio_data.fd_map);
1391 remote_fio_data.fd_map = NULL;
1392 remote_fio_data.fd_map_size = 0;
1397 remote_fileio_request (char *buf)
1401 remote_fileio_sig_init ();
1403 remote_fio_ctrl_c_flag = 0;
1404 remote_fio_no_longjmp = 0;
1406 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1411 remote_fileio_reply (-1, FILEIO_ENOSYS);
1414 remote_fileio_reply (-1, FILEIO_EINTR);
1420 remote_fileio_sig_exit ();
1424 set_system_call_allowed (char *args, int from_tty)
1429 int val = strtoul (args, &arg_end, 10);
1430 if (*args && *arg_end == '\0')
1432 remote_fio_system_call_allowed = !!val;
1436 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1440 show_system_call_allowed (char *args, int from_tty)
1443 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1444 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1445 remote_fio_system_call_allowed ? "" : "not ");
1449 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1450 struct cmd_list_element *remote_show_cmdlist)
1452 add_cmd ("system-call-allowed", no_class,
1453 set_system_call_allowed,
1454 _("Set if the host system(3) call is allowed for the target."),
1455 &remote_set_cmdlist);
1456 add_cmd ("system-call-allowed", no_class,
1457 show_system_call_allowed,
1458 _("Show if the host system(3) call is allowed for the target."),
1459 &remote_show_cmdlist);