remote-fileio.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /* Remote File-I/O communications
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* See the GDB User Guide for details of the GDB remote protocol. */
  15. #include "defs.h"
  16. #include "gdbcmd.h"
  17. #include "remote.h"
  18. #include "gdbsupport/gdb_wait.h"
  19. #include <sys/stat.h>
  20. #include "remote-fileio.h"
  21. #include "gdbsupport/event-loop.h"
  22. #include "target.h"
  23. #include "filenames.h"
  24. #include "gdbsupport/filestuff.h"
  25. #include <fcntl.h>
  26. #include "gdbsupport/gdb_sys_time.h"
  27. #ifdef __CYGWIN__
  28. #include <sys/cygwin.h> /* For cygwin_conv_path. */
  29. #endif
  30. #include <signal.h>
  31. static struct {
  32. int *fd_map;
  33. int fd_map_size;
  34. } remote_fio_data;
  35. #define FIO_FD_INVALID -1
  36. #define FIO_FD_CONSOLE_IN -2
  37. #define FIO_FD_CONSOLE_OUT -3
  38. static int remote_fio_system_call_allowed = 0;
  39. static int
  40. remote_fileio_init_fd_map (void)
  41. {
  42. int i;
  43. if (!remote_fio_data.fd_map)
  44. {
  45. remote_fio_data.fd_map = XNEWVEC (int, 10);
  46. remote_fio_data.fd_map_size = 10;
  47. remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
  48. remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
  49. remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
  50. for (i = 3; i < 10; ++i)
  51. remote_fio_data.fd_map[i] = FIO_FD_INVALID;
  52. }
  53. return 3;
  54. }
  55. static int
  56. remote_fileio_resize_fd_map (void)
  57. {
  58. int i = remote_fio_data.fd_map_size;
  59. if (!remote_fio_data.fd_map)
  60. return remote_fileio_init_fd_map ();
  61. remote_fio_data.fd_map_size += 10;
  62. remote_fio_data.fd_map =
  63. (int *) xrealloc (remote_fio_data.fd_map,
  64. remote_fio_data.fd_map_size * sizeof (int));
  65. for (; i < remote_fio_data.fd_map_size; i++)
  66. remote_fio_data.fd_map[i] = FIO_FD_INVALID;
  67. return remote_fio_data.fd_map_size - 10;
  68. }
  69. static int
  70. remote_fileio_next_free_fd (void)
  71. {
  72. int i;
  73. for (i = 0; i < remote_fio_data.fd_map_size; ++i)
  74. if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
  75. return i;
  76. return remote_fileio_resize_fd_map ();
  77. }
  78. static int
  79. remote_fileio_fd_to_targetfd (int fd)
  80. {
  81. int target_fd = remote_fileio_next_free_fd ();
  82. remote_fio_data.fd_map[target_fd] = fd;
  83. return target_fd;
  84. }
  85. static int
  86. remote_fileio_map_fd (int target_fd)
  87. {
  88. remote_fileio_init_fd_map ();
  89. if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
  90. return FIO_FD_INVALID;
  91. return remote_fio_data.fd_map[target_fd];
  92. }
  93. static void
  94. remote_fileio_close_target_fd (int target_fd)
  95. {
  96. remote_fileio_init_fd_map ();
  97. if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
  98. remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
  99. }
  100. static int
  101. remote_fileio_oflags_to_host (long flags)
  102. {
  103. int hflags = 0;
  104. if (flags & FILEIO_O_CREAT)
  105. hflags |= O_CREAT;
  106. if (flags & FILEIO_O_EXCL)
  107. hflags |= O_EXCL;
  108. if (flags & FILEIO_O_TRUNC)
  109. hflags |= O_TRUNC;
  110. if (flags & FILEIO_O_APPEND)
  111. hflags |= O_APPEND;
  112. if (flags & FILEIO_O_RDONLY)
  113. hflags |= O_RDONLY;
  114. if (flags & FILEIO_O_WRONLY)
  115. hflags |= O_WRONLY;
  116. if (flags & FILEIO_O_RDWR)
  117. hflags |= O_RDWR;
  118. /* On systems supporting binary and text mode, always open files in
  119. binary mode. */
  120. #ifdef O_BINARY
  121. hflags |= O_BINARY;
  122. #endif
  123. return hflags;
  124. }
  125. static mode_t
  126. remote_fileio_mode_to_host (long mode, int open_call)
  127. {
  128. mode_t hmode = 0;
  129. if (!open_call)
  130. {
  131. if (mode & FILEIO_S_IFREG)
  132. hmode |= S_IFREG;
  133. if (mode & FILEIO_S_IFDIR)
  134. hmode |= S_IFDIR;
  135. if (mode & FILEIO_S_IFCHR)
  136. hmode |= S_IFCHR;
  137. }
  138. if (mode & FILEIO_S_IRUSR)
  139. hmode |= S_IRUSR;
  140. if (mode & FILEIO_S_IWUSR)
  141. hmode |= S_IWUSR;
  142. if (mode & FILEIO_S_IXUSR)
  143. hmode |= S_IXUSR;
  144. #ifdef S_IRGRP
  145. if (mode & FILEIO_S_IRGRP)
  146. hmode |= S_IRGRP;
  147. #endif
  148. #ifdef S_IWGRP
  149. if (mode & FILEIO_S_IWGRP)
  150. hmode |= S_IWGRP;
  151. #endif
  152. #ifdef S_IXGRP
  153. if (mode & FILEIO_S_IXGRP)
  154. hmode |= S_IXGRP;
  155. #endif
  156. if (mode & FILEIO_S_IROTH)
  157. hmode |= S_IROTH;
  158. #ifdef S_IWOTH
  159. if (mode & FILEIO_S_IWOTH)
  160. hmode |= S_IWOTH;
  161. #endif
  162. #ifdef S_IXOTH
  163. if (mode & FILEIO_S_IXOTH)
  164. hmode |= S_IXOTH;
  165. #endif
  166. return hmode;
  167. }
  168. static int
  169. remote_fileio_seek_flag_to_host (long num, int *flag)
  170. {
  171. if (!flag)
  172. return 0;
  173. switch (num)
  174. {
  175. case FILEIO_SEEK_SET:
  176. *flag = SEEK_SET;
  177. break;
  178. case FILEIO_SEEK_CUR:
  179. *flag = SEEK_CUR;
  180. break;
  181. case FILEIO_SEEK_END:
  182. *flag = SEEK_END;
  183. break;
  184. default:
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. static int
  190. remote_fileio_extract_long (char **buf, LONGEST *retlong)
  191. {
  192. char *c;
  193. int sign = 1;
  194. if (!buf || !*buf || !**buf || !retlong)
  195. return -1;
  196. c = strchr (*buf, ',');
  197. if (c)
  198. *c++ = '\0';
  199. else
  200. c = strchr (*buf, '\0');
  201. while (strchr ("+-", **buf))
  202. {
  203. if (**buf == '-')
  204. sign = -sign;
  205. ++*buf;
  206. }
  207. for (*retlong = 0; **buf; ++*buf)
  208. {
  209. *retlong <<= 4;
  210. if (**buf >= '0' && **buf <= '9')
  211. *retlong += **buf - '0';
  212. else if (**buf >= 'a' && **buf <= 'f')
  213. *retlong += **buf - 'a' + 10;
  214. else if (**buf >= 'A' && **buf <= 'F')
  215. *retlong += **buf - 'A' + 10;
  216. else
  217. return -1;
  218. }
  219. *retlong *= sign;
  220. *buf = c;
  221. return 0;
  222. }
  223. static int
  224. remote_fileio_extract_int (char **buf, long *retint)
  225. {
  226. int ret;
  227. LONGEST retlong;
  228. if (!retint)
  229. return -1;
  230. ret = remote_fileio_extract_long (buf, &retlong);
  231. if (!ret)
  232. *retint = (long) retlong;
  233. return ret;
  234. }
  235. static int
  236. remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
  237. {
  238. char *c;
  239. LONGEST retlong;
  240. if (!buf || !*buf || !**buf || !ptrval || !length)
  241. return -1;
  242. c = strchr (*buf, '/');
  243. if (!c)
  244. return -1;
  245. *c++ = '\0';
  246. if (remote_fileio_extract_long (buf, &retlong))
  247. return -1;
  248. *ptrval = (CORE_ADDR) retlong;
  249. *buf = c;
  250. if (remote_fileio_extract_long (buf, &retlong))
  251. return -1;
  252. *length = (int) retlong;
  253. return 0;
  254. }
  255. static void
  256. remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
  257. {
  258. host_to_bigendian (num, (char *) fnum, 8);
  259. }
  260. static void
  261. remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
  262. {
  263. host_to_fileio_time (tv->tv_sec, ftv->ftv_sec);
  264. remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
  265. }
  266. /* The quit handler originally installed. */
  267. static quit_handler_ftype *remote_fileio_o_quit_handler;
  268. /* What to do on a QUIT call while handling a file I/O request. We
  269. throw a quit exception, which is caught by remote_fileio_request
  270. and translated to an EINTR reply back to the target. */
  271. static void
  272. remote_fileio_quit_handler (void)
  273. {
  274. if (check_quit_flag ())
  275. quit ();
  276. }
  277. static void
  278. remote_fileio_reply (remote_target *remote, int retcode, int error)
  279. {
  280. char buf[32];
  281. int ctrl_c = check_quit_flag ();
  282. strcpy (buf, "F");
  283. if (retcode < 0)
  284. {
  285. strcat (buf, "-");
  286. retcode = -retcode;
  287. }
  288. sprintf (buf + strlen (buf), "%x", retcode);
  289. if (error || ctrl_c)
  290. {
  291. if (error && ctrl_c)
  292. error = FILEIO_EINTR;
  293. if (error < 0)
  294. {
  295. strcat (buf, "-");
  296. error = -error;
  297. }
  298. sprintf (buf + strlen (buf), ",%x", error);
  299. if (ctrl_c)
  300. strcat (buf, ",C");
  301. }
  302. quit_handler = remote_fileio_o_quit_handler;
  303. putpkt (remote, buf);
  304. }
  305. static void
  306. remote_fileio_ioerror (remote_target *remote)
  307. {
  308. remote_fileio_reply (remote, -1, FILEIO_EIO);
  309. }
  310. static void
  311. remote_fileio_badfd (remote_target *remote)
  312. {
  313. remote_fileio_reply (remote, -1, FILEIO_EBADF);
  314. }
  315. static void
  316. remote_fileio_return_errno (remote_target *remote, int retcode)
  317. {
  318. remote_fileio_reply (remote, retcode, retcode < 0
  319. ? host_to_fileio_error (errno) : 0);
  320. }
  321. static void
  322. remote_fileio_return_success (remote_target *remote, int retcode)
  323. {
  324. remote_fileio_reply (remote, retcode, 0);
  325. }
  326. static void
  327. remote_fileio_func_open (remote_target *remote, char *buf)
  328. {
  329. CORE_ADDR ptrval;
  330. int length;
  331. long num;
  332. int flags, fd;
  333. mode_t mode;
  334. char *pathname;
  335. struct stat st;
  336. /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
  337. if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
  338. {
  339. remote_fileio_ioerror (remote);
  340. return;
  341. }
  342. /* 2. Parameter: open flags */
  343. if (remote_fileio_extract_int (&buf, &num))
  344. {
  345. remote_fileio_ioerror (remote);
  346. return;
  347. }
  348. flags = remote_fileio_oflags_to_host (num);
  349. /* 3. Parameter: open mode */
  350. if (remote_fileio_extract_int (&buf, &num))
  351. {
  352. remote_fileio_ioerror (remote);
  353. return;
  354. }
  355. mode = remote_fileio_mode_to_host (num, 1);
  356. /* Request pathname. */
  357. pathname = (char *) alloca (length);
  358. if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
  359. {
  360. remote_fileio_ioerror (remote);
  361. return;
  362. }
  363. /* Check if pathname exists and is not a regular file or directory. If so,
  364. return an appropriate error code. Same for trying to open directories
  365. for writing. */
  366. if (!stat (pathname, &st))
  367. {
  368. if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
  369. {
  370. remote_fileio_reply (remote, -1, FILEIO_ENODEV);
  371. return;
  372. }
  373. if (S_ISDIR (st.st_mode)
  374. && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
  375. {
  376. remote_fileio_reply (remote, -1, FILEIO_EISDIR);
  377. return;
  378. }
  379. }
  380. fd = gdb_open_cloexec (pathname, flags, mode).release ();
  381. if (fd < 0)
  382. {
  383. remote_fileio_return_errno (remote, -1);
  384. return;
  385. }
  386. fd = remote_fileio_fd_to_targetfd (fd);
  387. remote_fileio_return_success (remote, fd);
  388. }
  389. static void
  390. remote_fileio_func_close (remote_target *remote, char *buf)
  391. {
  392. long num;
  393. int fd;
  394. /* Parameter: file descriptor */
  395. if (remote_fileio_extract_int (&buf, &num))
  396. {
  397. remote_fileio_ioerror (remote);
  398. return;
  399. }
  400. fd = remote_fileio_map_fd ((int) num);
  401. if (fd == FIO_FD_INVALID)
  402. {
  403. remote_fileio_badfd (remote);
  404. return;
  405. }
  406. if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
  407. remote_fileio_return_errno (remote, -1);
  408. remote_fileio_close_target_fd ((int) num);
  409. remote_fileio_return_success (remote, 0);
  410. }
  411. static void
  412. remote_fileio_func_read (remote_target *remote, char *buf)
  413. {
  414. long target_fd, num;
  415. LONGEST lnum;
  416. CORE_ADDR ptrval;
  417. int fd, ret;
  418. gdb_byte *buffer;
  419. size_t length;
  420. off_t old_offset, new_offset;
  421. /* 1. Parameter: file descriptor */
  422. if (remote_fileio_extract_int (&buf, &target_fd))
  423. {
  424. remote_fileio_ioerror (remote);
  425. return;
  426. }
  427. fd = remote_fileio_map_fd ((int) target_fd);
  428. if (fd == FIO_FD_INVALID)
  429. {
  430. remote_fileio_badfd (remote);
  431. return;
  432. }
  433. /* 2. Parameter: buffer pointer */
  434. if (remote_fileio_extract_long (&buf, &lnum))
  435. {
  436. remote_fileio_ioerror (remote);
  437. return;
  438. }
  439. ptrval = (CORE_ADDR) lnum;
  440. /* 3. Parameter: buffer length */
  441. if (remote_fileio_extract_int (&buf, &num))
  442. {
  443. remote_fileio_ioerror (remote);
  444. return;
  445. }
  446. length = (size_t) num;
  447. switch (fd)
  448. {
  449. case FIO_FD_CONSOLE_OUT:
  450. remote_fileio_badfd (remote);
  451. return;
  452. case FIO_FD_CONSOLE_IN:
  453. {
  454. static char *remaining_buf = NULL;
  455. static int remaining_length = 0;
  456. buffer = (gdb_byte *) xmalloc (16384);
  457. if (remaining_buf)
  458. {
  459. if (remaining_length > length)
  460. {
  461. memcpy (buffer, remaining_buf, length);
  462. memmove (remaining_buf, remaining_buf + length,
  463. remaining_length - length);
  464. remaining_length -= length;
  465. ret = length;
  466. }
  467. else
  468. {
  469. memcpy (buffer, remaining_buf, remaining_length);
  470. xfree (remaining_buf);
  471. remaining_buf = NULL;
  472. ret = remaining_length;
  473. }
  474. }
  475. else
  476. {
  477. /* Windows (at least XP and Server 2003) has difficulty
  478. with large reads from consoles. If a handle is
  479. backed by a real console device, overly large reads
  480. from the handle will fail and set errno == ENOMEM.
  481. On a Windows Server 2003 system where I tested,
  482. reading 26608 bytes from the console was OK, but
  483. anything above 26609 bytes would fail. The limit has
  484. been observed to vary on different systems. So, we
  485. limit this read to something smaller than that - by a
  486. safe margin, in case the limit depends on system
  487. resources or version. */
  488. ret = gdb_stdtargin->read ((char *) buffer, 16383);
  489. if (ret > 0 && (size_t)ret > length)
  490. {
  491. remaining_buf = (char *) xmalloc (ret - length);
  492. remaining_length = ret - length;
  493. memcpy (remaining_buf, buffer + length, remaining_length);
  494. ret = length;
  495. }
  496. }
  497. }
  498. break;
  499. default:
  500. buffer = (gdb_byte *) xmalloc (length);
  501. /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
  502. for read() to return -1 even if "some" bytes have been read. It
  503. has been corrected in SUSv2 but that doesn't help us much...
  504. Therefore a complete solution must check how many bytes have been
  505. read on EINTR to return a more reliable value to the target */
  506. old_offset = lseek (fd, 0, SEEK_CUR);
  507. ret = read (fd, buffer, length);
  508. if (ret < 0 && errno == EINTR)
  509. {
  510. new_offset = lseek (fd, 0, SEEK_CUR);
  511. /* If some data has been read, return the number of bytes read.
  512. The Ctrl-C flag is set in remote_fileio_reply() anyway. */
  513. if (old_offset != new_offset)
  514. ret = new_offset - old_offset;
  515. }
  516. break;
  517. }
  518. if (ret > 0)
  519. {
  520. errno = target_write_memory (ptrval, buffer, ret);
  521. if (errno != 0)
  522. ret = -1;
  523. }
  524. if (ret < 0)
  525. remote_fileio_return_errno (remote, -1);
  526. else
  527. remote_fileio_return_success (remote, ret);
  528. xfree (buffer);
  529. }
  530. static void
  531. remote_fileio_func_write (remote_target *remote, char *buf)
  532. {
  533. long target_fd, num;
  534. LONGEST lnum;
  535. CORE_ADDR ptrval;
  536. int fd, ret;
  537. gdb_byte *buffer;
  538. size_t length;
  539. /* 1. Parameter: file descriptor */
  540. if (remote_fileio_extract_int (&buf, &target_fd))
  541. {
  542. remote_fileio_ioerror (remote);
  543. return;
  544. }
  545. fd = remote_fileio_map_fd ((int) target_fd);
  546. if (fd == FIO_FD_INVALID)
  547. {
  548. remote_fileio_badfd (remote);
  549. return;
  550. }
  551. /* 2. Parameter: buffer pointer */
  552. if (remote_fileio_extract_long (&buf, &lnum))
  553. {
  554. remote_fileio_ioerror (remote);
  555. return;
  556. }
  557. ptrval = (CORE_ADDR) lnum;
  558. /* 3. Parameter: buffer length */
  559. if (remote_fileio_extract_int (&buf, &num))
  560. {
  561. remote_fileio_ioerror (remote);
  562. return;
  563. }
  564. length = (size_t) num;
  565. buffer = (gdb_byte *) xmalloc (length);
  566. if (target_read_memory (ptrval, buffer, length) != 0)
  567. {
  568. xfree (buffer);
  569. remote_fileio_ioerror (remote);
  570. return;
  571. }
  572. switch (fd)
  573. {
  574. case FIO_FD_CONSOLE_IN:
  575. remote_fileio_badfd (remote);
  576. xfree (buffer);
  577. return;
  578. case FIO_FD_CONSOLE_OUT:
  579. {
  580. ui_file *file = target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr;
  581. file->write ((char *) buffer, length);
  582. file->flush ();
  583. ret = length;
  584. }
  585. break;
  586. default:
  587. ret = write (fd, buffer, length);
  588. if (ret < 0 && errno == EACCES)
  589. errno = EBADF; /* Cygwin returns EACCESS when writing to a
  590. R/O file. */
  591. break;
  592. }
  593. if (ret < 0)
  594. remote_fileio_return_errno (remote, -1);
  595. else
  596. remote_fileio_return_success (remote, ret);
  597. xfree (buffer);
  598. }
  599. static void
  600. remote_fileio_func_lseek (remote_target *remote, char *buf)
  601. {
  602. long num;
  603. LONGEST lnum;
  604. int fd, flag;
  605. off_t offset, ret;
  606. /* 1. Parameter: file descriptor */
  607. if (remote_fileio_extract_int (&buf, &num))
  608. {
  609. remote_fileio_ioerror (remote);
  610. return;
  611. }
  612. fd = remote_fileio_map_fd ((int) num);
  613. if (fd == FIO_FD_INVALID)
  614. {
  615. remote_fileio_badfd (remote);
  616. return;
  617. }
  618. else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
  619. {
  620. remote_fileio_reply (remote, -1, FILEIO_ESPIPE);
  621. return;
  622. }
  623. /* 2. Parameter: offset */
  624. if (remote_fileio_extract_long (&buf, &lnum))
  625. {
  626. remote_fileio_ioerror (remote);
  627. return;
  628. }
  629. offset = (off_t) lnum;
  630. /* 3. Parameter: flag */
  631. if (remote_fileio_extract_int (&buf, &num))
  632. {
  633. remote_fileio_ioerror (remote);
  634. return;
  635. }
  636. if (remote_fileio_seek_flag_to_host (num, &flag))
  637. {
  638. remote_fileio_reply (remote, -1, FILEIO_EINVAL);
  639. return;
  640. }
  641. ret = lseek (fd, offset, flag);
  642. if (ret == (off_t) -1)
  643. remote_fileio_return_errno (remote, -1);
  644. else
  645. remote_fileio_return_success (remote, ret);
  646. }
  647. static void
  648. remote_fileio_func_rename (remote_target *remote, char *buf)
  649. {
  650. CORE_ADDR old_ptr, new_ptr;
  651. int old_len, new_len;
  652. char *oldpath, *newpath;
  653. int ret, of, nf;
  654. struct stat ost, nst;
  655. /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
  656. if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
  657. {
  658. remote_fileio_ioerror (remote);
  659. return;
  660. }
  661. /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
  662. if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
  663. {
  664. remote_fileio_ioerror (remote);
  665. return;
  666. }
  667. /* Request oldpath using 'm' packet */
  668. oldpath = (char *) alloca (old_len);
  669. if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
  670. {
  671. remote_fileio_ioerror (remote);
  672. return;
  673. }
  674. /* Request newpath using 'm' packet */
  675. newpath = (char *) alloca (new_len);
  676. if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
  677. {
  678. remote_fileio_ioerror (remote);
  679. return;
  680. }
  681. /* Only operate on regular files and directories. */
  682. of = stat (oldpath, &ost);
  683. nf = stat (newpath, &nst);
  684. if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
  685. || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
  686. {
  687. remote_fileio_reply (remote, -1, FILEIO_EACCES);
  688. return;
  689. }
  690. ret = rename (oldpath, newpath);
  691. if (ret == -1)
  692. {
  693. /* Special case: newpath is a non-empty directory. Some systems
  694. return ENOTEMPTY, some return EEXIST. We coerce that to be
  695. always EEXIST. */
  696. if (errno == ENOTEMPTY)
  697. errno = EEXIST;
  698. #ifdef __CYGWIN__
  699. /* Workaround some Cygwin problems with correct errnos. */
  700. if (errno == EACCES)
  701. {
  702. if (!of && !nf && S_ISDIR (nst.st_mode))
  703. {
  704. if (S_ISREG (ost.st_mode))
  705. errno = EISDIR;
  706. else
  707. {
  708. char oldfullpath[PATH_MAX];
  709. char newfullpath[PATH_MAX];
  710. int len;
  711. cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
  712. PATH_MAX);
  713. cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
  714. PATH_MAX);
  715. len = strlen (oldfullpath);
  716. if (IS_DIR_SEPARATOR (newfullpath[len])
  717. && !filename_ncmp (oldfullpath, newfullpath, len))
  718. errno = EINVAL;
  719. else
  720. errno = EEXIST;
  721. }
  722. }
  723. }
  724. #endif
  725. remote_fileio_return_errno (remote, -1);
  726. }
  727. else
  728. remote_fileio_return_success (remote, ret);
  729. }
  730. static void
  731. remote_fileio_func_unlink (remote_target *remote, char *buf)
  732. {
  733. CORE_ADDR ptrval;
  734. int length;
  735. char *pathname;
  736. int ret;
  737. struct stat st;
  738. /* Parameter: Ptr to pathname / length incl. trailing zero */
  739. if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
  740. {
  741. remote_fileio_ioerror (remote);
  742. return;
  743. }
  744. /* Request pathname using 'm' packet */
  745. pathname = (char *) alloca (length);
  746. if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
  747. {
  748. remote_fileio_ioerror (remote);
  749. return;
  750. }
  751. /* Only operate on regular files (and directories, which allows to return
  752. the correct return code). */
  753. if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
  754. {
  755. remote_fileio_reply (remote, -1, FILEIO_ENODEV);
  756. return;
  757. }
  758. ret = unlink (pathname);
  759. if (ret == -1)
  760. remote_fileio_return_errno (remote, -1);
  761. else
  762. remote_fileio_return_success (remote, ret);
  763. }
  764. static void
  765. remote_fileio_func_stat (remote_target *remote, char *buf)
  766. {
  767. CORE_ADDR statptr, nameptr;
  768. int ret, namelength;
  769. char *pathname;
  770. LONGEST lnum;
  771. struct stat st;
  772. struct fio_stat fst;
  773. /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
  774. if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
  775. {
  776. remote_fileio_ioerror (remote);
  777. return;
  778. }
  779. /* 2. Parameter: Ptr to struct stat */
  780. if (remote_fileio_extract_long (&buf, &lnum))
  781. {
  782. remote_fileio_ioerror (remote);
  783. return;
  784. }
  785. statptr = (CORE_ADDR) lnum;
  786. /* Request pathname using 'm' packet */
  787. pathname = (char *) alloca (namelength);
  788. if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
  789. {
  790. remote_fileio_ioerror (remote);
  791. return;
  792. }
  793. ret = stat (pathname, &st);
  794. if (ret == -1)
  795. {
  796. remote_fileio_return_errno (remote, -1);
  797. return;
  798. }
  799. /* Only operate on regular files and directories. */
  800. if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
  801. {
  802. remote_fileio_reply (remote, -1, FILEIO_EACCES);
  803. return;
  804. }
  805. if (statptr)
  806. {
  807. host_to_fileio_stat (&st, &fst);
  808. host_to_fileio_uint (0, fst.fst_dev);
  809. errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
  810. if (errno != 0)
  811. {
  812. remote_fileio_return_errno (remote, -1);
  813. return;
  814. }
  815. }
  816. remote_fileio_return_success (remote, ret);
  817. }
  818. static void
  819. remote_fileio_func_fstat (remote_target *remote, char *buf)
  820. {
  821. CORE_ADDR ptrval;
  822. int fd, ret;
  823. long target_fd;
  824. LONGEST lnum;
  825. struct stat st;
  826. struct fio_stat fst;
  827. struct timeval tv;
  828. /* 1. Parameter: file descriptor */
  829. if (remote_fileio_extract_int (&buf, &target_fd))
  830. {
  831. remote_fileio_ioerror (remote);
  832. return;
  833. }
  834. fd = remote_fileio_map_fd ((int) target_fd);
  835. if (fd == FIO_FD_INVALID)
  836. {
  837. remote_fileio_badfd (remote);
  838. return;
  839. }
  840. /* 2. Parameter: Ptr to struct stat */
  841. if (remote_fileio_extract_long (&buf, &lnum))
  842. {
  843. remote_fileio_ioerror (remote);
  844. return;
  845. }
  846. ptrval = (CORE_ADDR) lnum;
  847. if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
  848. {
  849. host_to_fileio_uint (1, fst.fst_dev);
  850. memset (&st, 0, sizeof (st));
  851. st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
  852. st.st_nlink = 1;
  853. #ifdef HAVE_GETUID
  854. st.st_uid = getuid ();
  855. #endif
  856. #ifdef HAVE_GETGID
  857. st.st_gid = getgid ();
  858. #endif
  859. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  860. st.st_blksize = 512;
  861. #endif
  862. #if HAVE_STRUCT_STAT_ST_BLOCKS
  863. st.st_blocks = 0;
  864. #endif
  865. if (!gettimeofday (&tv, NULL))
  866. st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
  867. else
  868. st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
  869. ret = 0;
  870. }
  871. else
  872. ret = fstat (fd, &st);
  873. if (ret == -1)
  874. {
  875. remote_fileio_return_errno (remote, -1);
  876. return;
  877. }
  878. if (ptrval)
  879. {
  880. host_to_fileio_stat (&st, &fst);
  881. errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
  882. if (errno != 0)
  883. {
  884. remote_fileio_return_errno (remote, -1);
  885. return;
  886. }
  887. }
  888. remote_fileio_return_success (remote, ret);
  889. }
  890. static void
  891. remote_fileio_func_gettimeofday (remote_target *remote, char *buf)
  892. {
  893. LONGEST lnum;
  894. CORE_ADDR ptrval;
  895. int ret;
  896. struct timeval tv;
  897. struct fio_timeval ftv;
  898. /* 1. Parameter: struct timeval pointer */
  899. if (remote_fileio_extract_long (&buf, &lnum))
  900. {
  901. remote_fileio_ioerror (remote);
  902. return;
  903. }
  904. ptrval = (CORE_ADDR) lnum;
  905. /* 2. Parameter: some pointer value... */
  906. if (remote_fileio_extract_long (&buf, &lnum))
  907. {
  908. remote_fileio_ioerror (remote);
  909. return;
  910. }
  911. /* ...which has to be NULL. */
  912. if (lnum)
  913. {
  914. remote_fileio_reply (remote, -1, FILEIO_EINVAL);
  915. return;
  916. }
  917. ret = gettimeofday (&tv, NULL);
  918. if (ret == -1)
  919. {
  920. remote_fileio_return_errno (remote, -1);
  921. return;
  922. }
  923. if (ptrval)
  924. {
  925. remote_fileio_to_fio_timeval (&tv, &ftv);
  926. errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
  927. if (errno != 0)
  928. {
  929. remote_fileio_return_errno (remote, -1);
  930. return;
  931. }
  932. }
  933. remote_fileio_return_success (remote, ret);
  934. }
  935. static void
  936. remote_fileio_func_isatty (remote_target *remote, char *buf)
  937. {
  938. long target_fd;
  939. int fd;
  940. /* Parameter: file descriptor */
  941. if (remote_fileio_extract_int (&buf, &target_fd))
  942. {
  943. remote_fileio_ioerror (remote);
  944. return;
  945. }
  946. fd = remote_fileio_map_fd ((int) target_fd);
  947. int ret = fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT ? 1 : 0;
  948. remote_fileio_return_success (remote, ret);
  949. }
  950. static void
  951. remote_fileio_func_system (remote_target *remote, char *buf)
  952. {
  953. CORE_ADDR ptrval;
  954. int ret, length;
  955. char *cmdline = NULL;
  956. /* Parameter: Ptr to commandline / length incl. trailing zero */
  957. if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
  958. {
  959. remote_fileio_ioerror (remote);
  960. return;
  961. }
  962. if (length)
  963. {
  964. /* Request commandline using 'm' packet */
  965. cmdline = (char *) alloca (length);
  966. if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
  967. {
  968. remote_fileio_ioerror (remote);
  969. return;
  970. }
  971. }
  972. /* Check if system(3) has been explicitly allowed using the
  973. `set remote system-call-allowed 1' command. If length is 0,
  974. indicating a NULL parameter to the system call, return zero to
  975. indicate a shell is not available. Otherwise fail with EPERM. */
  976. if (!remote_fio_system_call_allowed)
  977. {
  978. if (!length)
  979. remote_fileio_return_success (remote, 0);
  980. else
  981. remote_fileio_reply (remote, -1, FILEIO_EPERM);
  982. return;
  983. }
  984. ret = system (cmdline);
  985. if (!length)
  986. remote_fileio_return_success (remote, ret);
  987. else if (ret == -1)
  988. remote_fileio_return_errno (remote, -1);
  989. else
  990. remote_fileio_return_success (remote, WEXITSTATUS (ret));
  991. }
  992. static struct {
  993. const char *name;
  994. void (*func)(remote_target *remote, char *);
  995. } remote_fio_func_map[] = {
  996. { "open", remote_fileio_func_open },
  997. { "close", remote_fileio_func_close },
  998. { "read", remote_fileio_func_read },
  999. { "write", remote_fileio_func_write },
  1000. { "lseek", remote_fileio_func_lseek },
  1001. { "rename", remote_fileio_func_rename },
  1002. { "unlink", remote_fileio_func_unlink },
  1003. { "stat", remote_fileio_func_stat },
  1004. { "fstat", remote_fileio_func_fstat },
  1005. { "gettimeofday", remote_fileio_func_gettimeofday },
  1006. { "isatty", remote_fileio_func_isatty },
  1007. { "system", remote_fileio_func_system },
  1008. { NULL, NULL }
  1009. };
  1010. static void
  1011. do_remote_fileio_request (remote_target *remote, char *buf)
  1012. {
  1013. char *c;
  1014. int idx;
  1015. quit_handler = remote_fileio_quit_handler;
  1016. c = strchr (++buf, ',');
  1017. if (c)
  1018. *c++ = '\0';
  1019. else
  1020. c = strchr (buf, '\0');
  1021. for (idx = 0; remote_fio_func_map[idx].name; ++idx)
  1022. if (!strcmp (remote_fio_func_map[idx].name, buf))
  1023. break;
  1024. if (!remote_fio_func_map[idx].name)
  1025. remote_fileio_reply (remote, -1, FILEIO_ENOSYS);
  1026. else
  1027. remote_fio_func_map[idx].func (remote, c);
  1028. }
  1029. /* Close any open descriptors, and reinitialize the file mapping. */
  1030. void
  1031. remote_fileio_reset (void)
  1032. {
  1033. int ix;
  1034. for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
  1035. {
  1036. int fd = remote_fio_data.fd_map[ix];
  1037. if (fd >= 0)
  1038. close (fd);
  1039. }
  1040. if (remote_fio_data.fd_map)
  1041. {
  1042. xfree (remote_fio_data.fd_map);
  1043. remote_fio_data.fd_map = NULL;
  1044. remote_fio_data.fd_map_size = 0;
  1045. }
  1046. }
  1047. /* Handle a file I/O request. BUF points to the packet containing the
  1048. request. CTRLC_PENDING_P should be nonzero if the target has not
  1049. acknowledged the Ctrl-C sent asynchronously earlier. */
  1050. void
  1051. remote_fileio_request (remote_target *remote, char *buf, int ctrlc_pending_p)
  1052. {
  1053. /* Save the previous quit handler, so we can restore it. No need
  1054. for a cleanup since we catch all exceptions below. Note that the
  1055. quit handler is also restored by remote_fileio_reply just before
  1056. pushing a packet. */
  1057. remote_fileio_o_quit_handler = quit_handler;
  1058. if (ctrlc_pending_p)
  1059. {
  1060. /* If the target hasn't responded to the Ctrl-C sent
  1061. asynchronously earlier, take this opportunity to send the
  1062. Ctrl-C synchronously. */
  1063. set_quit_flag ();
  1064. remote_fileio_reply (remote, -1, FILEIO_EINTR);
  1065. }
  1066. else
  1067. {
  1068. try
  1069. {
  1070. do_remote_fileio_request (remote, buf);
  1071. }
  1072. catch (const gdb_exception &ex)
  1073. {
  1074. if (ex.reason == RETURN_QUIT)
  1075. remote_fileio_reply (remote, -1, FILEIO_EINTR);
  1076. else
  1077. remote_fileio_reply (remote, -1, FILEIO_EIO);
  1078. }
  1079. }
  1080. quit_handler = remote_fileio_o_quit_handler;
  1081. }
  1082. /* Unpack an fio_uint_t. */
  1083. static unsigned int
  1084. remote_fileio_to_host_uint (fio_uint_t fnum)
  1085. {
  1086. return extract_unsigned_integer ((gdb_byte *) fnum, 4,
  1087. BFD_ENDIAN_BIG);
  1088. }
  1089. /* Unpack an fio_ulong_t. */
  1090. static ULONGEST
  1091. remote_fileio_to_host_ulong (fio_ulong_t fnum)
  1092. {
  1093. return extract_unsigned_integer ((gdb_byte *) fnum, 8,
  1094. BFD_ENDIAN_BIG);
  1095. }
  1096. /* Unpack an fio_mode_t. */
  1097. static mode_t
  1098. remote_fileio_to_host_mode (fio_mode_t fnum)
  1099. {
  1100. return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
  1101. 0);
  1102. }
  1103. /* Unpack an fio_time_t. */
  1104. static time_t
  1105. remote_fileio_to_host_time (fio_time_t fnum)
  1106. {
  1107. return remote_fileio_to_host_uint (fnum);
  1108. }
  1109. /* See remote-fileio.h. */
  1110. void
  1111. remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
  1112. {
  1113. memset (st, 0, sizeof (struct stat));
  1114. st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
  1115. st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
  1116. st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
  1117. st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
  1118. st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
  1119. st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
  1120. st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
  1121. st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
  1122. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  1123. st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
  1124. #endif
  1125. #if HAVE_STRUCT_STAT_ST_BLOCKS
  1126. st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
  1127. #endif
  1128. st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
  1129. st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
  1130. st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
  1131. }
  1132. static void
  1133. set_system_call_allowed (const char *args, int from_tty)
  1134. {
  1135. if (args)
  1136. {
  1137. char *arg_end;
  1138. int val = strtoul (args, &arg_end, 10);
  1139. if (*args && *arg_end == '\0')
  1140. {
  1141. remote_fio_system_call_allowed = !!val;
  1142. return;
  1143. }
  1144. }
  1145. error (_("Illegal argument for \"set remote system-call-allowed\" command"));
  1146. }
  1147. static void
  1148. show_system_call_allowed (const char *args, int from_tty)
  1149. {
  1150. if (args)
  1151. error (_("Garbage after \"show remote "
  1152. "system-call-allowed\" command: `%s'"), args);
  1153. gdb_printf ("Calling host system(3) call from target is %sallowed\n",
  1154. remote_fio_system_call_allowed ? "" : "not ");
  1155. }
  1156. void
  1157. initialize_remote_fileio (struct cmd_list_element **remote_set_cmdlist,
  1158. struct cmd_list_element **remote_show_cmdlist)
  1159. {
  1160. add_cmd ("system-call-allowed", no_class,
  1161. set_system_call_allowed,
  1162. _("Set if the host system(3) call is allowed for the target."),
  1163. remote_set_cmdlist);
  1164. add_cmd ("system-call-allowed", no_class,
  1165. show_system_call_allowed,
  1166. _("Show if the host system(3) call is allowed for the target."),
  1167. remote_show_cmdlist);
  1168. }