2007-08-04 Michael Snyder <msnyder@access-company.com>
[platform/upstream/binutils.git] / gdb / remote-fileio.c
1 /* Remote File-I/O communications
2
3    Copyright (C) 2003, 2005, 2006, 2007 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         xfree (buffer);
854         return;
855       case FIO_FD_CONSOLE_OUT:
856         ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
857                        (char *) buffer, length);
858         gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
859         ret = length;
860         break;
861       default:
862         ret = write (fd, buffer, length);
863         if (ret < 0 && errno == EACCES)
864           errno = EBADF; /* Cygwin returns EACCESS when writing to a R/O file.*/
865         break;
866     }
867
868   if (ret < 0)
869     remote_fileio_return_errno (-1);
870   else
871     remote_fileio_return_success (ret);
872
873   xfree (buffer);
874 }
875
876 static void
877 remote_fileio_func_lseek (char *buf)
878 {
879   long num;
880   LONGEST lnum;
881   int fd, flag;
882   off_t offset, ret;
883
884   /* 1. Parameter: file descriptor */
885   if (remote_fileio_extract_int (&buf, &num))
886     {
887       remote_fileio_ioerror ();
888       return;
889     }
890   fd = remote_fileio_map_fd ((int) num);
891   if (fd == FIO_FD_INVALID)
892     {
893       remote_fileio_badfd ();
894       return;
895     }
896   else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
897     {
898       remote_fileio_reply (-1, FILEIO_ESPIPE);
899       return;
900     }
901
902   /* 2. Parameter: offset */
903   if (remote_fileio_extract_long (&buf, &lnum))
904     {
905       remote_fileio_ioerror ();
906       return;
907     }
908   offset = (off_t) lnum;
909   /* 3. Parameter: flag */
910   if (remote_fileio_extract_int (&buf, &num))
911     {
912       remote_fileio_ioerror ();
913       return;
914     }
915   if (remote_fileio_seek_flag_to_host (num, &flag))
916     {
917       remote_fileio_reply (-1, FILEIO_EINVAL);
918       return;
919     }
920   
921   remote_fio_no_longjmp = 1;
922   ret = lseek (fd, offset, flag);
923
924   if (ret == (off_t) -1)
925     remote_fileio_return_errno (-1);
926   else
927     remote_fileio_return_success (ret);
928 }
929
930 static void
931 remote_fileio_func_rename (char *buf)
932 {
933   CORE_ADDR old_ptr, new_ptr;
934   int old_len, new_len, retlength;
935   char *oldpath, *newpath;
936   int ret, of, nf;
937   struct stat ost, nst;
938
939   /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
940   if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
941     {
942       remote_fileio_ioerror ();
943       return;
944     }
945   
946   /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
947   if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
948     {
949       remote_fileio_ioerror ();
950       return;
951     }
952   
953   /* Request oldpath using 'm' packet */
954   oldpath = alloca (old_len);
955   retlength = remote_read_bytes (old_ptr, (gdb_byte *) oldpath, old_len);
956   if (retlength != old_len)
957     {
958       remote_fileio_ioerror ();
959       return;
960     }
961   
962   /* Request newpath using 'm' packet */
963   newpath = alloca (new_len);
964   retlength = remote_read_bytes (new_ptr, (gdb_byte *) newpath, new_len);
965   if (retlength != new_len)
966     {
967       remote_fileio_ioerror ();
968       return;
969     }
970   
971   /* Only operate on regular files and directories */
972   of = stat (oldpath, &ost);
973   nf = stat (newpath, &nst);
974   if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
975       || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
976     {
977       remote_fileio_reply (-1, FILEIO_EACCES);
978       return;
979     }
980
981   remote_fio_no_longjmp = 1;
982   ret = rename (oldpath, newpath);
983
984   if (ret == -1)
985     {
986       /* Special case: newpath is a non-empty directory.  Some systems
987          return ENOTEMPTY, some return EEXIST.  We coerce that to be
988          always EEXIST. */
989       if (errno == ENOTEMPTY)
990         errno = EEXIST;
991 #ifdef __CYGWIN__
992       /* Workaround some Cygwin problems with correct errnos. */
993       if (errno == EACCES)
994         {
995           if (!of && !nf && S_ISDIR (nst.st_mode))
996             {
997               if (S_ISREG (ost.st_mode))
998                 errno = EISDIR;
999               else
1000                 {
1001                   char oldfullpath[PATH_MAX + 1];
1002                   char newfullpath[PATH_MAX + 1];
1003                   int len;
1004
1005                   cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
1006                   cygwin_conv_to_full_posix_path (newpath, newfullpath);
1007                   len = strlen (oldfullpath);
1008                   if (newfullpath[len] == '/'
1009                       && !strncmp (oldfullpath, newfullpath, len))
1010                     errno = EINVAL;
1011                   else
1012                     errno = EEXIST;
1013                 }
1014             }
1015         }
1016 #endif
1017
1018       remote_fileio_return_errno (-1);
1019     }
1020   else
1021     remote_fileio_return_success (ret);
1022 }
1023
1024 static void
1025 remote_fileio_func_unlink (char *buf)
1026 {
1027   CORE_ADDR ptrval;
1028   int length, retlength;
1029   char *pathname;
1030   int ret;
1031   struct stat st;
1032
1033   /* Parameter: Ptr to pathname / length incl. trailing zero */
1034   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1035     {
1036       remote_fileio_ioerror ();
1037       return;
1038     }
1039   /* Request pathname using 'm' packet */
1040   pathname = alloca (length);
1041   retlength = remote_read_bytes (ptrval, (gdb_byte *) pathname, length);
1042   if (retlength != length)
1043     {
1044       remote_fileio_ioerror ();
1045       return;
1046     }
1047
1048   /* Only operate on regular files (and directories, which allows to return
1049      the correct return code) */
1050   if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1051     {
1052       remote_fileio_reply (-1, FILEIO_ENODEV);
1053       return;
1054     }
1055
1056   remote_fio_no_longjmp = 1;
1057   ret = unlink (pathname);
1058
1059   if (ret == -1)
1060     remote_fileio_return_errno (-1);
1061   else
1062     remote_fileio_return_success (ret);
1063 }
1064
1065 static void
1066 remote_fileio_func_stat (char *buf)
1067 {
1068   CORE_ADDR statptr, nameptr;
1069   int ret, namelength, retlength;
1070   char *pathname;
1071   LONGEST lnum;
1072   struct stat st;
1073   struct fio_stat fst;
1074
1075   /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1076   if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1077     {
1078       remote_fileio_ioerror ();
1079       return;
1080     }
1081
1082   /* 2. Parameter: Ptr to struct stat */
1083   if (remote_fileio_extract_long (&buf, &lnum))
1084     {
1085       remote_fileio_ioerror ();
1086       return;
1087     }
1088   statptr = (CORE_ADDR) lnum;
1089   
1090   /* Request pathname using 'm' packet */
1091   pathname = alloca (namelength);
1092   retlength = remote_read_bytes (nameptr, (gdb_byte *) pathname, namelength);
1093   if (retlength != namelength)
1094     {
1095       remote_fileio_ioerror ();
1096       return;
1097     }
1098
1099   remote_fio_no_longjmp = 1;
1100   ret = stat (pathname, &st);
1101
1102   if (ret == -1)
1103     {
1104       remote_fileio_return_errno (-1);
1105       return;
1106     }
1107   /* Only operate on regular files and directories */
1108   if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1109     {
1110       remote_fileio_reply (-1, FILEIO_EACCES);
1111       return;
1112     }
1113   if (statptr)
1114     {
1115       remote_fileio_to_fio_stat (&st, &fst);
1116       remote_fileio_to_fio_uint (0, fst.fst_dev);
1117       
1118       retlength = remote_fileio_write_bytes (statptr,
1119                                              (gdb_byte *) &fst, sizeof fst);
1120       if (retlength != sizeof fst)
1121         {
1122           remote_fileio_return_errno (-1);
1123           return;
1124         }
1125     }
1126   remote_fileio_return_success (ret);
1127 }
1128
1129 static void
1130 remote_fileio_func_fstat (char *buf)
1131 {
1132   CORE_ADDR ptrval;
1133   int fd, ret, retlength;
1134   long target_fd;
1135   LONGEST lnum;
1136   struct stat st;
1137   struct fio_stat fst;
1138   struct timeval tv;
1139
1140   /* 1. Parameter: file descriptor */
1141   if (remote_fileio_extract_int (&buf, &target_fd))
1142     {
1143       remote_fileio_ioerror ();
1144       return;
1145     }
1146   fd = remote_fileio_map_fd ((int) target_fd);
1147   if (fd == FIO_FD_INVALID)
1148     {
1149       remote_fileio_badfd ();
1150       return;
1151     }
1152   /* 2. Parameter: Ptr to struct stat */
1153   if (remote_fileio_extract_long (&buf, &lnum))
1154     {
1155       remote_fileio_ioerror ();
1156       return;
1157     }
1158   ptrval = (CORE_ADDR) lnum;
1159
1160   remote_fio_no_longjmp = 1;
1161   if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1162     {
1163       remote_fileio_to_fio_uint (1, fst.fst_dev);
1164       st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1165       st.st_nlink = 1;
1166 #ifdef HAVE_GETUID
1167       st.st_uid = getuid ();
1168 #else
1169       st.st_uid = 0;
1170 #endif
1171 #ifdef HAVE_GETGID
1172       st.st_gid = getgid ();
1173 #else
1174       st.st_gid = 0;
1175 #endif
1176       st.st_rdev = 0;
1177       st.st_size = 0;
1178 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1179       st.st_blksize = 512;
1180 #endif
1181 #if HAVE_STRUCT_STAT_ST_BLOCKS
1182       st.st_blocks = 0;
1183 #endif
1184       if (!gettimeofday (&tv, NULL))
1185         st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1186       else
1187         st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1188       ret = 0;
1189     }
1190   else
1191     ret = fstat (fd, &st);
1192
1193   if (ret == -1)
1194     {
1195       remote_fileio_return_errno (-1);
1196       return;
1197     }
1198   if (ptrval)
1199     {
1200       remote_fileio_to_fio_stat (&st, &fst);
1201
1202       retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &fst, sizeof fst);
1203       if (retlength != sizeof fst)
1204         {
1205           remote_fileio_return_errno (-1);
1206           return;
1207         }
1208     }
1209   remote_fileio_return_success (ret);
1210 }
1211
1212 static void
1213 remote_fileio_func_gettimeofday (char *buf)
1214 {
1215   LONGEST lnum;
1216   CORE_ADDR ptrval;
1217   int ret, retlength;
1218   struct timeval tv;
1219   struct fio_timeval ftv;
1220
1221   /* 1. Parameter: struct timeval pointer */
1222   if (remote_fileio_extract_long (&buf, &lnum))
1223     {
1224       remote_fileio_ioerror ();
1225       return;
1226     }
1227   ptrval = (CORE_ADDR) lnum;
1228   /* 2. Parameter: some pointer value... */
1229   if (remote_fileio_extract_long (&buf, &lnum))
1230     {
1231       remote_fileio_ioerror ();
1232       return;
1233     }
1234   /* ...which has to be NULL */
1235   if (lnum)
1236     {
1237       remote_fileio_reply (-1, FILEIO_EINVAL);
1238       return;
1239     }
1240
1241   remote_fio_no_longjmp = 1;
1242   ret = gettimeofday (&tv, NULL);
1243
1244   if (ret == -1)
1245     {
1246       remote_fileio_return_errno (-1);
1247       return;
1248     }
1249
1250   if (ptrval)
1251     {
1252       remote_fileio_to_fio_timeval (&tv, &ftv);
1253
1254       retlength = remote_fileio_write_bytes (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1255       if (retlength != sizeof ftv)
1256         {
1257           remote_fileio_return_errno (-1);
1258           return;
1259         }
1260     }
1261   remote_fileio_return_success (ret);
1262 }
1263
1264 static void
1265 remote_fileio_func_isatty (char *buf)
1266 {
1267   long target_fd;
1268   int fd;
1269
1270   /* Parameter: file descriptor */
1271   if (remote_fileio_extract_int (&buf, &target_fd))
1272     {
1273       remote_fileio_ioerror ();
1274       return;
1275     }
1276   remote_fio_no_longjmp = 1;
1277   fd = remote_fileio_map_fd ((int) target_fd);
1278   remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1279                                 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1280 }
1281
1282 static void
1283 remote_fileio_func_system (char *buf)
1284 {
1285   CORE_ADDR ptrval;
1286   int ret, length, retlength;
1287   char *cmdline = NULL;
1288
1289   /* Parameter: Ptr to commandline / length incl. trailing zero */
1290   if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1291     {
1292       remote_fileio_ioerror ();
1293       return;
1294     }
1295
1296   if (length)
1297     {
1298       /* Request commandline using 'm' packet */
1299       cmdline = alloca (length);
1300       retlength = remote_read_bytes (ptrval, (gdb_byte *) cmdline, length);
1301       if (retlength != length)
1302         {
1303           remote_fileio_ioerror ();
1304           return;
1305         }
1306     }
1307   
1308   /* Check if system(3) has been explicitely allowed using the
1309      `set remote system-call-allowed 1' command.  If length is 0,
1310      indicating a NULL parameter to the system call, return zero to
1311      indicate a shell is not available.  Otherwise fail with EPERM.  */
1312   if (!remote_fio_system_call_allowed)
1313     {
1314       if (!length)
1315         remote_fileio_return_success (0);
1316       else
1317         remote_fileio_reply (-1, FILEIO_EPERM);
1318       return;
1319     }
1320
1321   remote_fio_no_longjmp = 1;
1322   ret = system (cmdline);
1323
1324   if (!length)
1325     remote_fileio_return_success (ret);
1326   else if (ret == -1)
1327     remote_fileio_return_errno (-1);
1328   else
1329     remote_fileio_return_success (WEXITSTATUS (ret));
1330 }
1331
1332 static struct {
1333   char *name;
1334   void (*func)(char *);
1335 } remote_fio_func_map[] = {
1336   { "open", remote_fileio_func_open },
1337   { "close", remote_fileio_func_close },
1338   { "read", remote_fileio_func_read },
1339   { "write", remote_fileio_func_write },
1340   { "lseek", remote_fileio_func_lseek },
1341   { "rename", remote_fileio_func_rename },
1342   { "unlink", remote_fileio_func_unlink },
1343   { "stat", remote_fileio_func_stat },
1344   { "fstat", remote_fileio_func_fstat },
1345   { "gettimeofday", remote_fileio_func_gettimeofday },
1346   { "isatty", remote_fileio_func_isatty },
1347   { "system", remote_fileio_func_system },
1348   { NULL, NULL }
1349 };
1350
1351 static int
1352 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1353 {
1354   char *buf = buf_arg;
1355   char *c;
1356   int idx;
1357
1358   remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1359
1360   c = strchr (++buf, ',');
1361   if (c)
1362     *c++ = '\0';
1363   else
1364     c = strchr (buf, '\0');
1365   for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1366     if (!strcmp (remote_fio_func_map[idx].name, buf))
1367       break;
1368   if (!remote_fio_func_map[idx].name)   /* ERROR: No such function. */
1369     return RETURN_ERROR;
1370   remote_fio_func_map[idx].func (c);
1371   return 0;
1372 }
1373
1374 /* Close any open descriptors, and reinitialize the file mapping.  */
1375
1376 void
1377 remote_fileio_reset (void)
1378 {
1379   int ix;
1380
1381   for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1382     {
1383       int fd = remote_fio_data.fd_map[ix];
1384
1385       if (fd >= 0)
1386         close (fd);
1387     }
1388   if (remote_fio_data.fd_map)
1389     {
1390       free (remote_fio_data.fd_map);
1391       remote_fio_data.fd_map = NULL;
1392       remote_fio_data.fd_map_size = 0;
1393     }
1394 }
1395
1396 void
1397 remote_fileio_request (char *buf)
1398 {
1399   int ex;
1400
1401   remote_fileio_sig_init ();
1402
1403   remote_fio_ctrl_c_flag = 0;
1404   remote_fio_no_longjmp = 0;
1405
1406   ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1407                          RETURN_MASK_ALL);
1408   switch (ex)
1409     {
1410       case RETURN_ERROR:
1411         remote_fileio_reply (-1, FILEIO_ENOSYS);
1412         break;
1413       case RETURN_QUIT:
1414         remote_fileio_reply (-1, FILEIO_EINTR);
1415         break;
1416       default:
1417         break;
1418     }
1419
1420   remote_fileio_sig_exit ();
1421 }
1422
1423 static void
1424 set_system_call_allowed (char *args, int from_tty)
1425 {
1426   if (args)
1427     {
1428       char *arg_end;
1429       int val = strtoul (args, &arg_end, 10);
1430       if (*args && *arg_end == '\0')
1431         {
1432           remote_fio_system_call_allowed = !!val;
1433           return;
1434         }
1435     }
1436   error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1437 }
1438
1439 static void
1440 show_system_call_allowed (char *args, int from_tty)
1441 {
1442   if (args)
1443     error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args);
1444   printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1445                      remote_fio_system_call_allowed ? "" : "not ");
1446 }
1447
1448 void
1449 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1450                           struct cmd_list_element *remote_show_cmdlist)
1451 {
1452   add_cmd ("system-call-allowed", no_class,
1453            set_system_call_allowed,
1454            _("Set if the host system(3) call is allowed for the target."),
1455            &remote_set_cmdlist);
1456   add_cmd ("system-call-allowed", no_class,
1457            show_system_call_allowed,
1458            _("Show if the host system(3) call is allowed for the target."),
1459            &remote_show_cmdlist);
1460 }