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