1 /* Remote File-I/O communications
3 Copyright 2003, 2005 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"
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
)
420 /* `st_dev' is set in the calling function */
421 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
422 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
423 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
424 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
425 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
426 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
427 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
428 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blksize
, fst
->fst_blksize
);
429 #if HAVE_STRUCT_STAT_ST_BLOCKS
430 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
432 /* FIXME: This is correct for DJGPP, but other systems that don't
433 have st_blocks, if any, might prefer 512 instead of st_blksize.
434 (eliz, 30-12-2003) */
435 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ st
->st_blksize
- 1)
436 / (LONGEST
) st
->st_blksize
,
439 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
440 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
441 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
445 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
447 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
448 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
451 static int remote_fio_ctrl_c_flag
= 0;
452 static int remote_fio_no_longjmp
= 0;
454 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
455 static struct sigaction remote_fio_sa
;
456 static struct sigaction remote_fio_osa
;
458 static void (*remote_fio_ofunc
)(int);
462 remote_fileio_sig_init (void)
464 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
465 remote_fio_sa
.sa_handler
= SIG_IGN
;
466 sigemptyset (&remote_fio_sa
.sa_mask
);
467 remote_fio_sa
.sa_flags
= 0;
468 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
470 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
475 remote_fileio_sig_set (void (*sigint_func
)(int))
477 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
478 remote_fio_sa
.sa_handler
= sigint_func
;
479 sigemptyset (&remote_fio_sa
.sa_mask
);
480 remote_fio_sa
.sa_flags
= 0;
481 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
483 signal (SIGINT
, sigint_func
);
488 remote_fileio_sig_exit (void)
490 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
491 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
493 signal (SIGINT
, remote_fio_ofunc
);
498 remote_fileio_ctrl_c_signal_handler (int signo
)
500 remote_fileio_sig_set (SIG_IGN
);
501 remote_fio_ctrl_c_flag
= 1;
502 if (!remote_fio_no_longjmp
)
503 deprecated_throw_reason (RETURN_QUIT
);
504 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
508 remote_fileio_reply (int retcode
, int error
)
512 remote_fileio_sig_set (SIG_IGN
);
519 sprintf (buf
+ strlen (buf
), "%x", retcode
);
520 if (error
|| remote_fio_ctrl_c_flag
)
522 if (error
&& remote_fio_ctrl_c_flag
)
523 error
= FILEIO_EINTR
;
529 sprintf (buf
+ strlen (buf
), ",%x", error
);
530 if (remote_fio_ctrl_c_flag
)
533 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
538 remote_fileio_ioerror (void)
540 remote_fileio_reply (-1, FILEIO_EIO
);
544 remote_fileio_badfd (void)
546 remote_fileio_reply (-1, FILEIO_EBADF
);
550 remote_fileio_return_errno (int retcode
)
552 remote_fileio_reply (retcode
,
553 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
557 remote_fileio_return_success (int retcode
)
559 remote_fileio_reply (retcode
, 0);
562 /* Wrapper function for remote_write_bytes() which has the disadvantage to
563 write only one packet, regardless of the requested number of bytes to
564 transfer. This wrapper calls remote_write_bytes() as often as needed. */
566 remote_fileio_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
568 int ret
= 0, written
;
570 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
581 remote_fileio_func_open (char *buf
)
584 int length
, retlength
;
591 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
592 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
594 remote_fileio_ioerror ();
597 /* 2. Parameter: open flags */
598 if (remote_fileio_extract_int (&buf
, &num
))
600 remote_fileio_ioerror ();
603 flags
= remote_fileio_oflags_to_host (num
);
604 /* 3. Parameter: open mode */
605 if (remote_fileio_extract_int (&buf
, &num
))
607 remote_fileio_ioerror ();
610 mode
= remote_fileio_mode_to_host (num
, 1);
612 /* Request pathname using 'm' packet */
613 pathname
= alloca (length
);
614 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
615 if (retlength
!= length
)
617 remote_fileio_ioerror ();
621 /* Check if pathname exists and is not a regular file or directory. If so,
622 return an appropriate error code. Same for trying to open directories
624 if (!stat (pathname
, &st
))
626 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
628 remote_fileio_reply (-1, FILEIO_ENODEV
);
631 if (S_ISDIR (st
.st_mode
)
632 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
634 remote_fileio_reply (-1, FILEIO_EISDIR
);
639 remote_fio_no_longjmp
= 1;
640 fd
= open (pathname
, flags
, mode
);
643 remote_fileio_return_errno (-1);
647 fd
= remote_fileio_fd_to_targetfd (fd
);
648 remote_fileio_return_success (fd
);
652 remote_fileio_func_close (char *buf
)
657 /* Parameter: file descriptor */
658 if (remote_fileio_extract_int (&buf
, &num
))
660 remote_fileio_ioerror ();
663 fd
= remote_fileio_map_fd ((int) num
);
664 if (fd
== FIO_FD_INVALID
)
666 remote_fileio_badfd ();
670 remote_fio_no_longjmp
= 1;
671 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
672 remote_fileio_return_errno (-1);
673 remote_fileio_close_target_fd ((int) num
);
674 remote_fileio_return_success (0);
678 remote_fileio_func_read (char *buf
)
683 int fd
, ret
, retlength
;
686 off_t old_offset
, new_offset
;
688 /* 1. Parameter: file descriptor */
689 if (remote_fileio_extract_int (&buf
, &target_fd
))
691 remote_fileio_ioerror ();
694 fd
= remote_fileio_map_fd ((int) target_fd
);
695 if (fd
== FIO_FD_INVALID
)
697 remote_fileio_badfd ();
700 /* 2. Parameter: buffer pointer */
701 if (remote_fileio_extract_long (&buf
, &lnum
))
703 remote_fileio_ioerror ();
706 ptrval
= (CORE_ADDR
) lnum
;
707 /* 3. Parameter: buffer length */
708 if (remote_fileio_extract_int (&buf
, &num
))
710 remote_fileio_ioerror ();
713 length
= (size_t) num
;
717 case FIO_FD_CONSOLE_OUT
:
718 remote_fileio_badfd ();
720 case FIO_FD_CONSOLE_IN
:
722 static char *remaining_buf
= NULL
;
723 static int remaining_length
= 0;
725 buffer
= (char *) xmalloc (32768);
728 remote_fio_no_longjmp
= 1;
729 if (remaining_length
> length
)
731 memcpy (buffer
, remaining_buf
, length
);
732 memmove (remaining_buf
, remaining_buf
+ length
,
733 remaining_length
- length
);
734 remaining_length
-= length
;
739 memcpy (buffer
, remaining_buf
, remaining_length
);
740 xfree (remaining_buf
);
741 remaining_buf
= NULL
;
742 ret
= remaining_length
;
747 ret
= ui_file_read (gdb_stdtargin
, buffer
, 32767);
748 remote_fio_no_longjmp
= 1;
749 if (ret
> 0 && (size_t)ret
> length
)
751 remaining_buf
= (char *) xmalloc (ret
- length
);
752 remaining_length
= ret
- length
;
753 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
760 buffer
= (char *) xmalloc (length
);
761 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
762 for read() to return -1 even if "some" bytes have been read. It
763 has been corrected in SUSv2 but that doesn't help us much...
764 Therefore a complete solution must check how many bytes have been
765 read on EINTR to return a more reliable value to the target */
766 old_offset
= lseek (fd
, 0, SEEK_CUR
);
767 remote_fio_no_longjmp
= 1;
768 ret
= read (fd
, buffer
, length
);
769 if (ret
< 0 && errno
== EINTR
)
771 new_offset
= lseek (fd
, 0, SEEK_CUR
);
772 /* If some data has been read, return the number of bytes read.
773 The Ctrl-C flag is set in remote_fileio_reply() anyway */
774 if (old_offset
!= new_offset
)
775 ret
= new_offset
- old_offset
;
782 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
783 if (retlength
!= ret
)
784 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
788 remote_fileio_return_errno (-1);
790 remote_fileio_return_success (ret
);
796 remote_fileio_func_write (char *buf
)
801 int fd
, ret
, retlength
;
805 /* 1. Parameter: file descriptor */
806 if (remote_fileio_extract_int (&buf
, &target_fd
))
808 remote_fileio_ioerror ();
811 fd
= remote_fileio_map_fd ((int) target_fd
);
812 if (fd
== FIO_FD_INVALID
)
814 remote_fileio_badfd ();
817 /* 2. Parameter: buffer pointer */
818 if (remote_fileio_extract_long (&buf
, &lnum
))
820 remote_fileio_ioerror ();
823 ptrval
= (CORE_ADDR
) lnum
;
824 /* 3. Parameter: buffer length */
825 if (remote_fileio_extract_int (&buf
, &num
))
827 remote_fileio_ioerror ();
830 length
= (size_t) num
;
832 buffer
= (char *) xmalloc (length
);
833 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
834 if (retlength
!= length
)
837 remote_fileio_ioerror ();
841 remote_fio_no_longjmp
= 1;
844 case FIO_FD_CONSOLE_IN
:
845 remote_fileio_badfd ();
847 case FIO_FD_CONSOLE_OUT
:
848 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
, buffer
,
850 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
854 ret
= write (fd
, buffer
, length
);
855 if (ret
< 0 && errno
== EACCES
)
856 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
861 remote_fileio_return_errno (-1);
863 remote_fileio_return_success (ret
);
869 remote_fileio_func_lseek (char *buf
)
876 /* 1. Parameter: file descriptor */
877 if (remote_fileio_extract_int (&buf
, &num
))
879 remote_fileio_ioerror ();
882 fd
= remote_fileio_map_fd ((int) num
);
883 if (fd
== FIO_FD_INVALID
)
885 remote_fileio_badfd ();
888 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
890 remote_fileio_reply (-1, FILEIO_ESPIPE
);
894 /* 2. Parameter: offset */
895 if (remote_fileio_extract_long (&buf
, &lnum
))
897 remote_fileio_ioerror ();
900 offset
= (off_t
) lnum
;
901 /* 3. Parameter: flag */
902 if (remote_fileio_extract_int (&buf
, &num
))
904 remote_fileio_ioerror ();
907 if (remote_fileio_seek_flag_to_host (num
, &flag
))
909 remote_fileio_reply (-1, FILEIO_EINVAL
);
913 remote_fio_no_longjmp
= 1;
914 ret
= lseek (fd
, offset
, flag
);
916 if (ret
== (off_t
) -1)
917 remote_fileio_return_errno (-1);
919 remote_fileio_return_success (ret
);
923 remote_fileio_func_rename (char *buf
)
926 int length
, retlength
;
927 char *oldpath
, *newpath
;
929 struct stat ost
, nst
;
931 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
932 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
934 remote_fileio_ioerror ();
937 /* Request oldpath using 'm' packet */
938 oldpath
= alloca (length
);
939 retlength
= remote_read_bytes (ptrval
, oldpath
, length
);
940 if (retlength
!= length
)
942 remote_fileio_ioerror ();
945 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
946 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
948 remote_fileio_ioerror ();
951 /* Request newpath using 'm' packet */
952 newpath
= alloca (length
);
953 retlength
= remote_read_bytes (ptrval
, newpath
, length
);
954 if (retlength
!= length
)
956 remote_fileio_ioerror ();
960 /* Only operate on regular files and directories */
961 of
= stat (oldpath
, &ost
);
962 nf
= stat (newpath
, &nst
);
963 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
964 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
966 remote_fileio_reply (-1, FILEIO_EACCES
);
970 remote_fio_no_longjmp
= 1;
971 ret
= rename (oldpath
, newpath
);
975 /* Special case: newpath is a non-empty directory. Some systems
976 return ENOTEMPTY, some return EEXIST. We coerce that to be
978 if (errno
== ENOTEMPTY
)
981 /* Workaround some Cygwin problems with correct errnos. */
984 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
986 if (S_ISREG (ost
.st_mode
))
990 char oldfullpath
[PATH_MAX
+ 1];
991 char newfullpath
[PATH_MAX
+ 1];
994 cygwin_conv_to_full_posix_path (oldpath
, oldfullpath
);
995 cygwin_conv_to_full_posix_path (newpath
, newfullpath
);
996 len
= strlen (oldfullpath
);
997 if (newfullpath
[len
] == '/'
998 && !strncmp (oldfullpath
, newfullpath
, len
))
1007 remote_fileio_return_errno (-1);
1010 remote_fileio_return_success (ret
);
1014 remote_fileio_func_unlink (char *buf
)
1017 int length
, retlength
;
1022 /* Parameter: Ptr to pathname / length incl. trailing zero */
1023 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1025 remote_fileio_ioerror ();
1028 /* Request pathname using 'm' packet */
1029 pathname
= alloca (length
);
1030 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
1031 if (retlength
!= length
)
1033 remote_fileio_ioerror ();
1037 /* Only operate on regular files (and directories, which allows to return
1038 the correct return code) */
1039 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1041 remote_fileio_reply (-1, FILEIO_ENODEV
);
1045 remote_fio_no_longjmp
= 1;
1046 ret
= unlink (pathname
);
1049 remote_fileio_return_errno (-1);
1051 remote_fileio_return_success (ret
);
1055 remote_fileio_func_stat (char *buf
)
1058 int ret
, length
, retlength
;
1062 struct fio_stat fst
;
1064 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1065 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1067 remote_fileio_ioerror ();
1070 /* Request pathname using 'm' packet */
1071 pathname
= alloca (length
);
1072 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
1073 if (retlength
!= length
)
1075 remote_fileio_ioerror ();
1079 /* 2. Parameter: Ptr to struct stat */
1080 if (remote_fileio_extract_long (&buf
, &lnum
))
1082 remote_fileio_ioerror ();
1085 ptrval
= (CORE_ADDR
) lnum
;
1087 remote_fio_no_longjmp
= 1;
1088 ret
= stat (pathname
, &st
);
1092 remote_fileio_return_errno (-1);
1095 /* Only operate on regular files and directories */
1096 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1098 remote_fileio_reply (-1, FILEIO_EACCES
);
1103 remote_fileio_to_fio_stat (&st
, &fst
);
1104 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1106 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &fst
, sizeof fst
);
1107 if (retlength
!= sizeof fst
)
1109 remote_fileio_return_errno (-1);
1113 remote_fileio_return_success (ret
);
1117 remote_fileio_func_fstat (char *buf
)
1120 int fd
, ret
, retlength
;
1124 struct fio_stat fst
;
1127 /* 1. Parameter: file descriptor */
1128 if (remote_fileio_extract_int (&buf
, &target_fd
))
1130 remote_fileio_ioerror ();
1133 fd
= remote_fileio_map_fd ((int) target_fd
);
1134 if (fd
== FIO_FD_INVALID
)
1136 remote_fileio_badfd ();
1139 /* 2. Parameter: Ptr to struct stat */
1140 if (remote_fileio_extract_long (&buf
, &lnum
))
1142 remote_fileio_ioerror ();
1145 ptrval
= (CORE_ADDR
) lnum
;
1147 remote_fio_no_longjmp
= 1;
1148 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1150 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1151 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1153 st
.st_uid
= getuid ();
1154 st
.st_gid
= getgid ();
1157 st
.st_blksize
= 512;
1158 #if HAVE_STRUCT_STAT_ST_BLOCKS
1161 if (!gettimeofday (&tv
, NULL
))
1162 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1164 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1168 ret
= fstat (fd
, &st
);
1172 remote_fileio_return_errno (-1);
1177 remote_fileio_to_fio_stat (&st
, &fst
);
1179 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &fst
, sizeof fst
);
1180 if (retlength
!= sizeof fst
)
1182 remote_fileio_return_errno (-1);
1186 remote_fileio_return_success (ret
);
1190 remote_fileio_func_gettimeofday (char *buf
)
1196 struct fio_timeval ftv
;
1198 /* 1. Parameter: struct timeval pointer */
1199 if (remote_fileio_extract_long (&buf
, &lnum
))
1201 remote_fileio_ioerror ();
1204 ptrval
= (CORE_ADDR
) lnum
;
1205 /* 2. Parameter: some pointer value... */
1206 if (remote_fileio_extract_long (&buf
, &lnum
))
1208 remote_fileio_ioerror ();
1211 /* ...which has to be NULL */
1214 remote_fileio_reply (-1, FILEIO_EINVAL
);
1218 remote_fio_no_longjmp
= 1;
1219 ret
= gettimeofday (&tv
, NULL
);
1223 remote_fileio_return_errno (-1);
1229 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1231 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &ftv
, sizeof ftv
);
1232 if (retlength
!= sizeof ftv
)
1234 remote_fileio_return_errno (-1);
1238 remote_fileio_return_success (ret
);
1242 remote_fileio_func_isatty (char *buf
)
1247 /* Parameter: file descriptor */
1248 if (remote_fileio_extract_int (&buf
, &target_fd
))
1250 remote_fileio_ioerror ();
1253 remote_fio_no_longjmp
= 1;
1254 fd
= remote_fileio_map_fd ((int) target_fd
);
1255 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1256 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1260 remote_fileio_func_system (char *buf
)
1263 int ret
, length
, retlength
;
1266 /* Check if system(3) has been explicitely allowed using the
1267 `set remote system-call-allowed 1' command. If not, return
1269 if (!remote_fio_system_call_allowed
)
1271 remote_fileio_reply (-1, FILEIO_EPERM
);
1275 /* Parameter: Ptr to commandline / length incl. trailing zero */
1276 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1278 remote_fileio_ioerror ();
1281 /* Request commandline using 'm' packet */
1282 cmdline
= alloca (length
);
1283 retlength
= remote_read_bytes (ptrval
, cmdline
, length
);
1284 if (retlength
!= length
)
1286 remote_fileio_ioerror ();
1290 remote_fio_no_longjmp
= 1;
1291 ret
= system (cmdline
);
1294 remote_fileio_return_errno (-1);
1296 remote_fileio_return_success (WEXITSTATUS (ret
));
1301 void (*func
)(char *);
1302 } remote_fio_func_map
[] = {
1303 "open", remote_fileio_func_open
,
1304 "close", remote_fileio_func_close
,
1305 "read", remote_fileio_func_read
,
1306 "write", remote_fileio_func_write
,
1307 "lseek", remote_fileio_func_lseek
,
1308 "rename", remote_fileio_func_rename
,
1309 "unlink", remote_fileio_func_unlink
,
1310 "stat", remote_fileio_func_stat
,
1311 "fstat", remote_fileio_func_fstat
,
1312 "gettimeofday", remote_fileio_func_gettimeofday
,
1313 "isatty", remote_fileio_func_isatty
,
1314 "system", remote_fileio_func_system
,
1319 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1321 char *buf
= buf_arg
;
1325 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1327 c
= strchr (++buf
, ',');
1331 c
= strchr (buf
, '\0');
1332 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1333 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1335 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1336 return RETURN_ERROR
;
1337 remote_fio_func_map
[idx
].func (c
);
1342 remote_fileio_request (char *buf
)
1346 remote_fileio_sig_init ();
1348 remote_fio_ctrl_c_flag
= 0;
1349 remote_fio_no_longjmp
= 0;
1351 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1356 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1359 remote_fileio_reply (-1, FILEIO_EINTR
);
1365 remote_fileio_sig_exit ();
1369 set_system_call_allowed (char *args
, int from_tty
)
1374 int val
= strtoul (args
, &arg_end
, 10);
1375 if (*args
&& *arg_end
== '\0')
1377 remote_fio_system_call_allowed
= !!val
;
1381 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1385 show_system_call_allowed (char *args
, int from_tty
)
1388 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1389 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1390 remote_fio_system_call_allowed
? "" : "not ");
1394 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1395 struct cmd_list_element
*remote_show_cmdlist
)
1397 add_cmd ("system-call-allowed", no_class
,
1398 set_system_call_allowed
,
1399 _("Set if the host system(3) call is allowed for the target."),
1400 &remote_set_cmdlist
);
1401 add_cmd ("system-call-allowed", no_class
,
1402 show_system_call_allowed
,
1403 _("Show if the host system(3) call is allowed for the target."),
1404 &remote_show_cmdlist
);