ignore invalid DOF provider sections
[binutils-gdb.git] / gdb / remote-fileio.c
bloba33acf91ef6c3d60c89807b24a9e85700796a087
1 /* Remote File-I/O communications
3 Copyright (C) 2003-2015 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. */
22 #include "defs.h"
23 #include "gdbcmd.h"
24 #include "remote.h"
25 #include "gdb_wait.h"
26 #include <sys/stat.h>
27 #include "remote-fileio.h"
28 #include "event-loop.h"
29 #include "target.h"
30 #include "filenames.h"
31 #include "filestuff.h"
33 #include <fcntl.h>
34 #include <sys/time.h>
35 #ifdef __CYGWIN__
36 #include <sys/cygwin.h> /* For cygwin_conv_path. */
37 #endif
38 #include <signal.h>
40 static struct {
41 int *fd_map;
42 int fd_map_size;
43 } remote_fio_data;
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;
53 static int
54 remote_fileio_init_fd_map (void)
56 int i;
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;
68 return 3;
71 static int
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;
87 static int
88 remote_fileio_next_free_fd (void)
90 int i;
92 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
93 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
94 return i;
95 return remote_fileio_resize_fd_map ();
98 static int
99 remote_fileio_fd_to_targetfd (int fd)
101 int target_fd = remote_fileio_next_free_fd ();
103 remote_fio_data.fd_map[target_fd] = fd;
104 return target_fd;
107 static int
108 remote_fileio_map_fd (int target_fd)
110 remote_fileio_init_fd_map ();
111 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
112 return FIO_FD_INVALID;
113 return remote_fio_data.fd_map[target_fd];
116 static void
117 remote_fileio_close_target_fd (int target_fd)
119 remote_fileio_init_fd_map ();
120 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
121 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
124 static int
125 remote_fileio_oflags_to_host (long flags)
127 int hflags = 0;
129 if (flags & FILEIO_O_CREAT)
130 hflags |= O_CREAT;
131 if (flags & FILEIO_O_EXCL)
132 hflags |= O_EXCL;
133 if (flags & FILEIO_O_TRUNC)
134 hflags |= O_TRUNC;
135 if (flags & FILEIO_O_APPEND)
136 hflags |= O_APPEND;
137 if (flags & FILEIO_O_RDONLY)
138 hflags |= O_RDONLY;
139 if (flags & FILEIO_O_WRONLY)
140 hflags |= O_WRONLY;
141 if (flags & FILEIO_O_RDWR)
142 hflags |= O_RDWR;
143 /* On systems supporting binary and text mode, always open files in
144 binary mode. */
145 #ifdef O_BINARY
146 hflags |= O_BINARY;
147 #endif
148 return hflags;
151 static mode_t
152 remote_fileio_mode_to_host (long mode, int open_call)
154 mode_t hmode = 0;
156 if (!open_call)
158 if (mode & FILEIO_S_IFREG)
159 hmode |= S_IFREG;
160 if (mode & FILEIO_S_IFDIR)
161 hmode |= S_IFDIR;
162 if (mode & FILEIO_S_IFCHR)
163 hmode |= S_IFCHR;
165 if (mode & FILEIO_S_IRUSR)
166 hmode |= S_IRUSR;
167 if (mode & FILEIO_S_IWUSR)
168 hmode |= S_IWUSR;
169 if (mode & FILEIO_S_IXUSR)
170 hmode |= S_IXUSR;
171 #ifdef S_IRGRP
172 if (mode & FILEIO_S_IRGRP)
173 hmode |= S_IRGRP;
174 #endif
175 #ifdef S_IWGRP
176 if (mode & FILEIO_S_IWGRP)
177 hmode |= S_IWGRP;
178 #endif
179 #ifdef S_IXGRP
180 if (mode & FILEIO_S_IXGRP)
181 hmode |= S_IXGRP;
182 #endif
183 if (mode & FILEIO_S_IROTH)
184 hmode |= S_IROTH;
185 #ifdef S_IWOTH
186 if (mode & FILEIO_S_IWOTH)
187 hmode |= S_IWOTH;
188 #endif
189 #ifdef S_IXOTH
190 if (mode & FILEIO_S_IXOTH)
191 hmode |= S_IXOTH;
192 #endif
193 return hmode;
196 static int
197 remote_fileio_seek_flag_to_host (long num, int *flag)
199 if (!flag)
200 return 0;
201 switch (num)
203 case FILEIO_SEEK_SET:
204 *flag = SEEK_SET;
205 break;
206 case FILEIO_SEEK_CUR:
207 *flag = SEEK_CUR;
208 break;
209 case FILEIO_SEEK_END:
210 *flag = SEEK_END;
211 break;
212 default:
213 return -1;
215 return 0;
218 static int
219 remote_fileio_extract_long (char **buf, LONGEST *retlong)
221 char *c;
222 int sign = 1;
224 if (!buf || !*buf || !**buf || !retlong)
225 return -1;
226 c = strchr (*buf, ',');
227 if (c)
228 *c++ = '\0';
229 else
230 c = strchr (*buf, '\0');
231 while (strchr ("+-", **buf))
233 if (**buf == '-')
234 sign = -sign;
235 ++*buf;
237 for (*retlong = 0; **buf; ++*buf)
239 *retlong <<= 4;
240 if (**buf >= '0' && **buf <= '9')
241 *retlong += **buf - '0';
242 else if (**buf >= 'a' && **buf <= 'f')
243 *retlong += **buf - 'a' + 10;
244 else if (**buf >= 'A' && **buf <= 'F')
245 *retlong += **buf - 'A' + 10;
246 else
247 return -1;
249 *retlong *= sign;
250 *buf = c;
251 return 0;
254 static int
255 remote_fileio_extract_int (char **buf, long *retint)
257 int ret;
258 LONGEST retlong;
260 if (!retint)
261 return -1;
262 ret = remote_fileio_extract_long (buf, &retlong);
263 if (!ret)
264 *retint = (long) retlong;
265 return ret;
268 static int
269 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
271 char *c;
272 LONGEST retlong;
274 if (!buf || !*buf || !**buf || !ptrval || !length)
275 return -1;
276 c = strchr (*buf, '/');
277 if (!c)
278 return -1;
279 *c++ = '\0';
280 if (remote_fileio_extract_long (buf, &retlong))
281 return -1;
282 *ptrval = (CORE_ADDR) retlong;
283 *buf = c;
284 if (remote_fileio_extract_long (buf, &retlong))
285 return -1;
286 *length = (int) retlong;
287 return 0;
290 static void
291 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
293 host_to_bigendian (num, (char *) fnum, 8);
296 static void
297 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
299 host_to_fileio_time (tv->tv_sec, ftv->ftv_sec);
300 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
303 static int remote_fio_ctrl_c_flag = 0;
304 static int remote_fio_no_longjmp = 0;
306 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
307 static struct sigaction remote_fio_sa;
308 static struct sigaction remote_fio_osa;
309 #else
310 static void (*remote_fio_ofunc)(int);
311 #endif
313 static void
314 remote_fileio_sig_init (void)
316 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
317 remote_fio_sa.sa_handler = SIG_IGN;
318 sigemptyset (&remote_fio_sa.sa_mask);
319 remote_fio_sa.sa_flags = 0;
320 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
321 #else
322 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
323 #endif
326 static void
327 remote_fileio_sig_set (void (*sigint_func)(int))
329 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
330 remote_fio_sa.sa_handler = sigint_func;
331 sigemptyset (&remote_fio_sa.sa_mask);
332 remote_fio_sa.sa_flags = 0;
333 sigaction (SIGINT, &remote_fio_sa, NULL);
334 #else
335 signal (SIGINT, sigint_func);
336 #endif
339 static void
340 remote_fileio_sig_exit (void)
342 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
343 sigaction (SIGINT, &remote_fio_osa, NULL);
344 #else
345 signal (SIGINT, remote_fio_ofunc);
346 #endif
349 static void
350 async_remote_fileio_interrupt (gdb_client_data arg)
352 quit ();
355 static void
356 remote_fileio_ctrl_c_signal_handler (int signo)
358 remote_fileio_sig_set (SIG_IGN);
359 remote_fio_ctrl_c_flag = 1;
360 if (!remote_fio_no_longjmp)
361 gdb_call_async_signal_handler (sigint_fileio_token, 1);
362 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
365 static void
366 remote_fileio_reply (int retcode, int error)
368 char buf[32];
370 remote_fileio_sig_set (SIG_IGN);
371 strcpy (buf, "F");
372 if (retcode < 0)
374 strcat (buf, "-");
375 retcode = -retcode;
377 sprintf (buf + strlen (buf), "%x", retcode);
378 if (error || remote_fio_ctrl_c_flag)
380 if (error && remote_fio_ctrl_c_flag)
381 error = FILEIO_EINTR;
382 if (error < 0)
384 strcat (buf, "-");
385 error = -error;
387 sprintf (buf + strlen (buf), ",%x", error);
388 if (remote_fio_ctrl_c_flag)
389 strcat (buf, ",C");
391 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
392 putpkt (buf);
395 static void
396 remote_fileio_ioerror (void)
398 remote_fileio_reply (-1, FILEIO_EIO);
401 static void
402 remote_fileio_badfd (void)
404 remote_fileio_reply (-1, FILEIO_EBADF);
407 static void
408 remote_fileio_return_errno (int retcode)
410 remote_fileio_reply (retcode, retcode < 0
411 ? host_to_fileio_error (errno) : 0);
414 static void
415 remote_fileio_return_success (int retcode)
417 remote_fileio_reply (retcode, 0);
420 static void
421 remote_fileio_func_open (char *buf)
423 CORE_ADDR ptrval;
424 int length;
425 long num;
426 int flags, fd;
427 mode_t mode;
428 char *pathname;
429 struct stat st;
431 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
432 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
434 remote_fileio_ioerror ();
435 return;
437 /* 2. Parameter: open flags */
438 if (remote_fileio_extract_int (&buf, &num))
440 remote_fileio_ioerror ();
441 return;
443 flags = remote_fileio_oflags_to_host (num);
444 /* 3. Parameter: open mode */
445 if (remote_fileio_extract_int (&buf, &num))
447 remote_fileio_ioerror ();
448 return;
450 mode = remote_fileio_mode_to_host (num, 1);
452 /* Request pathname. */
453 pathname = alloca (length);
454 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
456 remote_fileio_ioerror ();
457 return;
460 /* Check if pathname exists and is not a regular file or directory. If so,
461 return an appropriate error code. Same for trying to open directories
462 for writing. */
463 if (!stat (pathname, &st))
465 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
467 remote_fileio_reply (-1, FILEIO_ENODEV);
468 return;
470 if (S_ISDIR (st.st_mode)
471 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
473 remote_fileio_reply (-1, FILEIO_EISDIR);
474 return;
478 remote_fio_no_longjmp = 1;
479 fd = gdb_open_cloexec (pathname, flags, mode);
480 if (fd < 0)
482 remote_fileio_return_errno (-1);
483 return;
486 fd = remote_fileio_fd_to_targetfd (fd);
487 remote_fileio_return_success (fd);
490 static void
491 remote_fileio_func_close (char *buf)
493 long num;
494 int fd;
496 /* Parameter: file descriptor */
497 if (remote_fileio_extract_int (&buf, &num))
499 remote_fileio_ioerror ();
500 return;
502 fd = remote_fileio_map_fd ((int) num);
503 if (fd == FIO_FD_INVALID)
505 remote_fileio_badfd ();
506 return;
509 remote_fio_no_longjmp = 1;
510 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
511 remote_fileio_return_errno (-1);
512 remote_fileio_close_target_fd ((int) num);
513 remote_fileio_return_success (0);
516 static void
517 remote_fileio_func_read (char *buf)
519 long target_fd, num;
520 LONGEST lnum;
521 CORE_ADDR ptrval;
522 int fd, ret;
523 gdb_byte *buffer;
524 size_t length;
525 off_t old_offset, new_offset;
527 /* 1. Parameter: file descriptor */
528 if (remote_fileio_extract_int (&buf, &target_fd))
530 remote_fileio_ioerror ();
531 return;
533 fd = remote_fileio_map_fd ((int) target_fd);
534 if (fd == FIO_FD_INVALID)
536 remote_fileio_badfd ();
537 return;
539 /* 2. Parameter: buffer pointer */
540 if (remote_fileio_extract_long (&buf, &lnum))
542 remote_fileio_ioerror ();
543 return;
545 ptrval = (CORE_ADDR) lnum;
546 /* 3. Parameter: buffer length */
547 if (remote_fileio_extract_int (&buf, &num))
549 remote_fileio_ioerror ();
550 return;
552 length = (size_t) num;
554 switch (fd)
556 case FIO_FD_CONSOLE_OUT:
557 remote_fileio_badfd ();
558 return;
559 case FIO_FD_CONSOLE_IN:
561 static char *remaining_buf = NULL;
562 static int remaining_length = 0;
564 buffer = (gdb_byte *) xmalloc (16384);
565 if (remaining_buf)
567 remote_fio_no_longjmp = 1;
568 if (remaining_length > length)
570 memcpy (buffer, remaining_buf, length);
571 memmove (remaining_buf, remaining_buf + length,
572 remaining_length - length);
573 remaining_length -= length;
574 ret = length;
576 else
578 memcpy (buffer, remaining_buf, remaining_length);
579 xfree (remaining_buf);
580 remaining_buf = NULL;
581 ret = remaining_length;
584 else
586 /* Windows (at least XP and Server 2003) has difficulty
587 with large reads from consoles. If a handle is
588 backed by a real console device, overly large reads
589 from the handle will fail and set errno == ENOMEM.
590 On a Windows Server 2003 system where I tested,
591 reading 26608 bytes from the console was OK, but
592 anything above 26609 bytes would fail. The limit has
593 been observed to vary on different systems. So, we
594 limit this read to something smaller than that - by a
595 safe margin, in case the limit depends on system
596 resources or version. */
597 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
598 remote_fio_no_longjmp = 1;
599 if (ret > 0 && (size_t)ret > length)
601 remaining_buf = (char *) xmalloc (ret - length);
602 remaining_length = ret - length;
603 memcpy (remaining_buf, buffer + length, remaining_length);
604 ret = length;
608 break;
609 default:
610 buffer = (gdb_byte *) xmalloc (length);
611 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
612 for read() to return -1 even if "some" bytes have been read. It
613 has been corrected in SUSv2 but that doesn't help us much...
614 Therefore a complete solution must check how many bytes have been
615 read on EINTR to return a more reliable value to the target */
616 old_offset = lseek (fd, 0, SEEK_CUR);
617 remote_fio_no_longjmp = 1;
618 ret = read (fd, buffer, length);
619 if (ret < 0 && errno == EINTR)
621 new_offset = lseek (fd, 0, SEEK_CUR);
622 /* If some data has been read, return the number of bytes read.
623 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
624 if (old_offset != new_offset)
625 ret = new_offset - old_offset;
627 break;
630 if (ret > 0)
632 errno = target_write_memory (ptrval, buffer, ret);
633 if (errno != 0)
634 ret = -1;
637 if (ret < 0)
638 remote_fileio_return_errno (-1);
639 else
640 remote_fileio_return_success (ret);
642 xfree (buffer);
645 static void
646 remote_fileio_func_write (char *buf)
648 long target_fd, num;
649 LONGEST lnum;
650 CORE_ADDR ptrval;
651 int fd, ret;
652 gdb_byte *buffer;
653 size_t length;
655 /* 1. Parameter: file descriptor */
656 if (remote_fileio_extract_int (&buf, &target_fd))
658 remote_fileio_ioerror ();
659 return;
661 fd = remote_fileio_map_fd ((int) target_fd);
662 if (fd == FIO_FD_INVALID)
664 remote_fileio_badfd ();
665 return;
667 /* 2. Parameter: buffer pointer */
668 if (remote_fileio_extract_long (&buf, &lnum))
670 remote_fileio_ioerror ();
671 return;
673 ptrval = (CORE_ADDR) lnum;
674 /* 3. Parameter: buffer length */
675 if (remote_fileio_extract_int (&buf, &num))
677 remote_fileio_ioerror ();
678 return;
680 length = (size_t) num;
682 buffer = (gdb_byte *) xmalloc (length);
683 if (target_read_memory (ptrval, buffer, length) != 0)
685 xfree (buffer);
686 remote_fileio_ioerror ();
687 return;
690 remote_fio_no_longjmp = 1;
691 switch (fd)
693 case FIO_FD_CONSOLE_IN:
694 remote_fileio_badfd ();
695 xfree (buffer);
696 return;
697 case FIO_FD_CONSOLE_OUT:
698 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
699 (char *) buffer, length);
700 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
701 ret = length;
702 break;
703 default:
704 ret = write (fd, buffer, length);
705 if (ret < 0 && errno == EACCES)
706 errno = EBADF; /* Cygwin returns EACCESS when writing to a
707 R/O file. */
708 break;
711 if (ret < 0)
712 remote_fileio_return_errno (-1);
713 else
714 remote_fileio_return_success (ret);
716 xfree (buffer);
719 static void
720 remote_fileio_func_lseek (char *buf)
722 long num;
723 LONGEST lnum;
724 int fd, flag;
725 off_t offset, ret;
727 /* 1. Parameter: file descriptor */
728 if (remote_fileio_extract_int (&buf, &num))
730 remote_fileio_ioerror ();
731 return;
733 fd = remote_fileio_map_fd ((int) num);
734 if (fd == FIO_FD_INVALID)
736 remote_fileio_badfd ();
737 return;
739 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
741 remote_fileio_reply (-1, FILEIO_ESPIPE);
742 return;
745 /* 2. Parameter: offset */
746 if (remote_fileio_extract_long (&buf, &lnum))
748 remote_fileio_ioerror ();
749 return;
751 offset = (off_t) lnum;
752 /* 3. Parameter: flag */
753 if (remote_fileio_extract_int (&buf, &num))
755 remote_fileio_ioerror ();
756 return;
758 if (remote_fileio_seek_flag_to_host (num, &flag))
760 remote_fileio_reply (-1, FILEIO_EINVAL);
761 return;
764 remote_fio_no_longjmp = 1;
765 ret = lseek (fd, offset, flag);
767 if (ret == (off_t) -1)
768 remote_fileio_return_errno (-1);
769 else
770 remote_fileio_return_success (ret);
773 static void
774 remote_fileio_func_rename (char *buf)
776 CORE_ADDR old_ptr, new_ptr;
777 int old_len, new_len;
778 char *oldpath, *newpath;
779 int ret, of, nf;
780 struct stat ost, nst;
782 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
783 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
785 remote_fileio_ioerror ();
786 return;
789 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
790 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
792 remote_fileio_ioerror ();
793 return;
796 /* Request oldpath using 'm' packet */
797 oldpath = alloca (old_len);
798 if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
800 remote_fileio_ioerror ();
801 return;
804 /* Request newpath using 'm' packet */
805 newpath = alloca (new_len);
806 if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
808 remote_fileio_ioerror ();
809 return;
812 /* Only operate on regular files and directories. */
813 of = stat (oldpath, &ost);
814 nf = stat (newpath, &nst);
815 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
816 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
818 remote_fileio_reply (-1, FILEIO_EACCES);
819 return;
822 remote_fio_no_longjmp = 1;
823 ret = rename (oldpath, newpath);
825 if (ret == -1)
827 /* Special case: newpath is a non-empty directory. Some systems
828 return ENOTEMPTY, some return EEXIST. We coerce that to be
829 always EEXIST. */
830 if (errno == ENOTEMPTY)
831 errno = EEXIST;
832 #ifdef __CYGWIN__
833 /* Workaround some Cygwin problems with correct errnos. */
834 if (errno == EACCES)
836 if (!of && !nf && S_ISDIR (nst.st_mode))
838 if (S_ISREG (ost.st_mode))
839 errno = EISDIR;
840 else
842 char oldfullpath[PATH_MAX];
843 char newfullpath[PATH_MAX];
844 int len;
846 cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
847 PATH_MAX);
848 cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
849 PATH_MAX);
850 len = strlen (oldfullpath);
851 if (IS_DIR_SEPARATOR (newfullpath[len])
852 && !filename_ncmp (oldfullpath, newfullpath, len))
853 errno = EINVAL;
854 else
855 errno = EEXIST;
859 #endif
861 remote_fileio_return_errno (-1);
863 else
864 remote_fileio_return_success (ret);
867 static void
868 remote_fileio_func_unlink (char *buf)
870 CORE_ADDR ptrval;
871 int length;
872 char *pathname;
873 int ret;
874 struct stat st;
876 /* Parameter: Ptr to pathname / length incl. trailing zero */
877 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
879 remote_fileio_ioerror ();
880 return;
882 /* Request pathname using 'm' packet */
883 pathname = alloca (length);
884 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
886 remote_fileio_ioerror ();
887 return;
890 /* Only operate on regular files (and directories, which allows to return
891 the correct return code). */
892 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
894 remote_fileio_reply (-1, FILEIO_ENODEV);
895 return;
898 remote_fio_no_longjmp = 1;
899 ret = unlink (pathname);
901 if (ret == -1)
902 remote_fileio_return_errno (-1);
903 else
904 remote_fileio_return_success (ret);
907 static void
908 remote_fileio_func_stat (char *buf)
910 CORE_ADDR statptr, nameptr;
911 int ret, namelength;
912 char *pathname;
913 LONGEST lnum;
914 struct stat st;
915 struct fio_stat fst;
917 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
918 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
920 remote_fileio_ioerror ();
921 return;
924 /* 2. Parameter: Ptr to struct stat */
925 if (remote_fileio_extract_long (&buf, &lnum))
927 remote_fileio_ioerror ();
928 return;
930 statptr = (CORE_ADDR) lnum;
932 /* Request pathname using 'm' packet */
933 pathname = alloca (namelength);
934 if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
936 remote_fileio_ioerror ();
937 return;
940 remote_fio_no_longjmp = 1;
941 ret = stat (pathname, &st);
943 if (ret == -1)
945 remote_fileio_return_errno (-1);
946 return;
948 /* Only operate on regular files and directories. */
949 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
951 remote_fileio_reply (-1, FILEIO_EACCES);
952 return;
954 if (statptr)
956 host_to_fileio_stat (&st, &fst);
957 host_to_fileio_uint (0, fst.fst_dev);
959 errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
960 if (errno != 0)
962 remote_fileio_return_errno (-1);
963 return;
966 remote_fileio_return_success (ret);
969 static void
970 remote_fileio_func_fstat (char *buf)
972 CORE_ADDR ptrval;
973 int fd, ret;
974 long target_fd;
975 LONGEST lnum;
976 struct stat st;
977 struct fio_stat fst;
978 struct timeval tv;
980 /* 1. Parameter: file descriptor */
981 if (remote_fileio_extract_int (&buf, &target_fd))
983 remote_fileio_ioerror ();
984 return;
986 fd = remote_fileio_map_fd ((int) target_fd);
987 if (fd == FIO_FD_INVALID)
989 remote_fileio_badfd ();
990 return;
992 /* 2. Parameter: Ptr to struct stat */
993 if (remote_fileio_extract_long (&buf, &lnum))
995 remote_fileio_ioerror ();
996 return;
998 ptrval = (CORE_ADDR) lnum;
1000 remote_fio_no_longjmp = 1;
1001 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1003 host_to_fileio_uint (1, fst.fst_dev);
1004 memset (&st, 0, sizeof (st));
1005 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1006 st.st_nlink = 1;
1007 #ifdef HAVE_GETUID
1008 st.st_uid = getuid ();
1009 #endif
1010 #ifdef HAVE_GETGID
1011 st.st_gid = getgid ();
1012 #endif
1013 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1014 st.st_blksize = 512;
1015 #endif
1016 #if HAVE_STRUCT_STAT_ST_BLOCKS
1017 st.st_blocks = 0;
1018 #endif
1019 if (!gettimeofday (&tv, NULL))
1020 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1021 else
1022 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1023 ret = 0;
1025 else
1026 ret = fstat (fd, &st);
1028 if (ret == -1)
1030 remote_fileio_return_errno (-1);
1031 return;
1033 if (ptrval)
1035 host_to_fileio_stat (&st, &fst);
1037 errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1038 if (errno != 0)
1040 remote_fileio_return_errno (-1);
1041 return;
1044 remote_fileio_return_success (ret);
1047 static void
1048 remote_fileio_func_gettimeofday (char *buf)
1050 LONGEST lnum;
1051 CORE_ADDR ptrval;
1052 int ret;
1053 struct timeval tv;
1054 struct fio_timeval ftv;
1056 /* 1. Parameter: struct timeval pointer */
1057 if (remote_fileio_extract_long (&buf, &lnum))
1059 remote_fileio_ioerror ();
1060 return;
1062 ptrval = (CORE_ADDR) lnum;
1063 /* 2. Parameter: some pointer value... */
1064 if (remote_fileio_extract_long (&buf, &lnum))
1066 remote_fileio_ioerror ();
1067 return;
1069 /* ...which has to be NULL. */
1070 if (lnum)
1072 remote_fileio_reply (-1, FILEIO_EINVAL);
1073 return;
1076 remote_fio_no_longjmp = 1;
1077 ret = gettimeofday (&tv, NULL);
1079 if (ret == -1)
1081 remote_fileio_return_errno (-1);
1082 return;
1085 if (ptrval)
1087 remote_fileio_to_fio_timeval (&tv, &ftv);
1089 errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1090 if (errno != 0)
1092 remote_fileio_return_errno (-1);
1093 return;
1096 remote_fileio_return_success (ret);
1099 static void
1100 remote_fileio_func_isatty (char *buf)
1102 long target_fd;
1103 int fd;
1105 /* Parameter: file descriptor */
1106 if (remote_fileio_extract_int (&buf, &target_fd))
1108 remote_fileio_ioerror ();
1109 return;
1111 remote_fio_no_longjmp = 1;
1112 fd = remote_fileio_map_fd ((int) target_fd);
1113 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1114 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1117 static void
1118 remote_fileio_func_system (char *buf)
1120 CORE_ADDR ptrval;
1121 int ret, length;
1122 char *cmdline = NULL;
1124 /* Parameter: Ptr to commandline / length incl. trailing zero */
1125 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1127 remote_fileio_ioerror ();
1128 return;
1131 if (length)
1133 /* Request commandline using 'm' packet */
1134 cmdline = alloca (length);
1135 if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
1137 remote_fileio_ioerror ();
1138 return;
1142 /* Check if system(3) has been explicitely allowed using the
1143 `set remote system-call-allowed 1' command. If length is 0,
1144 indicating a NULL parameter to the system call, return zero to
1145 indicate a shell is not available. Otherwise fail with EPERM. */
1146 if (!remote_fio_system_call_allowed)
1148 if (!length)
1149 remote_fileio_return_success (0);
1150 else
1151 remote_fileio_reply (-1, FILEIO_EPERM);
1152 return;
1155 remote_fio_no_longjmp = 1;
1156 ret = system (cmdline);
1158 if (!length)
1159 remote_fileio_return_success (ret);
1160 else if (ret == -1)
1161 remote_fileio_return_errno (-1);
1162 else
1163 remote_fileio_return_success (WEXITSTATUS (ret));
1166 static struct {
1167 char *name;
1168 void (*func)(char *);
1169 } remote_fio_func_map[] = {
1170 { "open", remote_fileio_func_open },
1171 { "close", remote_fileio_func_close },
1172 { "read", remote_fileio_func_read },
1173 { "write", remote_fileio_func_write },
1174 { "lseek", remote_fileio_func_lseek },
1175 { "rename", remote_fileio_func_rename },
1176 { "unlink", remote_fileio_func_unlink },
1177 { "stat", remote_fileio_func_stat },
1178 { "fstat", remote_fileio_func_fstat },
1179 { "gettimeofday", remote_fileio_func_gettimeofday },
1180 { "isatty", remote_fileio_func_isatty },
1181 { "system", remote_fileio_func_system },
1182 { NULL, NULL }
1185 static int
1186 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1188 char *buf = buf_arg;
1189 char *c;
1190 int idx;
1192 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1194 c = strchr (++buf, ',');
1195 if (c)
1196 *c++ = '\0';
1197 else
1198 c = strchr (buf, '\0');
1199 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1200 if (!strcmp (remote_fio_func_map[idx].name, buf))
1201 break;
1202 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1203 return RETURN_ERROR;
1204 remote_fio_func_map[idx].func (c);
1205 return 0;
1208 /* Close any open descriptors, and reinitialize the file mapping. */
1210 void
1211 remote_fileio_reset (void)
1213 int ix;
1215 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1217 int fd = remote_fio_data.fd_map[ix];
1219 if (fd >= 0)
1220 close (fd);
1222 if (remote_fio_data.fd_map)
1224 xfree (remote_fio_data.fd_map);
1225 remote_fio_data.fd_map = NULL;
1226 remote_fio_data.fd_map_size = 0;
1230 /* Handle a file I/O request. BUF points to the packet containing the
1231 request. CTRLC_PENDING_P should be nonzero if the target has not
1232 acknowledged the Ctrl-C sent asynchronously earlier. */
1234 void
1235 remote_fileio_request (char *buf, int ctrlc_pending_p)
1237 int ex;
1239 remote_fileio_sig_init ();
1241 if (ctrlc_pending_p)
1243 /* If the target hasn't responded to the Ctrl-C sent
1244 asynchronously earlier, take this opportunity to send the
1245 Ctrl-C synchronously. */
1246 remote_fio_ctrl_c_flag = 1;
1247 remote_fio_no_longjmp = 0;
1248 remote_fileio_reply (-1, FILEIO_EINTR);
1250 else
1252 remote_fio_ctrl_c_flag = 0;
1253 remote_fio_no_longjmp = 0;
1255 ex = catch_exceptions (current_uiout,
1256 do_remote_fileio_request, (void *)buf,
1257 RETURN_MASK_ALL);
1258 switch (ex)
1260 case RETURN_ERROR:
1261 remote_fileio_reply (-1, FILEIO_ENOSYS);
1262 break;
1263 case RETURN_QUIT:
1264 remote_fileio_reply (-1, FILEIO_EINTR);
1265 break;
1266 default:
1267 break;
1271 remote_fileio_sig_exit ();
1275 /* Unpack an fio_uint_t. */
1277 static unsigned int
1278 remote_fileio_to_host_uint (fio_uint_t fnum)
1280 return extract_unsigned_integer ((gdb_byte *) fnum, 4,
1281 BFD_ENDIAN_BIG);
1284 /* Unpack an fio_ulong_t. */
1286 static ULONGEST
1287 remote_fileio_to_host_ulong (fio_ulong_t fnum)
1289 return extract_unsigned_integer ((gdb_byte *) fnum, 8,
1290 BFD_ENDIAN_BIG);
1293 /* Unpack an fio_mode_t. */
1295 static mode_t
1296 remote_fileio_to_host_mode (fio_mode_t fnum)
1298 return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
1302 /* Unpack an fio_time_t. */
1304 static time_t
1305 remote_fileio_to_host_time (fio_time_t fnum)
1307 return remote_fileio_to_host_uint (fnum);
1311 /* See remote-fileio.h. */
1313 void
1314 remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
1316 memset (st, 0, sizeof (struct stat));
1318 st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
1319 st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
1320 st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
1321 st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
1322 st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
1323 st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
1324 st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
1325 st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
1326 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1327 st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
1328 #endif
1329 #if HAVE_STRUCT_STAT_ST_BLOCKS
1330 st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
1331 #endif
1332 st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
1333 st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
1334 st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
1338 static void
1339 set_system_call_allowed (char *args, int from_tty)
1341 if (args)
1343 char *arg_end;
1344 int val = strtoul (args, &arg_end, 10);
1346 if (*args && *arg_end == '\0')
1348 remote_fio_system_call_allowed = !!val;
1349 return;
1352 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1355 static void
1356 show_system_call_allowed (char *args, int from_tty)
1358 if (args)
1359 error (_("Garbage after \"show remote "
1360 "system-call-allowed\" command: `%s'"), args);
1361 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1362 remote_fio_system_call_allowed ? "" : "not ");
1365 void
1366 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1367 struct cmd_list_element *remote_show_cmdlist)
1369 sigint_fileio_token =
1370 create_async_signal_handler (async_remote_fileio_interrupt, NULL);
1372 add_cmd ("system-call-allowed", no_class,
1373 set_system_call_allowed,
1374 _("Set if the host system(3) call is allowed for the target."),
1375 &remote_set_cmdlist);
1376 add_cmd ("system-call-allowed", no_class,
1377 show_system_call_allowed,
1378 _("Show if the host system(3) call is allowed for the target."),
1379 &remote_show_cmdlist);