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