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