* utils.c (internal_vproblem): Guard call to fork with
[platform/upstream/binutils.git] / gdb / remote-fileio.c
1 /* Remote File-I/O communications
2
3    Copyright 2003, 2005 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., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, 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 (mode & S_IFREG)
196     tmode |= FILEIO_S_IFREG;
197   if (mode & S_IFDIR)
198     tmode |= FILEIO_S_IFDIR;
199   if (mode & S_IFCHR)
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   /* `st_dev' is set in the calling function */
421   remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
422   remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
423   remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
424   remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
425   remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
426   remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
427   remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
428   remote_fileio_to_fio_ulong ((LONGEST) st->st_blksize, fst->fst_blksize);
429 #if HAVE_STRUCT_STAT_ST_BLOCKS
430   remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
431 #else
432   /* FIXME: This is correct for DJGPP, but other systems that don't
433      have st_blocks, if any, might prefer 512 instead of st_blksize.
434      (eliz, 30-12-2003)  */
435   remote_fileio_to_fio_ulong (((LONGEST) st->st_size + st->st_blksize - 1)
436                               / (LONGEST) st->st_blksize,
437                               fst->fst_blocks);
438 #endif
439   remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
440   remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
441   remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
442 }
443
444 static void
445 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
446 {
447   remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
448   remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
449 }
450
451 static int remote_fio_ctrl_c_flag = 0;
452 static int remote_fio_no_longjmp = 0;
453
454 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
455 static struct sigaction remote_fio_sa;
456 static struct sigaction remote_fio_osa;
457 #else
458 static void (*remote_fio_ofunc)(int);
459 #endif
460
461 static void
462 remote_fileio_sig_init (void)
463 {
464 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
465   remote_fio_sa.sa_handler = SIG_IGN;
466   sigemptyset (&remote_fio_sa.sa_mask);
467   remote_fio_sa.sa_flags = 0;
468   sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
469 #else
470   remote_fio_ofunc = signal (SIGINT, SIG_IGN);
471 #endif
472 }
473
474 static void
475 remote_fileio_sig_set (void (*sigint_func)(int))
476 {
477 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
478   remote_fio_sa.sa_handler = sigint_func;
479   sigemptyset (&remote_fio_sa.sa_mask);
480   remote_fio_sa.sa_flags = 0;
481   sigaction (SIGINT, &remote_fio_sa, NULL);
482 #else
483   signal (SIGINT, sigint_func);
484 #endif
485 }
486
487 static void
488 remote_fileio_sig_exit (void)
489 {
490 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
491   sigaction (SIGINT, &remote_fio_osa, NULL);
492 #else
493   signal (SIGINT, remote_fio_ofunc);
494 #endif
495 }
496
497 static void
498 remote_fileio_ctrl_c_signal_handler (int signo)
499 {
500   remote_fileio_sig_set (SIG_IGN);
501   remote_fio_ctrl_c_flag = 1;
502   if (!remote_fio_no_longjmp)
503     deprecated_throw_reason (RETURN_QUIT);
504   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
505 }
506
507 static void
508 remote_fileio_reply (int retcode, int error)
509 {
510   char buf[32];
511
512   remote_fileio_sig_set (SIG_IGN);
513   strcpy (buf, "F");
514   if (retcode < 0)
515     {
516       strcat (buf, "-");
517       retcode = -retcode;
518     }
519   sprintf (buf + strlen (buf), "%x", retcode);
520   if (error || remote_fio_ctrl_c_flag)
521     {
522       if (error && remote_fio_ctrl_c_flag)
523         error = FILEIO_EINTR;
524       if (error < 0)
525         {
526           strcat (buf, "-");
527           error = -error;
528         }
529       sprintf (buf + strlen (buf), ",%x", error);
530       if (remote_fio_ctrl_c_flag)
531         strcat (buf, ",C");
532     }
533   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
534   putpkt (buf);
535 }
536
537 static void
538 remote_fileio_ioerror (void)
539 {
540   remote_fileio_reply (-1, FILEIO_EIO);
541 }
542
543 static void
544 remote_fileio_badfd (void)
545 {
546   remote_fileio_reply (-1, FILEIO_EBADF);
547 }
548
549 static void
550 remote_fileio_return_errno (int retcode)
551 {
552   remote_fileio_reply (retcode,
553                        retcode < 0 ? remote_fileio_errno_to_target (errno) : 0);
554 }
555
556 static void
557 remote_fileio_return_success (int retcode)
558 {
559   remote_fileio_reply (retcode, 0);
560 }
561
562 /* Wrapper function for remote_write_bytes() which has the disadvantage to
563    write only one packet, regardless of the requested number of bytes to
564    transfer.  This wrapper calls remote_write_bytes() as often as needed. */
565 static int
566 remote_fileio_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
567 {
568   int ret = 0, written;
569
570   while (len > 0 && (written = remote_write_bytes (memaddr, myaddr, len)) > 0)
571     {
572       len -= written;
573       memaddr += written;
574       myaddr += written;
575       ret += written;
576     }
577   return ret;
578 }
579
580 static void
581 remote_fileio_func_open (char *buf)
582 {
583   CORE_ADDR ptrval;
584   int length, retlength;
585   long num;
586   int flags, fd;
587   mode_t mode;
588   char *pathname;
589   struct stat st;
590
591   /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
592   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
593     {
594       remote_fileio_ioerror ();
595       return;
596     }
597   /* 2. Parameter: open flags */
598   if (remote_fileio_extract_int (&buf, &num))
599     {
600       remote_fileio_ioerror ();
601       return;
602     }
603   flags = remote_fileio_oflags_to_host (num);
604   /* 3. Parameter: open mode */
605   if (remote_fileio_extract_int (&buf, &num))
606     {
607       remote_fileio_ioerror ();
608       return;
609     }
610   mode = remote_fileio_mode_to_host (num, 1);
611
612   /* Request pathname using 'm' packet */
613   pathname = alloca (length);
614   retlength = remote_read_bytes (ptrval, pathname, length);
615   if (retlength != length)
616     {
617       remote_fileio_ioerror ();
618       return;
619     }
620
621   /* Check if pathname exists and is not a regular file or directory.  If so,
622      return an appropriate error code.  Same for trying to open directories
623      for writing. */
624   if (!stat (pathname, &st))
625     {
626       if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
627         {
628           remote_fileio_reply (-1, FILEIO_ENODEV);
629           return;
630         }
631       if (S_ISDIR (st.st_mode)
632           && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
633         {
634           remote_fileio_reply (-1, FILEIO_EISDIR);
635           return;
636         }
637     }
638
639   remote_fio_no_longjmp = 1;
640   fd = open (pathname, flags, mode);
641   if (fd < 0)
642     {
643       remote_fileio_return_errno (-1);
644       return;
645     }
646
647   fd = remote_fileio_fd_to_targetfd (fd);
648   remote_fileio_return_success (fd);
649 }
650
651 static void
652 remote_fileio_func_close (char *buf)
653 {
654   long num;
655   int fd;
656
657   /* Parameter: file descriptor */
658   if (remote_fileio_extract_int (&buf, &num))
659     {
660       remote_fileio_ioerror ();
661       return;
662     }
663   fd = remote_fileio_map_fd ((int) num);
664   if (fd == FIO_FD_INVALID)
665     {
666       remote_fileio_badfd ();
667       return;
668     }
669
670   remote_fio_no_longjmp = 1;
671   if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
672     remote_fileio_return_errno (-1);
673   remote_fileio_close_target_fd ((int) num);
674   remote_fileio_return_success (0);
675 }
676
677 static void
678 remote_fileio_func_read (char *buf)
679 {
680   long target_fd, num;
681   LONGEST lnum;
682   CORE_ADDR ptrval;
683   int fd, ret, retlength;
684   char *buffer;
685   size_t length;
686   off_t old_offset, new_offset;
687
688   /* 1. Parameter: file descriptor */
689   if (remote_fileio_extract_int (&buf, &target_fd))
690     {
691       remote_fileio_ioerror ();
692       return;
693     }
694   fd = remote_fileio_map_fd ((int) target_fd);
695   if (fd == FIO_FD_INVALID)
696     {
697       remote_fileio_badfd ();
698       return;
699     }
700   /* 2. Parameter: buffer pointer */
701   if (remote_fileio_extract_long (&buf, &lnum))
702     {
703       remote_fileio_ioerror ();
704       return;
705     }
706   ptrval = (CORE_ADDR) lnum;
707   /* 3. Parameter: buffer length */
708   if (remote_fileio_extract_int (&buf, &num))
709     {
710       remote_fileio_ioerror ();
711       return;
712     }
713   length = (size_t) num;
714
715   switch (fd)
716     {
717       case FIO_FD_CONSOLE_OUT:
718         remote_fileio_badfd ();
719         return;
720       case FIO_FD_CONSOLE_IN:
721         {
722           static char *remaining_buf = NULL;
723           static int remaining_length = 0;
724
725           buffer = (char *) xmalloc (32768);
726           if (remaining_buf)
727             {
728               remote_fio_no_longjmp = 1;
729               if (remaining_length > length)
730                 {
731                   memcpy (buffer, remaining_buf, length);
732                   memmove (remaining_buf, remaining_buf + length,
733                            remaining_length - length);
734                   remaining_length -= length;
735                   ret = length;
736                 }
737               else
738                 {
739                   memcpy (buffer, remaining_buf, remaining_length);
740                   xfree (remaining_buf);
741                   remaining_buf = NULL;
742                   ret = remaining_length;
743                 }
744             }
745           else
746             {
747               ret = ui_file_read (gdb_stdtargin, buffer, 32767);
748               remote_fio_no_longjmp = 1;
749               if (ret > 0 && (size_t)ret > length)
750                 {
751                   remaining_buf = (char *) xmalloc (ret - length);
752                   remaining_length = ret - length;
753                   memcpy (remaining_buf, buffer + length, remaining_length);
754                   ret = length;
755                 }
756             }
757         }
758         break;
759       default:
760         buffer = (char *) xmalloc (length);
761         /* POSIX defines EINTR behaviour of read in a weird way.  It's allowed
762            for read() to return -1 even if "some" bytes have been read.  It
763            has been corrected in SUSv2 but that doesn't help us much...
764            Therefore a complete solution must check how many bytes have been
765            read on EINTR to return a more reliable value to the target */
766         old_offset = lseek (fd, 0, SEEK_CUR);
767         remote_fio_no_longjmp = 1;
768         ret = read (fd, buffer, length);
769         if (ret < 0 && errno == EINTR)
770           {
771             new_offset = lseek (fd, 0, SEEK_CUR);
772             /* If some data has been read, return the number of bytes read.
773                The Ctrl-C flag is set in remote_fileio_reply() anyway */
774             if (old_offset != new_offset)
775               ret = new_offset - old_offset;
776           }
777         break;
778     }
779
780   if (ret > 0)
781     {
782       retlength = remote_fileio_write_bytes (ptrval, buffer, ret);
783       if (retlength != ret)
784         ret = -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
785     }
786
787   if (ret < 0)
788     remote_fileio_return_errno (-1);
789   else
790     remote_fileio_return_success (ret);
791
792   xfree (buffer);
793 }
794
795 static void
796 remote_fileio_func_write (char *buf)
797 {
798   long target_fd, num;
799   LONGEST lnum;
800   CORE_ADDR ptrval;
801   int fd, ret, retlength;
802   char *buffer;
803   size_t length;
804
805   /* 1. Parameter: file descriptor */
806   if (remote_fileio_extract_int (&buf, &target_fd))
807     {
808       remote_fileio_ioerror ();
809       return;
810     }
811   fd = remote_fileio_map_fd ((int) target_fd);
812   if (fd == FIO_FD_INVALID)
813     {
814       remote_fileio_badfd ();
815       return;
816     }
817   /* 2. Parameter: buffer pointer */
818   if (remote_fileio_extract_long (&buf, &lnum))
819     {
820       remote_fileio_ioerror ();
821       return;
822     }
823   ptrval = (CORE_ADDR) lnum;
824   /* 3. Parameter: buffer length */
825   if (remote_fileio_extract_int (&buf, &num))
826     {
827       remote_fileio_ioerror ();
828       return;
829     }
830   length = (size_t) num;
831     
832   buffer = (char *) xmalloc (length);
833   retlength = remote_read_bytes (ptrval, buffer, length);
834   if (retlength != length)
835     {
836       xfree (buffer);
837       remote_fileio_ioerror ();
838       return;
839     }
840
841   remote_fio_no_longjmp = 1;
842   switch (fd)
843     {
844       case FIO_FD_CONSOLE_IN:
845         remote_fileio_badfd ();
846         return;
847       case FIO_FD_CONSOLE_OUT:
848         ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr, buffer,
849                        length);
850         gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
851         ret = length;
852         break;
853       default:
854         ret = write (fd, buffer, length);
855         if (ret < 0 && errno == EACCES)
856           errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
857         break;
858     }
859
860   if (ret < 0)
861     remote_fileio_return_errno (-1);
862   else
863     remote_fileio_return_success (ret);
864
865   xfree (buffer);
866 }
867
868 static void
869 remote_fileio_func_lseek (char *buf)
870 {
871   long num;
872   LONGEST lnum;
873   int fd, flag;
874   off_t offset, ret;
875
876   /* 1. Parameter: file descriptor */
877   if (remote_fileio_extract_int (&buf, &num))
878     {
879       remote_fileio_ioerror ();
880       return;
881     }
882   fd = remote_fileio_map_fd ((int) num);
883   if (fd == FIO_FD_INVALID)
884     {
885       remote_fileio_badfd ();
886       return;
887     }
888   else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
889     {
890       remote_fileio_reply (-1, FILEIO_ESPIPE);
891       return;
892     }
893
894   /* 2. Parameter: offset */
895   if (remote_fileio_extract_long (&buf, &lnum))
896     {
897       remote_fileio_ioerror ();
898       return;
899     }
900   offset = (off_t) lnum;
901   /* 3. Parameter: flag */
902   if (remote_fileio_extract_int (&buf, &num))
903     {
904       remote_fileio_ioerror ();
905       return;
906     }
907   if (remote_fileio_seek_flag_to_host (num, &flag))
908     {
909       remote_fileio_reply (-1, FILEIO_EINVAL);
910       return;
911     }
912   
913   remote_fio_no_longjmp = 1;
914   ret = lseek (fd, offset, flag);
915
916   if (ret == (off_t) -1)
917     remote_fileio_return_errno (-1);
918   else
919     remote_fileio_return_success (ret);
920 }
921
922 static void
923 remote_fileio_func_rename (char *buf)
924 {
925   CORE_ADDR ptrval;
926   int length, retlength;
927   char *oldpath, *newpath;
928   int ret, of, nf;
929   struct stat ost, nst;
930
931   /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
932   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
933     {
934       remote_fileio_ioerror ();
935       return;
936     }
937   /* Request oldpath using 'm' packet */
938   oldpath = alloca (length);
939   retlength = remote_read_bytes (ptrval, oldpath, length);
940   if (retlength != length)
941     {
942       remote_fileio_ioerror ();
943       return;
944     }
945   /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
946   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
947     {
948       remote_fileio_ioerror ();
949       return;
950     }
951   /* Request newpath using 'm' packet */
952   newpath = alloca (length);
953   retlength = remote_read_bytes (ptrval, newpath, length);
954   if (retlength != length)
955     {
956       remote_fileio_ioerror ();
957       return;
958     }
959   
960   /* Only operate on regular files and directories */
961   of = stat (oldpath, &ost);
962   nf = stat (newpath, &nst);
963   if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
964       || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
965     {
966       remote_fileio_reply (-1, FILEIO_EACCES);
967       return;
968     }
969
970   remote_fio_no_longjmp = 1;
971   ret = rename (oldpath, newpath);
972
973   if (ret == -1)
974     {
975       /* Special case: newpath is a non-empty directory.  Some systems
976          return ENOTEMPTY, some return EEXIST.  We coerce that to be
977          always EEXIST. */
978       if (errno == ENOTEMPTY)
979         errno = EEXIST;
980 #ifdef __CYGWIN__
981       /* Workaround some Cygwin problems with correct errnos. */
982       if (errno == EACCES)
983         {
984           if (!of && !nf && S_ISDIR (nst.st_mode))
985             {
986               if (S_ISREG (ost.st_mode))
987                 errno = EISDIR;
988               else
989                 {
990                   char oldfullpath[PATH_MAX + 1];
991                   char newfullpath[PATH_MAX + 1];
992                   int len;
993
994                   cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
995                   cygwin_conv_to_full_posix_path (newpath, newfullpath);
996                   len = strlen (oldfullpath);
997                   if (newfullpath[len] == '/'
998                       && !strncmp (oldfullpath, newfullpath, len))
999                     errno = EINVAL;
1000                   else
1001                     errno = EEXIST;
1002                 }
1003             }
1004         }
1005 #endif
1006
1007       remote_fileio_return_errno (-1);
1008     }
1009   else
1010     remote_fileio_return_success (ret);
1011 }
1012
1013 static void
1014 remote_fileio_func_unlink (char *buf)
1015 {
1016   CORE_ADDR ptrval;
1017   int length, retlength;
1018   char *pathname;
1019   int ret;
1020   struct stat st;
1021
1022   /* Parameter: Ptr to pathname / length incl. trailing zero */
1023   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1024     {
1025       remote_fileio_ioerror ();
1026       return;
1027     }
1028   /* Request pathname using 'm' packet */
1029   pathname = alloca (length);
1030   retlength = remote_read_bytes (ptrval, pathname, length);
1031   if (retlength != length)
1032     {
1033       remote_fileio_ioerror ();
1034       return;
1035     }
1036
1037   /* Only operate on regular files (and directories, which allows to return
1038      the correct return code) */
1039   if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1040     {
1041       remote_fileio_reply (-1, FILEIO_ENODEV);
1042       return;
1043     }
1044
1045   remote_fio_no_longjmp = 1;
1046   ret = unlink (pathname);
1047
1048   if (ret == -1)
1049     remote_fileio_return_errno (-1);
1050   else
1051     remote_fileio_return_success (ret);
1052 }
1053
1054 static void
1055 remote_fileio_func_stat (char *buf)
1056 {
1057   CORE_ADDR ptrval;
1058   int ret, length, retlength;
1059   char *pathname;
1060   LONGEST lnum;
1061   struct stat st;
1062   struct fio_stat fst;
1063
1064   /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1065   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1066     {
1067       remote_fileio_ioerror ();
1068       return;
1069     }
1070   /* Request pathname using 'm' packet */
1071   pathname = alloca (length);
1072   retlength = remote_read_bytes (ptrval, pathname, length);
1073   if (retlength != length)
1074     {
1075       remote_fileio_ioerror ();
1076       return;
1077     }
1078
1079   /* 2. Parameter: Ptr to struct stat */
1080   if (remote_fileio_extract_long (&buf, &lnum))
1081     {
1082       remote_fileio_ioerror ();
1083       return;
1084     }
1085   ptrval = (CORE_ADDR) lnum;
1086
1087   remote_fio_no_longjmp = 1;
1088   ret = stat (pathname, &st);
1089
1090   if (ret == -1)
1091     {
1092       remote_fileio_return_errno (-1);
1093       return;
1094     }
1095   /* Only operate on regular files and directories */
1096   if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1097     {
1098       remote_fileio_reply (-1, FILEIO_EACCES);
1099       return;
1100     }
1101   if (ptrval)
1102     {
1103       remote_fileio_to_fio_stat (&st, &fst);
1104       remote_fileio_to_fio_uint (0, fst.fst_dev);
1105       
1106       retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1107       if (retlength != sizeof fst)
1108         {
1109           remote_fileio_return_errno (-1);
1110           return;
1111         }
1112     }
1113   remote_fileio_return_success (ret);
1114 }
1115
1116 static void
1117 remote_fileio_func_fstat (char *buf)
1118 {
1119   CORE_ADDR ptrval;
1120   int fd, ret, retlength;
1121   long target_fd;
1122   LONGEST lnum;
1123   struct stat st;
1124   struct fio_stat fst;
1125   struct timeval tv;
1126
1127   /* 1. Parameter: file descriptor */
1128   if (remote_fileio_extract_int (&buf, &target_fd))
1129     {
1130       remote_fileio_ioerror ();
1131       return;
1132     }
1133   fd = remote_fileio_map_fd ((int) target_fd);
1134   if (fd == FIO_FD_INVALID)
1135     {
1136       remote_fileio_badfd ();
1137       return;
1138     }
1139   /* 2. Parameter: Ptr to struct stat */
1140   if (remote_fileio_extract_long (&buf, &lnum))
1141     {
1142       remote_fileio_ioerror ();
1143       return;
1144     }
1145   ptrval = (CORE_ADDR) lnum;
1146
1147   remote_fio_no_longjmp = 1;
1148   if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1149     {
1150       remote_fileio_to_fio_uint (1, fst.fst_dev);
1151       st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1152       st.st_nlink = 1;
1153       st.st_uid = getuid ();
1154       st.st_gid = getgid ();
1155       st.st_rdev = 0;
1156       st.st_size = 0;
1157       st.st_blksize = 512;
1158 #if HAVE_STRUCT_STAT_ST_BLOCKS
1159       st.st_blocks = 0;
1160 #endif
1161       if (!gettimeofday (&tv, NULL))
1162         st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1163       else
1164         st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1165       ret = 0;
1166     }
1167   else
1168     ret = fstat (fd, &st);
1169
1170   if (ret == -1)
1171     {
1172       remote_fileio_return_errno (-1);
1173       return;
1174     }
1175   if (ptrval)
1176     {
1177       remote_fileio_to_fio_stat (&st, &fst);
1178
1179       retlength = remote_fileio_write_bytes (ptrval, (char *) &fst, sizeof fst);
1180       if (retlength != sizeof fst)
1181         {
1182           remote_fileio_return_errno (-1);
1183           return;
1184         }
1185     }
1186   remote_fileio_return_success (ret);
1187 }
1188
1189 static void
1190 remote_fileio_func_gettimeofday (char *buf)
1191 {
1192   LONGEST lnum;
1193   CORE_ADDR ptrval;
1194   int ret, retlength;
1195   struct timeval tv;
1196   struct fio_timeval ftv;
1197
1198   /* 1. Parameter: struct timeval pointer */
1199   if (remote_fileio_extract_long (&buf, &lnum))
1200     {
1201       remote_fileio_ioerror ();
1202       return;
1203     }
1204   ptrval = (CORE_ADDR) lnum;
1205   /* 2. Parameter: some pointer value... */
1206   if (remote_fileio_extract_long (&buf, &lnum))
1207     {
1208       remote_fileio_ioerror ();
1209       return;
1210     }
1211   /* ...which has to be NULL */
1212   if (lnum)
1213     {
1214       remote_fileio_reply (-1, FILEIO_EINVAL);
1215       return;
1216     }
1217
1218   remote_fio_no_longjmp = 1;
1219   ret = gettimeofday (&tv, NULL);
1220
1221   if (ret == -1)
1222     {
1223       remote_fileio_return_errno (-1);
1224       return;
1225     }
1226
1227   if (ptrval)
1228     {
1229       remote_fileio_to_fio_timeval (&tv, &ftv);
1230
1231       retlength = remote_fileio_write_bytes (ptrval, (char *) &ftv, sizeof ftv);
1232       if (retlength != sizeof ftv)
1233         {
1234           remote_fileio_return_errno (-1);
1235           return;
1236         }
1237     }
1238   remote_fileio_return_success (ret);
1239 }
1240
1241 static void
1242 remote_fileio_func_isatty (char *buf)
1243 {
1244   long target_fd;
1245   int fd;
1246
1247   /* Parameter: file descriptor */
1248   if (remote_fileio_extract_int (&buf, &target_fd))
1249     {
1250       remote_fileio_ioerror ();
1251       return;
1252     }
1253   remote_fio_no_longjmp = 1;
1254   fd = remote_fileio_map_fd ((int) target_fd);
1255   remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1256                                 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1257 }
1258
1259 static void
1260 remote_fileio_func_system (char *buf)
1261 {
1262   CORE_ADDR ptrval;
1263   int ret, length, retlength;
1264   char *cmdline;
1265
1266   /* Check if system(3) has been explicitely allowed using the
1267      `set remote system-call-allowed 1' command.  If not, return
1268      EPERM */
1269   if (!remote_fio_system_call_allowed)
1270     {
1271       remote_fileio_reply (-1, FILEIO_EPERM);
1272       return;
1273     }
1274
1275   /* Parameter: Ptr to commandline / length incl. trailing zero */
1276   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1277     {
1278       remote_fileio_ioerror ();
1279       return;
1280     }
1281   /* Request commandline using 'm' packet */
1282   cmdline = alloca (length);
1283   retlength = remote_read_bytes (ptrval, cmdline, length);
1284   if (retlength != length)
1285     {
1286       remote_fileio_ioerror ();
1287       return;
1288     }
1289
1290   remote_fio_no_longjmp = 1;
1291   ret = system (cmdline);
1292
1293   if (ret == -1)
1294     remote_fileio_return_errno (-1);
1295   else
1296     remote_fileio_return_success (WEXITSTATUS (ret));
1297 }
1298
1299 static struct {
1300   char *name;
1301   void (*func)(char *);
1302 } remote_fio_func_map[] = {
1303   "open", remote_fileio_func_open,
1304   "close", remote_fileio_func_close,
1305   "read", remote_fileio_func_read,
1306   "write", remote_fileio_func_write,
1307   "lseek", remote_fileio_func_lseek,
1308   "rename", remote_fileio_func_rename,
1309   "unlink", remote_fileio_func_unlink,
1310   "stat", remote_fileio_func_stat,
1311   "fstat", remote_fileio_func_fstat,
1312   "gettimeofday", remote_fileio_func_gettimeofday,
1313   "isatty", remote_fileio_func_isatty,
1314   "system", remote_fileio_func_system,
1315   NULL, NULL
1316 };
1317
1318 static int
1319 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1320 {
1321   char *buf = buf_arg;
1322   char *c;
1323   int idx;
1324
1325   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1326
1327   c = strchr (++buf, ',');
1328   if (c)
1329     *c++ = '\0';
1330   else
1331     c = strchr (buf, '\0');
1332   for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1333     if (!strcmp (remote_fio_func_map[idx].name, buf))
1334       break;
1335   if (!remote_fio_func_map[idx].name)   /* ERROR: No such function. */
1336     return RETURN_ERROR;
1337   remote_fio_func_map[idx].func (c);
1338   return 0;
1339 }
1340
1341 void
1342 remote_fileio_request (char *buf)
1343 {
1344   int ex;
1345
1346   remote_fileio_sig_init ();
1347
1348   remote_fio_ctrl_c_flag = 0;
1349   remote_fio_no_longjmp = 0;
1350
1351   ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1352                          RETURN_MASK_ALL);
1353   switch (ex)
1354     {
1355       case RETURN_ERROR:
1356         remote_fileio_reply (-1, FILEIO_ENOSYS);
1357         break;
1358       case RETURN_QUIT:
1359         remote_fileio_reply (-1, FILEIO_EINTR);
1360         break;
1361       default:
1362         break;
1363     }
1364
1365   remote_fileio_sig_exit ();
1366 }
1367
1368 static void
1369 set_system_call_allowed (char *args, int from_tty)
1370 {
1371   if (args)
1372     {
1373       char *arg_end;
1374       int val = strtoul (args, &arg_end, 10);
1375       if (*args && *arg_end == '\0')
1376         {
1377           remote_fio_system_call_allowed = !!val;
1378           return;
1379         }
1380     }
1381   error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1382 }
1383
1384 static void
1385 show_system_call_allowed (char *args, int from_tty)
1386 {
1387   if (args)
1388     error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1389   printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1390                      remote_fio_system_call_allowed ? "" : "not ");
1391 }
1392
1393 void
1394 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1395                           struct cmd_list_element *remote_show_cmdlist)
1396 {
1397   add_cmd ("system-call-allowed", no_class,
1398            set_system_call_allowed,
1399            _("Set if the host system(3) call is allowed for the target."),
1400            &remote_set_cmdlist);
1401   add_cmd ("system-call-allowed", no_class,
1402            show_system_call_allowed,
1403            _("Show if the host system(3) call is allowed for the target."),
1404            &remote_show_cmdlist);
1405 }