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"
35 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
44 #define FIO_FD_INVALID -1
45 #define FIO_FD_CONSOLE_IN -2
46 #define FIO_FD_CONSOLE_OUT -3
48 static int remote_fio_system_call_allowed
= 0;
51 remote_fileio_init_fd_map ()
55 if (!remote_fio_data
.fd_map
)
57 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
58 remote_fio_data
.fd_map_size
= 10;
59 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
60 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
61 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
62 for (i
= 3; i
< 10; ++i
)
63 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
69 remote_fileio_resize_fd_map ()
71 if (!remote_fio_data
.fd_map
)
72 return remote_fileio_init_fd_map ();
73 remote_fio_data
.fd_map_size
+= 10;
74 remote_fio_data
.fd_map
=
75 (int *) xrealloc (remote_fio_data
.fd_map
,
76 remote_fio_data
.fd_map_size
* sizeof (int));
77 return remote_fio_data
.fd_map_size
- 10;
81 remote_fileio_next_free_fd ()
85 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
86 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
88 return remote_fileio_resize_fd_map ();
92 remote_fileio_fd_to_targetfd (int fd
)
94 int target_fd
= remote_fileio_next_free_fd ();
95 remote_fio_data
.fd_map
[target_fd
] = fd
;
100 remote_fileio_map_fd (int target_fd
)
102 remote_fileio_init_fd_map ();
103 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
104 return FIO_FD_INVALID
;
105 return remote_fio_data
.fd_map
[target_fd
];
109 remote_fileio_close_target_fd (int target_fd
)
111 remote_fileio_init_fd_map ();
112 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
113 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
117 remote_fileio_oflags_to_host (long flags
)
121 if (flags
& FILEIO_O_CREAT
)
123 if (flags
& FILEIO_O_EXCL
)
125 if (flags
& FILEIO_O_TRUNC
)
127 if (flags
& FILEIO_O_APPEND
)
129 if (flags
& FILEIO_O_RDONLY
)
131 if (flags
& FILEIO_O_WRONLY
)
133 if (flags
& FILEIO_O_RDWR
)
135 /* On systems supporting binary and text mode, always open files in
144 remote_fileio_mode_to_host (long mode
, int open_call
)
150 if (mode
& FILEIO_S_IFREG
)
152 if (mode
& FILEIO_S_IFDIR
)
154 if (mode
& FILEIO_S_IFCHR
)
157 if (mode
& FILEIO_S_IRUSR
)
159 if (mode
& FILEIO_S_IWUSR
)
161 if (mode
& FILEIO_S_IXUSR
)
163 if (mode
& FILEIO_S_IRGRP
)
165 if (mode
& FILEIO_S_IWGRP
)
167 if (mode
& FILEIO_S_IXGRP
)
169 if (mode
& FILEIO_S_IROTH
)
171 if (mode
& FILEIO_S_IWOTH
)
173 if (mode
& FILEIO_S_IXOTH
)
179 remote_fileio_mode_to_target (mode_t mode
)
184 tmode
|= FILEIO_S_IFREG
;
186 tmode
|= FILEIO_S_IFDIR
;
188 tmode
|= FILEIO_S_IFCHR
;
190 tmode
|= FILEIO_S_IRUSR
;
192 tmode
|= FILEIO_S_IWUSR
;
194 tmode
|= FILEIO_S_IXUSR
;
196 tmode
|= FILEIO_S_IRGRP
;
198 tmode
|= FILEIO_S_IWGRP
;
200 tmode
|= FILEIO_S_IXGRP
;
202 tmode
|= FILEIO_S_IROTH
;
204 tmode
|= FILEIO_S_IWOTH
;
206 tmode
|= FILEIO_S_IXOTH
;
211 remote_fileio_errno_to_target (int error
)
218 return FILEIO_ENOENT
;
226 return FILEIO_EACCES
;
228 return FILEIO_EFAULT
;
232 return FILEIO_EEXIST
;
234 return FILEIO_ENODEV
;
236 return FILEIO_ENOTDIR
;
238 return FILEIO_EISDIR
;
240 return FILEIO_EINVAL
;
242 return FILEIO_ENFILE
;
244 return FILEIO_EMFILE
;
248 return FILEIO_ENOSPC
;
250 return FILEIO_ESPIPE
;
254 return FILEIO_ENOSYS
;
256 return FILEIO_ENAMETOOLONG
;
258 return FILEIO_EUNKNOWN
;
262 remote_fileio_seek_flag_to_host (long num
, int *flag
)
268 case FILEIO_SEEK_SET
:
271 case FILEIO_SEEK_CUR
:
274 case FILEIO_SEEK_END
:
284 remote_fileio_extract_long (char **buf
, long long *retlong
)
289 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
291 c
= strchr (*buf
, ',');
295 c
= strchr (*buf
, '\0');
296 while (strchr ("+-", **buf
))
302 for (*retlong
= 0; **buf
; ++*buf
)
305 if (**buf
>= '0' && **buf
<= '9')
306 *retlong
+= **buf
- '0';
307 else if (**buf
>= 'a' && **buf
<= 'f')
308 *retlong
+= **buf
- 'a' + 10;
309 else if (**buf
>= 'A' && **buf
<= 'F')
310 *retlong
+= **buf
- 'A' + 10;
320 remote_fileio_extract_int (char **buf
, long *retint
)
327 if (!(ret
= remote_fileio_extract_long (buf
, &retlong
)))
328 *retint
= (long) retlong
;
333 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
338 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
340 c
= strchr (*buf
, '/');
344 if (remote_fileio_extract_long (buf
, &retlong
))
346 *ptrval
= (CORE_ADDR
) retlong
;
348 if (remote_fileio_extract_long (buf
, &retlong
))
350 *length
= (int) retlong
;
354 /* Convert to big endian */
356 remote_fileio_to_be (long long num
, char *buf
, int bytes
)
360 for (i
= 0; i
< bytes
; ++i
)
361 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
365 remote_fileio_to_fio_int (long num
, fio_int_t fnum
)
367 remote_fileio_to_be ((long long) num
, (char *) fnum
, 4);
371 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
373 remote_fileio_to_be ((long long) num
, (char *) fnum
, 4);
377 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
379 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
383 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
385 remote_fileio_to_be ((long long) num
, (char *) fnum
, 4);
389 remote_fileio_to_fio_long (long long num
, fio_long_t fnum
)
391 remote_fileio_to_be (num
, (char *) fnum
, 8);
395 remote_fileio_to_fio_ulong (long long num
, fio_ulong_t fnum
)
397 remote_fileio_to_be (num
, (char *) fnum
, 8);
401 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
403 /* `st_dev' is set in the calling function */
404 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
405 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
406 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
407 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
408 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
409 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
410 remote_fileio_to_fio_ulong ((long long) st
->st_size
, fst
->fst_size
);
411 remote_fileio_to_fio_ulong ((long long) st
->st_blksize
, fst
->fst_blksize
);
412 remote_fileio_to_fio_ulong ((long long) st
->st_blocks
, fst
->fst_blocks
);
413 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
414 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
415 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
419 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
421 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
422 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
425 static int remote_fio_ctrl_c_flag
= 0;
426 static int remote_fio_no_longjmp
= 0;
428 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
429 static struct sigaction remote_fio_sa
;
430 static struct sigaction remote_fio_osa
;
432 static void (*remote_fio_ofunc
)(int);
436 remote_fileio_sig_init ()
438 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
439 remote_fio_sa
.sa_handler
= SIG_IGN
;
440 sigemptyset (&remote_fio_sa
.sa_mask
);
441 remote_fio_sa
.sa_flags
= 0;
442 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
444 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
449 remote_fileio_sig_set (void (*sigint_func
)(int))
451 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
452 remote_fio_sa
.sa_handler
= sigint_func
;
453 sigemptyset (&remote_fio_sa
.sa_mask
);
454 remote_fio_sa
.sa_flags
= 0;
455 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
457 signal (SIGINT
, sigint_func
);
462 remote_fileio_sig_exit ()
464 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
465 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
467 signal (SIGINT
, remote_fio_ofunc
);
472 remote_fileio_ctrl_c_signal_handler (int signo
)
474 remote_fileio_sig_set (SIG_IGN
);
475 remote_fio_ctrl_c_flag
= 1;
476 if (!remote_fio_no_longjmp
)
477 throw_exception (RETURN_QUIT
);
478 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
482 remote_fileio_reply (int retcode
, int error
)
486 remote_fileio_sig_set (SIG_IGN
);
493 sprintf (buf
+ strlen (buf
), "%x", retcode
);
494 if (error
|| remote_fio_ctrl_c_flag
)
496 if (error
&& remote_fio_ctrl_c_flag
)
497 error
= FILEIO_EINTR
;
503 sprintf (buf
+ strlen (buf
), ",%x", error
);
504 if (remote_fio_ctrl_c_flag
)
507 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
512 remote_fileio_ioerror ()
514 remote_fileio_reply (-1, FILEIO_EIO
);
518 remote_fileio_badfd ()
520 remote_fileio_reply (-1, FILEIO_EBADF
);
524 remote_fileio_return_errno (int retcode
)
526 remote_fileio_reply (retcode
,
527 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
531 remote_fileio_return_success (int retcode
)
533 remote_fileio_reply (retcode
, 0);
536 /* Wrapper function for remote_write_bytes() which has the disadvantage to
537 write only one packet, regardless of the requested number of bytes to
538 transfer. This wrapper calls remote_write_bytes() as often as needed. */
540 remote_fileio_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
542 int ret
= 0, written
;
544 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
555 remote_fileio_func_open (char *buf
)
558 int length
, retlength
;
565 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
566 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
568 remote_fileio_ioerror ();
571 /* 2. Parameter: open flags */
572 if (remote_fileio_extract_int (&buf
, &num
))
574 remote_fileio_ioerror ();
577 flags
= remote_fileio_oflags_to_host (num
);
578 /* 3. Parameter: open mode */
579 if (remote_fileio_extract_int (&buf
, &num
))
581 remote_fileio_ioerror ();
584 mode
= remote_fileio_mode_to_host (num
, 1);
586 /* Request pathname using 'm' packet */
587 pathname
= alloca (length
);
588 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
589 if (retlength
!= length
)
591 remote_fileio_ioerror ();
595 /* Check if pathname exists and is not a regular file or directory. If so,
596 return an appropriate error code. Same for trying to open directories
598 if (!stat (pathname
, &st
))
600 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
602 remote_fileio_reply (-1, FILEIO_ENODEV
);
605 if (S_ISDIR (st
.st_mode
)
606 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
608 remote_fileio_reply (-1, FILEIO_EISDIR
);
613 remote_fio_no_longjmp
= 1;
614 fd
= open (pathname
, flags
, mode
);
617 remote_fileio_return_errno (-1);
621 fd
= remote_fileio_fd_to_targetfd (fd
);
622 remote_fileio_return_success (fd
);
626 remote_fileio_func_close (char *buf
)
631 /* Parameter: file descriptor */
632 if (remote_fileio_extract_int (&buf
, &num
))
634 remote_fileio_ioerror ();
637 if ((fd
= remote_fileio_map_fd ((int) num
)) == FIO_FD_INVALID
)
639 remote_fileio_badfd ();
643 remote_fio_no_longjmp
= 1;
644 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
645 remote_fileio_return_errno (-1);
646 remote_fileio_close_target_fd ((int) num
);
647 remote_fileio_return_success (0);
651 remote_fileio_func_read (char *buf
)
656 int fd
, ret
, retlength
;
659 off_t old_offset
, new_offset
;
661 /* 1. Parameter: file descriptor */
662 if (remote_fileio_extract_int (&buf
, &target_fd
))
664 remote_fileio_ioerror ();
667 if ((fd
= remote_fileio_map_fd ((int) target_fd
)) == FIO_FD_INVALID
)
669 remote_fileio_badfd ();
672 /* 2. Parameter: buffer pointer */
673 if (remote_fileio_extract_long (&buf
, &lnum
))
675 remote_fileio_ioerror ();
678 ptrval
= (CORE_ADDR
) lnum
;
679 /* 3. Parameter: buffer length */
680 if (remote_fileio_extract_int (&buf
, &num
))
682 remote_fileio_ioerror ();
685 length
= (size_t) num
;
689 case FIO_FD_CONSOLE_OUT
:
690 remote_fileio_badfd ();
692 case FIO_FD_CONSOLE_IN
:
694 static char *remaining_buf
= NULL
;
695 static int remaining_length
= 0;
697 buffer
= (char *) xmalloc (32768);
700 remote_fio_no_longjmp
= 1;
701 if (remaining_length
> length
)
703 memcpy (buffer
, remaining_buf
, length
);
704 memmove (remaining_buf
, remaining_buf
+ length
,
705 remaining_length
- length
);
706 remaining_length
-= length
;
711 memcpy (buffer
, remaining_buf
, remaining_length
);
712 xfree (remaining_buf
);
713 remaining_buf
= NULL
;
714 ret
= remaining_length
;
719 ret
= ui_file_read (gdb_stdtargin
, buffer
, 32767);
720 remote_fio_no_longjmp
= 1;
721 if (ret
> 0 && (size_t)ret
> length
)
723 remaining_buf
= (char *) xmalloc (ret
- length
);
724 remaining_length
= ret
- length
;
725 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
732 buffer
= (char *) xmalloc (length
);
733 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
734 for read() to return -1 even if "some" bytes have been read. It
735 has been corrected in SUSv2 but that doesn't help us much...
736 Therefore a complete solution must check how many bytes have been
737 read on EINTR to return a more reliable value to the target */
738 old_offset
= lseek (fd
, 0, SEEK_CUR
);
739 remote_fio_no_longjmp
= 1;
740 ret
= read (fd
, buffer
, length
);
741 if (ret
< 0 && errno
== EINTR
)
743 new_offset
= lseek (fd
, 0, SEEK_CUR
);
744 /* If some data has been read, return the number of bytes read.
745 The Ctrl-C flag is set in remote_fileio_reply() anyway */
746 if (old_offset
!= new_offset
)
747 ret
= new_offset
- old_offset
;
754 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
755 if (retlength
!= ret
)
756 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
760 remote_fileio_return_errno (-1);
762 remote_fileio_return_success (ret
);
768 remote_fileio_func_write (char *buf
)
773 int fd
, ret
, retlength
;
777 /* 1. Parameter: file descriptor */
778 if (remote_fileio_extract_int (&buf
, &target_fd
))
780 remote_fileio_ioerror ();
783 if ((fd
= remote_fileio_map_fd ((int) target_fd
)) == FIO_FD_INVALID
)
785 remote_fileio_badfd ();
788 /* 2. Parameter: buffer pointer */
789 if (remote_fileio_extract_long (&buf
, &lnum
))
791 remote_fileio_ioerror ();
794 ptrval
= (CORE_ADDR
) lnum
;
795 /* 3. Parameter: buffer length */
796 if (remote_fileio_extract_int (&buf
, &num
))
798 remote_fileio_ioerror ();
801 length
= (size_t) num
;
803 buffer
= (char *) xmalloc (length
);
804 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
805 if (retlength
!= length
)
808 remote_fileio_ioerror ();
812 remote_fio_no_longjmp
= 1;
815 case FIO_FD_CONSOLE_IN
:
816 remote_fileio_badfd ();
818 case FIO_FD_CONSOLE_OUT
:
819 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
, buffer
,
821 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
825 ret
= write (fd
, buffer
, length
);
826 if (ret
< 0 && errno
== EACCES
)
827 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
832 remote_fileio_return_errno (-1);
834 remote_fileio_return_success (ret
);
840 remote_fileio_func_lseek (char *buf
)
847 /* 1. Parameter: file descriptor */
848 if (remote_fileio_extract_int (&buf
, &num
))
850 remote_fileio_ioerror ();
853 if ((fd
= remote_fileio_map_fd ((int) num
)) == FIO_FD_INVALID
)
855 remote_fileio_badfd ();
858 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
860 remote_fileio_reply (-1, FILEIO_ESPIPE
);
864 /* 2. Parameter: offset */
865 if (remote_fileio_extract_long (&buf
, &lnum
))
867 remote_fileio_ioerror ();
870 offset
= (off_t
) lnum
;
871 /* 3. Parameter: flag */
872 if (remote_fileio_extract_int (&buf
, &num
))
874 remote_fileio_ioerror ();
877 if (remote_fileio_seek_flag_to_host (num
, &flag
))
879 remote_fileio_reply (-1, FILEIO_EINVAL
);
883 remote_fio_no_longjmp
= 1;
884 ret
= lseek (fd
, offset
, flag
);
886 if (ret
== (off_t
) -1)
887 remote_fileio_return_errno (-1);
889 remote_fileio_return_success (ret
);
893 remote_fileio_func_rename (char *buf
)
896 int length
, retlength
;
897 char *oldpath
, *newpath
;
899 struct stat ost
, nst
;
901 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
902 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
904 remote_fileio_ioerror ();
907 /* Request oldpath using 'm' packet */
908 oldpath
= alloca (length
);
909 retlength
= remote_read_bytes (ptrval
, oldpath
, length
);
910 if (retlength
!= length
)
912 remote_fileio_ioerror ();
915 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
916 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
918 remote_fileio_ioerror ();
921 /* Request newpath using 'm' packet */
922 newpath
= alloca (length
);
923 retlength
= remote_read_bytes (ptrval
, newpath
, length
);
924 if (retlength
!= length
)
926 remote_fileio_ioerror ();
930 /* Only operate on regular files and directories */
931 if ((!(of
= stat (oldpath
, &ost
))
932 && !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
933 || (!(nf
= stat (newpath
, &nst
))
934 && !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
936 remote_fileio_reply (-1, FILEIO_EACCES
);
940 remote_fio_no_longjmp
= 1;
941 ret
= rename (oldpath
, newpath
);
945 /* Special case: newpath is a non-empty directory. Some systems
946 return ENOTEMPTY, some return EEXIST. We coerce that to be
948 if (errno
== ENOTEMPTY
)
951 /* Workaround some Cygwin problems with correct errnos. */
954 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
956 if (S_ISREG (ost
.st_mode
))
960 char oldfullpath
[PATH_MAX
+ 1];
961 char newfullpath
[PATH_MAX
+ 1];
964 cygwin_conv_to_full_posix_path (oldpath
, oldfullpath
);
965 cygwin_conv_to_full_posix_path (newpath
, newfullpath
);
966 len
= strlen (oldfullpath
);
967 if (newfullpath
[len
] == '/'
968 && !strncmp (oldfullpath
, newfullpath
, len
))
977 remote_fileio_return_errno (-1);
980 remote_fileio_return_success (ret
);
984 remote_fileio_func_unlink (char *buf
)
987 int length
, retlength
;
992 /* Parameter: Ptr to pathname / length incl. trailing zero */
993 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
995 remote_fileio_ioerror ();
998 /* Request pathname using 'm' packet */
999 pathname
= alloca (length
);
1000 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
1001 if (retlength
!= length
)
1003 remote_fileio_ioerror ();
1007 /* Only operate on regular files (and directories, which allows to return
1008 the correct return code) */
1009 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1011 remote_fileio_reply (-1, FILEIO_ENODEV
);
1015 remote_fio_no_longjmp
= 1;
1016 ret
= unlink (pathname
);
1019 remote_fileio_return_errno (-1);
1021 remote_fileio_return_success (ret
);
1025 remote_fileio_func_stat (char *buf
)
1028 int ret
, length
, retlength
;
1032 struct fio_stat fst
;
1034 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1035 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1037 remote_fileio_ioerror ();
1040 /* Request pathname using 'm' packet */
1041 pathname
= alloca (length
);
1042 retlength
= remote_read_bytes (ptrval
, pathname
, length
);
1043 if (retlength
!= length
)
1045 remote_fileio_ioerror ();
1049 /* 2. Parameter: Ptr to struct stat */
1050 if (remote_fileio_extract_long (&buf
, &lnum
))
1052 remote_fileio_ioerror ();
1055 ptrval
= (CORE_ADDR
) lnum
;
1057 remote_fio_no_longjmp
= 1;
1058 ret
= stat (pathname
, &st
);
1062 remote_fileio_return_errno (-1);
1065 /* Only operate on regular files and directories */
1066 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1068 remote_fileio_reply (-1, FILEIO_EACCES
);
1073 remote_fileio_to_fio_stat (&st
, &fst
);
1074 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1076 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &fst
, sizeof fst
);
1077 if (retlength
!= sizeof fst
)
1079 remote_fileio_return_errno (-1);
1083 remote_fileio_return_success (ret
);
1087 remote_fileio_func_fstat (char *buf
)
1090 int fd
, ret
, retlength
;
1094 struct fio_stat fst
;
1097 /* 1. Parameter: file descriptor */
1098 if (remote_fileio_extract_int (&buf
, &target_fd
))
1100 remote_fileio_ioerror ();
1103 if ((fd
= remote_fileio_map_fd ((int) target_fd
)) == FIO_FD_INVALID
)
1105 remote_fileio_badfd ();
1108 /* 2. Parameter: Ptr to struct stat */
1109 if (remote_fileio_extract_long (&buf
, &lnum
))
1111 remote_fileio_ioerror ();
1114 ptrval
= (CORE_ADDR
) lnum
;
1116 remote_fio_no_longjmp
= 1;
1117 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1119 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1120 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1122 st
.st_uid
= getuid ();
1123 st
.st_gid
= getgid ();
1126 st
.st_blksize
= 512;
1128 if (!gettimeofday (&tv
, NULL
))
1129 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1131 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1135 ret
= fstat (fd
, &st
);
1139 remote_fileio_return_errno (-1);
1144 remote_fileio_to_fio_stat (&st
, &fst
);
1146 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &fst
, sizeof fst
);
1147 if (retlength
!= sizeof fst
)
1149 remote_fileio_return_errno (-1);
1153 remote_fileio_return_success (ret
);
1157 remote_fileio_func_gettimeofday (char *buf
)
1163 struct fio_timeval ftv
;
1165 /* 1. Parameter: struct timeval pointer */
1166 if (remote_fileio_extract_long (&buf
, &lnum
))
1168 remote_fileio_ioerror ();
1171 ptrval
= (CORE_ADDR
) lnum
;
1172 /* 2. Parameter: some pointer value... */
1173 if (remote_fileio_extract_long (&buf
, &lnum
))
1175 remote_fileio_ioerror ();
1178 /* ...which has to be NULL */
1181 remote_fileio_reply (-1, FILEIO_EINVAL
);
1185 remote_fio_no_longjmp
= 1;
1186 ret
= gettimeofday (&tv
, NULL
);
1190 remote_fileio_return_errno (-1);
1196 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1198 retlength
= remote_fileio_write_bytes (ptrval
, (char *) &ftv
, sizeof ftv
);
1199 if (retlength
!= sizeof ftv
)
1201 remote_fileio_return_errno (-1);
1205 remote_fileio_return_success (ret
);
1209 remote_fileio_func_isatty (char *buf
)
1214 /* Parameter: file descriptor */
1215 if (remote_fileio_extract_int (&buf
, &target_fd
))
1217 remote_fileio_ioerror ();
1220 remote_fio_no_longjmp
= 1;
1221 fd
= remote_fileio_map_fd ((int) target_fd
);
1222 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1223 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1227 remote_fileio_func_system (char *buf
)
1230 int ret
, length
, retlength
;
1233 /* Check if system(3) has been explicitely allowed using the
1234 `set remote system-call-allowed 1' command. If not, return
1236 if (!remote_fio_system_call_allowed
)
1238 remote_fileio_reply (-1, FILEIO_EPERM
);
1242 /* Parameter: Ptr to commandline / length incl. trailing zero */
1243 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1245 remote_fileio_ioerror ();
1248 /* Request commandline using 'm' packet */
1249 cmdline
= alloca (length
);
1250 retlength
= remote_read_bytes (ptrval
, cmdline
, length
);
1251 if (retlength
!= length
)
1253 remote_fileio_ioerror ();
1257 remote_fio_no_longjmp
= 1;
1258 ret
= system (cmdline
);
1261 remote_fileio_return_errno (-1);
1263 remote_fileio_return_success (WEXITSTATUS (ret
));
1268 void (*func
)(char *);
1269 } remote_fio_func_map
[] = {
1270 "open", remote_fileio_func_open
,
1271 "close", remote_fileio_func_close
,
1272 "read", remote_fileio_func_read
,
1273 "write", remote_fileio_func_write
,
1274 "lseek", remote_fileio_func_lseek
,
1275 "rename", remote_fileio_func_rename
,
1276 "unlink", remote_fileio_func_unlink
,
1277 "stat", remote_fileio_func_stat
,
1278 "fstat", remote_fileio_func_fstat
,
1279 "gettimeofday", remote_fileio_func_gettimeofday
,
1280 "isatty", remote_fileio_func_isatty
,
1281 "system", remote_fileio_func_system
,
1286 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1288 char *buf
= buf_arg
;
1292 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1294 c
= strchr (++buf
, ',');
1298 c
= strchr (buf
, '\0');
1299 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1300 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1302 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1303 return RETURN_ERROR
;
1304 remote_fio_func_map
[idx
].func (c
);
1309 remote_fileio_request (char *buf
)
1313 remote_fileio_sig_init ();
1315 remote_fio_ctrl_c_flag
= 0;
1316 remote_fio_no_longjmp
= 0;
1318 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1319 NULL
, RETURN_MASK_ALL
);
1323 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1326 remote_fileio_reply (-1, FILEIO_EINTR
);
1332 remote_fileio_sig_exit ();
1336 set_system_call_allowed (char *args
, int from_tty
)
1341 int val
= strtoul (args
, &arg_end
, 10);
1342 if (*args
&& *arg_end
== '\0')
1344 remote_fio_system_call_allowed
= !!val
;
1348 error ("Illegal argument for \"set remote system-call-allowed\" command");
1352 show_system_call_allowed (char *args
, int from_tty
)
1355 error ("Garbage after \"show remote system-call-allowed\" command: `%s'", args
);
1356 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1357 remote_fio_system_call_allowed
? "" : "not ");
1361 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1362 struct cmd_list_element
*remote_show_cmdlist
)
1364 add_cmd ("system-call-allowed", no_class
,
1365 set_system_call_allowed
,
1366 "Set if the host system(3) call is allowed for the target.\n",
1367 &remote_set_cmdlist
);
1368 add_cmd ("system-call-allowed", no_class
,
1369 show_system_call_allowed
,
1370 "Show if the host system(3) call is allowed for the target.\n",
1371 &remote_show_cmdlist
);