15796c8dcSSimon Schubert /* Remote File-I/O communications
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert /* See the GDB User Guide for details of the GDB remote protocol. */
215796c8dcSSimon Schubert
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert #include "gdbcmd.h"
255796c8dcSSimon Schubert #include "remote.h"
265796c8dcSSimon Schubert #include "gdb/fileio.h"
275796c8dcSSimon Schubert #include "gdb_wait.h"
285796c8dcSSimon Schubert #include "gdb_stat.h"
295796c8dcSSimon Schubert #include "exceptions.h"
305796c8dcSSimon Schubert #include "remote-fileio.h"
315796c8dcSSimon Schubert #include "event-loop.h"
32c50c785cSJohn Marino #include "target.h"
33c50c785cSJohn Marino #include "filenames.h"
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert #include <fcntl.h>
365796c8dcSSimon Schubert #include <sys/time.h>
375796c8dcSSimon Schubert #ifdef __CYGWIN__
38a45ae5f8SJohn Marino #include <sys/cygwin.h> /* For cygwin_conv_path. */
395796c8dcSSimon Schubert #endif
405796c8dcSSimon Schubert #include <signal.h>
415796c8dcSSimon Schubert
425796c8dcSSimon Schubert static struct {
435796c8dcSSimon Schubert int *fd_map;
445796c8dcSSimon Schubert int fd_map_size;
455796c8dcSSimon Schubert } remote_fio_data;
465796c8dcSSimon Schubert
475796c8dcSSimon Schubert #define FIO_FD_INVALID -1
485796c8dcSSimon Schubert #define FIO_FD_CONSOLE_IN -2
495796c8dcSSimon Schubert #define FIO_FD_CONSOLE_OUT -3
505796c8dcSSimon Schubert
515796c8dcSSimon Schubert static int remote_fio_system_call_allowed = 0;
525796c8dcSSimon Schubert
535796c8dcSSimon Schubert static struct async_signal_handler *sigint_fileio_token;
545796c8dcSSimon Schubert
555796c8dcSSimon Schubert static int
remote_fileio_init_fd_map(void)565796c8dcSSimon Schubert remote_fileio_init_fd_map (void)
575796c8dcSSimon Schubert {
585796c8dcSSimon Schubert int i;
595796c8dcSSimon Schubert
605796c8dcSSimon Schubert if (!remote_fio_data.fd_map)
615796c8dcSSimon Schubert {
625796c8dcSSimon Schubert remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
635796c8dcSSimon Schubert remote_fio_data.fd_map_size = 10;
645796c8dcSSimon Schubert remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
655796c8dcSSimon Schubert remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
665796c8dcSSimon Schubert remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
675796c8dcSSimon Schubert for (i = 3; i < 10; ++i)
685796c8dcSSimon Schubert remote_fio_data.fd_map[i] = FIO_FD_INVALID;
695796c8dcSSimon Schubert }
705796c8dcSSimon Schubert return 3;
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert
735796c8dcSSimon Schubert static int
remote_fileio_resize_fd_map(void)745796c8dcSSimon Schubert remote_fileio_resize_fd_map (void)
755796c8dcSSimon Schubert {
765796c8dcSSimon Schubert int i = remote_fio_data.fd_map_size;
775796c8dcSSimon Schubert
785796c8dcSSimon Schubert if (!remote_fio_data.fd_map)
795796c8dcSSimon Schubert return remote_fileio_init_fd_map ();
805796c8dcSSimon Schubert remote_fio_data.fd_map_size += 10;
815796c8dcSSimon Schubert remote_fio_data.fd_map =
825796c8dcSSimon Schubert (int *) xrealloc (remote_fio_data.fd_map,
835796c8dcSSimon Schubert remote_fio_data.fd_map_size * sizeof (int));
845796c8dcSSimon Schubert for (; i < remote_fio_data.fd_map_size; i++)
855796c8dcSSimon Schubert remote_fio_data.fd_map[i] = FIO_FD_INVALID;
865796c8dcSSimon Schubert return remote_fio_data.fd_map_size - 10;
875796c8dcSSimon Schubert }
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert static int
remote_fileio_next_free_fd(void)905796c8dcSSimon Schubert remote_fileio_next_free_fd (void)
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert int i;
935796c8dcSSimon Schubert
945796c8dcSSimon Schubert for (i = 0; i < remote_fio_data.fd_map_size; ++i)
955796c8dcSSimon Schubert if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
965796c8dcSSimon Schubert return i;
975796c8dcSSimon Schubert return remote_fileio_resize_fd_map ();
985796c8dcSSimon Schubert }
995796c8dcSSimon Schubert
1005796c8dcSSimon Schubert static int
remote_fileio_fd_to_targetfd(int fd)1015796c8dcSSimon Schubert remote_fileio_fd_to_targetfd (int fd)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert int target_fd = remote_fileio_next_free_fd ();
104cf7f2e2dSJohn Marino
1055796c8dcSSimon Schubert remote_fio_data.fd_map[target_fd] = fd;
1065796c8dcSSimon Schubert return target_fd;
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert
1095796c8dcSSimon Schubert static int
remote_fileio_map_fd(int target_fd)1105796c8dcSSimon Schubert remote_fileio_map_fd (int target_fd)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert remote_fileio_init_fd_map ();
1135796c8dcSSimon Schubert if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
1145796c8dcSSimon Schubert return FIO_FD_INVALID;
1155796c8dcSSimon Schubert return remote_fio_data.fd_map[target_fd];
1165796c8dcSSimon Schubert }
1175796c8dcSSimon Schubert
1185796c8dcSSimon Schubert static void
remote_fileio_close_target_fd(int target_fd)1195796c8dcSSimon Schubert remote_fileio_close_target_fd (int target_fd)
1205796c8dcSSimon Schubert {
1215796c8dcSSimon Schubert remote_fileio_init_fd_map ();
1225796c8dcSSimon Schubert if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
1235796c8dcSSimon Schubert remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert
1265796c8dcSSimon Schubert static int
remote_fileio_oflags_to_host(long flags)1275796c8dcSSimon Schubert remote_fileio_oflags_to_host (long flags)
1285796c8dcSSimon Schubert {
1295796c8dcSSimon Schubert int hflags = 0;
1305796c8dcSSimon Schubert
1315796c8dcSSimon Schubert if (flags & FILEIO_O_CREAT)
1325796c8dcSSimon Schubert hflags |= O_CREAT;
1335796c8dcSSimon Schubert if (flags & FILEIO_O_EXCL)
1345796c8dcSSimon Schubert hflags |= O_EXCL;
1355796c8dcSSimon Schubert if (flags & FILEIO_O_TRUNC)
1365796c8dcSSimon Schubert hflags |= O_TRUNC;
1375796c8dcSSimon Schubert if (flags & FILEIO_O_APPEND)
1385796c8dcSSimon Schubert hflags |= O_APPEND;
1395796c8dcSSimon Schubert if (flags & FILEIO_O_RDONLY)
1405796c8dcSSimon Schubert hflags |= O_RDONLY;
1415796c8dcSSimon Schubert if (flags & FILEIO_O_WRONLY)
1425796c8dcSSimon Schubert hflags |= O_WRONLY;
1435796c8dcSSimon Schubert if (flags & FILEIO_O_RDWR)
1445796c8dcSSimon Schubert hflags |= O_RDWR;
1455796c8dcSSimon Schubert /* On systems supporting binary and text mode, always open files in
1465796c8dcSSimon Schubert binary mode. */
1475796c8dcSSimon Schubert #ifdef O_BINARY
1485796c8dcSSimon Schubert hflags |= O_BINARY;
1495796c8dcSSimon Schubert #endif
1505796c8dcSSimon Schubert return hflags;
1515796c8dcSSimon Schubert }
1525796c8dcSSimon Schubert
1535796c8dcSSimon Schubert static mode_t
remote_fileio_mode_to_host(long mode,int open_call)1545796c8dcSSimon Schubert remote_fileio_mode_to_host (long mode, int open_call)
1555796c8dcSSimon Schubert {
1565796c8dcSSimon Schubert mode_t hmode = 0;
1575796c8dcSSimon Schubert
1585796c8dcSSimon Schubert if (!open_call)
1595796c8dcSSimon Schubert {
1605796c8dcSSimon Schubert if (mode & FILEIO_S_IFREG)
1615796c8dcSSimon Schubert hmode |= S_IFREG;
1625796c8dcSSimon Schubert if (mode & FILEIO_S_IFDIR)
1635796c8dcSSimon Schubert hmode |= S_IFDIR;
1645796c8dcSSimon Schubert if (mode & FILEIO_S_IFCHR)
1655796c8dcSSimon Schubert hmode |= S_IFCHR;
1665796c8dcSSimon Schubert }
1675796c8dcSSimon Schubert if (mode & FILEIO_S_IRUSR)
1685796c8dcSSimon Schubert hmode |= S_IRUSR;
1695796c8dcSSimon Schubert if (mode & FILEIO_S_IWUSR)
1705796c8dcSSimon Schubert hmode |= S_IWUSR;
1715796c8dcSSimon Schubert if (mode & FILEIO_S_IXUSR)
1725796c8dcSSimon Schubert hmode |= S_IXUSR;
1735796c8dcSSimon Schubert #ifdef S_IRGRP
1745796c8dcSSimon Schubert if (mode & FILEIO_S_IRGRP)
1755796c8dcSSimon Schubert hmode |= S_IRGRP;
1765796c8dcSSimon Schubert #endif
1775796c8dcSSimon Schubert #ifdef S_IWGRP
1785796c8dcSSimon Schubert if (mode & FILEIO_S_IWGRP)
1795796c8dcSSimon Schubert hmode |= S_IWGRP;
1805796c8dcSSimon Schubert #endif
1815796c8dcSSimon Schubert #ifdef S_IXGRP
1825796c8dcSSimon Schubert if (mode & FILEIO_S_IXGRP)
1835796c8dcSSimon Schubert hmode |= S_IXGRP;
1845796c8dcSSimon Schubert #endif
1855796c8dcSSimon Schubert if (mode & FILEIO_S_IROTH)
1865796c8dcSSimon Schubert hmode |= S_IROTH;
1875796c8dcSSimon Schubert #ifdef S_IWOTH
1885796c8dcSSimon Schubert if (mode & FILEIO_S_IWOTH)
1895796c8dcSSimon Schubert hmode |= S_IWOTH;
1905796c8dcSSimon Schubert #endif
1915796c8dcSSimon Schubert #ifdef S_IXOTH
1925796c8dcSSimon Schubert if (mode & FILEIO_S_IXOTH)
1935796c8dcSSimon Schubert hmode |= S_IXOTH;
1945796c8dcSSimon Schubert #endif
1955796c8dcSSimon Schubert return hmode;
1965796c8dcSSimon Schubert }
1975796c8dcSSimon Schubert
1985796c8dcSSimon Schubert static LONGEST
remote_fileio_mode_to_target(mode_t mode)1995796c8dcSSimon Schubert remote_fileio_mode_to_target (mode_t mode)
2005796c8dcSSimon Schubert {
2015796c8dcSSimon Schubert mode_t tmode = 0;
2025796c8dcSSimon Schubert
2035796c8dcSSimon Schubert if (S_ISREG(mode))
2045796c8dcSSimon Schubert tmode |= FILEIO_S_IFREG;
2055796c8dcSSimon Schubert if (S_ISDIR(mode))
2065796c8dcSSimon Schubert tmode |= FILEIO_S_IFDIR;
2075796c8dcSSimon Schubert if (S_ISCHR(mode))
2085796c8dcSSimon Schubert tmode |= FILEIO_S_IFCHR;
2095796c8dcSSimon Schubert if (mode & S_IRUSR)
2105796c8dcSSimon Schubert tmode |= FILEIO_S_IRUSR;
2115796c8dcSSimon Schubert if (mode & S_IWUSR)
2125796c8dcSSimon Schubert tmode |= FILEIO_S_IWUSR;
2135796c8dcSSimon Schubert if (mode & S_IXUSR)
2145796c8dcSSimon Schubert tmode |= FILEIO_S_IXUSR;
2155796c8dcSSimon Schubert #ifdef S_IRGRP
2165796c8dcSSimon Schubert if (mode & S_IRGRP)
2175796c8dcSSimon Schubert tmode |= FILEIO_S_IRGRP;
2185796c8dcSSimon Schubert #endif
2195796c8dcSSimon Schubert #ifdef S_IWRGRP
2205796c8dcSSimon Schubert if (mode & S_IWGRP)
2215796c8dcSSimon Schubert tmode |= FILEIO_S_IWGRP;
2225796c8dcSSimon Schubert #endif
2235796c8dcSSimon Schubert #ifdef S_IXGRP
2245796c8dcSSimon Schubert if (mode & S_IXGRP)
2255796c8dcSSimon Schubert tmode |= FILEIO_S_IXGRP;
2265796c8dcSSimon Schubert #endif
2275796c8dcSSimon Schubert if (mode & S_IROTH)
2285796c8dcSSimon Schubert tmode |= FILEIO_S_IROTH;
2295796c8dcSSimon Schubert #ifdef S_IWOTH
2305796c8dcSSimon Schubert if (mode & S_IWOTH)
2315796c8dcSSimon Schubert tmode |= FILEIO_S_IWOTH;
2325796c8dcSSimon Schubert #endif
2335796c8dcSSimon Schubert #ifdef S_IXOTH
2345796c8dcSSimon Schubert if (mode & S_IXOTH)
2355796c8dcSSimon Schubert tmode |= FILEIO_S_IXOTH;
2365796c8dcSSimon Schubert #endif
2375796c8dcSSimon Schubert return tmode;
2385796c8dcSSimon Schubert }
2395796c8dcSSimon Schubert
2405796c8dcSSimon Schubert static int
remote_fileio_errno_to_target(int error)2415796c8dcSSimon Schubert remote_fileio_errno_to_target (int error)
2425796c8dcSSimon Schubert {
2435796c8dcSSimon Schubert switch (error)
2445796c8dcSSimon Schubert {
2455796c8dcSSimon Schubert case EPERM:
2465796c8dcSSimon Schubert return FILEIO_EPERM;
2475796c8dcSSimon Schubert case ENOENT:
2485796c8dcSSimon Schubert return FILEIO_ENOENT;
2495796c8dcSSimon Schubert case EINTR:
2505796c8dcSSimon Schubert return FILEIO_EINTR;
2515796c8dcSSimon Schubert case EIO:
2525796c8dcSSimon Schubert return FILEIO_EIO;
2535796c8dcSSimon Schubert case EBADF:
2545796c8dcSSimon Schubert return FILEIO_EBADF;
2555796c8dcSSimon Schubert case EACCES:
2565796c8dcSSimon Schubert return FILEIO_EACCES;
2575796c8dcSSimon Schubert case EFAULT:
2585796c8dcSSimon Schubert return FILEIO_EFAULT;
2595796c8dcSSimon Schubert case EBUSY:
2605796c8dcSSimon Schubert return FILEIO_EBUSY;
2615796c8dcSSimon Schubert case EEXIST:
2625796c8dcSSimon Schubert return FILEIO_EEXIST;
2635796c8dcSSimon Schubert case ENODEV:
2645796c8dcSSimon Schubert return FILEIO_ENODEV;
2655796c8dcSSimon Schubert case ENOTDIR:
2665796c8dcSSimon Schubert return FILEIO_ENOTDIR;
2675796c8dcSSimon Schubert case EISDIR:
2685796c8dcSSimon Schubert return FILEIO_EISDIR;
2695796c8dcSSimon Schubert case EINVAL:
2705796c8dcSSimon Schubert return FILEIO_EINVAL;
2715796c8dcSSimon Schubert case ENFILE:
2725796c8dcSSimon Schubert return FILEIO_ENFILE;
2735796c8dcSSimon Schubert case EMFILE:
2745796c8dcSSimon Schubert return FILEIO_EMFILE;
2755796c8dcSSimon Schubert case EFBIG:
2765796c8dcSSimon Schubert return FILEIO_EFBIG;
2775796c8dcSSimon Schubert case ENOSPC:
2785796c8dcSSimon Schubert return FILEIO_ENOSPC;
2795796c8dcSSimon Schubert case ESPIPE:
2805796c8dcSSimon Schubert return FILEIO_ESPIPE;
2815796c8dcSSimon Schubert case EROFS:
2825796c8dcSSimon Schubert return FILEIO_EROFS;
2835796c8dcSSimon Schubert case ENOSYS:
2845796c8dcSSimon Schubert return FILEIO_ENOSYS;
2855796c8dcSSimon Schubert case ENAMETOOLONG:
2865796c8dcSSimon Schubert return FILEIO_ENAMETOOLONG;
2875796c8dcSSimon Schubert }
2885796c8dcSSimon Schubert return FILEIO_EUNKNOWN;
2895796c8dcSSimon Schubert }
2905796c8dcSSimon Schubert
2915796c8dcSSimon Schubert static int
remote_fileio_seek_flag_to_host(long num,int * flag)2925796c8dcSSimon Schubert remote_fileio_seek_flag_to_host (long num, int *flag)
2935796c8dcSSimon Schubert {
2945796c8dcSSimon Schubert if (!flag)
2955796c8dcSSimon Schubert return 0;
2965796c8dcSSimon Schubert switch (num)
2975796c8dcSSimon Schubert {
2985796c8dcSSimon Schubert case FILEIO_SEEK_SET:
2995796c8dcSSimon Schubert *flag = SEEK_SET;
3005796c8dcSSimon Schubert break;
3015796c8dcSSimon Schubert case FILEIO_SEEK_CUR:
3025796c8dcSSimon Schubert *flag = SEEK_CUR;
3035796c8dcSSimon Schubert break;
3045796c8dcSSimon Schubert case FILEIO_SEEK_END:
3055796c8dcSSimon Schubert *flag = SEEK_END;
3065796c8dcSSimon Schubert break;
3075796c8dcSSimon Schubert default:
3085796c8dcSSimon Schubert return -1;
3095796c8dcSSimon Schubert }
3105796c8dcSSimon Schubert return 0;
3115796c8dcSSimon Schubert }
3125796c8dcSSimon Schubert
3135796c8dcSSimon Schubert static int
remote_fileio_extract_long(char ** buf,LONGEST * retlong)3145796c8dcSSimon Schubert remote_fileio_extract_long (char **buf, LONGEST *retlong)
3155796c8dcSSimon Schubert {
3165796c8dcSSimon Schubert char *c;
3175796c8dcSSimon Schubert int sign = 1;
3185796c8dcSSimon Schubert
3195796c8dcSSimon Schubert if (!buf || !*buf || !**buf || !retlong)
3205796c8dcSSimon Schubert return -1;
3215796c8dcSSimon Schubert c = strchr (*buf, ',');
3225796c8dcSSimon Schubert if (c)
3235796c8dcSSimon Schubert *c++ = '\0';
3245796c8dcSSimon Schubert else
3255796c8dcSSimon Schubert c = strchr (*buf, '\0');
3265796c8dcSSimon Schubert while (strchr ("+-", **buf))
3275796c8dcSSimon Schubert {
3285796c8dcSSimon Schubert if (**buf == '-')
3295796c8dcSSimon Schubert sign = -sign;
3305796c8dcSSimon Schubert ++*buf;
3315796c8dcSSimon Schubert }
3325796c8dcSSimon Schubert for (*retlong = 0; **buf; ++*buf)
3335796c8dcSSimon Schubert {
3345796c8dcSSimon Schubert *retlong <<= 4;
3355796c8dcSSimon Schubert if (**buf >= '0' && **buf <= '9')
3365796c8dcSSimon Schubert *retlong += **buf - '0';
3375796c8dcSSimon Schubert else if (**buf >= 'a' && **buf <= 'f')
3385796c8dcSSimon Schubert *retlong += **buf - 'a' + 10;
3395796c8dcSSimon Schubert else if (**buf >= 'A' && **buf <= 'F')
3405796c8dcSSimon Schubert *retlong += **buf - 'A' + 10;
3415796c8dcSSimon Schubert else
3425796c8dcSSimon Schubert return -1;
3435796c8dcSSimon Schubert }
3445796c8dcSSimon Schubert *retlong *= sign;
3455796c8dcSSimon Schubert *buf = c;
3465796c8dcSSimon Schubert return 0;
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert
3495796c8dcSSimon Schubert static int
remote_fileio_extract_int(char ** buf,long * retint)3505796c8dcSSimon Schubert remote_fileio_extract_int (char **buf, long *retint)
3515796c8dcSSimon Schubert {
3525796c8dcSSimon Schubert int ret;
3535796c8dcSSimon Schubert LONGEST retlong;
3545796c8dcSSimon Schubert
3555796c8dcSSimon Schubert if (!retint)
3565796c8dcSSimon Schubert return -1;
3575796c8dcSSimon Schubert ret = remote_fileio_extract_long (buf, &retlong);
3585796c8dcSSimon Schubert if (!ret)
3595796c8dcSSimon Schubert *retint = (long) retlong;
3605796c8dcSSimon Schubert return ret;
3615796c8dcSSimon Schubert }
3625796c8dcSSimon Schubert
3635796c8dcSSimon Schubert static int
remote_fileio_extract_ptr_w_len(char ** buf,CORE_ADDR * ptrval,int * length)3645796c8dcSSimon Schubert remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
3655796c8dcSSimon Schubert {
3665796c8dcSSimon Schubert char *c;
3675796c8dcSSimon Schubert LONGEST retlong;
3685796c8dcSSimon Schubert
3695796c8dcSSimon Schubert if (!buf || !*buf || !**buf || !ptrval || !length)
3705796c8dcSSimon Schubert return -1;
3715796c8dcSSimon Schubert c = strchr (*buf, '/');
3725796c8dcSSimon Schubert if (!c)
3735796c8dcSSimon Schubert return -1;
3745796c8dcSSimon Schubert *c++ = '\0';
3755796c8dcSSimon Schubert if (remote_fileio_extract_long (buf, &retlong))
3765796c8dcSSimon Schubert return -1;
3775796c8dcSSimon Schubert *ptrval = (CORE_ADDR) retlong;
3785796c8dcSSimon Schubert *buf = c;
3795796c8dcSSimon Schubert if (remote_fileio_extract_long (buf, &retlong))
3805796c8dcSSimon Schubert return -1;
3815796c8dcSSimon Schubert *length = (int) retlong;
3825796c8dcSSimon Schubert return 0;
3835796c8dcSSimon Schubert }
3845796c8dcSSimon Schubert
385c50c785cSJohn Marino /* Convert to big endian. */
3865796c8dcSSimon Schubert static void
remote_fileio_to_be(LONGEST num,char * buf,int bytes)3875796c8dcSSimon Schubert remote_fileio_to_be (LONGEST num, char *buf, int bytes)
3885796c8dcSSimon Schubert {
3895796c8dcSSimon Schubert int i;
3905796c8dcSSimon Schubert
3915796c8dcSSimon Schubert for (i = 0; i < bytes; ++i)
3925796c8dcSSimon Schubert buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
3935796c8dcSSimon Schubert }
3945796c8dcSSimon Schubert
3955796c8dcSSimon Schubert static void
remote_fileio_to_fio_uint(long num,fio_uint_t fnum)3965796c8dcSSimon Schubert remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
3975796c8dcSSimon Schubert {
3985796c8dcSSimon Schubert remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
3995796c8dcSSimon Schubert }
4005796c8dcSSimon Schubert
4015796c8dcSSimon Schubert static void
remote_fileio_to_fio_mode(mode_t num,fio_mode_t fnum)4025796c8dcSSimon Schubert remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
4035796c8dcSSimon Schubert {
4045796c8dcSSimon Schubert remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
4055796c8dcSSimon Schubert }
4065796c8dcSSimon Schubert
4075796c8dcSSimon Schubert static void
remote_fileio_to_fio_time(time_t num,fio_time_t fnum)4085796c8dcSSimon Schubert remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
4095796c8dcSSimon Schubert {
4105796c8dcSSimon Schubert remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
4115796c8dcSSimon Schubert }
4125796c8dcSSimon Schubert
4135796c8dcSSimon Schubert static void
remote_fileio_to_fio_long(LONGEST num,fio_long_t fnum)4145796c8dcSSimon Schubert remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
4155796c8dcSSimon Schubert {
4165796c8dcSSimon Schubert remote_fileio_to_be (num, (char *) fnum, 8);
4175796c8dcSSimon Schubert }
4185796c8dcSSimon Schubert
4195796c8dcSSimon Schubert static void
remote_fileio_to_fio_ulong(LONGEST num,fio_ulong_t fnum)4205796c8dcSSimon Schubert remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
4215796c8dcSSimon Schubert {
4225796c8dcSSimon Schubert remote_fileio_to_be (num, (char *) fnum, 8);
4235796c8dcSSimon Schubert }
4245796c8dcSSimon Schubert
4255796c8dcSSimon Schubert static void
remote_fileio_to_fio_stat(struct stat * st,struct fio_stat * fst)4265796c8dcSSimon Schubert remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
4275796c8dcSSimon Schubert {
4285796c8dcSSimon Schubert LONGEST blksize;
4295796c8dcSSimon Schubert
430c50c785cSJohn Marino /* `st_dev' is set in the calling function. */
4315796c8dcSSimon Schubert remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
4325796c8dcSSimon Schubert remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
4335796c8dcSSimon Schubert remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
4345796c8dcSSimon Schubert remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
4355796c8dcSSimon Schubert remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
4365796c8dcSSimon Schubert remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
4375796c8dcSSimon Schubert remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
4385796c8dcSSimon Schubert #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
4395796c8dcSSimon Schubert blksize = st->st_blksize;
4405796c8dcSSimon Schubert #else
4415796c8dcSSimon Schubert blksize = 512;
4425796c8dcSSimon Schubert #endif
4435796c8dcSSimon Schubert remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
4445796c8dcSSimon Schubert #if HAVE_STRUCT_STAT_ST_BLOCKS
4455796c8dcSSimon Schubert remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
4465796c8dcSSimon Schubert #else
4475796c8dcSSimon Schubert /* FIXME: This is correct for DJGPP, but other systems that don't
4485796c8dcSSimon Schubert have st_blocks, if any, might prefer 512 instead of st_blksize.
4495796c8dcSSimon Schubert (eliz, 30-12-2003) */
4505796c8dcSSimon Schubert remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
4515796c8dcSSimon Schubert / blksize,
4525796c8dcSSimon Schubert fst->fst_blocks);
4535796c8dcSSimon Schubert #endif
4545796c8dcSSimon Schubert remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
4555796c8dcSSimon Schubert remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
4565796c8dcSSimon Schubert remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
4575796c8dcSSimon Schubert }
4585796c8dcSSimon Schubert
4595796c8dcSSimon Schubert static void
remote_fileio_to_fio_timeval(struct timeval * tv,struct fio_timeval * ftv)4605796c8dcSSimon Schubert remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
4615796c8dcSSimon Schubert {
4625796c8dcSSimon Schubert remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
4635796c8dcSSimon Schubert remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
4645796c8dcSSimon Schubert }
4655796c8dcSSimon Schubert
4665796c8dcSSimon Schubert static int remote_fio_ctrl_c_flag = 0;
4675796c8dcSSimon Schubert static int remote_fio_no_longjmp = 0;
4685796c8dcSSimon Schubert
4695796c8dcSSimon Schubert #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
4705796c8dcSSimon Schubert static struct sigaction remote_fio_sa;
4715796c8dcSSimon Schubert static struct sigaction remote_fio_osa;
4725796c8dcSSimon Schubert #else
4735796c8dcSSimon Schubert static void (*remote_fio_ofunc)(int);
4745796c8dcSSimon Schubert #endif
4755796c8dcSSimon Schubert
4765796c8dcSSimon Schubert static void
remote_fileio_sig_init(void)4775796c8dcSSimon Schubert remote_fileio_sig_init (void)
4785796c8dcSSimon Schubert {
4795796c8dcSSimon Schubert #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
4805796c8dcSSimon Schubert remote_fio_sa.sa_handler = SIG_IGN;
4815796c8dcSSimon Schubert sigemptyset (&remote_fio_sa.sa_mask);
4825796c8dcSSimon Schubert remote_fio_sa.sa_flags = 0;
4835796c8dcSSimon Schubert sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
4845796c8dcSSimon Schubert #else
4855796c8dcSSimon Schubert remote_fio_ofunc = signal (SIGINT, SIG_IGN);
4865796c8dcSSimon Schubert #endif
4875796c8dcSSimon Schubert }
4885796c8dcSSimon Schubert
4895796c8dcSSimon Schubert static void
remote_fileio_sig_set(void (* sigint_func)(int))4905796c8dcSSimon Schubert remote_fileio_sig_set (void (*sigint_func)(int))
4915796c8dcSSimon Schubert {
4925796c8dcSSimon Schubert #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
4935796c8dcSSimon Schubert remote_fio_sa.sa_handler = sigint_func;
4945796c8dcSSimon Schubert sigemptyset (&remote_fio_sa.sa_mask);
4955796c8dcSSimon Schubert remote_fio_sa.sa_flags = 0;
4965796c8dcSSimon Schubert sigaction (SIGINT, &remote_fio_sa, NULL);
4975796c8dcSSimon Schubert #else
4985796c8dcSSimon Schubert signal (SIGINT, sigint_func);
4995796c8dcSSimon Schubert #endif
5005796c8dcSSimon Schubert }
5015796c8dcSSimon Schubert
5025796c8dcSSimon Schubert static void
remote_fileio_sig_exit(void)5035796c8dcSSimon Schubert remote_fileio_sig_exit (void)
5045796c8dcSSimon Schubert {
5055796c8dcSSimon Schubert #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
5065796c8dcSSimon Schubert sigaction (SIGINT, &remote_fio_osa, NULL);
5075796c8dcSSimon Schubert #else
5085796c8dcSSimon Schubert signal (SIGINT, remote_fio_ofunc);
5095796c8dcSSimon Schubert #endif
5105796c8dcSSimon Schubert }
5115796c8dcSSimon Schubert
5125796c8dcSSimon Schubert static void
async_remote_fileio_interrupt(gdb_client_data arg)5135796c8dcSSimon Schubert async_remote_fileio_interrupt (gdb_client_data arg)
5145796c8dcSSimon Schubert {
5155796c8dcSSimon Schubert deprecated_throw_reason (RETURN_QUIT);
5165796c8dcSSimon Schubert }
5175796c8dcSSimon Schubert
5185796c8dcSSimon Schubert static void
remote_fileio_ctrl_c_signal_handler(int signo)5195796c8dcSSimon Schubert remote_fileio_ctrl_c_signal_handler (int signo)
5205796c8dcSSimon Schubert {
5215796c8dcSSimon Schubert remote_fileio_sig_set (SIG_IGN);
5225796c8dcSSimon Schubert remote_fio_ctrl_c_flag = 1;
5235796c8dcSSimon Schubert if (!remote_fio_no_longjmp)
5245796c8dcSSimon Schubert gdb_call_async_signal_handler (sigint_fileio_token, 1);
5255796c8dcSSimon Schubert remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
5265796c8dcSSimon Schubert }
5275796c8dcSSimon Schubert
5285796c8dcSSimon Schubert static void
remote_fileio_reply(int retcode,int error)5295796c8dcSSimon Schubert remote_fileio_reply (int retcode, int error)
5305796c8dcSSimon Schubert {
5315796c8dcSSimon Schubert char buf[32];
5325796c8dcSSimon Schubert
5335796c8dcSSimon Schubert remote_fileio_sig_set (SIG_IGN);
5345796c8dcSSimon Schubert strcpy (buf, "F");
5355796c8dcSSimon Schubert if (retcode < 0)
5365796c8dcSSimon Schubert {
5375796c8dcSSimon Schubert strcat (buf, "-");
5385796c8dcSSimon Schubert retcode = -retcode;
5395796c8dcSSimon Schubert }
5405796c8dcSSimon Schubert sprintf (buf + strlen (buf), "%x", retcode);
5415796c8dcSSimon Schubert if (error || remote_fio_ctrl_c_flag)
5425796c8dcSSimon Schubert {
5435796c8dcSSimon Schubert if (error && remote_fio_ctrl_c_flag)
5445796c8dcSSimon Schubert error = FILEIO_EINTR;
5455796c8dcSSimon Schubert if (error < 0)
5465796c8dcSSimon Schubert {
5475796c8dcSSimon Schubert strcat (buf, "-");
5485796c8dcSSimon Schubert error = -error;
5495796c8dcSSimon Schubert }
5505796c8dcSSimon Schubert sprintf (buf + strlen (buf), ",%x", error);
5515796c8dcSSimon Schubert if (remote_fio_ctrl_c_flag)
5525796c8dcSSimon Schubert strcat (buf, ",C");
5535796c8dcSSimon Schubert }
5545796c8dcSSimon Schubert remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
5555796c8dcSSimon Schubert putpkt (buf);
5565796c8dcSSimon Schubert }
5575796c8dcSSimon Schubert
5585796c8dcSSimon Schubert static void
remote_fileio_ioerror(void)5595796c8dcSSimon Schubert remote_fileio_ioerror (void)
5605796c8dcSSimon Schubert {
5615796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EIO);
5625796c8dcSSimon Schubert }
5635796c8dcSSimon Schubert
5645796c8dcSSimon Schubert static void
remote_fileio_badfd(void)5655796c8dcSSimon Schubert remote_fileio_badfd (void)
5665796c8dcSSimon Schubert {
5675796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EBADF);
5685796c8dcSSimon Schubert }
5695796c8dcSSimon Schubert
5705796c8dcSSimon Schubert static void
remote_fileio_return_errno(int retcode)5715796c8dcSSimon Schubert remote_fileio_return_errno (int retcode)
5725796c8dcSSimon Schubert {
573c50c785cSJohn Marino remote_fileio_reply (retcode, retcode < 0
574c50c785cSJohn Marino ? remote_fileio_errno_to_target (errno) : 0);
5755796c8dcSSimon Schubert }
5765796c8dcSSimon Schubert
5775796c8dcSSimon Schubert static void
remote_fileio_return_success(int retcode)5785796c8dcSSimon Schubert remote_fileio_return_success (int retcode)
5795796c8dcSSimon Schubert {
5805796c8dcSSimon Schubert remote_fileio_reply (retcode, 0);
5815796c8dcSSimon Schubert }
5825796c8dcSSimon Schubert
5835796c8dcSSimon Schubert static void
remote_fileio_func_open(char * buf)5845796c8dcSSimon Schubert remote_fileio_func_open (char *buf)
5855796c8dcSSimon Schubert {
5865796c8dcSSimon Schubert CORE_ADDR ptrval;
587c50c785cSJohn Marino int length;
5885796c8dcSSimon Schubert long num;
5895796c8dcSSimon Schubert int flags, fd;
5905796c8dcSSimon Schubert mode_t mode;
5915796c8dcSSimon Schubert char *pathname;
5925796c8dcSSimon Schubert struct stat st;
5935796c8dcSSimon Schubert
594c50c785cSJohn Marino /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
5955796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
5965796c8dcSSimon Schubert {
5975796c8dcSSimon Schubert remote_fileio_ioerror ();
5985796c8dcSSimon Schubert return;
5995796c8dcSSimon Schubert }
6005796c8dcSSimon Schubert /* 2. Parameter: open flags */
6015796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
6025796c8dcSSimon Schubert {
6035796c8dcSSimon Schubert remote_fileio_ioerror ();
6045796c8dcSSimon Schubert return;
6055796c8dcSSimon Schubert }
6065796c8dcSSimon Schubert flags = remote_fileio_oflags_to_host (num);
6075796c8dcSSimon Schubert /* 3. Parameter: open mode */
6085796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
6095796c8dcSSimon Schubert {
6105796c8dcSSimon Schubert remote_fileio_ioerror ();
6115796c8dcSSimon Schubert return;
6125796c8dcSSimon Schubert }
6135796c8dcSSimon Schubert mode = remote_fileio_mode_to_host (num, 1);
6145796c8dcSSimon Schubert
615c50c785cSJohn Marino /* Request pathname. */
6165796c8dcSSimon Schubert pathname = alloca (length);
617c50c785cSJohn Marino if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
6185796c8dcSSimon Schubert {
6195796c8dcSSimon Schubert remote_fileio_ioerror ();
6205796c8dcSSimon Schubert return;
6215796c8dcSSimon Schubert }
6225796c8dcSSimon Schubert
6235796c8dcSSimon Schubert /* Check if pathname exists and is not a regular file or directory. If so,
6245796c8dcSSimon Schubert return an appropriate error code. Same for trying to open directories
6255796c8dcSSimon Schubert for writing. */
6265796c8dcSSimon Schubert if (!stat (pathname, &st))
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
6295796c8dcSSimon Schubert {
6305796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_ENODEV);
6315796c8dcSSimon Schubert return;
6325796c8dcSSimon Schubert }
6335796c8dcSSimon Schubert if (S_ISDIR (st.st_mode)
6345796c8dcSSimon Schubert && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
6355796c8dcSSimon Schubert {
6365796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EISDIR);
6375796c8dcSSimon Schubert return;
6385796c8dcSSimon Schubert }
6395796c8dcSSimon Schubert }
6405796c8dcSSimon Schubert
6415796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
6425796c8dcSSimon Schubert fd = open (pathname, flags, mode);
6435796c8dcSSimon Schubert if (fd < 0)
6445796c8dcSSimon Schubert {
6455796c8dcSSimon Schubert remote_fileio_return_errno (-1);
6465796c8dcSSimon Schubert return;
6475796c8dcSSimon Schubert }
6485796c8dcSSimon Schubert
6495796c8dcSSimon Schubert fd = remote_fileio_fd_to_targetfd (fd);
6505796c8dcSSimon Schubert remote_fileio_return_success (fd);
6515796c8dcSSimon Schubert }
6525796c8dcSSimon Schubert
6535796c8dcSSimon Schubert static void
remote_fileio_func_close(char * buf)6545796c8dcSSimon Schubert remote_fileio_func_close (char *buf)
6555796c8dcSSimon Schubert {
6565796c8dcSSimon Schubert long num;
6575796c8dcSSimon Schubert int fd;
6585796c8dcSSimon Schubert
6595796c8dcSSimon Schubert /* Parameter: file descriptor */
6605796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
6615796c8dcSSimon Schubert {
6625796c8dcSSimon Schubert remote_fileio_ioerror ();
6635796c8dcSSimon Schubert return;
6645796c8dcSSimon Schubert }
6655796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) num);
6665796c8dcSSimon Schubert if (fd == FIO_FD_INVALID)
6675796c8dcSSimon Schubert {
6685796c8dcSSimon Schubert remote_fileio_badfd ();
6695796c8dcSSimon Schubert return;
6705796c8dcSSimon Schubert }
6715796c8dcSSimon Schubert
6725796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
6735796c8dcSSimon Schubert if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
6745796c8dcSSimon Schubert remote_fileio_return_errno (-1);
6755796c8dcSSimon Schubert remote_fileio_close_target_fd ((int) num);
6765796c8dcSSimon Schubert remote_fileio_return_success (0);
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert
6795796c8dcSSimon Schubert static void
remote_fileio_func_read(char * buf)6805796c8dcSSimon Schubert remote_fileio_func_read (char *buf)
6815796c8dcSSimon Schubert {
6825796c8dcSSimon Schubert long target_fd, num;
6835796c8dcSSimon Schubert LONGEST lnum;
6845796c8dcSSimon Schubert CORE_ADDR ptrval;
685*ef5ccd6cSJohn Marino int fd, ret;
6865796c8dcSSimon Schubert gdb_byte *buffer;
6875796c8dcSSimon Schubert size_t length;
6885796c8dcSSimon Schubert off_t old_offset, new_offset;
6895796c8dcSSimon Schubert
6905796c8dcSSimon Schubert /* 1. Parameter: file descriptor */
6915796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &target_fd))
6925796c8dcSSimon Schubert {
6935796c8dcSSimon Schubert remote_fileio_ioerror ();
6945796c8dcSSimon Schubert return;
6955796c8dcSSimon Schubert }
6965796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) target_fd);
6975796c8dcSSimon Schubert if (fd == FIO_FD_INVALID)
6985796c8dcSSimon Schubert {
6995796c8dcSSimon Schubert remote_fileio_badfd ();
7005796c8dcSSimon Schubert return;
7015796c8dcSSimon Schubert }
7025796c8dcSSimon Schubert /* 2. Parameter: buffer pointer */
7035796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
7045796c8dcSSimon Schubert {
7055796c8dcSSimon Schubert remote_fileio_ioerror ();
7065796c8dcSSimon Schubert return;
7075796c8dcSSimon Schubert }
7085796c8dcSSimon Schubert ptrval = (CORE_ADDR) lnum;
7095796c8dcSSimon Schubert /* 3. Parameter: buffer length */
7105796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
7115796c8dcSSimon Schubert {
7125796c8dcSSimon Schubert remote_fileio_ioerror ();
7135796c8dcSSimon Schubert return;
7145796c8dcSSimon Schubert }
7155796c8dcSSimon Schubert length = (size_t) num;
7165796c8dcSSimon Schubert
7175796c8dcSSimon Schubert switch (fd)
7185796c8dcSSimon Schubert {
7195796c8dcSSimon Schubert case FIO_FD_CONSOLE_OUT:
7205796c8dcSSimon Schubert remote_fileio_badfd ();
7215796c8dcSSimon Schubert return;
7225796c8dcSSimon Schubert case FIO_FD_CONSOLE_IN:
7235796c8dcSSimon Schubert {
7245796c8dcSSimon Schubert static char *remaining_buf = NULL;
7255796c8dcSSimon Schubert static int remaining_length = 0;
7265796c8dcSSimon Schubert
727cf7f2e2dSJohn Marino buffer = (gdb_byte *) xmalloc (16384);
7285796c8dcSSimon Schubert if (remaining_buf)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
7315796c8dcSSimon Schubert if (remaining_length > length)
7325796c8dcSSimon Schubert {
7335796c8dcSSimon Schubert memcpy (buffer, remaining_buf, length);
7345796c8dcSSimon Schubert memmove (remaining_buf, remaining_buf + length,
7355796c8dcSSimon Schubert remaining_length - length);
7365796c8dcSSimon Schubert remaining_length -= length;
7375796c8dcSSimon Schubert ret = length;
7385796c8dcSSimon Schubert }
7395796c8dcSSimon Schubert else
7405796c8dcSSimon Schubert {
7415796c8dcSSimon Schubert memcpy (buffer, remaining_buf, remaining_length);
7425796c8dcSSimon Schubert xfree (remaining_buf);
7435796c8dcSSimon Schubert remaining_buf = NULL;
7445796c8dcSSimon Schubert ret = remaining_length;
7455796c8dcSSimon Schubert }
7465796c8dcSSimon Schubert }
7475796c8dcSSimon Schubert else
7485796c8dcSSimon Schubert {
749cf7f2e2dSJohn Marino /* Windows (at least XP and Server 2003) has difficulty
750cf7f2e2dSJohn Marino with large reads from consoles. If a handle is
751cf7f2e2dSJohn Marino backed by a real console device, overly large reads
752cf7f2e2dSJohn Marino from the handle will fail and set errno == ENOMEM.
753cf7f2e2dSJohn Marino On a Windows Server 2003 system where I tested,
754cf7f2e2dSJohn Marino reading 26608 bytes from the console was OK, but
755cf7f2e2dSJohn Marino anything above 26609 bytes would fail. The limit has
756cf7f2e2dSJohn Marino been observed to vary on different systems. So, we
757cf7f2e2dSJohn Marino limit this read to something smaller than that - by a
758cf7f2e2dSJohn Marino safe margin, in case the limit depends on system
759cf7f2e2dSJohn Marino resources or version. */
760cf7f2e2dSJohn Marino ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
7615796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
7625796c8dcSSimon Schubert if (ret > 0 && (size_t)ret > length)
7635796c8dcSSimon Schubert {
7645796c8dcSSimon Schubert remaining_buf = (char *) xmalloc (ret - length);
7655796c8dcSSimon Schubert remaining_length = ret - length;
7665796c8dcSSimon Schubert memcpy (remaining_buf, buffer + length, remaining_length);
7675796c8dcSSimon Schubert ret = length;
7685796c8dcSSimon Schubert }
7695796c8dcSSimon Schubert }
7705796c8dcSSimon Schubert }
7715796c8dcSSimon Schubert break;
7725796c8dcSSimon Schubert default:
7735796c8dcSSimon Schubert buffer = (gdb_byte *) xmalloc (length);
7745796c8dcSSimon Schubert /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
7755796c8dcSSimon Schubert for read() to return -1 even if "some" bytes have been read. It
7765796c8dcSSimon Schubert has been corrected in SUSv2 but that doesn't help us much...
7775796c8dcSSimon Schubert Therefore a complete solution must check how many bytes have been
7785796c8dcSSimon Schubert read on EINTR to return a more reliable value to the target */
7795796c8dcSSimon Schubert old_offset = lseek (fd, 0, SEEK_CUR);
7805796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
7815796c8dcSSimon Schubert ret = read (fd, buffer, length);
7825796c8dcSSimon Schubert if (ret < 0 && errno == EINTR)
7835796c8dcSSimon Schubert {
7845796c8dcSSimon Schubert new_offset = lseek (fd, 0, SEEK_CUR);
7855796c8dcSSimon Schubert /* If some data has been read, return the number of bytes read.
786c50c785cSJohn Marino The Ctrl-C flag is set in remote_fileio_reply() anyway. */
7875796c8dcSSimon Schubert if (old_offset != new_offset)
7885796c8dcSSimon Schubert ret = new_offset - old_offset;
7895796c8dcSSimon Schubert }
7905796c8dcSSimon Schubert break;
7915796c8dcSSimon Schubert }
7925796c8dcSSimon Schubert
7935796c8dcSSimon Schubert if (ret > 0)
7945796c8dcSSimon Schubert {
795c50c785cSJohn Marino errno = target_write_memory (ptrval, buffer, ret);
796c50c785cSJohn Marino if (errno != 0)
797c50c785cSJohn Marino ret = -1;
7985796c8dcSSimon Schubert }
7995796c8dcSSimon Schubert
8005796c8dcSSimon Schubert if (ret < 0)
8015796c8dcSSimon Schubert remote_fileio_return_errno (-1);
8025796c8dcSSimon Schubert else
8035796c8dcSSimon Schubert remote_fileio_return_success (ret);
8045796c8dcSSimon Schubert
8055796c8dcSSimon Schubert xfree (buffer);
8065796c8dcSSimon Schubert }
8075796c8dcSSimon Schubert
8085796c8dcSSimon Schubert static void
remote_fileio_func_write(char * buf)8095796c8dcSSimon Schubert remote_fileio_func_write (char *buf)
8105796c8dcSSimon Schubert {
8115796c8dcSSimon Schubert long target_fd, num;
8125796c8dcSSimon Schubert LONGEST lnum;
8135796c8dcSSimon Schubert CORE_ADDR ptrval;
814c50c785cSJohn Marino int fd, ret;
8155796c8dcSSimon Schubert gdb_byte *buffer;
8165796c8dcSSimon Schubert size_t length;
8175796c8dcSSimon Schubert
8185796c8dcSSimon Schubert /* 1. Parameter: file descriptor */
8195796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &target_fd))
8205796c8dcSSimon Schubert {
8215796c8dcSSimon Schubert remote_fileio_ioerror ();
8225796c8dcSSimon Schubert return;
8235796c8dcSSimon Schubert }
8245796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) target_fd);
8255796c8dcSSimon Schubert if (fd == FIO_FD_INVALID)
8265796c8dcSSimon Schubert {
8275796c8dcSSimon Schubert remote_fileio_badfd ();
8285796c8dcSSimon Schubert return;
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert /* 2. Parameter: buffer pointer */
8315796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
8325796c8dcSSimon Schubert {
8335796c8dcSSimon Schubert remote_fileio_ioerror ();
8345796c8dcSSimon Schubert return;
8355796c8dcSSimon Schubert }
8365796c8dcSSimon Schubert ptrval = (CORE_ADDR) lnum;
8375796c8dcSSimon Schubert /* 3. Parameter: buffer length */
8385796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
8395796c8dcSSimon Schubert {
8405796c8dcSSimon Schubert remote_fileio_ioerror ();
8415796c8dcSSimon Schubert return;
8425796c8dcSSimon Schubert }
8435796c8dcSSimon Schubert length = (size_t) num;
8445796c8dcSSimon Schubert
8455796c8dcSSimon Schubert buffer = (gdb_byte *) xmalloc (length);
846c50c785cSJohn Marino if (target_read_memory (ptrval, buffer, length) != 0)
8475796c8dcSSimon Schubert {
8485796c8dcSSimon Schubert xfree (buffer);
8495796c8dcSSimon Schubert remote_fileio_ioerror ();
8505796c8dcSSimon Schubert return;
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert
8535796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
8545796c8dcSSimon Schubert switch (fd)
8555796c8dcSSimon Schubert {
8565796c8dcSSimon Schubert case FIO_FD_CONSOLE_IN:
8575796c8dcSSimon Schubert remote_fileio_badfd ();
8585796c8dcSSimon Schubert xfree (buffer);
8595796c8dcSSimon Schubert return;
8605796c8dcSSimon Schubert case FIO_FD_CONSOLE_OUT:
8615796c8dcSSimon Schubert ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
8625796c8dcSSimon Schubert (char *) buffer, length);
8635796c8dcSSimon Schubert gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
8645796c8dcSSimon Schubert ret = length;
8655796c8dcSSimon Schubert break;
8665796c8dcSSimon Schubert default:
8675796c8dcSSimon Schubert ret = write (fd, buffer, length);
8685796c8dcSSimon Schubert if (ret < 0 && errno == EACCES)
869c50c785cSJohn Marino errno = EBADF; /* Cygwin returns EACCESS when writing to a
870c50c785cSJohn Marino R/O file. */
8715796c8dcSSimon Schubert break;
8725796c8dcSSimon Schubert }
8735796c8dcSSimon Schubert
8745796c8dcSSimon Schubert if (ret < 0)
8755796c8dcSSimon Schubert remote_fileio_return_errno (-1);
8765796c8dcSSimon Schubert else
8775796c8dcSSimon Schubert remote_fileio_return_success (ret);
8785796c8dcSSimon Schubert
8795796c8dcSSimon Schubert xfree (buffer);
8805796c8dcSSimon Schubert }
8815796c8dcSSimon Schubert
8825796c8dcSSimon Schubert static void
remote_fileio_func_lseek(char * buf)8835796c8dcSSimon Schubert remote_fileio_func_lseek (char *buf)
8845796c8dcSSimon Schubert {
8855796c8dcSSimon Schubert long num;
8865796c8dcSSimon Schubert LONGEST lnum;
8875796c8dcSSimon Schubert int fd, flag;
8885796c8dcSSimon Schubert off_t offset, ret;
8895796c8dcSSimon Schubert
8905796c8dcSSimon Schubert /* 1. Parameter: file descriptor */
8915796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
8925796c8dcSSimon Schubert {
8935796c8dcSSimon Schubert remote_fileio_ioerror ();
8945796c8dcSSimon Schubert return;
8955796c8dcSSimon Schubert }
8965796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) num);
8975796c8dcSSimon Schubert if (fd == FIO_FD_INVALID)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert remote_fileio_badfd ();
9005796c8dcSSimon Schubert return;
9015796c8dcSSimon Schubert }
9025796c8dcSSimon Schubert else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
9035796c8dcSSimon Schubert {
9045796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_ESPIPE);
9055796c8dcSSimon Schubert return;
9065796c8dcSSimon Schubert }
9075796c8dcSSimon Schubert
9085796c8dcSSimon Schubert /* 2. Parameter: offset */
9095796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
9105796c8dcSSimon Schubert {
9115796c8dcSSimon Schubert remote_fileio_ioerror ();
9125796c8dcSSimon Schubert return;
9135796c8dcSSimon Schubert }
9145796c8dcSSimon Schubert offset = (off_t) lnum;
9155796c8dcSSimon Schubert /* 3. Parameter: flag */
9165796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &num))
9175796c8dcSSimon Schubert {
9185796c8dcSSimon Schubert remote_fileio_ioerror ();
9195796c8dcSSimon Schubert return;
9205796c8dcSSimon Schubert }
9215796c8dcSSimon Schubert if (remote_fileio_seek_flag_to_host (num, &flag))
9225796c8dcSSimon Schubert {
9235796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EINVAL);
9245796c8dcSSimon Schubert return;
9255796c8dcSSimon Schubert }
9265796c8dcSSimon Schubert
9275796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
9285796c8dcSSimon Schubert ret = lseek (fd, offset, flag);
9295796c8dcSSimon Schubert
9305796c8dcSSimon Schubert if (ret == (off_t) -1)
9315796c8dcSSimon Schubert remote_fileio_return_errno (-1);
9325796c8dcSSimon Schubert else
9335796c8dcSSimon Schubert remote_fileio_return_success (ret);
9345796c8dcSSimon Schubert }
9355796c8dcSSimon Schubert
9365796c8dcSSimon Schubert static void
remote_fileio_func_rename(char * buf)9375796c8dcSSimon Schubert remote_fileio_func_rename (char *buf)
9385796c8dcSSimon Schubert {
9395796c8dcSSimon Schubert CORE_ADDR old_ptr, new_ptr;
940c50c785cSJohn Marino int old_len, new_len;
9415796c8dcSSimon Schubert char *oldpath, *newpath;
9425796c8dcSSimon Schubert int ret, of, nf;
9435796c8dcSSimon Schubert struct stat ost, nst;
9445796c8dcSSimon Schubert
9455796c8dcSSimon Schubert /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
9465796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
9475796c8dcSSimon Schubert {
9485796c8dcSSimon Schubert remote_fileio_ioerror ();
9495796c8dcSSimon Schubert return;
9505796c8dcSSimon Schubert }
9515796c8dcSSimon Schubert
9525796c8dcSSimon Schubert /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
9535796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
9545796c8dcSSimon Schubert {
9555796c8dcSSimon Schubert remote_fileio_ioerror ();
9565796c8dcSSimon Schubert return;
9575796c8dcSSimon Schubert }
9585796c8dcSSimon Schubert
9595796c8dcSSimon Schubert /* Request oldpath using 'm' packet */
9605796c8dcSSimon Schubert oldpath = alloca (old_len);
961c50c785cSJohn Marino if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
9625796c8dcSSimon Schubert {
9635796c8dcSSimon Schubert remote_fileio_ioerror ();
9645796c8dcSSimon Schubert return;
9655796c8dcSSimon Schubert }
9665796c8dcSSimon Schubert
9675796c8dcSSimon Schubert /* Request newpath using 'm' packet */
9685796c8dcSSimon Schubert newpath = alloca (new_len);
969c50c785cSJohn Marino if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
9705796c8dcSSimon Schubert {
9715796c8dcSSimon Schubert remote_fileio_ioerror ();
9725796c8dcSSimon Schubert return;
9735796c8dcSSimon Schubert }
9745796c8dcSSimon Schubert
975c50c785cSJohn Marino /* Only operate on regular files and directories. */
9765796c8dcSSimon Schubert of = stat (oldpath, &ost);
9775796c8dcSSimon Schubert nf = stat (newpath, &nst);
9785796c8dcSSimon Schubert if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
9795796c8dcSSimon Schubert || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
9805796c8dcSSimon Schubert {
9815796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EACCES);
9825796c8dcSSimon Schubert return;
9835796c8dcSSimon Schubert }
9845796c8dcSSimon Schubert
9855796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
9865796c8dcSSimon Schubert ret = rename (oldpath, newpath);
9875796c8dcSSimon Schubert
9885796c8dcSSimon Schubert if (ret == -1)
9895796c8dcSSimon Schubert {
9905796c8dcSSimon Schubert /* Special case: newpath is a non-empty directory. Some systems
9915796c8dcSSimon Schubert return ENOTEMPTY, some return EEXIST. We coerce that to be
9925796c8dcSSimon Schubert always EEXIST. */
9935796c8dcSSimon Schubert if (errno == ENOTEMPTY)
9945796c8dcSSimon Schubert errno = EEXIST;
9955796c8dcSSimon Schubert #ifdef __CYGWIN__
9965796c8dcSSimon Schubert /* Workaround some Cygwin problems with correct errnos. */
9975796c8dcSSimon Schubert if (errno == EACCES)
9985796c8dcSSimon Schubert {
9995796c8dcSSimon Schubert if (!of && !nf && S_ISDIR (nst.st_mode))
10005796c8dcSSimon Schubert {
10015796c8dcSSimon Schubert if (S_ISREG (ost.st_mode))
10025796c8dcSSimon Schubert errno = EISDIR;
10035796c8dcSSimon Schubert else
10045796c8dcSSimon Schubert {
1005cf7f2e2dSJohn Marino char oldfullpath[PATH_MAX];
1006cf7f2e2dSJohn Marino char newfullpath[PATH_MAX];
10075796c8dcSSimon Schubert int len;
10085796c8dcSSimon Schubert
1009cf7f2e2dSJohn Marino cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
1010cf7f2e2dSJohn Marino PATH_MAX);
1011cf7f2e2dSJohn Marino cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
1012cf7f2e2dSJohn Marino PATH_MAX);
10135796c8dcSSimon Schubert len = strlen (oldfullpath);
1014c50c785cSJohn Marino if (IS_DIR_SEPARATOR (newfullpath[len])
1015c50c785cSJohn Marino && !filename_ncmp (oldfullpath, newfullpath, len))
10165796c8dcSSimon Schubert errno = EINVAL;
10175796c8dcSSimon Schubert else
10185796c8dcSSimon Schubert errno = EEXIST;
10195796c8dcSSimon Schubert }
10205796c8dcSSimon Schubert }
10215796c8dcSSimon Schubert }
10225796c8dcSSimon Schubert #endif
10235796c8dcSSimon Schubert
10245796c8dcSSimon Schubert remote_fileio_return_errno (-1);
10255796c8dcSSimon Schubert }
10265796c8dcSSimon Schubert else
10275796c8dcSSimon Schubert remote_fileio_return_success (ret);
10285796c8dcSSimon Schubert }
10295796c8dcSSimon Schubert
10305796c8dcSSimon Schubert static void
remote_fileio_func_unlink(char * buf)10315796c8dcSSimon Schubert remote_fileio_func_unlink (char *buf)
10325796c8dcSSimon Schubert {
10335796c8dcSSimon Schubert CORE_ADDR ptrval;
1034c50c785cSJohn Marino int length;
10355796c8dcSSimon Schubert char *pathname;
10365796c8dcSSimon Schubert int ret;
10375796c8dcSSimon Schubert struct stat st;
10385796c8dcSSimon Schubert
10395796c8dcSSimon Schubert /* Parameter: Ptr to pathname / length incl. trailing zero */
10405796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
10415796c8dcSSimon Schubert {
10425796c8dcSSimon Schubert remote_fileio_ioerror ();
10435796c8dcSSimon Schubert return;
10445796c8dcSSimon Schubert }
10455796c8dcSSimon Schubert /* Request pathname using 'm' packet */
10465796c8dcSSimon Schubert pathname = alloca (length);
1047c50c785cSJohn Marino if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
10485796c8dcSSimon Schubert {
10495796c8dcSSimon Schubert remote_fileio_ioerror ();
10505796c8dcSSimon Schubert return;
10515796c8dcSSimon Schubert }
10525796c8dcSSimon Schubert
10535796c8dcSSimon Schubert /* Only operate on regular files (and directories, which allows to return
1054c50c785cSJohn Marino the correct return code). */
10555796c8dcSSimon Schubert if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
10565796c8dcSSimon Schubert {
10575796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_ENODEV);
10585796c8dcSSimon Schubert return;
10595796c8dcSSimon Schubert }
10605796c8dcSSimon Schubert
10615796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
10625796c8dcSSimon Schubert ret = unlink (pathname);
10635796c8dcSSimon Schubert
10645796c8dcSSimon Schubert if (ret == -1)
10655796c8dcSSimon Schubert remote_fileio_return_errno (-1);
10665796c8dcSSimon Schubert else
10675796c8dcSSimon Schubert remote_fileio_return_success (ret);
10685796c8dcSSimon Schubert }
10695796c8dcSSimon Schubert
10705796c8dcSSimon Schubert static void
remote_fileio_func_stat(char * buf)10715796c8dcSSimon Schubert remote_fileio_func_stat (char *buf)
10725796c8dcSSimon Schubert {
10735796c8dcSSimon Schubert CORE_ADDR statptr, nameptr;
1074c50c785cSJohn Marino int ret, namelength;
10755796c8dcSSimon Schubert char *pathname;
10765796c8dcSSimon Schubert LONGEST lnum;
10775796c8dcSSimon Schubert struct stat st;
10785796c8dcSSimon Schubert struct fio_stat fst;
10795796c8dcSSimon Schubert
10805796c8dcSSimon Schubert /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
10815796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
10825796c8dcSSimon Schubert {
10835796c8dcSSimon Schubert remote_fileio_ioerror ();
10845796c8dcSSimon Schubert return;
10855796c8dcSSimon Schubert }
10865796c8dcSSimon Schubert
10875796c8dcSSimon Schubert /* 2. Parameter: Ptr to struct stat */
10885796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
10895796c8dcSSimon Schubert {
10905796c8dcSSimon Schubert remote_fileio_ioerror ();
10915796c8dcSSimon Schubert return;
10925796c8dcSSimon Schubert }
10935796c8dcSSimon Schubert statptr = (CORE_ADDR) lnum;
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert /* Request pathname using 'm' packet */
10965796c8dcSSimon Schubert pathname = alloca (namelength);
1097c50c785cSJohn Marino if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
10985796c8dcSSimon Schubert {
10995796c8dcSSimon Schubert remote_fileio_ioerror ();
11005796c8dcSSimon Schubert return;
11015796c8dcSSimon Schubert }
11025796c8dcSSimon Schubert
11035796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
11045796c8dcSSimon Schubert ret = stat (pathname, &st);
11055796c8dcSSimon Schubert
11065796c8dcSSimon Schubert if (ret == -1)
11075796c8dcSSimon Schubert {
11085796c8dcSSimon Schubert remote_fileio_return_errno (-1);
11095796c8dcSSimon Schubert return;
11105796c8dcSSimon Schubert }
1111c50c785cSJohn Marino /* Only operate on regular files and directories. */
11125796c8dcSSimon Schubert if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
11135796c8dcSSimon Schubert {
11145796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EACCES);
11155796c8dcSSimon Schubert return;
11165796c8dcSSimon Schubert }
11175796c8dcSSimon Schubert if (statptr)
11185796c8dcSSimon Schubert {
11195796c8dcSSimon Schubert remote_fileio_to_fio_stat (&st, &fst);
11205796c8dcSSimon Schubert remote_fileio_to_fio_uint (0, fst.fst_dev);
11215796c8dcSSimon Schubert
1122c50c785cSJohn Marino errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
1123c50c785cSJohn Marino if (errno != 0)
11245796c8dcSSimon Schubert {
11255796c8dcSSimon Schubert remote_fileio_return_errno (-1);
11265796c8dcSSimon Schubert return;
11275796c8dcSSimon Schubert }
11285796c8dcSSimon Schubert }
11295796c8dcSSimon Schubert remote_fileio_return_success (ret);
11305796c8dcSSimon Schubert }
11315796c8dcSSimon Schubert
11325796c8dcSSimon Schubert static void
remote_fileio_func_fstat(char * buf)11335796c8dcSSimon Schubert remote_fileio_func_fstat (char *buf)
11345796c8dcSSimon Schubert {
11355796c8dcSSimon Schubert CORE_ADDR ptrval;
1136*ef5ccd6cSJohn Marino int fd, ret;
11375796c8dcSSimon Schubert long target_fd;
11385796c8dcSSimon Schubert LONGEST lnum;
11395796c8dcSSimon Schubert struct stat st;
11405796c8dcSSimon Schubert struct fio_stat fst;
11415796c8dcSSimon Schubert struct timeval tv;
11425796c8dcSSimon Schubert
11435796c8dcSSimon Schubert /* 1. Parameter: file descriptor */
11445796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &target_fd))
11455796c8dcSSimon Schubert {
11465796c8dcSSimon Schubert remote_fileio_ioerror ();
11475796c8dcSSimon Schubert return;
11485796c8dcSSimon Schubert }
11495796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) target_fd);
11505796c8dcSSimon Schubert if (fd == FIO_FD_INVALID)
11515796c8dcSSimon Schubert {
11525796c8dcSSimon Schubert remote_fileio_badfd ();
11535796c8dcSSimon Schubert return;
11545796c8dcSSimon Schubert }
11555796c8dcSSimon Schubert /* 2. Parameter: Ptr to struct stat */
11565796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
11575796c8dcSSimon Schubert {
11585796c8dcSSimon Schubert remote_fileio_ioerror ();
11595796c8dcSSimon Schubert return;
11605796c8dcSSimon Schubert }
11615796c8dcSSimon Schubert ptrval = (CORE_ADDR) lnum;
11625796c8dcSSimon Schubert
11635796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
11645796c8dcSSimon Schubert if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
11655796c8dcSSimon Schubert {
11665796c8dcSSimon Schubert remote_fileio_to_fio_uint (1, fst.fst_dev);
1167c50c785cSJohn Marino memset (&st, 0, sizeof (st));
11685796c8dcSSimon Schubert st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
11695796c8dcSSimon Schubert st.st_nlink = 1;
11705796c8dcSSimon Schubert #ifdef HAVE_GETUID
11715796c8dcSSimon Schubert st.st_uid = getuid ();
11725796c8dcSSimon Schubert #endif
11735796c8dcSSimon Schubert #ifdef HAVE_GETGID
11745796c8dcSSimon Schubert st.st_gid = getgid ();
11755796c8dcSSimon Schubert #endif
11765796c8dcSSimon Schubert #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
11775796c8dcSSimon Schubert st.st_blksize = 512;
11785796c8dcSSimon Schubert #endif
11795796c8dcSSimon Schubert #if HAVE_STRUCT_STAT_ST_BLOCKS
11805796c8dcSSimon Schubert st.st_blocks = 0;
11815796c8dcSSimon Schubert #endif
11825796c8dcSSimon Schubert if (!gettimeofday (&tv, NULL))
11835796c8dcSSimon Schubert st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
11845796c8dcSSimon Schubert else
11855796c8dcSSimon Schubert st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
11865796c8dcSSimon Schubert ret = 0;
11875796c8dcSSimon Schubert }
11885796c8dcSSimon Schubert else
11895796c8dcSSimon Schubert ret = fstat (fd, &st);
11905796c8dcSSimon Schubert
11915796c8dcSSimon Schubert if (ret == -1)
11925796c8dcSSimon Schubert {
11935796c8dcSSimon Schubert remote_fileio_return_errno (-1);
11945796c8dcSSimon Schubert return;
11955796c8dcSSimon Schubert }
11965796c8dcSSimon Schubert if (ptrval)
11975796c8dcSSimon Schubert {
11985796c8dcSSimon Schubert remote_fileio_to_fio_stat (&st, &fst);
11995796c8dcSSimon Schubert
1200c50c785cSJohn Marino errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1201c50c785cSJohn Marino if (errno != 0)
12025796c8dcSSimon Schubert {
12035796c8dcSSimon Schubert remote_fileio_return_errno (-1);
12045796c8dcSSimon Schubert return;
12055796c8dcSSimon Schubert }
12065796c8dcSSimon Schubert }
12075796c8dcSSimon Schubert remote_fileio_return_success (ret);
12085796c8dcSSimon Schubert }
12095796c8dcSSimon Schubert
12105796c8dcSSimon Schubert static void
remote_fileio_func_gettimeofday(char * buf)12115796c8dcSSimon Schubert remote_fileio_func_gettimeofday (char *buf)
12125796c8dcSSimon Schubert {
12135796c8dcSSimon Schubert LONGEST lnum;
12145796c8dcSSimon Schubert CORE_ADDR ptrval;
1215*ef5ccd6cSJohn Marino int ret;
12165796c8dcSSimon Schubert struct timeval tv;
12175796c8dcSSimon Schubert struct fio_timeval ftv;
12185796c8dcSSimon Schubert
12195796c8dcSSimon Schubert /* 1. Parameter: struct timeval pointer */
12205796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
12215796c8dcSSimon Schubert {
12225796c8dcSSimon Schubert remote_fileio_ioerror ();
12235796c8dcSSimon Schubert return;
12245796c8dcSSimon Schubert }
12255796c8dcSSimon Schubert ptrval = (CORE_ADDR) lnum;
12265796c8dcSSimon Schubert /* 2. Parameter: some pointer value... */
12275796c8dcSSimon Schubert if (remote_fileio_extract_long (&buf, &lnum))
12285796c8dcSSimon Schubert {
12295796c8dcSSimon Schubert remote_fileio_ioerror ();
12305796c8dcSSimon Schubert return;
12315796c8dcSSimon Schubert }
1232c50c785cSJohn Marino /* ...which has to be NULL. */
12335796c8dcSSimon Schubert if (lnum)
12345796c8dcSSimon Schubert {
12355796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EINVAL);
12365796c8dcSSimon Schubert return;
12375796c8dcSSimon Schubert }
12385796c8dcSSimon Schubert
12395796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
12405796c8dcSSimon Schubert ret = gettimeofday (&tv, NULL);
12415796c8dcSSimon Schubert
12425796c8dcSSimon Schubert if (ret == -1)
12435796c8dcSSimon Schubert {
12445796c8dcSSimon Schubert remote_fileio_return_errno (-1);
12455796c8dcSSimon Schubert return;
12465796c8dcSSimon Schubert }
12475796c8dcSSimon Schubert
12485796c8dcSSimon Schubert if (ptrval)
12495796c8dcSSimon Schubert {
12505796c8dcSSimon Schubert remote_fileio_to_fio_timeval (&tv, &ftv);
12515796c8dcSSimon Schubert
1252c50c785cSJohn Marino errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1253c50c785cSJohn Marino if (errno != 0)
12545796c8dcSSimon Schubert {
12555796c8dcSSimon Schubert remote_fileio_return_errno (-1);
12565796c8dcSSimon Schubert return;
12575796c8dcSSimon Schubert }
12585796c8dcSSimon Schubert }
12595796c8dcSSimon Schubert remote_fileio_return_success (ret);
12605796c8dcSSimon Schubert }
12615796c8dcSSimon Schubert
12625796c8dcSSimon Schubert static void
remote_fileio_func_isatty(char * buf)12635796c8dcSSimon Schubert remote_fileio_func_isatty (char *buf)
12645796c8dcSSimon Schubert {
12655796c8dcSSimon Schubert long target_fd;
12665796c8dcSSimon Schubert int fd;
12675796c8dcSSimon Schubert
12685796c8dcSSimon Schubert /* Parameter: file descriptor */
12695796c8dcSSimon Schubert if (remote_fileio_extract_int (&buf, &target_fd))
12705796c8dcSSimon Schubert {
12715796c8dcSSimon Schubert remote_fileio_ioerror ();
12725796c8dcSSimon Schubert return;
12735796c8dcSSimon Schubert }
12745796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
12755796c8dcSSimon Schubert fd = remote_fileio_map_fd ((int) target_fd);
12765796c8dcSSimon Schubert remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
12775796c8dcSSimon Schubert fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
12785796c8dcSSimon Schubert }
12795796c8dcSSimon Schubert
12805796c8dcSSimon Schubert static void
remote_fileio_func_system(char * buf)12815796c8dcSSimon Schubert remote_fileio_func_system (char *buf)
12825796c8dcSSimon Schubert {
12835796c8dcSSimon Schubert CORE_ADDR ptrval;
1284*ef5ccd6cSJohn Marino int ret, length;
12855796c8dcSSimon Schubert char *cmdline = NULL;
12865796c8dcSSimon Schubert
12875796c8dcSSimon Schubert /* Parameter: Ptr to commandline / length incl. trailing zero */
12885796c8dcSSimon Schubert if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
12895796c8dcSSimon Schubert {
12905796c8dcSSimon Schubert remote_fileio_ioerror ();
12915796c8dcSSimon Schubert return;
12925796c8dcSSimon Schubert }
12935796c8dcSSimon Schubert
12945796c8dcSSimon Schubert if (length)
12955796c8dcSSimon Schubert {
12965796c8dcSSimon Schubert /* Request commandline using 'm' packet */
12975796c8dcSSimon Schubert cmdline = alloca (length);
1298c50c785cSJohn Marino if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
12995796c8dcSSimon Schubert {
13005796c8dcSSimon Schubert remote_fileio_ioerror ();
13015796c8dcSSimon Schubert return;
13025796c8dcSSimon Schubert }
13035796c8dcSSimon Schubert }
13045796c8dcSSimon Schubert
13055796c8dcSSimon Schubert /* Check if system(3) has been explicitely allowed using the
13065796c8dcSSimon Schubert `set remote system-call-allowed 1' command. If length is 0,
13075796c8dcSSimon Schubert indicating a NULL parameter to the system call, return zero to
13085796c8dcSSimon Schubert indicate a shell is not available. Otherwise fail with EPERM. */
13095796c8dcSSimon Schubert if (!remote_fio_system_call_allowed)
13105796c8dcSSimon Schubert {
13115796c8dcSSimon Schubert if (!length)
13125796c8dcSSimon Schubert remote_fileio_return_success (0);
13135796c8dcSSimon Schubert else
13145796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EPERM);
13155796c8dcSSimon Schubert return;
13165796c8dcSSimon Schubert }
13175796c8dcSSimon Schubert
13185796c8dcSSimon Schubert remote_fio_no_longjmp = 1;
13195796c8dcSSimon Schubert ret = system (cmdline);
13205796c8dcSSimon Schubert
13215796c8dcSSimon Schubert if (!length)
13225796c8dcSSimon Schubert remote_fileio_return_success (ret);
13235796c8dcSSimon Schubert else if (ret == -1)
13245796c8dcSSimon Schubert remote_fileio_return_errno (-1);
13255796c8dcSSimon Schubert else
13265796c8dcSSimon Schubert remote_fileio_return_success (WEXITSTATUS (ret));
13275796c8dcSSimon Schubert }
13285796c8dcSSimon Schubert
13295796c8dcSSimon Schubert static struct {
13305796c8dcSSimon Schubert char *name;
13315796c8dcSSimon Schubert void (*func)(char *);
13325796c8dcSSimon Schubert } remote_fio_func_map[] = {
13335796c8dcSSimon Schubert { "open", remote_fileio_func_open },
13345796c8dcSSimon Schubert { "close", remote_fileio_func_close },
13355796c8dcSSimon Schubert { "read", remote_fileio_func_read },
13365796c8dcSSimon Schubert { "write", remote_fileio_func_write },
13375796c8dcSSimon Schubert { "lseek", remote_fileio_func_lseek },
13385796c8dcSSimon Schubert { "rename", remote_fileio_func_rename },
13395796c8dcSSimon Schubert { "unlink", remote_fileio_func_unlink },
13405796c8dcSSimon Schubert { "stat", remote_fileio_func_stat },
13415796c8dcSSimon Schubert { "fstat", remote_fileio_func_fstat },
13425796c8dcSSimon Schubert { "gettimeofday", remote_fileio_func_gettimeofday },
13435796c8dcSSimon Schubert { "isatty", remote_fileio_func_isatty },
13445796c8dcSSimon Schubert { "system", remote_fileio_func_system },
13455796c8dcSSimon Schubert { NULL, NULL }
13465796c8dcSSimon Schubert };
13475796c8dcSSimon Schubert
13485796c8dcSSimon Schubert static int
do_remote_fileio_request(struct ui_out * uiout,void * buf_arg)13495796c8dcSSimon Schubert do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
13505796c8dcSSimon Schubert {
13515796c8dcSSimon Schubert char *buf = buf_arg;
13525796c8dcSSimon Schubert char *c;
13535796c8dcSSimon Schubert int idx;
13545796c8dcSSimon Schubert
13555796c8dcSSimon Schubert remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
13565796c8dcSSimon Schubert
13575796c8dcSSimon Schubert c = strchr (++buf, ',');
13585796c8dcSSimon Schubert if (c)
13595796c8dcSSimon Schubert *c++ = '\0';
13605796c8dcSSimon Schubert else
13615796c8dcSSimon Schubert c = strchr (buf, '\0');
13625796c8dcSSimon Schubert for (idx = 0; remote_fio_func_map[idx].name; ++idx)
13635796c8dcSSimon Schubert if (!strcmp (remote_fio_func_map[idx].name, buf))
13645796c8dcSSimon Schubert break;
13655796c8dcSSimon Schubert if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
13665796c8dcSSimon Schubert return RETURN_ERROR;
13675796c8dcSSimon Schubert remote_fio_func_map[idx].func (c);
13685796c8dcSSimon Schubert return 0;
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert
13715796c8dcSSimon Schubert /* Close any open descriptors, and reinitialize the file mapping. */
13725796c8dcSSimon Schubert
13735796c8dcSSimon Schubert void
remote_fileio_reset(void)13745796c8dcSSimon Schubert remote_fileio_reset (void)
13755796c8dcSSimon Schubert {
13765796c8dcSSimon Schubert int ix;
13775796c8dcSSimon Schubert
13785796c8dcSSimon Schubert for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
13795796c8dcSSimon Schubert {
13805796c8dcSSimon Schubert int fd = remote_fio_data.fd_map[ix];
13815796c8dcSSimon Schubert
13825796c8dcSSimon Schubert if (fd >= 0)
13835796c8dcSSimon Schubert close (fd);
13845796c8dcSSimon Schubert }
13855796c8dcSSimon Schubert if (remote_fio_data.fd_map)
13865796c8dcSSimon Schubert {
13875796c8dcSSimon Schubert xfree (remote_fio_data.fd_map);
13885796c8dcSSimon Schubert remote_fio_data.fd_map = NULL;
13895796c8dcSSimon Schubert remote_fio_data.fd_map_size = 0;
13905796c8dcSSimon Schubert }
13915796c8dcSSimon Schubert }
13925796c8dcSSimon Schubert
1393cf7f2e2dSJohn Marino /* Handle a file I/O request. BUF points to the packet containing the
1394cf7f2e2dSJohn Marino request. CTRLC_PENDING_P should be nonzero if the target has not
1395cf7f2e2dSJohn Marino acknowledged the Ctrl-C sent asynchronously earlier. */
1396cf7f2e2dSJohn Marino
13975796c8dcSSimon Schubert void
remote_fileio_request(char * buf,int ctrlc_pending_p)1398cf7f2e2dSJohn Marino remote_fileio_request (char *buf, int ctrlc_pending_p)
13995796c8dcSSimon Schubert {
14005796c8dcSSimon Schubert int ex;
14015796c8dcSSimon Schubert
14025796c8dcSSimon Schubert remote_fileio_sig_init ();
14035796c8dcSSimon Schubert
1404cf7f2e2dSJohn Marino if (ctrlc_pending_p)
1405cf7f2e2dSJohn Marino {
1406cf7f2e2dSJohn Marino /* If the target hasn't responded to the Ctrl-C sent
1407cf7f2e2dSJohn Marino asynchronously earlier, take this opportunity to send the
1408cf7f2e2dSJohn Marino Ctrl-C synchronously. */
1409cf7f2e2dSJohn Marino remote_fio_ctrl_c_flag = 1;
1410cf7f2e2dSJohn Marino remote_fio_no_longjmp = 0;
1411cf7f2e2dSJohn Marino remote_fileio_reply (-1, FILEIO_EINTR);
1412cf7f2e2dSJohn Marino }
1413cf7f2e2dSJohn Marino else
1414cf7f2e2dSJohn Marino {
14155796c8dcSSimon Schubert remote_fio_ctrl_c_flag = 0;
14165796c8dcSSimon Schubert remote_fio_no_longjmp = 0;
14175796c8dcSSimon Schubert
1418a45ae5f8SJohn Marino ex = catch_exceptions (current_uiout,
1419a45ae5f8SJohn Marino do_remote_fileio_request, (void *)buf,
14205796c8dcSSimon Schubert RETURN_MASK_ALL);
14215796c8dcSSimon Schubert switch (ex)
14225796c8dcSSimon Schubert {
14235796c8dcSSimon Schubert case RETURN_ERROR:
14245796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_ENOSYS);
14255796c8dcSSimon Schubert break;
14265796c8dcSSimon Schubert case RETURN_QUIT:
14275796c8dcSSimon Schubert remote_fileio_reply (-1, FILEIO_EINTR);
14285796c8dcSSimon Schubert break;
14295796c8dcSSimon Schubert default:
14305796c8dcSSimon Schubert break;
14315796c8dcSSimon Schubert }
1432cf7f2e2dSJohn Marino }
14335796c8dcSSimon Schubert
14345796c8dcSSimon Schubert remote_fileio_sig_exit ();
14355796c8dcSSimon Schubert }
14365796c8dcSSimon Schubert
14375796c8dcSSimon Schubert static void
set_system_call_allowed(char * args,int from_tty)14385796c8dcSSimon Schubert set_system_call_allowed (char *args, int from_tty)
14395796c8dcSSimon Schubert {
14405796c8dcSSimon Schubert if (args)
14415796c8dcSSimon Schubert {
14425796c8dcSSimon Schubert char *arg_end;
14435796c8dcSSimon Schubert int val = strtoul (args, &arg_end, 10);
1444cf7f2e2dSJohn Marino
14455796c8dcSSimon Schubert if (*args && *arg_end == '\0')
14465796c8dcSSimon Schubert {
14475796c8dcSSimon Schubert remote_fio_system_call_allowed = !!val;
14485796c8dcSSimon Schubert return;
14495796c8dcSSimon Schubert }
14505796c8dcSSimon Schubert }
14515796c8dcSSimon Schubert error (_("Illegal argument for \"set remote system-call-allowed\" command"));
14525796c8dcSSimon Schubert }
14535796c8dcSSimon Schubert
14545796c8dcSSimon Schubert static void
show_system_call_allowed(char * args,int from_tty)14555796c8dcSSimon Schubert show_system_call_allowed (char *args, int from_tty)
14565796c8dcSSimon Schubert {
14575796c8dcSSimon Schubert if (args)
1458c50c785cSJohn Marino error (_("Garbage after \"show remote "
1459c50c785cSJohn Marino "system-call-allowed\" command: `%s'"), args);
14605796c8dcSSimon Schubert printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
14615796c8dcSSimon Schubert remote_fio_system_call_allowed ? "" : "not ");
14625796c8dcSSimon Schubert }
14635796c8dcSSimon Schubert
14645796c8dcSSimon Schubert void
initialize_remote_fileio(struct cmd_list_element * remote_set_cmdlist,struct cmd_list_element * remote_show_cmdlist)14655796c8dcSSimon Schubert initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
14665796c8dcSSimon Schubert struct cmd_list_element *remote_show_cmdlist)
14675796c8dcSSimon Schubert {
14685796c8dcSSimon Schubert sigint_fileio_token =
14695796c8dcSSimon Schubert create_async_signal_handler (async_remote_fileio_interrupt, NULL);
14705796c8dcSSimon Schubert
14715796c8dcSSimon Schubert add_cmd ("system-call-allowed", no_class,
14725796c8dcSSimon Schubert set_system_call_allowed,
14735796c8dcSSimon Schubert _("Set if the host system(3) call is allowed for the target."),
14745796c8dcSSimon Schubert &remote_set_cmdlist);
14755796c8dcSSimon Schubert add_cmd ("system-call-allowed", no_class,
14765796c8dcSSimon Schubert show_system_call_allowed,
14775796c8dcSSimon Schubert _("Show if the host system(3) call is allowed for the target."),
14785796c8dcSSimon Schubert &remote_show_cmdlist);
14795796c8dcSSimon Schubert }
1480