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 int i = remote_fio_data.fd_map_size;
75 if (!remote_fio_data.fd_map)
76 return remote_fileio_init_fd_map ();
77 remote_fio_data.fd_map_size += 10;
78 remote_fio_data.fd_map =
79 (int *) xrealloc (remote_fio_data.fd_map,
80 remote_fio_data.fd_map_size * sizeof (int));
81 for (; i < remote_fio_data.fd_map_size; i++)
82 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
83 return remote_fio_data.fd_map_size - 10;
87 remote_fileio_next_free_fd (void)
91 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
92 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
94 return remote_fileio_resize_fd_map ();
98 remote_fileio_fd_to_targetfd (int fd)
100 int target_fd = remote_fileio_next_free_fd ();
101 remote_fio_data.fd_map[target_fd] = fd;
106 remote_fileio_map_fd (int target_fd)
108 remote_fileio_init_fd_map ();
109 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
110 return FIO_FD_INVALID;
111 return remote_fio_data.fd_map[target_fd];
115 remote_fileio_close_target_fd (int target_fd)
117 remote_fileio_init_fd_map ();
118 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
119 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
123 remote_fileio_oflags_to_host (long flags)
127 if (flags & FILEIO_O_CREAT)
129 if (flags & FILEIO_O_EXCL)
131 if (flags & FILEIO_O_TRUNC)
133 if (flags & FILEIO_O_APPEND)
135 if (flags & FILEIO_O_RDONLY)
137 if (flags & FILEIO_O_WRONLY)
139 if (flags & FILEIO_O_RDWR)
141 /* On systems supporting binary and text mode, always open files in
150 remote_fileio_mode_to_host (long mode, int open_call)
156 if (mode & FILEIO_S_IFREG)
158 if (mode & FILEIO_S_IFDIR)
160 if (mode & FILEIO_S_IFCHR)
163 if (mode & FILEIO_S_IRUSR)
165 if (mode & FILEIO_S_IWUSR)
167 if (mode & FILEIO_S_IXUSR)
170 if (mode & FILEIO_S_IRGRP)
174 if (mode & FILEIO_S_IWGRP)
178 if (mode & FILEIO_S_IXGRP)
181 if (mode & FILEIO_S_IROTH)
184 if (mode & FILEIO_S_IWOTH)
188 if (mode & FILEIO_S_IXOTH)
195 remote_fileio_mode_to_target (mode_t mode)
200 tmode |= FILEIO_S_IFREG;
202 tmode |= FILEIO_S_IFDIR;
204 tmode |= FILEIO_S_IFCHR;
206 tmode |= FILEIO_S_IRUSR;
208 tmode |= FILEIO_S_IWUSR;
210 tmode |= FILEIO_S_IXUSR;
213 tmode |= FILEIO_S_IRGRP;
217 tmode |= FILEIO_S_IWGRP;
221 tmode |= FILEIO_S_IXGRP;
224 tmode |= FILEIO_S_IROTH;
227 tmode |= FILEIO_S_IWOTH;
231 tmode |= FILEIO_S_IXOTH;
237 remote_fileio_errno_to_target (int error)
244 return FILEIO_ENOENT;
252 return FILEIO_EACCES;
254 return FILEIO_EFAULT;
258 return FILEIO_EEXIST;
260 return FILEIO_ENODEV;
262 return FILEIO_ENOTDIR;
264 return FILEIO_EISDIR;
266 return FILEIO_EINVAL;
268 return FILEIO_ENFILE;
270 return FILEIO_EMFILE;
274 return FILEIO_ENOSPC;
276 return FILEIO_ESPIPE;
280 return FILEIO_ENOSYS;
282 return FILEIO_ENAMETOOLONG;
284 return FILEIO_EUNKNOWN;
288 remote_fileio_seek_flag_to_host (long num, int *flag)
294 case FILEIO_SEEK_SET:
297 case FILEIO_SEEK_CUR:
300 case FILEIO_SEEK_END:
310 remote_fileio_extract_long (char **buf, LONGEST *retlong)
315 if (!buf || !*buf || !**buf || !retlong)
317 c = strchr (*buf, ',');
321 c = strchr (*buf, '\0');
322 while (strchr ("+-", **buf))
328 for (*retlong = 0; **buf; ++*buf)
331 if (**buf >= '0' && **buf <= '9')
332 *retlong += **buf - '0';
333 else if (**buf >= 'a' && **buf <= 'f')
334 *retlong += **buf - 'a' + 10;
335 else if (**buf >= 'A' && **buf <= 'F')
336 *retlong += **buf - 'A' + 10;
346 remote_fileio_extract_int (char **buf, long *retint)
353 ret = remote_fileio_extract_long (buf, &retlong);
355 *retint = (long) retlong;
360 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
365 if (!buf || !*buf || !**buf || !ptrval || !length)
367 c = strchr (*buf, '/');
371 if (remote_fileio_extract_long (buf, &retlong))
373 *ptrval = (CORE_ADDR) retlong;
375 if (remote_fileio_extract_long (buf, &retlong))
377 *length = (int) retlong;
381 /* Convert to big endian */
383 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
387 for (i = 0; i < bytes; ++i)
388 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
392 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
394 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
398 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
400 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
404 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
406 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
410 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
412 remote_fileio_to_be (num, (char *) fnum, 8);
416 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
418 remote_fileio_to_be (num, (char *) fnum, 8);
422 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
426 /* `st_dev' is set in the calling function */
427 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
428 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
429 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
430 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
431 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
432 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
433 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
434 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
435 blksize = st->st_blksize;
439 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
440 #if HAVE_STRUCT_STAT_ST_BLOCKS
441 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
443 /* FIXME: This is correct for DJGPP, but other systems that don't
444 have st_blocks, if any, might prefer 512 instead of st_blksize.
445 (eliz, 30-12-2003) */
446 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
450 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
451 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
452 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
456 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
458 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
459 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
462 static int remote_fio_ctrl_c_flag = 0;
463 static int remote_fio_no_longjmp = 0;
465 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
466 static struct sigaction remote_fio_sa;
467 static struct sigaction remote_fio_osa;
469 static void (*remote_fio_ofunc)(int);
473 remote_fileio_sig_init (void)
475 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
476 remote_fio_sa.sa_handler = SIG_IGN;
477 sigemptyset (&remote_fio_sa.sa_mask);
478 remote_fio_sa.sa_flags = 0;
479 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
481 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
486 remote_fileio_sig_set (void (*sigint_func)(int))
488 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
489 remote_fio_sa.sa_handler = sigint_func;
490 sigemptyset (&remote_fio_sa.sa_mask);
491 remote_fio_sa.sa_flags = 0;
492 sigaction (SIGINT, &remote_fio_sa, NULL);
494 signal (SIGINT, sigint_func);
499 remote_fileio_sig_exit (void)
501 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
502 sigaction (SIGINT, &remote_fio_osa, NULL);
504 signal (SIGINT, remote_fio_ofunc);
509 remote_fileio_ctrl_c_signal_handler (int signo)
511 remote_fileio_sig_set (SIG_IGN);
512 remote_fio_ctrl_c_flag = 1;
513 if (!remote_fio_no_longjmp)
514 deprecated_throw_reason (RETURN_QUIT);
515 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
519 remote_fileio_reply (int retcode, int error)
523 remote_fileio_sig_set (SIG_IGN);
530 sprintf (buf + strlen (buf), "%x", retcode);
531 if (error || remote_fio_ctrl_c_flag)
533 if (error && remote_fio_ctrl_c_flag)
534 error = FILEIO_EINTR;
540 sprintf (buf + strlen (buf), ",%x", error);
541 if (remote_fio_ctrl_c_flag)
544 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
549 remote_fileio_ioerror (void)
551 remote_fileio_reply (-1, FILEIO_EIO);
555 remote_fileio_badfd (void)
557 remote_fileio_reply (-1, FILEIO_EBADF);
561 remote_fileio_return_errno (int retcode)
563 remote_fileio_reply (retcode,
564 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
568 remote_fileio_return_success (int retcode)
570 remote_fileio_reply (retcode, 0);
573 /* Wrapper function for remote_write_bytes() which has the disadvantage to
574 write only one packet, regardless of the requested number of bytes to
575 transfer. This wrapper calls remote_write_bytes() as often as needed. */
577 remote_fileio_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
579 int ret = 0, written;
581 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
592 remote_fileio_func_open (char *buf)
595 int length, retlength;
602 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
603 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
605 remote_fileio_ioerror ();
608 /* 2. Parameter: open flags */
609 if (remote_fileio_extract_int (&buf, &num))
611 remote_fileio_ioerror ();
614 flags = remote_fileio_oflags_to_host (num);
615 /* 3. Parameter: open mode */
616 if (remote_fileio_extract_int (&buf, &num))
618 remote_fileio_ioerror ();
621 mode = remote_fileio_mode_to_host (num, 1);
623 /* Request pathname using 'm' packet */
624 pathname = alloca (length);
625 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
626 if (retlength != length)
628 remote_fileio_ioerror ();
632 /* Check if pathname exists and is not a regular file or directory. If so,
633 return an appropriate error code. Same for trying to open directories
635 if (!stat (pathname, &st))
637 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
639 remote_fileio_reply (-1, FILEIO_ENODEV);
642 if (S_ISDIR (st.st_mode)
643 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
645 remote_fileio_reply (-1, FILEIO_EISDIR);
650 remote_fio_no_longjmp = 1;
651 fd = open (pathname, flags, mode);
654 remote_fileio_return_errno (-1);
658 fd = remote_fileio_fd_to_targetfd (fd);
659 remote_fileio_return_success (fd);
663 remote_fileio_func_close (char *buf)
668 /* Parameter: file descriptor */
669 if (remote_fileio_extract_int (&buf, &num))
671 remote_fileio_ioerror ();
674 fd = remote_fileio_map_fd ((int) num);
675 if (fd == FIO_FD_INVALID)
677 remote_fileio_badfd ();
681 remote_fio_no_longjmp = 1;
682 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
683 remote_fileio_return_errno (-1);
684 remote_fileio_close_target_fd ((int) num);
685 remote_fileio_return_success (0);
689 remote_fileio_func_read (char *buf)
694 int fd, ret, retlength;
697 off_t old_offset, new_offset;
699 /* 1. Parameter: file descriptor */
700 if (remote_fileio_extract_int (&buf, &target_fd))
702 remote_fileio_ioerror ();
705 fd = remote_fileio_map_fd ((int) target_fd);
706 if (fd == FIO_FD_INVALID)
708 remote_fileio_badfd ();
711 /* 2. Parameter: buffer pointer */
712 if (remote_fileio_extract_long (&buf, &lnum))
714 remote_fileio_ioerror ();
717 ptrval = (CORE_ADDR) lnum;
718 /* 3. Parameter: buffer length */
719 if (remote_fileio_extract_int (&buf, &num))
721 remote_fileio_ioerror ();
724 length = (size_t) num;
728 case FIO_FD_CONSOLE_OUT:
729 remote_fileio_badfd ();
731 case FIO_FD_CONSOLE_IN:
733 static char *remaining_buf = NULL;
734 static int remaining_length = 0;
736 buffer = (gdb_byte *) xmalloc (32768);
739 remote_fio_no_longjmp = 1;
740 if (remaining_length > length)
742 memcpy (buffer, remaining_buf, length);
743 memmove (remaining_buf, remaining_buf + length,
744 remaining_length - length);
745 remaining_length -= length;
750 memcpy (buffer, remaining_buf, remaining_length);
751 xfree (remaining_buf);
752 remaining_buf = NULL;
753 ret = remaining_length;
758 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 32767);
759 remote_fio_no_longjmp = 1;
760 if (ret > 0 && (size_t)ret > length)
762 remaining_buf = (char *) xmalloc (ret - length);
763 remaining_length = ret - length;
764 memcpy (remaining_buf, buffer + length, remaining_length);
771 buffer = (gdb_byte *) xmalloc (length);
772 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
773 for read() to return -1 even if "some" bytes have been read. It
774 has been corrected in SUSv2 but that doesn't help us much...
775 Therefore a complete solution must check how many bytes have been
776 read on EINTR to return a more reliable value to the target */
777 old_offset = lseek (fd, 0, SEEK_CUR);
778 remote_fio_no_longjmp = 1;
779 ret = read (fd, buffer, length);
780 if (ret < 0 && errno == EINTR)
782 new_offset = lseek (fd, 0, SEEK_CUR);
783 /* If some data has been read, return the number of bytes read.
784 The Ctrl-C flag is set in remote_fileio_reply() anyway */
785 if (old_offset != new_offset)
786 ret = new_offset - old_offset;
793 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
794 if (retlength != ret)
795 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
799 remote_fileio_return_errno (-1);
801 remote_fileio_return_success (ret);
807 remote_fileio_func_write (char *buf)
812 int fd, ret, retlength;
816 /* 1. Parameter: file descriptor */
817 if (remote_fileio_extract_int (&buf, &target_fd))
819 remote_fileio_ioerror ();
822 fd = remote_fileio_map_fd ((int) target_fd);
823 if (fd == FIO_FD_INVALID)
825 remote_fileio_badfd ();
828 /* 2. Parameter: buffer pointer */
829 if (remote_fileio_extract_long (&buf, &lnum))
831 remote_fileio_ioerror ();
834 ptrval = (CORE_ADDR) lnum;
835 /* 3. Parameter: buffer length */
836 if (remote_fileio_extract_int (&buf, &num))
838 remote_fileio_ioerror ();
841 length = (size_t) num;
843 buffer = (gdb_byte *) xmalloc (length);
844 retlength = remote_read_bytes (ptrval, buffer, length);
845 if (retlength != length)
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 R/O file.*/
873 remote_fileio_return_errno (-1);
875 remote_fileio_return_success (ret);
881 remote_fileio_func_lseek (char *buf)
888 /* 1. Parameter: file descriptor */
889 if (remote_fileio_extract_int (&buf, &num))
891 remote_fileio_ioerror ();
894 fd = remote_fileio_map_fd ((int) num);
895 if (fd == FIO_FD_INVALID)
897 remote_fileio_badfd ();
900 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
902 remote_fileio_reply (-1, FILEIO_ESPIPE);
906 /* 2. Parameter: offset */
907 if (remote_fileio_extract_long (&buf, &lnum))
909 remote_fileio_ioerror ();
912 offset = (off_t) lnum;
913 /* 3. Parameter: flag */
914 if (remote_fileio_extract_int (&buf, &num))
916 remote_fileio_ioerror ();
919 if (remote_fileio_seek_flag_to_host (num, &flag))
921 remote_fileio_reply (-1, FILEIO_EINVAL);
925 remote_fio_no_longjmp = 1;
926 ret = lseek (fd, offset, flag);
928 if (ret == (off_t) -1)
929 remote_fileio_return_errno (-1);
931 remote_fileio_return_success (ret);
935 remote_fileio_func_rename (char *buf)
937 CORE_ADDR old_ptr, new_ptr;
938 int old_len, new_len, retlength;
939 char *oldpath, *newpath;
941 struct stat ost, nst;
943 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
944 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
946 remote_fileio_ioerror ();
950 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
951 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
953 remote_fileio_ioerror ();
957 /* Request oldpath using 'm' packet */
958 oldpath = alloca (old_len);
959 retlength = remote_read_bytes (old_ptr, (gdb_byte *) oldpath, old_len);
960 if (retlength != old_len)
962 remote_fileio_ioerror ();
966 /* Request newpath using 'm' packet */
967 newpath = alloca (new_len);
968 retlength = remote_read_bytes (new_ptr, (gdb_byte *) newpath, new_len);
969 if (retlength != new_len)
971 remote_fileio_ioerror ();
975 /* Only operate on regular files and directories */
976 of = stat (oldpath, &ost);
977 nf = stat (newpath, &nst);
978 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
979 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
981 remote_fileio_reply (-1, FILEIO_EACCES);
985 remote_fio_no_longjmp = 1;
986 ret = rename (oldpath, newpath);
990 /* Special case: newpath is a non-empty directory. Some systems
991 return ENOTEMPTY, some return EEXIST. We coerce that to be
993 if (errno == ENOTEMPTY)
996 /* Workaround some Cygwin problems with correct errnos. */
999 if (!of && !nf && S_ISDIR (nst.st_mode))
1001 if (S_ISREG (ost.st_mode))
1005 char oldfullpath[PATH_MAX + 1];
1006 char newfullpath[PATH_MAX + 1];
1009 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
1010 cygwin_conv_to_full_posix_path (newpath, newfullpath);
1011 len = strlen (oldfullpath);
1012 if (newfullpath[len] == '/'
1013 && !strncmp (oldfullpath, newfullpath, len))
1022 remote_fileio_return_errno (-1);
1025 remote_fileio_return_success (ret);
1029 remote_fileio_func_unlink (char *buf)
1032 int length, retlength;
1037 /* Parameter: Ptr to pathname / length incl. trailing zero */
1038 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1040 remote_fileio_ioerror ();
1043 /* Request pathname using 'm' packet */
1044 pathname = alloca (length);
1045 retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
1046 if (retlength != length)
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, retlength;
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 retlength = remote_read_bytes (nameptr, (gdb_byte *) pathname, namelength);
1097 if (retlength != namelength)
1099 remote_fileio_ioerror ();
1103 remote_fio_no_longjmp = 1;
1104 ret = stat (pathname, &st);
1108 remote_fileio_return_errno (-1);
1111 /* Only operate on regular files and directories */
1112 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1114 remote_fileio_reply (-1, FILEIO_EACCES);
1119 remote_fileio_to_fio_stat (&st, &fst);
1120 remote_fileio_to_fio_uint (0, fst.fst_dev);
1122 retlength = remote_fileio_write_bytes (statptr,
1123 (gdb_byte *) &fst, sizeof fst);
1124 if (retlength != sizeof fst)
1126 remote_fileio_return_errno (-1);
1130 remote_fileio_return_success (ret);
1134 remote_fileio_func_fstat (char *buf)
1137 int fd, ret, retlength;
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 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1171 st.st_uid = getuid ();
1176 st.st_gid = getgid ();
1182 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1183 st.st_blksize = 512;
1185 #if HAVE_STRUCT_STAT_ST_BLOCKS
1188 if (!gettimeofday (&tv, NULL))
1189 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1191 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1195 ret = fstat (fd, &st);
1199 remote_fileio_return_errno (-1);
1204 remote_fileio_to_fio_stat (&st, &fst);
1206 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &fst, sizeof fst);
1207 if (retlength != sizeof fst)
1209 remote_fileio_return_errno (-1);
1213 remote_fileio_return_success (ret);
1217 remote_fileio_func_gettimeofday (char *buf)
1223 struct fio_timeval ftv;
1225 /* 1. Parameter: struct timeval pointer */
1226 if (remote_fileio_extract_long (&buf, &lnum))
1228 remote_fileio_ioerror ();
1231 ptrval = (CORE_ADDR) lnum;
1232 /* 2. Parameter: some pointer value... */
1233 if (remote_fileio_extract_long (&buf, &lnum))
1235 remote_fileio_ioerror ();
1238 /* ...which has to be NULL */
1241 remote_fileio_reply (-1, FILEIO_EINVAL);
1245 remote_fio_no_longjmp = 1;
1246 ret = gettimeofday (&tv, NULL);
1250 remote_fileio_return_errno (-1);
1256 remote_fileio_to_fio_timeval (&tv, &ftv);
1258 retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1259 if (retlength != sizeof ftv)
1261 remote_fileio_return_errno (-1);
1265 remote_fileio_return_success (ret);
1269 remote_fileio_func_isatty (char *buf)
1274 /* Parameter: file descriptor */
1275 if (remote_fileio_extract_int (&buf, &target_fd))
1277 remote_fileio_ioerror ();
1280 remote_fio_no_longjmp = 1;
1281 fd = remote_fileio_map_fd ((int) target_fd);
1282 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1283 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1287 remote_fileio_func_system (char *buf)
1290 int ret, length, retlength;
1291 char *cmdline = NULL;
1293 /* Parameter: Ptr to commandline / length incl. trailing zero */
1294 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1296 remote_fileio_ioerror ();
1302 /* Request commandline using 'm' packet */
1303 cmdline = alloca (length);
1304 retlength = remote_read_bytes (ptrval, (gdb_byte *) cmdline, length);
1305 if (retlength != length)
1307 remote_fileio_ioerror ();
1312 /* Check if system(3) has been explicitely allowed using the
1313 `set remote system-call-allowed 1' command. If length is 0,
1314 indicating a NULL parameter to the system call, return zero to
1315 indicate a shell is not available. Otherwise fail with EPERM. */
1316 if (!remote_fio_system_call_allowed)
1319 remote_fileio_return_success (0);
1321 remote_fileio_reply (-1, FILEIO_EPERM);
1325 remote_fio_no_longjmp = 1;
1326 ret = system (cmdline);
1329 remote_fileio_return_success (ret);
1331 remote_fileio_return_errno (-1);
1333 remote_fileio_return_success (WEXITSTATUS (ret));
1338 void (*func)(char *);
1339 } remote_fio_func_map[] = {
1340 { "open", remote_fileio_func_open },
1341 { "close", remote_fileio_func_close },
1342 { "read", remote_fileio_func_read },
1343 { "write", remote_fileio_func_write },
1344 { "lseek", remote_fileio_func_lseek },
1345 { "rename", remote_fileio_func_rename },
1346 { "unlink", remote_fileio_func_unlink },
1347 { "stat", remote_fileio_func_stat },
1348 { "fstat", remote_fileio_func_fstat },
1349 { "gettimeofday", remote_fileio_func_gettimeofday },
1350 { "isatty", remote_fileio_func_isatty },
1351 { "system", remote_fileio_func_system },
1356 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1358 char *buf = buf_arg;
1362 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1364 c = strchr (++buf, ',');
1368 c = strchr (buf, '\0');
1369 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1370 if (!strcmp (remote_fio_func_map[idx].name, buf))
1372 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1373 return RETURN_ERROR;
1374 remote_fio_func_map[idx].func (c);
1378 /* Close any open descriptors, and reinitialize the file mapping. */
1381 remote_fileio_reset (void)
1385 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1387 int fd = remote_fio_data.fd_map[ix];
1392 if (remote_fio_data.fd_map)
1394 free (remote_fio_data.fd_map);
1395 remote_fio_data.fd_map = NULL;
1396 remote_fio_data.fd_map_size = 0;
1401 remote_fileio_request (char *buf)
1405 remote_fileio_sig_init ();
1407 remote_fio_ctrl_c_flag = 0;
1408 remote_fio_no_longjmp = 0;
1410 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1415 remote_fileio_reply (-1, FILEIO_ENOSYS);
1418 remote_fileio_reply (-1, FILEIO_EINTR);
1424 remote_fileio_sig_exit ();
1428 set_system_call_allowed (char *args, int from_tty)
1433 int val = strtoul (args, &arg_end, 10);
1434 if (*args && *arg_end == '\0')
1436 remote_fio_system_call_allowed = !!val;
1440 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1444 show_system_call_allowed (char *args, int from_tty)
1447 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1448 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1449 remote_fio_system_call_allowed ? "" : "not ");
1453 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1454 struct cmd_list_element *remote_show_cmdlist)
1456 add_cmd ("system-call-allowed", no_class,
1457 set_system_call_allowed,
1458 _("Set if the host system(3) call is allowed for the target."),
1459 &remote_set_cmdlist);
1460 add_cmd ("system-call-allowed", no_class,
1461 show_system_call_allowed,
1462 _("Show if the host system(3) call is allowed for the target."),
1463 &remote_show_cmdlist);