* remote-fileio.c (remote_fileio_mode_to_target): Use
[platform/upstream/binutils.git] / gdb / remote-fileio.c
1 /* Remote File-I/O communications
2
3    Copyright (C) 2003, 2005, 2006 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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.  */
21
22 /* See the GDB User Guide for details of the GDB remote protocol. */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "gdbcmd.h"
27 #include "remote.h"
28 #include "gdb/fileio.h"
29 #include "gdb_wait.h"
30 #include "gdb_stat.h"
31 #include "exceptions.h"
32 #include "remote-fileio.h"
33
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #ifdef __CYGWIN__
37 #include <sys/cygwin.h>         /* For cygwin_conv_to_full_posix_path.  */
38 #endif
39 #include <signal.h>
40
41 static struct {
42   int *fd_map;
43   int fd_map_size;
44 } remote_fio_data;
45
46 #define FIO_FD_INVALID          -1
47 #define FIO_FD_CONSOLE_IN       -2
48 #define FIO_FD_CONSOLE_OUT      -3
49
50 static int remote_fio_system_call_allowed = 0;
51
52 static int
53 remote_fileio_init_fd_map (void)
54 {
55   int i;
56
57   if (!remote_fio_data.fd_map)
58     {
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;
66     }
67   return 3;
68 }
69
70 static int
71 remote_fileio_resize_fd_map (void)
72 {
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;
80 }
81
82 static int
83 remote_fileio_next_free_fd (void)
84 {
85   int i;
86
87   for (i = 0; i < remote_fio_data.fd_map_size; ++i)
88     if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
89       return i;
90   return remote_fileio_resize_fd_map ();
91 }
92
93 static int
94 remote_fileio_fd_to_targetfd (int fd)
95 {
96   int target_fd = remote_fileio_next_free_fd ();
97   remote_fio_data.fd_map[target_fd] = fd;
98   return target_fd;
99 }
100
101 static int
102 remote_fileio_map_fd (int target_fd)
103 {
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];
108 }
109
110 static void
111 remote_fileio_close_target_fd (int target_fd)
112 {
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;
116 }
117
118 static int
119 remote_fileio_oflags_to_host (long flags)
120 {
121   int hflags = 0;
122
123   if (flags & FILEIO_O_CREAT)
124     hflags |= O_CREAT;
125   if (flags & FILEIO_O_EXCL)
126     hflags |= O_EXCL;
127   if (flags & FILEIO_O_TRUNC)
128     hflags |= O_TRUNC;
129   if (flags & FILEIO_O_APPEND)
130     hflags |= O_APPEND;
131   if (flags & FILEIO_O_RDONLY)
132     hflags |= O_RDONLY;
133   if (flags & FILEIO_O_WRONLY)
134     hflags |= O_WRONLY;
135   if (flags & FILEIO_O_RDWR)
136     hflags |= O_RDWR;
137 /* On systems supporting binary and text mode, always open files in
138    binary mode. */
139 #ifdef O_BINARY
140   hflags |= O_BINARY;
141 #endif
142   return hflags;
143 }
144
145 static mode_t
146 remote_fileio_mode_to_host (long mode, int open_call)
147 {
148   mode_t hmode = 0;
149
150   if (!open_call)
151     {
152       if (mode & FILEIO_S_IFREG)
153         hmode |= S_IFREG;
154       if (mode & FILEIO_S_IFDIR)
155         hmode |= S_IFDIR;
156       if (mode & FILEIO_S_IFCHR)
157         hmode |= S_IFCHR;
158     }
159   if (mode & FILEIO_S_IRUSR)
160     hmode |= S_IRUSR;
161   if (mode & FILEIO_S_IWUSR)
162     hmode |= S_IWUSR;
163   if (mode & FILEIO_S_IXUSR)
164     hmode |= S_IXUSR;
165 #ifdef S_IRGRP
166   if (mode & FILEIO_S_IRGRP)
167     hmode |= S_IRGRP;
168 #endif
169 #ifdef S_IWGRP
170   if (mode & FILEIO_S_IWGRP)
171     hmode |= S_IWGRP;
172 #endif
173 #ifdef S_IXGRP
174   if (mode & FILEIO_S_IXGRP)
175     hmode |= S_IXGRP;
176 #endif
177   if (mode & FILEIO_S_IROTH)
178     hmode |= S_IROTH;
179 #ifdef S_IWOTH
180   if (mode & FILEIO_S_IWOTH)
181     hmode |= S_IWOTH;
182 #endif
183 #ifdef S_IXOTH
184   if (mode & FILEIO_S_IXOTH)
185     hmode |= S_IXOTH;
186 #endif
187   return hmode;
188 }
189
190 static LONGEST
191 remote_fileio_mode_to_target (mode_t mode)
192 {
193   mode_t tmode = 0;
194
195   if (S_ISREG(mode))
196     tmode |= FILEIO_S_IFREG;
197   if (S_ISDIR(mode))
198     tmode |= FILEIO_S_IFDIR;
199   if (S_ISCHR(mode))
200     tmode |= FILEIO_S_IFCHR;
201   if (mode & S_IRUSR)
202     tmode |= FILEIO_S_IRUSR;
203   if (mode & S_IWUSR)
204     tmode |= FILEIO_S_IWUSR;
205   if (mode & S_IXUSR)
206     tmode |= FILEIO_S_IXUSR;
207 #ifdef S_IRGRP
208   if (mode & S_IRGRP)
209     tmode |= FILEIO_S_IRGRP;
210 #endif
211 #ifdef S_IWRGRP
212   if (mode & S_IWGRP)
213     tmode |= FILEIO_S_IWGRP;
214 #endif
215 #ifdef S_IXGRP
216   if (mode & S_IXGRP)
217     tmode |= FILEIO_S_IXGRP;
218 #endif
219   if (mode & S_IROTH)
220     tmode |= FILEIO_S_IROTH;
221 #ifdef S_IWOTH
222   if (mode & S_IWOTH)
223     tmode |= FILEIO_S_IWOTH;
224 #endif
225 #ifdef S_IXOTH
226   if (mode & S_IXOTH)
227     tmode |= FILEIO_S_IXOTH;
228 #endif
229   return tmode;
230 }
231
232 static int
233 remote_fileio_errno_to_target (int error)
234 {
235   switch (error)
236     {
237       case EPERM:
238         return FILEIO_EPERM;
239       case ENOENT:
240         return FILEIO_ENOENT;
241       case EINTR:
242         return FILEIO_EINTR;
243       case EIO:
244         return FILEIO_EIO;
245       case EBADF:
246         return FILEIO_EBADF;
247       case EACCES:
248         return FILEIO_EACCES;
249       case EFAULT:
250         return FILEIO_EFAULT;
251       case EBUSY:
252         return FILEIO_EBUSY;
253       case EEXIST:
254         return FILEIO_EEXIST;
255       case ENODEV:
256         return FILEIO_ENODEV;
257       case ENOTDIR:
258         return FILEIO_ENOTDIR;
259       case EISDIR:
260         return FILEIO_EISDIR;
261       case EINVAL:
262         return FILEIO_EINVAL;
263       case ENFILE:
264         return FILEIO_ENFILE;
265       case EMFILE:
266         return FILEIO_EMFILE;
267       case EFBIG:
268         return FILEIO_EFBIG;
269       case ENOSPC:
270         return FILEIO_ENOSPC;
271       case ESPIPE:
272         return FILEIO_ESPIPE;
273       case EROFS:
274         return FILEIO_EROFS;
275       case ENOSYS:
276         return FILEIO_ENOSYS;
277       case ENAMETOOLONG:
278         return FILEIO_ENAMETOOLONG;
279     }
280   return FILEIO_EUNKNOWN;
281 }
282
283 static int
284 remote_fileio_seek_flag_to_host (long num, int *flag)
285 {
286   if (!flag)
287     return 0;
288   switch (num)
289     {
290       case FILEIO_SEEK_SET:
291         *flag = SEEK_SET;
292         break;
293       case FILEIO_SEEK_CUR:
294         *flag =  SEEK_CUR;
295         break;
296       case FILEIO_SEEK_END:
297         *flag =  SEEK_END;
298         break;
299       default:
300         return -1;
301     }
302   return 0;
303 }
304
305 static int
306 remote_fileio_extract_long (char **buf, LONGEST *retlong)
307 {
308   char *c;
309   int sign = 1;
310
311   if (!buf || !*buf || !**buf || !retlong)
312     return -1;
313   c = strchr (*buf, ',');
314   if (c)
315     *c++ = '\0';
316   else
317     c = strchr (*buf, '\0');
318   while (strchr ("+-", **buf))
319     {
320       if (**buf == '-')
321         sign = -sign;
322       ++*buf;
323     }
324   for (*retlong = 0; **buf; ++*buf)
325     {
326       *retlong <<= 4;
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;
333       else
334         return -1;
335     }
336   *retlong *= sign;
337   *buf = c;
338   return 0;
339 }
340
341 static int
342 remote_fileio_extract_int (char **buf, long *retint)
343 {
344   int ret;
345   LONGEST retlong;
346
347   if (!retint)
348     return -1;
349   ret = remote_fileio_extract_long (buf, &retlong);
350   if (!ret)
351     *retint = (long) retlong;
352   return ret;
353 }
354
355 static int
356 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
357 {
358   char *c;
359   LONGEST retlong;
360
361   if (!buf || !*buf || !**buf || !ptrval || !length)
362     return -1;
363   c = strchr (*buf, '/');
364   if (!c)
365     return -1;
366   *c++ = '\0';
367   if (remote_fileio_extract_long (buf, &retlong))
368     return -1;
369   *ptrval = (CORE_ADDR) retlong;
370   *buf = c;
371   if (remote_fileio_extract_long (buf, &retlong))
372     return -1;
373   *length = (int) retlong;
374   return 0;
375 }
376
377 /* Convert to big endian */
378 static void
379 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
380 {
381   int i;
382
383   for (i = 0; i < bytes; ++i)
384     buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
385 }
386
387 static void
388 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
389 {
390   remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
391 }
392
393 static void
394 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
395 {
396   remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
397 }
398
399 static void
400 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
401 {
402   remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
403 }
404
405 static void
406 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
407 {
408   remote_fileio_to_be (num, (char *) fnum, 8);
409 }
410
411 static void
412 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
413 {
414   remote_fileio_to_be (num, (char *) fnum, 8);
415 }
416
417 static void
418 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
419 {
420   LONGEST blksize;
421
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;
432 #else
433   blksize = 512;
434 #endif
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);
438 #else
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)
443                               / blksize,
444                               fst->fst_blocks);
445 #endif
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);
449 }
450
451 static void
452 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
453 {
454   remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
455   remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
456 }
457
458 static int remote_fio_ctrl_c_flag = 0;
459 static int remote_fio_no_longjmp = 0;
460
461 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
462 static struct sigaction remote_fio_sa;
463 static struct sigaction remote_fio_osa;
464 #else
465 static void (*remote_fio_ofunc)(int);
466 #endif
467
468 static void
469 remote_fileio_sig_init (void)
470 {
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);
476 #else
477   remote_fio_ofunc = signal (SIGINT, SIG_IGN);
478 #endif
479 }
480
481 static void
482 remote_fileio_sig_set (void (*sigint_func)(int))
483 {
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);
489 #else
490   signal (SIGINT, sigint_func);
491 #endif
492 }
493
494 static void
495 remote_fileio_sig_exit (void)
496 {
497 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
498   sigaction (SIGINT, &remote_fio_osa, NULL);
499 #else
500   signal (SIGINT, remote_fio_ofunc);
501 #endif
502 }
503
504 static void
505 remote_fileio_ctrl_c_signal_handler (int signo)
506 {
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);
512 }
513
514 static void
515 remote_fileio_reply (int retcode, int error)
516 {
517   char buf[32];
518
519   remote_fileio_sig_set (SIG_IGN);
520   strcpy (buf, "F");
521   if (retcode < 0)
522     {
523       strcat (buf, "-");
524       retcode = -retcode;
525     }
526   sprintf (buf + strlen (buf), "%x", retcode);
527   if (error || remote_fio_ctrl_c_flag)
528     {
529       if (error && remote_fio_ctrl_c_flag)
530         error = FILEIO_EINTR;
531       if (error < 0)
532         {
533           strcat (buf, "-");
534           error = -error;
535         }
536       sprintf (buf + strlen (buf), ",%x", error);
537       if (remote_fio_ctrl_c_flag)
538         strcat (buf, ",C");
539     }
540   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
541   putpkt (buf);
542 }
543
544 static void
545 remote_fileio_ioerror (void)
546 {
547   remote_fileio_reply (-1, FILEIO_EIO);
548 }
549
550 static void
551 remote_fileio_badfd (void)
552 {
553   remote_fileio_reply (-1, FILEIO_EBADF);
554 }
555
556 static void
557 remote_fileio_return_errno (int retcode)
558 {
559   remote_fileio_reply (retcode,
560                        retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
561 }
562
563 static void
564 remote_fileio_return_success (int retcode)
565 {
566   remote_fileio_reply (retcode, 0);
567 }
568
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. */
572 static int
573 remote_fileio_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
574 {
575   int ret = 0, written;
576
577   while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
578     {
579       len -= written;
580       memaddr += written;
581       myaddr += written;
582       ret += written;
583     }
584   return ret;
585 }
586
587 static void
588 remote_fileio_func_open (char *buf)
589 {
590   CORE_ADDR ptrval;
591   int length, retlength;
592   long num;
593   int flags, fd;
594   mode_t mode;
595   char *pathname;
596   struct stat st;
597
598   /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
599   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
600     {
601       remote_fileio_ioerror ();
602       return;
603     }
604   /* 2. Parameter: open flags */
605   if (remote_fileio_extract_int (&buf, &num))
606     {
607       remote_fileio_ioerror ();
608       return;
609     }
610   flags = remote_fileio_oflags_to_host (num);
611   /* 3. Parameter: open mode */
612   if (remote_fileio_extract_int (&buf, &num))
613     {
614       remote_fileio_ioerror ();
615       return;
616     }
617   mode = remote_fileio_mode_to_host (num, 1);
618
619   /* Request pathname using 'm' packet */
620   pathname = alloca (length);
621   retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
622   if (retlength != length)
623     {
624       remote_fileio_ioerror ();
625       return;
626     }
627
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
630      for writing. */
631   if (!stat (pathname, &st))
632     {
633       if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
634         {
635           remote_fileio_reply (-1, FILEIO_ENODEV);
636           return;
637         }
638       if (S_ISDIR (st.st_mode)
639           && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
640         {
641           remote_fileio_reply (-1, FILEIO_EISDIR);
642           return;
643         }
644     }
645
646   remote_fio_no_longjmp = 1;
647   fd = open (pathname, flags, mode);
648   if (fd < 0)
649     {
650       remote_fileio_return_errno (-1);
651       return;
652     }
653
654   fd = remote_fileio_fd_to_targetfd (fd);
655   remote_fileio_return_success (fd);
656 }
657
658 static void
659 remote_fileio_func_close (char *buf)
660 {
661   long num;
662   int fd;
663
664   /* Parameter: file descriptor */
665   if (remote_fileio_extract_int (&buf, &num))
666     {
667       remote_fileio_ioerror ();
668       return;
669     }
670   fd = remote_fileio_map_fd ((int) num);
671   if (fd == FIO_FD_INVALID)
672     {
673       remote_fileio_badfd ();
674       return;
675     }
676
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);
682 }
683
684 static void
685 remote_fileio_func_read (char *buf)
686 {
687   long target_fd, num;
688   LONGEST lnum;
689   CORE_ADDR ptrval;
690   int fd, ret, retlength;
691   gdb_byte *buffer;
692   size_t length;
693   off_t old_offset, new_offset;
694
695   /* 1. Parameter: file descriptor */
696   if (remote_fileio_extract_int (&buf, &target_fd))
697     {
698       remote_fileio_ioerror ();
699       return;
700     }
701   fd = remote_fileio_map_fd ((int) target_fd);
702   if (fd == FIO_FD_INVALID)
703     {
704       remote_fileio_badfd ();
705       return;
706     }
707   /* 2. Parameter: buffer pointer */
708   if (remote_fileio_extract_long (&buf, &lnum))
709     {
710       remote_fileio_ioerror ();
711       return;
712     }
713   ptrval = (CORE_ADDR) lnum;
714   /* 3. Parameter: buffer length */
715   if (remote_fileio_extract_int (&buf, &num))
716     {
717       remote_fileio_ioerror ();
718       return;
719     }
720   length = (size_t) num;
721
722   switch (fd)
723     {
724       case FIO_FD_CONSOLE_OUT:
725         remote_fileio_badfd ();
726         return;
727       case FIO_FD_CONSOLE_IN:
728         {
729           static char *remaining_buf = NULL;
730           static int remaining_length = 0;
731
732           buffer = (gdb_byte *) xmalloc (32768);
733           if (remaining_buf)
734             {
735               remote_fio_no_longjmp = 1;
736               if (remaining_length > length)
737                 {
738                   memcpy (buffer, remaining_buf, length);
739                   memmove (remaining_buf, remaining_buf + length,
740                            remaining_length - length);
741                   remaining_length -= length;
742                   ret = length;
743                 }
744               else
745                 {
746                   memcpy (buffer, remaining_buf, remaining_length);
747                   xfree (remaining_buf);
748                   remaining_buf = NULL;
749                   ret = remaining_length;
750                 }
751             }
752           else
753             {
754               ret = ui_file_read (gdb_stdtargin, (char *) buffer, 32767);
755               remote_fio_no_longjmp = 1;
756               if (ret > 0 && (size_t)ret > length)
757                 {
758                   remaining_buf = (char *) xmalloc (ret - length);
759                   remaining_length = ret - length;
760                   memcpy (remaining_buf, buffer + length, remaining_length);
761                   ret = length;
762                 }
763             }
764         }
765         break;
766       default:
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)
777           {
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;
783           }
784         break;
785     }
786
787   if (ret > 0)
788     {
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() */
792     }
793
794   if (ret < 0)
795     remote_fileio_return_errno (-1);
796   else
797     remote_fileio_return_success (ret);
798
799   xfree (buffer);
800 }
801
802 static void
803 remote_fileio_func_write (char *buf)
804 {
805   long target_fd, num;
806   LONGEST lnum;
807   CORE_ADDR ptrval;
808   int fd, ret, retlength;
809   gdb_byte *buffer;
810   size_t length;
811
812   /* 1. Parameter: file descriptor */
813   if (remote_fileio_extract_int (&buf, &target_fd))
814     {
815       remote_fileio_ioerror ();
816       return;
817     }
818   fd = remote_fileio_map_fd ((int) target_fd);
819   if (fd == FIO_FD_INVALID)
820     {
821       remote_fileio_badfd ();
822       return;
823     }
824   /* 2. Parameter: buffer pointer */
825   if (remote_fileio_extract_long (&buf, &lnum))
826     {
827       remote_fileio_ioerror ();
828       return;
829     }
830   ptrval = (CORE_ADDR) lnum;
831   /* 3. Parameter: buffer length */
832   if (remote_fileio_extract_int (&buf, &num))
833     {
834       remote_fileio_ioerror ();
835       return;
836     }
837   length = (size_t) num;
838     
839   buffer = (gdb_byte *) xmalloc (length);
840   retlength = remote_read_bytes (ptrval, buffer, length);
841   if (retlength != length)
842     {
843       xfree (buffer);
844       remote_fileio_ioerror ();
845       return;
846     }
847
848   remote_fio_no_longjmp = 1;
849   switch (fd)
850     {
851       case FIO_FD_CONSOLE_IN:
852         remote_fileio_badfd ();
853         return;
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);
858         ret = length;
859         break;
860       default:
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.*/
864         break;
865     }
866
867   if (ret < 0)
868     remote_fileio_return_errno (-1);
869   else
870     remote_fileio_return_success (ret);
871
872   xfree (buffer);
873 }
874
875 static void
876 remote_fileio_func_lseek (char *buf)
877 {
878   long num;
879   LONGEST lnum;
880   int fd, flag;
881   off_t offset, ret;
882
883   /* 1. Parameter: file descriptor */
884   if (remote_fileio_extract_int (&buf, &num))
885     {
886       remote_fileio_ioerror ();
887       return;
888     }
889   fd = remote_fileio_map_fd ((int) num);
890   if (fd == FIO_FD_INVALID)
891     {
892       remote_fileio_badfd ();
893       return;
894     }
895   else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
896     {
897       remote_fileio_reply (-1, FILEIO_ESPIPE);
898       return;
899     }
900
901   /* 2. Parameter: offset */
902   if (remote_fileio_extract_long (&buf, &lnum))
903     {
904       remote_fileio_ioerror ();
905       return;
906     }
907   offset = (off_t) lnum;
908   /* 3. Parameter: flag */
909   if (remote_fileio_extract_int (&buf, &num))
910     {
911       remote_fileio_ioerror ();
912       return;
913     }
914   if (remote_fileio_seek_flag_to_host (num, &flag))
915     {
916       remote_fileio_reply (-1, FILEIO_EINVAL);
917       return;
918     }
919   
920   remote_fio_no_longjmp = 1;
921   ret = lseek (fd, offset, flag);
922
923   if (ret == (off_t) -1)
924     remote_fileio_return_errno (-1);
925   else
926     remote_fileio_return_success (ret);
927 }
928
929 static void
930 remote_fileio_func_rename (char *buf)
931 {
932   CORE_ADDR old_ptr, new_ptr;
933   int old_len, new_len, retlength;
934   char *oldpath, *newpath;
935   int ret, of, nf;
936   struct stat ost, nst;
937
938   /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
939   if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
940     {
941       remote_fileio_ioerror ();
942       return;
943     }
944   
945   /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
946   if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
947     {
948       remote_fileio_ioerror ();
949       return;
950     }
951   
952   /* Request oldpath using 'm' packet */
953   oldpath = alloca (old_len);
954   retlength = remote_read_bytes (old_ptr, (gdb_byte *) oldpath, old_len);
955   if (retlength != old_len)
956     {
957       remote_fileio_ioerror ();
958       return;
959     }
960   
961   /* Request newpath using 'm' packet */
962   newpath = alloca (new_len);
963   retlength = remote_read_bytes (new_ptr, (gdb_byte *) newpath, new_len);
964   if (retlength != new_len)
965     {
966       remote_fileio_ioerror ();
967       return;
968     }
969   
970   /* Only operate on regular files and directories */
971   of = stat (oldpath, &ost);
972   nf = stat (newpath, &nst);
973   if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
974       || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
975     {
976       remote_fileio_reply (-1, FILEIO_EACCES);
977       return;
978     }
979
980   remote_fio_no_longjmp = 1;
981   ret = rename (oldpath, newpath);
982
983   if (ret == -1)
984     {
985       /* Special case: newpath is a non-empty directory.  Some systems
986          return ENOTEMPTY, some return EEXIST.  We coerce that to be
987          always EEXIST. */
988       if (errno == ENOTEMPTY)
989         errno = EEXIST;
990 #ifdef __CYGWIN__
991       /* Workaround some Cygwin problems with correct errnos. */
992       if (errno == EACCES)
993         {
994           if (!of && !nf && S_ISDIR (nst.st_mode))
995             {
996               if (S_ISREG (ost.st_mode))
997                 errno = EISDIR;
998               else
999                 {
1000                   char oldfullpath[PATH_MAX + 1];
1001                   char newfullpath[PATH_MAX + 1];
1002                   int len;
1003
1004                   cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
1005                   cygwin_conv_to_full_posix_path (newpath, newfullpath);
1006                   len = strlen (oldfullpath);
1007                   if (newfullpath[len] == '/'
1008                       && !strncmp (oldfullpath, newfullpath, len))
1009                     errno = EINVAL;
1010                   else
1011                     errno = EEXIST;
1012                 }
1013             }
1014         }
1015 #endif
1016
1017       remote_fileio_return_errno (-1);
1018     }
1019   else
1020     remote_fileio_return_success (ret);
1021 }
1022
1023 static void
1024 remote_fileio_func_unlink (char *buf)
1025 {
1026   CORE_ADDR ptrval;
1027   int length, retlength;
1028   char *pathname;
1029   int ret;
1030   struct stat st;
1031
1032   /* Parameter: Ptr to pathname / length incl. trailing zero */
1033   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1034     {
1035       remote_fileio_ioerror ();
1036       return;
1037     }
1038   /* Request pathname using 'm' packet */
1039   pathname = alloca (length);
1040   retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
1041   if (retlength != length)
1042     {
1043       remote_fileio_ioerror ();
1044       return;
1045     }
1046
1047   /* Only operate on regular files (and directories, which allows to return
1048      the correct return code) */
1049   if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1050     {
1051       remote_fileio_reply (-1, FILEIO_ENODEV);
1052       return;
1053     }
1054
1055   remote_fio_no_longjmp = 1;
1056   ret = unlink (pathname);
1057
1058   if (ret == -1)
1059     remote_fileio_return_errno (-1);
1060   else
1061     remote_fileio_return_success (ret);
1062 }
1063
1064 static void
1065 remote_fileio_func_stat (char *buf)
1066 {
1067   CORE_ADDR statptr, nameptr;
1068   int ret, namelength, retlength;
1069   char *pathname;
1070   LONGEST lnum;
1071   struct stat st;
1072   struct fio_stat fst;
1073
1074   /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1075   if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1076     {
1077       remote_fileio_ioerror ();
1078       return;
1079     }
1080
1081   /* 2. Parameter: Ptr to struct stat */
1082   if (remote_fileio_extract_long (&buf, &lnum))
1083     {
1084       remote_fileio_ioerror ();
1085       return;
1086     }
1087   statptr = (CORE_ADDR) lnum;
1088   
1089   /* Request pathname using 'm' packet */
1090   pathname = alloca (namelength);
1091   retlength = remote_read_bytes (nameptr, (gdb_byte *) pathname, namelength);
1092   if (retlength != namelength)
1093     {
1094       remote_fileio_ioerror ();
1095       return;
1096     }
1097
1098   remote_fio_no_longjmp = 1;
1099   ret = stat (pathname, &st);
1100
1101   if (ret == -1)
1102     {
1103       remote_fileio_return_errno (-1);
1104       return;
1105     }
1106   /* Only operate on regular files and directories */
1107   if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1108     {
1109       remote_fileio_reply (-1, FILEIO_EACCES);
1110       return;
1111     }
1112   if (statptr)
1113     {
1114       remote_fileio_to_fio_stat (&st, &fst);
1115       remote_fileio_to_fio_uint (0, fst.fst_dev);
1116       
1117       retlength = remote_fileio_write_bytes (statptr,
1118                                              (gdb_byte *) &fst, sizeof fst);
1119       if (retlength != sizeof fst)
1120         {
1121           remote_fileio_return_errno (-1);
1122           return;
1123         }
1124     }
1125   remote_fileio_return_success (ret);
1126 }
1127
1128 static void
1129 remote_fileio_func_fstat (char *buf)
1130 {
1131   CORE_ADDR ptrval;
1132   int fd, ret, retlength;
1133   long target_fd;
1134   LONGEST lnum;
1135   struct stat st;
1136   struct fio_stat fst;
1137   struct timeval tv;
1138
1139   /* 1. Parameter: file descriptor */
1140   if (remote_fileio_extract_int (&buf, &target_fd))
1141     {
1142       remote_fileio_ioerror ();
1143       return;
1144     }
1145   fd = remote_fileio_map_fd ((int) target_fd);
1146   if (fd == FIO_FD_INVALID)
1147     {
1148       remote_fileio_badfd ();
1149       return;
1150     }
1151   /* 2. Parameter: Ptr to struct stat */
1152   if (remote_fileio_extract_long (&buf, &lnum))
1153     {
1154       remote_fileio_ioerror ();
1155       return;
1156     }
1157   ptrval = (CORE_ADDR) lnum;
1158
1159   remote_fio_no_longjmp = 1;
1160   if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1161     {
1162       remote_fileio_to_fio_uint (1, fst.fst_dev);
1163       st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1164       st.st_nlink = 1;
1165 #ifdef HAVE_GETUID
1166       st.st_uid = getuid ();
1167 #else
1168       st.st_uid = 0;
1169 #endif
1170 #ifdef HAVE_GETGID
1171       st.st_gid = getgid ();
1172 #else
1173       st.st_gid = 0;
1174 #endif
1175       st.st_rdev = 0;
1176       st.st_size = 0;
1177 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1178       st.st_blksize = 512;
1179 #endif
1180 #if HAVE_STRUCT_STAT_ST_BLOCKS
1181       st.st_blocks = 0;
1182 #endif
1183       if (!gettimeofday (&tv, NULL))
1184         st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1185       else
1186         st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1187       ret = 0;
1188     }
1189   else
1190     ret = fstat (fd, &st);
1191
1192   if (ret == -1)
1193     {
1194       remote_fileio_return_errno (-1);
1195       return;
1196     }
1197   if (ptrval)
1198     {
1199       remote_fileio_to_fio_stat (&st, &fst);
1200
1201       retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &fst, sizeof fst);
1202       if (retlength != sizeof fst)
1203         {
1204           remote_fileio_return_errno (-1);
1205           return;
1206         }
1207     }
1208   remote_fileio_return_success (ret);
1209 }
1210
1211 static void
1212 remote_fileio_func_gettimeofday (char *buf)
1213 {
1214   LONGEST lnum;
1215   CORE_ADDR ptrval;
1216   int ret, retlength;
1217   struct timeval tv;
1218   struct fio_timeval ftv;
1219
1220   /* 1. Parameter: struct timeval pointer */
1221   if (remote_fileio_extract_long (&buf, &lnum))
1222     {
1223       remote_fileio_ioerror ();
1224       return;
1225     }
1226   ptrval = (CORE_ADDR) lnum;
1227   /* 2. Parameter: some pointer value... */
1228   if (remote_fileio_extract_long (&buf, &lnum))
1229     {
1230       remote_fileio_ioerror ();
1231       return;
1232     }
1233   /* ...which has to be NULL */
1234   if (lnum)
1235     {
1236       remote_fileio_reply (-1, FILEIO_EINVAL);
1237       return;
1238     }
1239
1240   remote_fio_no_longjmp = 1;
1241   ret = gettimeofday (&tv, NULL);
1242
1243   if (ret == -1)
1244     {
1245       remote_fileio_return_errno (-1);
1246       return;
1247     }
1248
1249   if (ptrval)
1250     {
1251       remote_fileio_to_fio_timeval (&tv, &ftv);
1252
1253       retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1254       if (retlength != sizeof ftv)
1255         {
1256           remote_fileio_return_errno (-1);
1257           return;
1258         }
1259     }
1260   remote_fileio_return_success (ret);
1261 }
1262
1263 static void
1264 remote_fileio_func_isatty (char *buf)
1265 {
1266   long target_fd;
1267   int fd;
1268
1269   /* Parameter: file descriptor */
1270   if (remote_fileio_extract_int (&buf, &target_fd))
1271     {
1272       remote_fileio_ioerror ();
1273       return;
1274     }
1275   remote_fio_no_longjmp = 1;
1276   fd = remote_fileio_map_fd ((int) target_fd);
1277   remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1278                                 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1279 }
1280
1281 static void
1282 remote_fileio_func_system (char *buf)
1283 {
1284   CORE_ADDR ptrval;
1285   int ret, length, retlength;
1286   char *cmdline = NULL;
1287
1288   /* Parameter: Ptr to commandline / length incl. trailing zero */
1289   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1290     {
1291       remote_fileio_ioerror ();
1292       return;
1293     }
1294
1295   if (length)
1296     {
1297       /* Request commandline using 'm' packet */
1298       cmdline = alloca (length);
1299       retlength = remote_read_bytes (ptrval, (gdb_byte *) cmdline, length);
1300       if (retlength != length)
1301         {
1302           remote_fileio_ioerror ();
1303           return;
1304         }
1305     }
1306   
1307   /* Check if system(3) has been explicitely allowed using the
1308      `set remote system-call-allowed 1' command.  If length is 0,
1309      indicating a NULL parameter to the system call, return zero to
1310      indicate a shell is not available.  Otherwise fail with EPERM.  */
1311   if (!remote_fio_system_call_allowed)
1312     {
1313       if (!length)
1314         remote_fileio_return_success (0);
1315       else
1316         remote_fileio_reply (-1, FILEIO_EPERM);
1317       return;
1318     }
1319
1320   remote_fio_no_longjmp = 1;
1321   ret = system (cmdline);
1322
1323   if (!length)
1324     remote_fileio_return_success (ret);
1325   else if (ret == -1)
1326     remote_fileio_return_errno (-1);
1327   else
1328     remote_fileio_return_success (WEXITSTATUS (ret));
1329 }
1330
1331 static struct {
1332   char *name;
1333   void (*func)(char *);
1334 } remote_fio_func_map[] = {
1335   "open", remote_fileio_func_open,
1336   "close", remote_fileio_func_close,
1337   "read", remote_fileio_func_read,
1338   "write", remote_fileio_func_write,
1339   "lseek", remote_fileio_func_lseek,
1340   "rename", remote_fileio_func_rename,
1341   "unlink", remote_fileio_func_unlink,
1342   "stat", remote_fileio_func_stat,
1343   "fstat", remote_fileio_func_fstat,
1344   "gettimeofday", remote_fileio_func_gettimeofday,
1345   "isatty", remote_fileio_func_isatty,
1346   "system", remote_fileio_func_system,
1347   NULL, NULL
1348 };
1349
1350 static int
1351 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1352 {
1353   char *buf = buf_arg;
1354   char *c;
1355   int idx;
1356
1357   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1358
1359   c = strchr (++buf, ',');
1360   if (c)
1361     *c++ = '\0';
1362   else
1363     c = strchr (buf, '\0');
1364   for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1365     if (!strcmp (remote_fio_func_map[idx].name, buf))
1366       break;
1367   if (!remote_fio_func_map[idx].name)   /* ERROR: No such function. */
1368     return RETURN_ERROR;
1369   remote_fio_func_map[idx].func (c);
1370   return 0;
1371 }
1372
1373 /* Close any open descriptors, and reinitialize the file mapping.  */
1374
1375 void
1376 remote_fileio_reset (void)
1377 {
1378   int ix;
1379
1380   for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1381     {
1382       int fd = remote_fio_data.fd_map[ix];
1383
1384       if (fd >= 0)
1385         close (fd);
1386     }
1387   if (remote_fio_data.fd_map)
1388     {
1389       free (remote_fio_data.fd_map);
1390       remote_fio_data.fd_map = NULL;
1391       remote_fio_data.fd_map_size = 0;
1392     }
1393 }
1394
1395 void
1396 remote_fileio_request (char *buf)
1397 {
1398   int ex;
1399
1400   remote_fileio_sig_init ();
1401
1402   remote_fio_ctrl_c_flag = 0;
1403   remote_fio_no_longjmp = 0;
1404
1405   ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1406                          RETURN_MASK_ALL);
1407   switch (ex)
1408     {
1409       case RETURN_ERROR:
1410         remote_fileio_reply (-1, FILEIO_ENOSYS);
1411         break;
1412       case RETURN_QUIT:
1413         remote_fileio_reply (-1, FILEIO_EINTR);
1414         break;
1415       default:
1416         break;
1417     }
1418
1419   remote_fileio_sig_exit ();
1420 }
1421
1422 static void
1423 set_system_call_allowed (char *args, int from_tty)
1424 {
1425   if (args)
1426     {
1427       char *arg_end;
1428       int val = strtoul (args, &arg_end, 10);
1429       if (*args && *arg_end == '\0')
1430         {
1431           remote_fio_system_call_allowed = !!val;
1432           return;
1433         }
1434     }
1435   error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1436 }
1437
1438 static void
1439 show_system_call_allowed (char *args, int from_tty)
1440 {
1441   if (args)
1442     error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1443   printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1444                      remote_fio_system_call_allowed ? "" : "not ");
1445 }
1446
1447 void
1448 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1449                           struct cmd_list_element *remote_show_cmdlist)
1450 {
1451   add_cmd ("system-call-allowed", no_class,
1452            set_system_call_allowed,
1453            _("Set if the host system(3) call is allowed for the target."),
1454            &remote_set_cmdlist);
1455   add_cmd ("system-call-allowed", no_class,
1456            show_system_call_allowed,
1457            _("Show if the host system(3) call is allowed for the target."),
1458            &remote_show_cmdlist);
1459 }