1 /* Remote File-I/O communications
3 Copyright 2003 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
28 #include "gdb/fileio.h"
34 #include <sys/types.h>
38 #include <sys/cygwin.h>
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;
55 remote_fileio_init_fd_map ()
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 ()
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 return remote_fio_data.fd_map_size - 10;
85 remote_fileio_next_free_fd ()
89 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
90 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
92 return remote_fileio_resize_fd_map ();
96 remote_fileio_fd_to_targetfd (int fd)
98 int target_fd = remote_fileio_next_free_fd ();
99 remote_fio_data.fd_map[target_fd] = fd;
104 remote_fileio_map_fd (int target_fd)
106 remote_fileio_init_fd_map ();
107 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
108 return FIO_FD_INVALID;
109 return remote_fio_data.fd_map[target_fd];
113 remote_fileio_close_target_fd (int target_fd)
115 remote_fileio_init_fd_map ();
116 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
117 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
121 remote_fileio_oflags_to_host (long flags)
125 if (flags & FILEIO_O_CREAT)
127 if (flags & FILEIO_O_EXCL)
129 if (flags & FILEIO_O_TRUNC)
131 if (flags & FILEIO_O_APPEND)
133 if (flags & FILEIO_O_RDONLY)
135 if (flags & FILEIO_O_WRONLY)
137 if (flags & FILEIO_O_RDWR)
139 /* On systems supporting binary and text mode, always open files in
148 remote_fileio_mode_to_host (long mode, int open_call)
154 if (mode & FILEIO_S_IFREG)
156 if (mode & FILEIO_S_IFDIR)
158 if (mode & FILEIO_S_IFCHR)
161 if (mode & FILEIO_S_IRUSR)
163 if (mode & FILEIO_S_IWUSR)
165 if (mode & FILEIO_S_IXUSR)
167 if (mode & FILEIO_S_IRGRP)
169 if (mode & FILEIO_S_IWGRP)
171 if (mode & FILEIO_S_IXGRP)
173 if (mode & FILEIO_S_IROTH)
175 if (mode & FILEIO_S_IWOTH)
177 if (mode & FILEIO_S_IXOTH)
183 remote_fileio_mode_to_target (mode_t mode)
188 tmode |= FILEIO_S_IFREG;
190 tmode |= FILEIO_S_IFDIR;
192 tmode |= FILEIO_S_IFCHR;
194 tmode |= FILEIO_S_IRUSR;
196 tmode |= FILEIO_S_IWUSR;
198 tmode |= FILEIO_S_IXUSR;
200 tmode |= FILEIO_S_IRGRP;
202 tmode |= FILEIO_S_IWGRP;
204 tmode |= FILEIO_S_IXGRP;
206 tmode |= FILEIO_S_IROTH;
208 tmode |= FILEIO_S_IWOTH;
210 tmode |= FILEIO_S_IXOTH;
215 remote_fileio_errno_to_target (int error)
222 return FILEIO_ENOENT;
230 return FILEIO_EACCES;
232 return FILEIO_EFAULT;
236 return FILEIO_EEXIST;
238 return FILEIO_ENODEV;
240 return FILEIO_ENOTDIR;
242 return FILEIO_EISDIR;
244 return FILEIO_EINVAL;
246 return FILEIO_ENFILE;
248 return FILEIO_EMFILE;
252 return FILEIO_ENOSPC;
254 return FILEIO_ESPIPE;
258 return FILEIO_ENOSYS;
260 return FILEIO_ENAMETOOLONG;
262 return FILEIO_EUNKNOWN;
266 remote_fileio_seek_flag_to_host (long num, int *flag)
272 case FILEIO_SEEK_SET:
275 case FILEIO_SEEK_CUR:
278 case FILEIO_SEEK_END:
288 remote_fileio_extract_long (char **buf, long long *retlong)
293 if (!buf || !*buf || !**buf || !retlong)
295 c = strchr (*buf, ',');
299 c = strchr (*buf, '\0');
300 while (strchr ("+-", **buf))
306 for (*retlong = 0; **buf; ++*buf)
309 if (**buf >= '0' && **buf <= '9')
310 *retlong += **buf - '0';
311 else if (**buf >= 'a' && **buf <= 'f')
312 *retlong += **buf - 'a' + 10;
313 else if (**buf >= 'A' && **buf <= 'F')
314 *retlong += **buf - 'A' + 10;
324 remote_fileio_extract_int (char **buf, long *retint)
331 if (!(ret = remote_fileio_extract_long (buf, &retlong)))
332 *retint = (long) retlong;
337 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
342 if (!buf || !*buf || !**buf || !ptrval || !length)
344 c = strchr (*buf, '/');
348 if (remote_fileio_extract_long (buf, &retlong))
350 *ptrval = (CORE_ADDR) retlong;
352 if (remote_fileio_extract_long (buf, &retlong))
354 *length = (int) retlong;
358 /* Convert to big endian */
360 remote_fileio_to_be (long long num, char *buf, int bytes)
364 for (i = 0; i < bytes; ++i)
365 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
369 remote_fileio_to_fio_int (long num, fio_int_t fnum)
371 remote_fileio_to_be ((long long) num, (char *) fnum, 4);
375 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
377 remote_fileio_to_be ((long long) num, (char *) fnum, 4);
381 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
383 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
387 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
389 remote_fileio_to_be ((long long) num, (char *) fnum, 4);
393 remote_fileio_to_fio_long (long long num, fio_long_t fnum)
395 remote_fileio_to_be (num, (char *) fnum, 8);
399 remote_fileio_to_fio_ulong (long long num, fio_ulong_t fnum)
401 remote_fileio_to_be (num, (char *) fnum, 8);
405 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
407 /* `st_dev' is set in the calling function */
408 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
409 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
410 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
411 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
412 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
413 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
414 remote_fileio_to_fio_ulong ((long long) st->st_size, fst->fst_size);
415 remote_fileio_to_fio_ulong ((long long) st->st_blksize, fst->fst_blksize);
416 remote_fileio_to_fio_ulong ((long long) st->st_blocks, fst->fst_blocks);
417 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
418 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
419 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
423 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
425 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
426 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
429 static int remote_fio_ctrl_c_flag = 0;
430 static int remote_fio_no_longjmp = 0;
431 jmp_buf remote_fio_jmp_buf;
433 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
434 static struct sigaction remote_fio_sa;
435 static struct sigaction remote_fio_osa;
437 static void (*remote_fio_ofunc)(int);
441 remote_fileio_sig_init ()
443 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
444 remote_fio_sa.sa_handler = SIG_IGN;
445 sigemptyset (&remote_fio_sa.sa_mask);
446 remote_fio_sa.sa_flags = 0;
447 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
449 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
454 remote_fileio_sig_set (void (*sigint_func)(int))
456 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
457 remote_fio_sa.sa_handler = sigint_func;
458 sigemptyset (&remote_fio_sa.sa_mask);
459 remote_fio_sa.sa_flags = 0;
460 sigaction (SIGINT, &remote_fio_sa, NULL);
462 signal (SIGINT, sigint_func);
467 remote_fileio_sig_exit ()
469 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
470 sigaction (SIGINT, &remote_fio_osa, NULL);
472 signal (SIGINT, remote_fio_ofunc);
477 remote_fileio_ctrl_c_signal_handler (int signo)
479 remote_fileio_sig_set (SIG_IGN);
480 remote_fio_ctrl_c_flag = 1;
481 if (!remote_fio_no_longjmp)
482 throw_exception (RETURN_QUIT);
483 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
487 remote_fileio_reply (int retcode, int error)
491 remote_fileio_sig_set (SIG_IGN);
498 sprintf (buf + strlen (buf), "%x", retcode);
499 if (error || remote_fio_ctrl_c_flag)
501 if (error && remote_fio_ctrl_c_flag)
502 error = FILEIO_EINTR;
508 sprintf (buf + strlen (buf), ",%x", error);
509 if (remote_fio_ctrl_c_flag)
512 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
517 remote_fileio_ioerror ()
519 remote_fileio_reply (-1, FILEIO_EIO);
523 remote_fileio_badfd ()
525 remote_fileio_reply (-1, FILEIO_EBADF);
529 remote_fileio_return_errno (int retcode)
531 remote_fileio_reply (retcode,
532 retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
536 remote_fileio_return_success (int retcode)
538 remote_fileio_reply (retcode, 0);
541 /* Wrapper function for remote_write_bytes() which has the disadvantage to
542 write only one packet, regardless of the requested number of bytes to
543 transfer. This wrapper calls remote_write_bytes() as often as needed. */
545 remote_fileio_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
547 int ret = 0, written;
549 while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
560 remote_fileio_func_open (char *buf)
563 int length, retlength;
570 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
571 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
573 remote_fileio_ioerror ();
576 /* 2. Parameter: open flags */
577 if (remote_fileio_extract_int (&buf, &num))
579 remote_fileio_ioerror ();
582 flags = remote_fileio_oflags_to_host (num);
583 /* 3. Parameter: open mode */
584 if (remote_fileio_extract_int (&buf, &num))
586 remote_fileio_ioerror ();
589 mode = remote_fileio_mode_to_host (num, 1);
591 /* Request pathname using 'm' packet */
592 pathname = alloca (length);
593 retlength = remote_read_bytes (ptrval, pathname, length);
594 if (retlength != length)
596 remote_fileio_ioerror ();
600 /* Check if pathname exists and is not a regular file or directory. If so,
601 return an appropriate error code. Same for trying to open directories
603 if (!stat (pathname, &st))
605 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
607 remote_fileio_reply (-1, FILEIO_ENODEV);
610 if (S_ISDIR (st.st_mode)
611 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
613 remote_fileio_reply (-1, FILEIO_EISDIR);
618 remote_fio_no_longjmp = 1;
619 fd = open (pathname, flags, mode);
622 remote_fileio_return_errno (-1);
626 fd = remote_fileio_fd_to_targetfd (fd);
627 remote_fileio_return_success (fd);
631 remote_fileio_func_close (char *buf)
636 /* Parameter: file descriptor */
637 if (remote_fileio_extract_int (&buf, &num))
639 remote_fileio_ioerror ();
642 if ((fd = remote_fileio_map_fd ((int) num)) == FIO_FD_INVALID)
644 remote_fileio_badfd ();
648 remote_fio_no_longjmp = 1;
649 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
650 remote_fileio_return_errno (-1);
651 remote_fileio_close_target_fd ((int) num);
652 remote_fileio_return_success (0);
656 remote_fileio_func_read (char *buf)
661 int fd, ret, retlength;
664 off_t old_offset, new_offset;
666 /* 1. Parameter: file descriptor */
667 if (remote_fileio_extract_int (&buf, &target_fd))
669 remote_fileio_ioerror ();
672 if ((fd = remote_fileio_map_fd ((int) target_fd)) == FIO_FD_INVALID)
674 remote_fileio_badfd ();
677 /* 2. Parameter: buffer pointer */
678 if (remote_fileio_extract_long (&buf, &lnum))
680 remote_fileio_ioerror ();
683 ptrval = (CORE_ADDR) lnum;
684 /* 3. Parameter: buffer length */
685 if (remote_fileio_extract_int (&buf, &num))
687 remote_fileio_ioerror ();
690 length = (size_t) num;
694 case FIO_FD_CONSOLE_OUT:
695 remote_fileio_badfd ();
697 case FIO_FD_CONSOLE_IN:
699 static char *remaining_buf = NULL;
700 static int remaining_length = 0;
702 buffer = (char *) xmalloc (32768);
705 remote_fio_no_longjmp = 1;
706 if (remaining_length > length)
708 memcpy (buffer, remaining_buf, length);
709 memmove (remaining_buf, remaining_buf + length,
710 remaining_length - length);
711 remaining_length -= length;
716 memcpy (buffer, remaining_buf, remaining_length);
717 xfree (remaining_buf);
718 remaining_buf = NULL;
719 ret = remaining_length;
724 ret = ui_file_read (gdb_stdtargin, buffer, 32767);
725 remote_fio_no_longjmp = 1;
726 if (ret > 0 && (size_t)ret > length)
728 remaining_buf = (char *) xmalloc (ret - length);
729 remaining_length = ret - length;
730 memcpy (remaining_buf, buffer + length, remaining_length);
737 buffer = (char *) xmalloc (length);
738 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
739 for read() to return -1 even if "some" bytes have been read. It
740 has been corrected in SUSv2 but that doesn't help us much...
741 Therefore a complete solution must check how many bytes have been
742 read on EINTR to return a more reliable value to the target */
743 old_offset = lseek (fd, 0, SEEK_CUR);
744 remote_fio_no_longjmp = 1;
745 ret = read (fd, buffer, length);
746 if (ret < 0 && errno == EINTR)
748 new_offset = lseek (fd, 0, SEEK_CUR);
749 /* If some data has been read, return the number of bytes read.
750 The Ctrl-C flag is set in remote_fileio_reply() anyway */
751 if (old_offset != new_offset)
752 ret = new_offset - old_offset;
759 retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
760 if (retlength != ret)
761 ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
765 remote_fileio_return_errno (-1);
767 remote_fileio_return_success (ret);
773 remote_fileio_func_write (char *buf)
778 int fd, ret, retlength;
782 /* 1. Parameter: file descriptor */
783 if (remote_fileio_extract_int (&buf, &target_fd))
785 remote_fileio_ioerror ();
788 if ((fd = remote_fileio_map_fd ((int) target_fd)) == FIO_FD_INVALID)
790 remote_fileio_badfd ();
793 /* 2. Parameter: buffer pointer */
794 if (remote_fileio_extract_long (&buf, &lnum))
796 remote_fileio_ioerror ();
799 ptrval = (CORE_ADDR) lnum;
800 /* 3. Parameter: buffer length */
801 if (remote_fileio_extract_int (&buf, &num))
803 remote_fileio_ioerror ();
806 length = (size_t) num;
808 buffer = (char *) xmalloc (length);
809 retlength = remote_read_bytes (ptrval, buffer, length);
810 if (retlength != length)
813 remote_fileio_ioerror ();
817 remote_fio_no_longjmp = 1;
820 case FIO_FD_CONSOLE_IN:
821 remote_fileio_badfd ();
823 case FIO_FD_CONSOLE_OUT:
824 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr, buffer,
826 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
830 ret = write (fd, buffer, length);
831 if (ret < 0 && errno == EACCES)
832 errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
837 remote_fileio_return_errno (-1);
839 remote_fileio_return_success (ret);
845 remote_fileio_func_lseek (char *buf)
852 /* 1. Parameter: file descriptor */
853 if (remote_fileio_extract_int (&buf, &num))
855 remote_fileio_ioerror ();
858 if ((fd = remote_fileio_map_fd ((int) num)) == FIO_FD_INVALID)
860 remote_fileio_badfd ();
863 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
865 remote_fileio_reply (-1, FILEIO_ESPIPE);
869 /* 2. Parameter: offset */
870 if (remote_fileio_extract_long (&buf, &lnum))
872 remote_fileio_ioerror ();
875 offset = (off_t) lnum;
876 /* 3. Parameter: flag */
877 if (remote_fileio_extract_int (&buf, &num))
879 remote_fileio_ioerror ();
882 if (remote_fileio_seek_flag_to_host (num, &flag))
884 remote_fileio_reply (-1, FILEIO_EINVAL);
888 remote_fio_no_longjmp = 1;
889 ret = lseek (fd, offset, flag);
891 if (ret == (off_t) -1)
892 remote_fileio_return_errno (-1);
894 remote_fileio_return_success (ret);
898 remote_fileio_func_rename (char *buf)
901 int length, retlength;
902 char *oldpath, *newpath;
904 struct stat ost, nst;
906 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
907 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
909 remote_fileio_ioerror ();
912 /* Request oldpath using 'm' packet */
913 oldpath = alloca (length);
914 retlength = remote_read_bytes (ptrval, oldpath, length);
915 if (retlength != length)
917 remote_fileio_ioerror ();
920 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
921 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
923 remote_fileio_ioerror ();
926 /* Request newpath using 'm' packet */
927 newpath = alloca (length);
928 retlength = remote_read_bytes (ptrval, newpath, length);
929 if (retlength != length)
931 remote_fileio_ioerror ();
935 /* Only operate on regular files and directories */
936 if ((!(of = stat (oldpath, &ost))
937 && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
938 || (!(nf = stat (newpath, &nst))
939 && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
941 remote_fileio_reply (-1, FILEIO_EACCES);
945 remote_fio_no_longjmp = 1;
946 ret = rename (oldpath, newpath);
950 /* Special case: newpath is a non-empty directory. Some systems
951 return ENOTEMPTY, some return EEXIST. We coerce that to be
953 if (errno == ENOTEMPTY)
956 /* Workaround some Cygwin problems with correct errnos. */
959 if (!of && !nf && S_ISDIR (nst.st_mode))
961 if (S_ISREG (ost.st_mode))
965 char oldfullpath[PATH_MAX + 1];
966 char newfullpath[PATH_MAX + 1];
969 cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
970 cygwin_conv_to_full_posix_path (newpath, newfullpath);
971 len = strlen (oldfullpath);
972 if (newfullpath[len] == '/'
973 && !strncmp (oldfullpath, newfullpath, len))
982 remote_fileio_return_errno (-1);
985 remote_fileio_return_success (ret);
989 remote_fileio_func_unlink (char *buf)
992 int length, retlength;
997 /* Parameter: Ptr to pathname / length incl. trailing zero */
998 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1000 remote_fileio_ioerror ();
1003 /* Request pathname using 'm' packet */
1004 pathname = alloca (length);
1005 retlength = remote_read_bytes (ptrval, pathname, length);
1006 if (retlength != length)
1008 remote_fileio_ioerror ();
1012 /* Only operate on regular files (and directories, which allows to return
1013 the correct return code) */
1014 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1016 remote_fileio_reply (-1, FILEIO_ENODEV);
1020 remote_fio_no_longjmp = 1;
1021 ret = unlink (pathname);
1024 remote_fileio_return_errno (-1);
1026 remote_fileio_return_success (ret);
1030 remote_fileio_func_stat (char *buf)
1033 int ret, length, retlength;
1037 struct fio_stat fst;
1039 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1040 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1042 remote_fileio_ioerror ();
1045 /* Request pathname using 'm' packet */
1046 pathname = alloca (length);
1047 retlength = remote_read_bytes (ptrval, pathname, length);
1048 if (retlength != length)
1050 remote_fileio_ioerror ();
1054 /* 2. Parameter: Ptr to struct stat */
1055 if (remote_fileio_extract_long (&buf, &lnum))
1057 remote_fileio_ioerror ();
1060 ptrval = (CORE_ADDR) lnum;
1062 remote_fio_no_longjmp = 1;
1063 ret = stat (pathname, &st);
1067 remote_fileio_return_errno (-1);
1070 /* Only operate on regular files and directories */
1071 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1073 remote_fileio_reply (-1, FILEIO_EACCES);
1078 remote_fileio_to_fio_stat (&st, &fst);
1079 remote_fileio_to_fio_uint (0, fst.fst_dev);
1081 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1082 if (retlength != sizeof fst)
1084 remote_fileio_return_errno (-1);
1088 remote_fileio_return_success (ret);
1092 remote_fileio_func_fstat (char *buf)
1095 int fd, ret, retlength;
1099 struct fio_stat fst;
1102 /* 1. Parameter: file descriptor */
1103 if (remote_fileio_extract_int (&buf, &target_fd))
1105 remote_fileio_ioerror ();
1108 if ((fd = remote_fileio_map_fd ((int) target_fd)) == FIO_FD_INVALID)
1110 remote_fileio_badfd ();
1113 /* 2. Parameter: Ptr to struct stat */
1114 if (remote_fileio_extract_long (&buf, &lnum))
1116 remote_fileio_ioerror ();
1119 ptrval = (CORE_ADDR) lnum;
1121 remote_fio_no_longjmp = 1;
1122 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1124 remote_fileio_to_fio_uint (1, fst.fst_dev);
1125 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1127 st.st_uid = getuid ();
1128 st.st_gid = getgid ();
1131 st.st_blksize = 512;
1133 if (!gettimeofday (&tv, NULL))
1134 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1136 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1140 ret = fstat (fd, &st);
1144 remote_fileio_return_errno (-1);
1149 remote_fileio_to_fio_stat (&st, &fst);
1151 retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1152 if (retlength != sizeof fst)
1154 remote_fileio_return_errno (-1);
1158 remote_fileio_return_success (ret);
1162 remote_fileio_func_gettimeofday (char *buf)
1168 struct fio_timeval ftv;
1170 /* 1. Parameter: struct timeval pointer */
1171 if (remote_fileio_extract_long (&buf, &lnum))
1173 remote_fileio_ioerror ();
1176 ptrval = (CORE_ADDR) lnum;
1177 /* 2. Parameter: some pointer value... */
1178 if (remote_fileio_extract_long (&buf, &lnum))
1180 remote_fileio_ioerror ();
1183 /* ...which has to be NULL */
1186 remote_fileio_reply (-1, FILEIO_EINVAL);
1190 remote_fio_no_longjmp = 1;
1191 ret = gettimeofday (&tv, NULL);
1195 remote_fileio_return_errno (-1);
1201 remote_fileio_to_fio_timeval (&tv, &ftv);
1203 retlength = remote_fileio_write_bytes (ptrval, (char *) &ftv, sizeof ftv);
1204 if (retlength != sizeof ftv)
1206 remote_fileio_return_errno (-1);
1210 remote_fileio_return_success (ret);
1214 remote_fileio_func_isatty (char *buf)
1219 /* Parameter: file descriptor */
1220 if (remote_fileio_extract_int (&buf, &target_fd))
1222 remote_fileio_ioerror ();
1225 remote_fio_no_longjmp = 1;
1226 fd = remote_fileio_map_fd ((int) target_fd);
1227 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1228 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1232 remote_fileio_func_system (char *buf)
1235 int ret, length, retlength;
1238 /* Check if system(3) has been explicitely allowed using the
1239 `set remote system-call-allowed 1' command. If not, return
1241 if (!remote_fio_system_call_allowed)
1243 remote_fileio_reply (-1, FILEIO_EPERM);
1247 /* Parameter: Ptr to commandline / length incl. trailing zero */
1248 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1250 remote_fileio_ioerror ();
1253 /* Request commandline using 'm' packet */
1254 cmdline = alloca (length);
1255 retlength = remote_read_bytes (ptrval, cmdline, length);
1256 if (retlength != length)
1258 remote_fileio_ioerror ();
1262 remote_fio_no_longjmp = 1;
1263 ret = system (cmdline);
1266 remote_fileio_return_errno (-1);
1268 remote_fileio_return_success (WEXITSTATUS (ret));
1273 void (*func)(char *);
1274 } remote_fio_func_map[] = {
1275 "open", remote_fileio_func_open,
1276 "close", remote_fileio_func_close,
1277 "read", remote_fileio_func_read,
1278 "write", remote_fileio_func_write,
1279 "lseek", remote_fileio_func_lseek,
1280 "rename", remote_fileio_func_rename,
1281 "unlink", remote_fileio_func_unlink,
1282 "stat", remote_fileio_func_stat,
1283 "fstat", remote_fileio_func_fstat,
1284 "gettimeofday", remote_fileio_func_gettimeofday,
1285 "isatty", remote_fileio_func_isatty,
1286 "system", remote_fileio_func_system,
1291 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1293 char *buf = buf_arg;
1297 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1299 c = strchr (++buf, ',');
1303 c = strchr (buf, '\0');
1304 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1305 if (!strcmp (remote_fio_func_map[idx].name, buf))
1307 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1308 return RETURN_ERROR;
1309 remote_fio_func_map[idx].func (c);
1314 remote_fileio_request (char *buf)
1318 remote_fileio_sig_init ();
1320 remote_fio_ctrl_c_flag = 0;
1321 remote_fio_no_longjmp = 0;
1323 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1324 NULL, RETURN_MASK_ALL);
1328 remote_fileio_reply (-1, FILEIO_ENOSYS);
1331 remote_fileio_reply (-1, FILEIO_EINTR);
1337 remote_fileio_sig_exit ();
1341 set_system_call_allowed (char *args, int from_tty)
1346 int val = strtoul (args, &arg_end, 10);
1347 if (*args && *arg_end == '\0')
1349 remote_fio_system_call_allowed = !!val;
1353 error ("Illegal argument for \"set remote system-call-allowed\" command");
1357 show_system_call_allowed (char *args, int from_tty)
1360 error ("Garbage after \"show remote system-call-allowed\" command: `%s'", args);
1361 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1362 remote_fio_system_call_allowed ? "" : "not ");
1366 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1367 struct cmd_list_element *remote_show_cmdlist)
1369 add_cmd ("system-call-allowed", no_class,
1370 set_system_call_allowed,
1371 "Set if the host system(3) call is allowed for the target.\n",
1372 &remote_set_cmdlist);
1373 add_cmd ("system-call-allowed", no_class,
1374 show_system_call_allowed,
1375 "Show if the host system(3) call is allowed for the target.\n",
1376 &remote_show_cmdlist);