1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
23 #include "gdb_string.h"
26 #include "gdb/fileio.h"
29 #include "exceptions.h"
30 #include "remote-fileio.h"
31 #include "event-loop.h"
36 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
45 #define FIO_FD_INVALID -1
46 #define FIO_FD_CONSOLE_IN -2
47 #define FIO_FD_CONSOLE_OUT -3
49 static int remote_fio_system_call_allowed
= 0;
51 static struct async_signal_handler
*sigint_fileio_token
;
54 remote_fileio_init_fd_map (void)
58 if (!remote_fio_data
.fd_map
)
60 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
61 remote_fio_data
.fd_map_size
= 10;
62 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
63 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
64 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
65 for (i
= 3; i
< 10; ++i
)
66 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
72 remote_fileio_resize_fd_map (void)
74 int i
= remote_fio_data
.fd_map_size
;
76 if (!remote_fio_data
.fd_map
)
77 return remote_fileio_init_fd_map ();
78 remote_fio_data
.fd_map_size
+= 10;
79 remote_fio_data
.fd_map
=
80 (int *) xrealloc (remote_fio_data
.fd_map
,
81 remote_fio_data
.fd_map_size
* sizeof (int));
82 for (; i
< remote_fio_data
.fd_map_size
; i
++)
83 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
84 return remote_fio_data
.fd_map_size
- 10;
88 remote_fileio_next_free_fd (void)
92 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
93 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
95 return remote_fileio_resize_fd_map ();
99 remote_fileio_fd_to_targetfd (int fd
)
101 int target_fd
= remote_fileio_next_free_fd ();
102 remote_fio_data
.fd_map
[target_fd
] = fd
;
107 remote_fileio_map_fd (int target_fd
)
109 remote_fileio_init_fd_map ();
110 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
111 return FIO_FD_INVALID
;
112 return remote_fio_data
.fd_map
[target_fd
];
116 remote_fileio_close_target_fd (int target_fd
)
118 remote_fileio_init_fd_map ();
119 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
120 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
124 remote_fileio_oflags_to_host (long flags
)
128 if (flags
& FILEIO_O_CREAT
)
130 if (flags
& FILEIO_O_EXCL
)
132 if (flags
& FILEIO_O_TRUNC
)
134 if (flags
& FILEIO_O_APPEND
)
136 if (flags
& FILEIO_O_RDONLY
)
138 if (flags
& FILEIO_O_WRONLY
)
140 if (flags
& FILEIO_O_RDWR
)
142 /* On systems supporting binary and text mode, always open files in
151 remote_fileio_mode_to_host (long mode
, int open_call
)
157 if (mode
& FILEIO_S_IFREG
)
159 if (mode
& FILEIO_S_IFDIR
)
161 if (mode
& FILEIO_S_IFCHR
)
164 if (mode
& FILEIO_S_IRUSR
)
166 if (mode
& FILEIO_S_IWUSR
)
168 if (mode
& FILEIO_S_IXUSR
)
171 if (mode
& FILEIO_S_IRGRP
)
175 if (mode
& FILEIO_S_IWGRP
)
179 if (mode
& FILEIO_S_IXGRP
)
182 if (mode
& FILEIO_S_IROTH
)
185 if (mode
& FILEIO_S_IWOTH
)
189 if (mode
& FILEIO_S_IXOTH
)
196 remote_fileio_mode_to_target (mode_t mode
)
201 tmode
|= FILEIO_S_IFREG
;
203 tmode
|= FILEIO_S_IFDIR
;
205 tmode
|= FILEIO_S_IFCHR
;
207 tmode
|= FILEIO_S_IRUSR
;
209 tmode
|= FILEIO_S_IWUSR
;
211 tmode
|= FILEIO_S_IXUSR
;
214 tmode
|= FILEIO_S_IRGRP
;
218 tmode
|= FILEIO_S_IWGRP
;
222 tmode
|= FILEIO_S_IXGRP
;
225 tmode
|= FILEIO_S_IROTH
;
228 tmode
|= FILEIO_S_IWOTH
;
232 tmode
|= FILEIO_S_IXOTH
;
238 remote_fileio_errno_to_target (int error
)
245 return FILEIO_ENOENT
;
253 return FILEIO_EACCES
;
255 return FILEIO_EFAULT
;
259 return FILEIO_EEXIST
;
261 return FILEIO_ENODEV
;
263 return FILEIO_ENOTDIR
;
265 return FILEIO_EISDIR
;
267 return FILEIO_EINVAL
;
269 return FILEIO_ENFILE
;
271 return FILEIO_EMFILE
;
275 return FILEIO_ENOSPC
;
277 return FILEIO_ESPIPE
;
281 return FILEIO_ENOSYS
;
283 return FILEIO_ENAMETOOLONG
;
285 return FILEIO_EUNKNOWN
;
289 remote_fileio_seek_flag_to_host (long num
, int *flag
)
295 case FILEIO_SEEK_SET
:
298 case FILEIO_SEEK_CUR
:
301 case FILEIO_SEEK_END
:
311 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
316 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
318 c
= strchr (*buf
, ',');
322 c
= strchr (*buf
, '\0');
323 while (strchr ("+-", **buf
))
329 for (*retlong
= 0; **buf
; ++*buf
)
332 if (**buf
>= '0' && **buf
<= '9')
333 *retlong
+= **buf
- '0';
334 else if (**buf
>= 'a' && **buf
<= 'f')
335 *retlong
+= **buf
- 'a' + 10;
336 else if (**buf
>= 'A' && **buf
<= 'F')
337 *retlong
+= **buf
- 'A' + 10;
347 remote_fileio_extract_int (char **buf
, long *retint
)
354 ret
= remote_fileio_extract_long (buf
, &retlong
);
356 *retint
= (long) retlong
;
361 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
366 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
368 c
= strchr (*buf
, '/');
372 if (remote_fileio_extract_long (buf
, &retlong
))
374 *ptrval
= (CORE_ADDR
) retlong
;
376 if (remote_fileio_extract_long (buf
, &retlong
))
378 *length
= (int) retlong
;
382 /* Convert to big endian */
384 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
388 for (i
= 0; i
< bytes
; ++i
)
389 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
393 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
395 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
399 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
401 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
405 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
407 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
411 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
413 remote_fileio_to_be (num
, (char *) fnum
, 8);
417 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
419 remote_fileio_to_be (num
, (char *) fnum
, 8);
423 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
427 /* `st_dev' is set in the calling function */
428 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
429 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
430 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
431 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
432 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
433 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
434 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
435 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
436 blksize
= st
->st_blksize
;
440 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
441 #if HAVE_STRUCT_STAT_ST_BLOCKS
442 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
444 /* FIXME: This is correct for DJGPP, but other systems that don't
445 have st_blocks, if any, might prefer 512 instead of st_blksize.
446 (eliz, 30-12-2003) */
447 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
451 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
452 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
453 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
457 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
459 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
460 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
463 static int remote_fio_ctrl_c_flag
= 0;
464 static int remote_fio_no_longjmp
= 0;
466 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
467 static struct sigaction remote_fio_sa
;
468 static struct sigaction remote_fio_osa
;
470 static void (*remote_fio_ofunc
)(int);
474 remote_fileio_sig_init (void)
476 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
477 remote_fio_sa
.sa_handler
= SIG_IGN
;
478 sigemptyset (&remote_fio_sa
.sa_mask
);
479 remote_fio_sa
.sa_flags
= 0;
480 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
482 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
487 remote_fileio_sig_set (void (*sigint_func
)(int))
489 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
490 remote_fio_sa
.sa_handler
= sigint_func
;
491 sigemptyset (&remote_fio_sa
.sa_mask
);
492 remote_fio_sa
.sa_flags
= 0;
493 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
495 signal (SIGINT
, sigint_func
);
500 remote_fileio_sig_exit (void)
502 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
503 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
505 signal (SIGINT
, remote_fio_ofunc
);
510 async_remote_fileio_interrupt (gdb_client_data arg
)
512 deprecated_throw_reason (RETURN_QUIT
);
516 remote_fileio_ctrl_c_signal_handler (int signo
)
518 remote_fileio_sig_set (SIG_IGN
);
519 remote_fio_ctrl_c_flag
= 1;
520 if (!remote_fio_no_longjmp
)
521 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
522 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
526 remote_fileio_reply (int retcode
, int error
)
530 remote_fileio_sig_set (SIG_IGN
);
537 sprintf (buf
+ strlen (buf
), "%x", retcode
);
538 if (error
|| remote_fio_ctrl_c_flag
)
540 if (error
&& remote_fio_ctrl_c_flag
)
541 error
= FILEIO_EINTR
;
547 sprintf (buf
+ strlen (buf
), ",%x", error
);
548 if (remote_fio_ctrl_c_flag
)
551 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
556 remote_fileio_ioerror (void)
558 remote_fileio_reply (-1, FILEIO_EIO
);
562 remote_fileio_badfd (void)
564 remote_fileio_reply (-1, FILEIO_EBADF
);
568 remote_fileio_return_errno (int retcode
)
570 remote_fileio_reply (retcode
,
571 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
575 remote_fileio_return_success (int retcode
)
577 remote_fileio_reply (retcode
, 0);
580 /* Wrapper function for remote_write_bytes() which has the disadvantage to
581 write only one packet, regardless of the requested number of bytes to
582 transfer. This wrapper calls remote_write_bytes() as often as needed. */
584 remote_fileio_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
586 int ret
= 0, written
;
588 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
599 remote_fileio_func_open (char *buf
)
602 int length
, retlength
;
609 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
610 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
612 remote_fileio_ioerror ();
615 /* 2. Parameter: open flags */
616 if (remote_fileio_extract_int (&buf
, &num
))
618 remote_fileio_ioerror ();
621 flags
= remote_fileio_oflags_to_host (num
);
622 /* 3. Parameter: open mode */
623 if (remote_fileio_extract_int (&buf
, &num
))
625 remote_fileio_ioerror ();
628 mode
= remote_fileio_mode_to_host (num
, 1);
630 /* Request pathname using 'm' packet */
631 pathname
= alloca (length
);
632 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
633 if (retlength
!= length
)
635 remote_fileio_ioerror ();
639 /* Check if pathname exists and is not a regular file or directory. If so,
640 return an appropriate error code. Same for trying to open directories
642 if (!stat (pathname
, &st
))
644 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
646 remote_fileio_reply (-1, FILEIO_ENODEV
);
649 if (S_ISDIR (st
.st_mode
)
650 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
652 remote_fileio_reply (-1, FILEIO_EISDIR
);
657 remote_fio_no_longjmp
= 1;
658 fd
= open (pathname
, flags
, mode
);
661 remote_fileio_return_errno (-1);
665 fd
= remote_fileio_fd_to_targetfd (fd
);
666 remote_fileio_return_success (fd
);
670 remote_fileio_func_close (char *buf
)
675 /* Parameter: file descriptor */
676 if (remote_fileio_extract_int (&buf
, &num
))
678 remote_fileio_ioerror ();
681 fd
= remote_fileio_map_fd ((int) num
);
682 if (fd
== FIO_FD_INVALID
)
684 remote_fileio_badfd ();
688 remote_fio_no_longjmp
= 1;
689 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
690 remote_fileio_return_errno (-1);
691 remote_fileio_close_target_fd ((int) num
);
692 remote_fileio_return_success (0);
696 remote_fileio_func_read (char *buf
)
701 int fd
, ret
, retlength
;
704 off_t old_offset
, new_offset
;
706 /* 1. Parameter: file descriptor */
707 if (remote_fileio_extract_int (&buf
, &target_fd
))
709 remote_fileio_ioerror ();
712 fd
= remote_fileio_map_fd ((int) target_fd
);
713 if (fd
== FIO_FD_INVALID
)
715 remote_fileio_badfd ();
718 /* 2. Parameter: buffer pointer */
719 if (remote_fileio_extract_long (&buf
, &lnum
))
721 remote_fileio_ioerror ();
724 ptrval
= (CORE_ADDR
) lnum
;
725 /* 3. Parameter: buffer length */
726 if (remote_fileio_extract_int (&buf
, &num
))
728 remote_fileio_ioerror ();
731 length
= (size_t) num
;
735 case FIO_FD_CONSOLE_OUT
:
736 remote_fileio_badfd ();
738 case FIO_FD_CONSOLE_IN
:
740 static char *remaining_buf
= NULL
;
741 static int remaining_length
= 0;
743 buffer
= (gdb_byte
*) xmalloc (32768);
746 remote_fio_no_longjmp
= 1;
747 if (remaining_length
> length
)
749 memcpy (buffer
, remaining_buf
, length
);
750 memmove (remaining_buf
, remaining_buf
+ length
,
751 remaining_length
- length
);
752 remaining_length
-= length
;
757 memcpy (buffer
, remaining_buf
, remaining_length
);
758 xfree (remaining_buf
);
759 remaining_buf
= NULL
;
760 ret
= remaining_length
;
765 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 32767);
766 remote_fio_no_longjmp
= 1;
767 if (ret
> 0 && (size_t)ret
> length
)
769 remaining_buf
= (char *) xmalloc (ret
- length
);
770 remaining_length
= ret
- length
;
771 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
778 buffer
= (gdb_byte
*) xmalloc (length
);
779 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
780 for read() to return -1 even if "some" bytes have been read. It
781 has been corrected in SUSv2 but that doesn't help us much...
782 Therefore a complete solution must check how many bytes have been
783 read on EINTR to return a more reliable value to the target */
784 old_offset
= lseek (fd
, 0, SEEK_CUR
);
785 remote_fio_no_longjmp
= 1;
786 ret
= read (fd
, buffer
, length
);
787 if (ret
< 0 && errno
== EINTR
)
789 new_offset
= lseek (fd
, 0, SEEK_CUR
);
790 /* If some data has been read, return the number of bytes read.
791 The Ctrl-C flag is set in remote_fileio_reply() anyway */
792 if (old_offset
!= new_offset
)
793 ret
= new_offset
- old_offset
;
800 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
801 if (retlength
!= ret
)
802 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
806 remote_fileio_return_errno (-1);
808 remote_fileio_return_success (ret
);
814 remote_fileio_func_write (char *buf
)
819 int fd
, ret
, retlength
;
823 /* 1. Parameter: file descriptor */
824 if (remote_fileio_extract_int (&buf
, &target_fd
))
826 remote_fileio_ioerror ();
829 fd
= remote_fileio_map_fd ((int) target_fd
);
830 if (fd
== FIO_FD_INVALID
)
832 remote_fileio_badfd ();
835 /* 2. Parameter: buffer pointer */
836 if (remote_fileio_extract_long (&buf
, &lnum
))
838 remote_fileio_ioerror ();
841 ptrval
= (CORE_ADDR
) lnum
;
842 /* 3. Parameter: buffer length */
843 if (remote_fileio_extract_int (&buf
, &num
))
845 remote_fileio_ioerror ();
848 length
= (size_t) num
;
850 buffer
= (gdb_byte
*) xmalloc (length
);
851 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
852 if (retlength
!= length
)
855 remote_fileio_ioerror ();
859 remote_fio_no_longjmp
= 1;
862 case FIO_FD_CONSOLE_IN
:
863 remote_fileio_badfd ();
866 case FIO_FD_CONSOLE_OUT
:
867 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
868 (char *) buffer
, length
);
869 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
873 ret
= write (fd
, buffer
, length
);
874 if (ret
< 0 && errno
== EACCES
)
875 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
880 remote_fileio_return_errno (-1);
882 remote_fileio_return_success (ret
);
888 remote_fileio_func_lseek (char *buf
)
895 /* 1. Parameter: file descriptor */
896 if (remote_fileio_extract_int (&buf
, &num
))
898 remote_fileio_ioerror ();
901 fd
= remote_fileio_map_fd ((int) num
);
902 if (fd
== FIO_FD_INVALID
)
904 remote_fileio_badfd ();
907 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
909 remote_fileio_reply (-1, FILEIO_ESPIPE
);
913 /* 2. Parameter: offset */
914 if (remote_fileio_extract_long (&buf
, &lnum
))
916 remote_fileio_ioerror ();
919 offset
= (off_t
) lnum
;
920 /* 3. Parameter: flag */
921 if (remote_fileio_extract_int (&buf
, &num
))
923 remote_fileio_ioerror ();
926 if (remote_fileio_seek_flag_to_host (num
, &flag
))
928 remote_fileio_reply (-1, FILEIO_EINVAL
);
932 remote_fio_no_longjmp
= 1;
933 ret
= lseek (fd
, offset
, flag
);
935 if (ret
== (off_t
) -1)
936 remote_fileio_return_errno (-1);
938 remote_fileio_return_success (ret
);
942 remote_fileio_func_rename (char *buf
)
944 CORE_ADDR old_ptr
, new_ptr
;
945 int old_len
, new_len
, retlength
;
946 char *oldpath
, *newpath
;
948 struct stat ost
, nst
;
950 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
951 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
953 remote_fileio_ioerror ();
957 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
958 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
960 remote_fileio_ioerror ();
964 /* Request oldpath using 'm' packet */
965 oldpath
= alloca (old_len
);
966 retlength
= remote_read_bytes (old_ptr
, (gdb_byte
*) oldpath
, old_len
);
967 if (retlength
!= old_len
)
969 remote_fileio_ioerror ();
973 /* Request newpath using 'm' packet */
974 newpath
= alloca (new_len
);
975 retlength
= remote_read_bytes (new_ptr
, (gdb_byte
*) newpath
, new_len
);
976 if (retlength
!= new_len
)
978 remote_fileio_ioerror ();
982 /* Only operate on regular files and directories */
983 of
= stat (oldpath
, &ost
);
984 nf
= stat (newpath
, &nst
);
985 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
986 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
988 remote_fileio_reply (-1, FILEIO_EACCES
);
992 remote_fio_no_longjmp
= 1;
993 ret
= rename (oldpath
, newpath
);
997 /* Special case: newpath is a non-empty directory. Some systems
998 return ENOTEMPTY, some return EEXIST. We coerce that to be
1000 if (errno
== ENOTEMPTY
)
1003 /* Workaround some Cygwin problems with correct errnos. */
1004 if (errno
== EACCES
)
1006 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1008 if (S_ISREG (ost
.st_mode
))
1012 char oldfullpath
[PATH_MAX
+ 1];
1013 char newfullpath
[PATH_MAX
+ 1];
1016 cygwin_conv_to_full_posix_path (oldpath
, oldfullpath
);
1017 cygwin_conv_to_full_posix_path (newpath
, newfullpath
);
1018 len
= strlen (oldfullpath
);
1019 if (newfullpath
[len
] == '/'
1020 && !strncmp (oldfullpath
, newfullpath
, len
))
1029 remote_fileio_return_errno (-1);
1032 remote_fileio_return_success (ret
);
1036 remote_fileio_func_unlink (char *buf
)
1039 int length
, retlength
;
1044 /* Parameter: Ptr to pathname / length incl. trailing zero */
1045 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1047 remote_fileio_ioerror ();
1050 /* Request pathname using 'm' packet */
1051 pathname
= alloca (length
);
1052 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1053 if (retlength
!= length
)
1055 remote_fileio_ioerror ();
1059 /* Only operate on regular files (and directories, which allows to return
1060 the correct return code) */
1061 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1063 remote_fileio_reply (-1, FILEIO_ENODEV
);
1067 remote_fio_no_longjmp
= 1;
1068 ret
= unlink (pathname
);
1071 remote_fileio_return_errno (-1);
1073 remote_fileio_return_success (ret
);
1077 remote_fileio_func_stat (char *buf
)
1079 CORE_ADDR statptr
, nameptr
;
1080 int ret
, namelength
, retlength
;
1084 struct fio_stat fst
;
1086 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1087 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1089 remote_fileio_ioerror ();
1093 /* 2. Parameter: Ptr to struct stat */
1094 if (remote_fileio_extract_long (&buf
, &lnum
))
1096 remote_fileio_ioerror ();
1099 statptr
= (CORE_ADDR
) lnum
;
1101 /* Request pathname using 'm' packet */
1102 pathname
= alloca (namelength
);
1103 retlength
= remote_read_bytes (nameptr
, (gdb_byte
*) pathname
, namelength
);
1104 if (retlength
!= namelength
)
1106 remote_fileio_ioerror ();
1110 remote_fio_no_longjmp
= 1;
1111 ret
= stat (pathname
, &st
);
1115 remote_fileio_return_errno (-1);
1118 /* Only operate on regular files and directories */
1119 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1121 remote_fileio_reply (-1, FILEIO_EACCES
);
1126 remote_fileio_to_fio_stat (&st
, &fst
);
1127 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1129 retlength
= remote_fileio_write_bytes (statptr
,
1130 (gdb_byte
*) &fst
, sizeof fst
);
1131 if (retlength
!= sizeof fst
)
1133 remote_fileio_return_errno (-1);
1137 remote_fileio_return_success (ret
);
1141 remote_fileio_func_fstat (char *buf
)
1144 int fd
, ret
, retlength
;
1148 struct fio_stat fst
;
1151 /* 1. Parameter: file descriptor */
1152 if (remote_fileio_extract_int (&buf
, &target_fd
))
1154 remote_fileio_ioerror ();
1157 fd
= remote_fileio_map_fd ((int) target_fd
);
1158 if (fd
== FIO_FD_INVALID
)
1160 remote_fileio_badfd ();
1163 /* 2. Parameter: Ptr to struct stat */
1164 if (remote_fileio_extract_long (&buf
, &lnum
))
1166 remote_fileio_ioerror ();
1169 ptrval
= (CORE_ADDR
) lnum
;
1171 remote_fio_no_longjmp
= 1;
1172 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1174 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1175 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1178 st
.st_uid
= getuid ();
1183 st
.st_gid
= getgid ();
1189 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1190 st
.st_blksize
= 512;
1192 #if HAVE_STRUCT_STAT_ST_BLOCKS
1195 if (!gettimeofday (&tv
, NULL
))
1196 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1198 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1202 ret
= fstat (fd
, &st
);
1206 remote_fileio_return_errno (-1);
1211 remote_fileio_to_fio_stat (&st
, &fst
);
1213 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1214 if (retlength
!= sizeof fst
)
1216 remote_fileio_return_errno (-1);
1220 remote_fileio_return_success (ret
);
1224 remote_fileio_func_gettimeofday (char *buf
)
1230 struct fio_timeval ftv
;
1232 /* 1. Parameter: struct timeval pointer */
1233 if (remote_fileio_extract_long (&buf
, &lnum
))
1235 remote_fileio_ioerror ();
1238 ptrval
= (CORE_ADDR
) lnum
;
1239 /* 2. Parameter: some pointer value... */
1240 if (remote_fileio_extract_long (&buf
, &lnum
))
1242 remote_fileio_ioerror ();
1245 /* ...which has to be NULL */
1248 remote_fileio_reply (-1, FILEIO_EINVAL
);
1252 remote_fio_no_longjmp
= 1;
1253 ret
= gettimeofday (&tv
, NULL
);
1257 remote_fileio_return_errno (-1);
1263 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1265 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1266 if (retlength
!= sizeof ftv
)
1268 remote_fileio_return_errno (-1);
1272 remote_fileio_return_success (ret
);
1276 remote_fileio_func_isatty (char *buf
)
1281 /* Parameter: file descriptor */
1282 if (remote_fileio_extract_int (&buf
, &target_fd
))
1284 remote_fileio_ioerror ();
1287 remote_fio_no_longjmp
= 1;
1288 fd
= remote_fileio_map_fd ((int) target_fd
);
1289 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1290 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1294 remote_fileio_func_system (char *buf
)
1297 int ret
, length
, retlength
;
1298 char *cmdline
= NULL
;
1300 /* Parameter: Ptr to commandline / length incl. trailing zero */
1301 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1303 remote_fileio_ioerror ();
1309 /* Request commandline using 'm' packet */
1310 cmdline
= alloca (length
);
1311 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1312 if (retlength
!= length
)
1314 remote_fileio_ioerror ();
1319 /* Check if system(3) has been explicitely allowed using the
1320 `set remote system-call-allowed 1' command. If length is 0,
1321 indicating a NULL parameter to the system call, return zero to
1322 indicate a shell is not available. Otherwise fail with EPERM. */
1323 if (!remote_fio_system_call_allowed
)
1326 remote_fileio_return_success (0);
1328 remote_fileio_reply (-1, FILEIO_EPERM
);
1332 remote_fio_no_longjmp
= 1;
1333 ret
= system (cmdline
);
1336 remote_fileio_return_success (ret
);
1338 remote_fileio_return_errno (-1);
1340 remote_fileio_return_success (WEXITSTATUS (ret
));
1345 void (*func
)(char *);
1346 } remote_fio_func_map
[] = {
1347 { "open", remote_fileio_func_open
},
1348 { "close", remote_fileio_func_close
},
1349 { "read", remote_fileio_func_read
},
1350 { "write", remote_fileio_func_write
},
1351 { "lseek", remote_fileio_func_lseek
},
1352 { "rename", remote_fileio_func_rename
},
1353 { "unlink", remote_fileio_func_unlink
},
1354 { "stat", remote_fileio_func_stat
},
1355 { "fstat", remote_fileio_func_fstat
},
1356 { "gettimeofday", remote_fileio_func_gettimeofday
},
1357 { "isatty", remote_fileio_func_isatty
},
1358 { "system", remote_fileio_func_system
},
1363 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1365 char *buf
= buf_arg
;
1369 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1371 c
= strchr (++buf
, ',');
1375 c
= strchr (buf
, '\0');
1376 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1377 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1379 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1380 return RETURN_ERROR
;
1381 remote_fio_func_map
[idx
].func (c
);
1385 /* Close any open descriptors, and reinitialize the file mapping. */
1388 remote_fileio_reset (void)
1392 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1394 int fd
= remote_fio_data
.fd_map
[ix
];
1399 if (remote_fio_data
.fd_map
)
1401 xfree (remote_fio_data
.fd_map
);
1402 remote_fio_data
.fd_map
= NULL
;
1403 remote_fio_data
.fd_map_size
= 0;
1408 remote_fileio_request (char *buf
)
1412 remote_fileio_sig_init ();
1414 remote_fio_ctrl_c_flag
= 0;
1415 remote_fio_no_longjmp
= 0;
1417 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1422 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1425 remote_fileio_reply (-1, FILEIO_EINTR
);
1431 remote_fileio_sig_exit ();
1435 set_system_call_allowed (char *args
, int from_tty
)
1440 int val
= strtoul (args
, &arg_end
, 10);
1441 if (*args
&& *arg_end
== '\0')
1443 remote_fio_system_call_allowed
= !!val
;
1447 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1451 show_system_call_allowed (char *args
, int from_tty
)
1454 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1455 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1456 remote_fio_system_call_allowed
? "" : "not ");
1460 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1461 struct cmd_list_element
*remote_show_cmdlist
)
1463 sigint_fileio_token
=
1464 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1466 add_cmd ("system-call-allowed", no_class
,
1467 set_system_call_allowed
,
1468 _("Set if the host system(3) call is allowed for the target."),
1469 &remote_set_cmdlist
);
1470 add_cmd ("system-call-allowed", no_class
,
1471 show_system_call_allowed
,
1472 _("Show if the host system(3) call is allowed for the target."),
1473 &remote_show_cmdlist
);