1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
28 #include "gdb/fileio.h"
31 #include "exceptions.h"
32 #include "remote-fileio.h"
37 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
46 #define FIO_FD_INVALID -1
47 #define FIO_FD_CONSOLE_IN -2
48 #define FIO_FD_CONSOLE_OUT -3
50 static int remote_fio_system_call_allowed
= 0;
53 remote_fileio_init_fd_map (void)
57 if (!remote_fio_data
.fd_map
)
59 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
60 remote_fio_data
.fd_map_size
= 10;
61 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
62 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
63 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
64 for (i
= 3; i
< 10; ++i
)
65 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
71 remote_fileio_resize_fd_map (void)
73 if (!remote_fio_data
.fd_map
)
74 return remote_fileio_init_fd_map ();
75 remote_fio_data
.fd_map_size
+= 10;
76 remote_fio_data
.fd_map
=
77 (int *) xrealloc (remote_fio_data
.fd_map
,
78 remote_fio_data
.fd_map_size
* sizeof (int));
79 return remote_fio_data
.fd_map_size
- 10;
83 remote_fileio_next_free_fd (void)
87 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
88 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
90 return remote_fileio_resize_fd_map ();
94 remote_fileio_fd_to_targetfd (int fd
)
96 int target_fd
= remote_fileio_next_free_fd ();
97 remote_fio_data
.fd_map
[target_fd
] = fd
;
102 remote_fileio_map_fd (int target_fd
)
104 remote_fileio_init_fd_map ();
105 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
106 return FIO_FD_INVALID
;
107 return remote_fio_data
.fd_map
[target_fd
];
111 remote_fileio_close_target_fd (int target_fd
)
113 remote_fileio_init_fd_map ();
114 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
115 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
119 remote_fileio_oflags_to_host (long flags
)
123 if (flags
& FILEIO_O_CREAT
)
125 if (flags
& FILEIO_O_EXCL
)
127 if (flags
& FILEIO_O_TRUNC
)
129 if (flags
& FILEIO_O_APPEND
)
131 if (flags
& FILEIO_O_RDONLY
)
133 if (flags
& FILEIO_O_WRONLY
)
135 if (flags
& FILEIO_O_RDWR
)
137 /* On systems supporting binary and text mode, always open files in
146 remote_fileio_mode_to_host (long mode
, int open_call
)
152 if (mode
& FILEIO_S_IFREG
)
154 if (mode
& FILEIO_S_IFDIR
)
156 if (mode
& FILEIO_S_IFCHR
)
159 if (mode
& FILEIO_S_IRUSR
)
161 if (mode
& FILEIO_S_IWUSR
)
163 if (mode
& FILEIO_S_IXUSR
)
166 if (mode
& FILEIO_S_IRGRP
)
170 if (mode
& FILEIO_S_IWGRP
)
174 if (mode
& FILEIO_S_IXGRP
)
177 if (mode
& FILEIO_S_IROTH
)
180 if (mode
& FILEIO_S_IWOTH
)
184 if (mode
& FILEIO_S_IXOTH
)
191 remote_fileio_mode_to_target (mode_t mode
)
196 tmode
|= FILEIO_S_IFREG
;
198 tmode
|= FILEIO_S_IFDIR
;
200 tmode
|= FILEIO_S_IFCHR
;
202 tmode
|= FILEIO_S_IRUSR
;
204 tmode
|= FILEIO_S_IWUSR
;
206 tmode
|= FILEIO_S_IXUSR
;
209 tmode
|= FILEIO_S_IRGRP
;
213 tmode
|= FILEIO_S_IWGRP
;
217 tmode
|= FILEIO_S_IXGRP
;
220 tmode
|= FILEIO_S_IROTH
;
223 tmode
|= FILEIO_S_IWOTH
;
227 tmode
|= FILEIO_S_IXOTH
;
233 remote_fileio_errno_to_target (int error
)
240 return FILEIO_ENOENT
;
248 return FILEIO_EACCES
;
250 return FILEIO_EFAULT
;
254 return FILEIO_EEXIST
;
256 return FILEIO_ENODEV
;
258 return FILEIO_ENOTDIR
;
260 return FILEIO_EISDIR
;
262 return FILEIO_EINVAL
;
264 return FILEIO_ENFILE
;
266 return FILEIO_EMFILE
;
270 return FILEIO_ENOSPC
;
272 return FILEIO_ESPIPE
;
276 return FILEIO_ENOSYS
;
278 return FILEIO_ENAMETOOLONG
;
280 return FILEIO_EUNKNOWN
;
284 remote_fileio_seek_flag_to_host (long num
, int *flag
)
290 case FILEIO_SEEK_SET
:
293 case FILEIO_SEEK_CUR
:
296 case FILEIO_SEEK_END
:
306 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
311 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
313 c
= strchr (*buf
, ',');
317 c
= strchr (*buf
, '\0');
318 while (strchr ("+-", **buf
))
324 for (*retlong
= 0; **buf
; ++*buf
)
327 if (**buf
>= '0' && **buf
<= '9')
328 *retlong
+= **buf
- '0';
329 else if (**buf
>= 'a' && **buf
<= 'f')
330 *retlong
+= **buf
- 'a' + 10;
331 else if (**buf
>= 'A' && **buf
<= 'F')
332 *retlong
+= **buf
- 'A' + 10;
342 remote_fileio_extract_int (char **buf
, long *retint
)
349 ret
= remote_fileio_extract_long (buf
, &retlong
);
351 *retint
= (long) retlong
;
356 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
361 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
363 c
= strchr (*buf
, '/');
367 if (remote_fileio_extract_long (buf
, &retlong
))
369 *ptrval
= (CORE_ADDR
) retlong
;
371 if (remote_fileio_extract_long (buf
, &retlong
))
373 *length
= (int) retlong
;
377 /* Convert to big endian */
379 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
383 for (i
= 0; i
< bytes
; ++i
)
384 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
388 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
390 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
394 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
396 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
400 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
402 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
406 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
408 remote_fileio_to_be (num
, (char *) fnum
, 8);
412 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
414 remote_fileio_to_be (num
, (char *) fnum
, 8);
418 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
422 /* `st_dev' is set in the calling function */
423 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
424 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
425 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
426 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
427 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
428 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
429 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
430 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
431 blksize
= st
->st_blksize
;
435 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
436 #if HAVE_STRUCT_STAT_ST_BLOCKS
437 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
439 /* FIXME: This is correct for DJGPP, but other systems that don't
440 have st_blocks, if any, might prefer 512 instead of st_blksize.
441 (eliz, 30-12-2003) */
442 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
446 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
447 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
448 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
452 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
454 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
455 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
458 static int remote_fio_ctrl_c_flag
= 0;
459 static int remote_fio_no_longjmp
= 0;
461 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
462 static struct sigaction remote_fio_sa
;
463 static struct sigaction remote_fio_osa
;
465 static void (*remote_fio_ofunc
)(int);
469 remote_fileio_sig_init (void)
471 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
472 remote_fio_sa
.sa_handler
= SIG_IGN
;
473 sigemptyset (&remote_fio_sa
.sa_mask
);
474 remote_fio_sa
.sa_flags
= 0;
475 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
477 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
482 remote_fileio_sig_set (void (*sigint_func
)(int))
484 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
485 remote_fio_sa
.sa_handler
= sigint_func
;
486 sigemptyset (&remote_fio_sa
.sa_mask
);
487 remote_fio_sa
.sa_flags
= 0;
488 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
490 signal (SIGINT
, sigint_func
);
495 remote_fileio_sig_exit (void)
497 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
498 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
500 signal (SIGINT
, remote_fio_ofunc
);
505 remote_fileio_ctrl_c_signal_handler (int signo
)
507 remote_fileio_sig_set (SIG_IGN
);
508 remote_fio_ctrl_c_flag
= 1;
509 if (!remote_fio_no_longjmp
)
510 deprecated_throw_reason (RETURN_QUIT
);
511 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
515 remote_fileio_reply (int retcode
, int error
)
519 remote_fileio_sig_set (SIG_IGN
);
526 sprintf (buf
+ strlen (buf
), "%x", retcode
);
527 if (error
|| remote_fio_ctrl_c_flag
)
529 if (error
&& remote_fio_ctrl_c_flag
)
530 error
= FILEIO_EINTR
;
536 sprintf (buf
+ strlen (buf
), ",%x", error
);
537 if (remote_fio_ctrl_c_flag
)
540 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
545 remote_fileio_ioerror (void)
547 remote_fileio_reply (-1, FILEIO_EIO
);
551 remote_fileio_badfd (void)
553 remote_fileio_reply (-1, FILEIO_EBADF
);
557 remote_fileio_return_errno (int retcode
)
559 remote_fileio_reply (retcode
,
560 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
564 remote_fileio_return_success (int retcode
)
566 remote_fileio_reply (retcode
, 0);
569 /* Wrapper function for remote_write_bytes() which has the disadvantage to
570 write only one packet, regardless of the requested number of bytes to
571 transfer. This wrapper calls remote_write_bytes() as often as needed. */
573 remote_fileio_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
575 int ret
= 0, written
;
577 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
588 remote_fileio_func_open (char *buf
)
591 int length
, retlength
;
598 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
599 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
601 remote_fileio_ioerror ();
604 /* 2. Parameter: open flags */
605 if (remote_fileio_extract_int (&buf
, &num
))
607 remote_fileio_ioerror ();
610 flags
= remote_fileio_oflags_to_host (num
);
611 /* 3. Parameter: open mode */
612 if (remote_fileio_extract_int (&buf
, &num
))
614 remote_fileio_ioerror ();
617 mode
= remote_fileio_mode_to_host (num
, 1);
619 /* Request pathname using 'm' packet */
620 pathname
= alloca (length
);
621 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
622 if (retlength
!= length
)
624 remote_fileio_ioerror ();
628 /* Check if pathname exists and is not a regular file or directory. If so,
629 return an appropriate error code. Same for trying to open directories
631 if (!stat (pathname
, &st
))
633 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
635 remote_fileio_reply (-1, FILEIO_ENODEV
);
638 if (S_ISDIR (st
.st_mode
)
639 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
641 remote_fileio_reply (-1, FILEIO_EISDIR
);
646 remote_fio_no_longjmp
= 1;
647 fd
= open (pathname
, flags
, mode
);
650 remote_fileio_return_errno (-1);
654 fd
= remote_fileio_fd_to_targetfd (fd
);
655 remote_fileio_return_success (fd
);
659 remote_fileio_func_close (char *buf
)
664 /* Parameter: file descriptor */
665 if (remote_fileio_extract_int (&buf
, &num
))
667 remote_fileio_ioerror ();
670 fd
= remote_fileio_map_fd ((int) num
);
671 if (fd
== FIO_FD_INVALID
)
673 remote_fileio_badfd ();
677 remote_fio_no_longjmp
= 1;
678 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
679 remote_fileio_return_errno (-1);
680 remote_fileio_close_target_fd ((int) num
);
681 remote_fileio_return_success (0);
685 remote_fileio_func_read (char *buf
)
690 int fd
, ret
, retlength
;
693 off_t old_offset
, new_offset
;
695 /* 1. Parameter: file descriptor */
696 if (remote_fileio_extract_int (&buf
, &target_fd
))
698 remote_fileio_ioerror ();
701 fd
= remote_fileio_map_fd ((int) target_fd
);
702 if (fd
== FIO_FD_INVALID
)
704 remote_fileio_badfd ();
707 /* 2. Parameter: buffer pointer */
708 if (remote_fileio_extract_long (&buf
, &lnum
))
710 remote_fileio_ioerror ();
713 ptrval
= (CORE_ADDR
) lnum
;
714 /* 3. Parameter: buffer length */
715 if (remote_fileio_extract_int (&buf
, &num
))
717 remote_fileio_ioerror ();
720 length
= (size_t) num
;
724 case FIO_FD_CONSOLE_OUT
:
725 remote_fileio_badfd ();
727 case FIO_FD_CONSOLE_IN
:
729 static char *remaining_buf
= NULL
;
730 static int remaining_length
= 0;
732 buffer
= (gdb_byte
*) xmalloc (32768);
735 remote_fio_no_longjmp
= 1;
736 if (remaining_length
> length
)
738 memcpy (buffer
, remaining_buf
, length
);
739 memmove (remaining_buf
, remaining_buf
+ length
,
740 remaining_length
- length
);
741 remaining_length
-= length
;
746 memcpy (buffer
, remaining_buf
, remaining_length
);
747 xfree (remaining_buf
);
748 remaining_buf
= NULL
;
749 ret
= remaining_length
;
754 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 32767);
755 remote_fio_no_longjmp
= 1;
756 if (ret
> 0 && (size_t)ret
> length
)
758 remaining_buf
= (char *) xmalloc (ret
- length
);
759 remaining_length
= ret
- length
;
760 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
767 buffer
= (gdb_byte
*) xmalloc (length
);
768 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
769 for read() to return -1 even if "some" bytes have been read. It
770 has been corrected in SUSv2 but that doesn't help us much...
771 Therefore a complete solution must check how many bytes have been
772 read on EINTR to return a more reliable value to the target */
773 old_offset
= lseek (fd
, 0, SEEK_CUR
);
774 remote_fio_no_longjmp
= 1;
775 ret
= read (fd
, buffer
, length
);
776 if (ret
< 0 && errno
== EINTR
)
778 new_offset
= lseek (fd
, 0, SEEK_CUR
);
779 /* If some data has been read, return the number of bytes read.
780 The Ctrl-C flag is set in remote_fileio_reply() anyway */
781 if (old_offset
!= new_offset
)
782 ret
= new_offset
- old_offset
;
789 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
790 if (retlength
!= ret
)
791 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
795 remote_fileio_return_errno (-1);
797 remote_fileio_return_success (ret
);
803 remote_fileio_func_write (char *buf
)
808 int fd
, ret
, retlength
;
812 /* 1. Parameter: file descriptor */
813 if (remote_fileio_extract_int (&buf
, &target_fd
))
815 remote_fileio_ioerror ();
818 fd
= remote_fileio_map_fd ((int) target_fd
);
819 if (fd
== FIO_FD_INVALID
)
821 remote_fileio_badfd ();
824 /* 2. Parameter: buffer pointer */
825 if (remote_fileio_extract_long (&buf
, &lnum
))
827 remote_fileio_ioerror ();
830 ptrval
= (CORE_ADDR
) lnum
;
831 /* 3. Parameter: buffer length */
832 if (remote_fileio_extract_int (&buf
, &num
))
834 remote_fileio_ioerror ();
837 length
= (size_t) num
;
839 buffer
= (gdb_byte
*) xmalloc (length
);
840 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
841 if (retlength
!= length
)
844 remote_fileio_ioerror ();
848 remote_fio_no_longjmp
= 1;
851 case FIO_FD_CONSOLE_IN
:
852 remote_fileio_badfd ();
854 case FIO_FD_CONSOLE_OUT
:
855 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
856 (char *) buffer
, length
);
857 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
861 ret
= write (fd
, buffer
, length
);
862 if (ret
< 0 && errno
== EACCES
)
863 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
868 remote_fileio_return_errno (-1);
870 remote_fileio_return_success (ret
);
876 remote_fileio_func_lseek (char *buf
)
883 /* 1. Parameter: file descriptor */
884 if (remote_fileio_extract_int (&buf
, &num
))
886 remote_fileio_ioerror ();
889 fd
= remote_fileio_map_fd ((int) num
);
890 if (fd
== FIO_FD_INVALID
)
892 remote_fileio_badfd ();
895 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
897 remote_fileio_reply (-1, FILEIO_ESPIPE
);
901 /* 2. Parameter: offset */
902 if (remote_fileio_extract_long (&buf
, &lnum
))
904 remote_fileio_ioerror ();
907 offset
= (off_t
) lnum
;
908 /* 3. Parameter: flag */
909 if (remote_fileio_extract_int (&buf
, &num
))
911 remote_fileio_ioerror ();
914 if (remote_fileio_seek_flag_to_host (num
, &flag
))
916 remote_fileio_reply (-1, FILEIO_EINVAL
);
920 remote_fio_no_longjmp
= 1;
921 ret
= lseek (fd
, offset
, flag
);
923 if (ret
== (off_t
) -1)
924 remote_fileio_return_errno (-1);
926 remote_fileio_return_success (ret
);
930 remote_fileio_func_rename (char *buf
)
933 int length
, retlength
;
934 char *oldpath
, *newpath
;
936 struct stat ost
, nst
;
938 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
939 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
941 remote_fileio_ioerror ();
944 /* Request oldpath using 'm' packet */
945 oldpath
= alloca (length
);
946 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) oldpath
, length
);
947 if (retlength
!= length
)
949 remote_fileio_ioerror ();
952 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
953 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
955 remote_fileio_ioerror ();
958 /* Request newpath using 'm' packet */
959 newpath
= alloca (length
);
960 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) newpath
, length
);
961 if (retlength
!= length
)
963 remote_fileio_ioerror ();
967 /* Only operate on regular files and directories */
968 of
= stat (oldpath
, &ost
);
969 nf
= stat (newpath
, &nst
);
970 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
971 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
973 remote_fileio_reply (-1, FILEIO_EACCES
);
977 remote_fio_no_longjmp
= 1;
978 ret
= rename (oldpath
, newpath
);
982 /* Special case: newpath is a non-empty directory. Some systems
983 return ENOTEMPTY, some return EEXIST. We coerce that to be
985 if (errno
== ENOTEMPTY
)
988 /* Workaround some Cygwin problems with correct errnos. */
991 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
993 if (S_ISREG (ost
.st_mode
))
997 char oldfullpath
[PATH_MAX
+ 1];
998 char newfullpath
[PATH_MAX
+ 1];
1001 cygwin_conv_to_full_posix_path (oldpath
, oldfullpath
);
1002 cygwin_conv_to_full_posix_path (newpath
, newfullpath
);
1003 len
= strlen (oldfullpath
);
1004 if (newfullpath
[len
] == '/'
1005 && !strncmp (oldfullpath
, newfullpath
, len
))
1014 remote_fileio_return_errno (-1);
1017 remote_fileio_return_success (ret
);
1021 remote_fileio_func_unlink (char *buf
)
1024 int length
, retlength
;
1029 /* Parameter: Ptr to pathname / length incl. trailing zero */
1030 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1032 remote_fileio_ioerror ();
1035 /* Request pathname using 'm' packet */
1036 pathname
= alloca (length
);
1037 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1038 if (retlength
!= length
)
1040 remote_fileio_ioerror ();
1044 /* Only operate on regular files (and directories, which allows to return
1045 the correct return code) */
1046 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1048 remote_fileio_reply (-1, FILEIO_ENODEV
);
1052 remote_fio_no_longjmp
= 1;
1053 ret
= unlink (pathname
);
1056 remote_fileio_return_errno (-1);
1058 remote_fileio_return_success (ret
);
1062 remote_fileio_func_stat (char *buf
)
1065 int ret
, length
, retlength
;
1069 struct fio_stat fst
;
1071 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1072 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1074 remote_fileio_ioerror ();
1077 /* Request pathname using 'm' packet */
1078 pathname
= alloca (length
);
1079 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1080 if (retlength
!= length
)
1082 remote_fileio_ioerror ();
1086 /* 2. Parameter: Ptr to struct stat */
1087 if (remote_fileio_extract_long (&buf
, &lnum
))
1089 remote_fileio_ioerror ();
1092 ptrval
= (CORE_ADDR
) lnum
;
1094 remote_fio_no_longjmp
= 1;
1095 ret
= stat (pathname
, &st
);
1099 remote_fileio_return_errno (-1);
1102 /* Only operate on regular files and directories */
1103 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1105 remote_fileio_reply (-1, FILEIO_EACCES
);
1110 remote_fileio_to_fio_stat (&st
, &fst
);
1111 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1113 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1114 if (retlength
!= sizeof fst
)
1116 remote_fileio_return_errno (-1);
1120 remote_fileio_return_success (ret
);
1124 remote_fileio_func_fstat (char *buf
)
1127 int fd
, ret
, retlength
;
1131 struct fio_stat fst
;
1134 /* 1. Parameter: file descriptor */
1135 if (remote_fileio_extract_int (&buf
, &target_fd
))
1137 remote_fileio_ioerror ();
1140 fd
= remote_fileio_map_fd ((int) target_fd
);
1141 if (fd
== FIO_FD_INVALID
)
1143 remote_fileio_badfd ();
1146 /* 2. Parameter: Ptr to struct stat */
1147 if (remote_fileio_extract_long (&buf
, &lnum
))
1149 remote_fileio_ioerror ();
1152 ptrval
= (CORE_ADDR
) lnum
;
1154 remote_fio_no_longjmp
= 1;
1155 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1157 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1158 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1161 st
.st_uid
= getuid ();
1166 st
.st_gid
= getgid ();
1172 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1173 st
.st_blksize
= 512;
1175 #if HAVE_STRUCT_STAT_ST_BLOCKS
1178 if (!gettimeofday (&tv
, NULL
))
1179 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1181 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1185 ret
= fstat (fd
, &st
);
1189 remote_fileio_return_errno (-1);
1194 remote_fileio_to_fio_stat (&st
, &fst
);
1196 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1197 if (retlength
!= sizeof fst
)
1199 remote_fileio_return_errno (-1);
1203 remote_fileio_return_success (ret
);
1207 remote_fileio_func_gettimeofday (char *buf
)
1213 struct fio_timeval ftv
;
1215 /* 1. Parameter: struct timeval pointer */
1216 if (remote_fileio_extract_long (&buf
, &lnum
))
1218 remote_fileio_ioerror ();
1221 ptrval
= (CORE_ADDR
) lnum
;
1222 /* 2. Parameter: some pointer value... */
1223 if (remote_fileio_extract_long (&buf
, &lnum
))
1225 remote_fileio_ioerror ();
1228 /* ...which has to be NULL */
1231 remote_fileio_reply (-1, FILEIO_EINVAL
);
1235 remote_fio_no_longjmp
= 1;
1236 ret
= gettimeofday (&tv
, NULL
);
1240 remote_fileio_return_errno (-1);
1246 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1248 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1249 if (retlength
!= sizeof ftv
)
1251 remote_fileio_return_errno (-1);
1255 remote_fileio_return_success (ret
);
1259 remote_fileio_func_isatty (char *buf
)
1264 /* Parameter: file descriptor */
1265 if (remote_fileio_extract_int (&buf
, &target_fd
))
1267 remote_fileio_ioerror ();
1270 remote_fio_no_longjmp
= 1;
1271 fd
= remote_fileio_map_fd ((int) target_fd
);
1272 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1273 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1277 remote_fileio_func_system (char *buf
)
1280 int ret
, length
, retlength
;
1283 /* Check if system(3) has been explicitely allowed using the
1284 `set remote system-call-allowed 1' command. If not, return
1286 if (!remote_fio_system_call_allowed
)
1288 remote_fileio_reply (-1, FILEIO_EPERM
);
1292 /* Parameter: Ptr to commandline / length incl. trailing zero */
1293 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1295 remote_fileio_ioerror ();
1298 /* Request commandline using 'm' packet */
1299 cmdline
= alloca (length
);
1300 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1301 if (retlength
!= length
)
1303 remote_fileio_ioerror ();
1307 remote_fio_no_longjmp
= 1;
1308 ret
= system (cmdline
);
1311 remote_fileio_return_errno (-1);
1313 remote_fileio_return_success (WEXITSTATUS (ret
));
1318 void (*func
)(char *);
1319 } remote_fio_func_map
[] = {
1320 "open", remote_fileio_func_open
,
1321 "close", remote_fileio_func_close
,
1322 "read", remote_fileio_func_read
,
1323 "write", remote_fileio_func_write
,
1324 "lseek", remote_fileio_func_lseek
,
1325 "rename", remote_fileio_func_rename
,
1326 "unlink", remote_fileio_func_unlink
,
1327 "stat", remote_fileio_func_stat
,
1328 "fstat", remote_fileio_func_fstat
,
1329 "gettimeofday", remote_fileio_func_gettimeofday
,
1330 "isatty", remote_fileio_func_isatty
,
1331 "system", remote_fileio_func_system
,
1336 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1338 char *buf
= buf_arg
;
1342 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1344 c
= strchr (++buf
, ',');
1348 c
= strchr (buf
, '\0');
1349 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1350 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1352 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1353 return RETURN_ERROR
;
1354 remote_fio_func_map
[idx
].func (c
);
1359 remote_fileio_request (char *buf
)
1363 remote_fileio_sig_init ();
1365 remote_fio_ctrl_c_flag
= 0;
1366 remote_fio_no_longjmp
= 0;
1368 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1373 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1376 remote_fileio_reply (-1, FILEIO_EINTR
);
1382 remote_fileio_sig_exit ();
1386 set_system_call_allowed (char *args
, int from_tty
)
1391 int val
= strtoul (args
, &arg_end
, 10);
1392 if (*args
&& *arg_end
== '\0')
1394 remote_fio_system_call_allowed
= !!val
;
1398 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1402 show_system_call_allowed (char *args
, int from_tty
)
1405 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1406 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1407 remote_fio_system_call_allowed
? "" : "not ");
1411 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1412 struct cmd_list_element
*remote_show_cmdlist
)
1414 add_cmd ("system-call-allowed", no_class
,
1415 set_system_call_allowed
,
1416 _("Set if the host system(3) call is allowed for the target."),
1417 &remote_set_cmdlist
);
1418 add_cmd ("system-call-allowed", no_class
,
1419 show_system_call_allowed
,
1420 _("Show if the host system(3) call is allowed for the target."),
1421 &remote_show_cmdlist
);