Commit | Line | Data |
---|---|---|
a6b151f1 | 1 | /* Host file transfer support for gdbserver. |
32d0add0 | 2 | Copyright (C) 2007-2015 Free Software Foundation, Inc. |
a6b151f1 DJ |
3 | |
4 | Contributed by CodeSourcery. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
32de4b9d | 10 | the Free Software Foundation; either version 3 of the License, or |
a6b151f1 DJ |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
32de4b9d | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
a6b151f1 DJ |
20 | |
21 | #include "server.h" | |
22 | #include "gdb/fileio.h" | |
533b0600 | 23 | #include "hostio.h" |
a6b151f1 | 24 | |
a6b151f1 DJ |
25 | #include <fcntl.h> |
26 | #include <limits.h> | |
27 | #include <unistd.h> | |
aa9e327f GB |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | |
7823a941 | 30 | #include "fileio.h" |
a6b151f1 DJ |
31 | |
32 | extern int remote_debug; | |
33 | ||
34 | struct fd_list | |
35 | { | |
36 | int fd; | |
37 | struct fd_list *next; | |
38 | }; | |
39 | ||
40 | static struct fd_list *open_fds; | |
41 | ||
42 | static int | |
43 | safe_fromhex (char a, int *nibble) | |
44 | { | |
45 | if (a >= '0' && a <= '9') | |
46 | *nibble = a - '0'; | |
47 | else if (a >= 'a' && a <= 'f') | |
48 | *nibble = a - 'a' + 10; | |
49 | else if (a >= 'A' && a <= 'F') | |
50 | *nibble = a - 'A' + 10; | |
51 | else | |
52 | return -1; | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
d5749ee7 PA |
57 | /* Filenames are hex encoded, so the maximum we can handle is half the |
58 | packet buffer size. Cap to PATH_MAX, if it is shorter. */ | |
59 | #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1)) | |
60 | # define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1) | |
61 | #else | |
62 | # define HOSTIO_PATH_MAX PATH_MAX | |
63 | #endif | |
64 | ||
a6b151f1 DJ |
65 | static int |
66 | require_filename (char **pp, char *filename) | |
67 | { | |
68 | int count; | |
69 | char *p; | |
70 | ||
71 | p = *pp; | |
72 | count = 0; | |
73 | ||
74 | while (*p && *p != ',') | |
75 | { | |
76 | int nib1, nib2; | |
77 | ||
78 | /* Don't allow overflow. */ | |
d5749ee7 | 79 | if (count >= HOSTIO_PATH_MAX - 1) |
a6b151f1 DJ |
80 | return -1; |
81 | ||
82 | if (safe_fromhex (p[0], &nib1) | |
83 | || safe_fromhex (p[1], &nib2)) | |
84 | return -1; | |
85 | ||
86 | filename[count++] = nib1 * 16 + nib2; | |
87 | p += 2; | |
88 | } | |
89 | ||
90 | filename[count] = '\0'; | |
91 | *pp = p; | |
92 | return 0; | |
93 | } | |
94 | ||
95 | static int | |
96 | require_int (char **pp, int *value) | |
97 | { | |
98 | char *p; | |
99 | int count; | |
100 | ||
101 | p = *pp; | |
102 | *value = 0; | |
103 | count = 0; | |
104 | ||
105 | while (*p && *p != ',') | |
106 | { | |
107 | int nib; | |
108 | ||
109 | /* Don't allow overflow. */ | |
110 | if (count >= 7) | |
111 | return -1; | |
112 | ||
113 | if (safe_fromhex (p[0], &nib)) | |
114 | return -1; | |
115 | *value = *value * 16 + nib; | |
116 | p++; | |
117 | count++; | |
118 | } | |
119 | ||
120 | *pp = p; | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static int | |
125 | require_data (char *p, int p_len, char **data, int *data_len) | |
126 | { | |
127 | int input_index, output_index, escaped; | |
128 | ||
bca929d3 | 129 | *data = xmalloc (p_len); |
a6b151f1 DJ |
130 | |
131 | output_index = 0; | |
132 | escaped = 0; | |
133 | for (input_index = 0; input_index < p_len; input_index++) | |
134 | { | |
135 | char b = p[input_index]; | |
136 | ||
137 | if (escaped) | |
138 | { | |
139 | (*data)[output_index++] = b ^ 0x20; | |
140 | escaped = 0; | |
141 | } | |
142 | else if (b == '}') | |
143 | escaped = 1; | |
144 | else | |
145 | (*data)[output_index++] = b; | |
146 | } | |
147 | ||
148 | if (escaped) | |
9130f83e | 149 | { |
8040bd49 | 150 | free (*data); |
9130f83e MS |
151 | return -1; |
152 | } | |
a6b151f1 DJ |
153 | |
154 | *data_len = output_index; | |
155 | return 0; | |
156 | } | |
157 | ||
158 | static int | |
159 | require_comma (char **pp) | |
160 | { | |
161 | if (**pp == ',') | |
162 | { | |
163 | (*pp)++; | |
164 | return 0; | |
165 | } | |
166 | else | |
167 | return -1; | |
168 | } | |
169 | ||
170 | static int | |
171 | require_end (char *p) | |
172 | { | |
173 | if (*p == '\0') | |
174 | return 0; | |
175 | else | |
176 | return -1; | |
177 | } | |
178 | ||
179 | static int | |
180 | require_valid_fd (int fd) | |
181 | { | |
182 | struct fd_list *fd_ptr; | |
183 | ||
184 | for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next) | |
185 | if (fd_ptr->fd == fd) | |
186 | return 0; | |
187 | ||
188 | return -1; | |
189 | } | |
190 | ||
59a016f0 PA |
191 | /* Fill in own_buf with the last hostio error packet, however it |
192 | suitable for the target. */ | |
a6b151f1 | 193 | static void |
59a016f0 | 194 | hostio_error (char *own_buf) |
a6b151f1 | 195 | { |
59a016f0 | 196 | the_target->hostio_last_error (own_buf); |
a6b151f1 DJ |
197 | } |
198 | ||
199 | static void | |
200 | hostio_packet_error (char *own_buf) | |
201 | { | |
59a016f0 | 202 | sprintf (own_buf, "F-1,%x", FILEIO_EINVAL); |
a6b151f1 DJ |
203 | } |
204 | ||
205 | static void | |
206 | hostio_reply (char *own_buf, int result) | |
207 | { | |
208 | sprintf (own_buf, "F%x", result); | |
209 | } | |
210 | ||
211 | static int | |
212 | hostio_reply_with_data (char *own_buf, char *buffer, int len, | |
213 | int *new_packet_len) | |
214 | { | |
215 | int input_index, output_index, out_maxlen; | |
216 | ||
217 | sprintf (own_buf, "F%x;", len); | |
218 | output_index = strlen (own_buf); | |
219 | ||
220 | out_maxlen = PBUFSIZ; | |
221 | ||
222 | for (input_index = 0; input_index < len; input_index++) | |
223 | { | |
224 | char b = buffer[input_index]; | |
225 | ||
226 | if (b == '$' || b == '#' || b == '}' || b == '*') | |
227 | { | |
228 | /* These must be escaped. */ | |
229 | if (output_index + 2 > out_maxlen) | |
230 | break; | |
231 | own_buf[output_index++] = '}'; | |
232 | own_buf[output_index++] = b ^ 0x20; | |
233 | } | |
234 | else | |
235 | { | |
236 | if (output_index + 1 > out_maxlen) | |
237 | break; | |
238 | own_buf[output_index++] = b; | |
239 | } | |
240 | } | |
241 | ||
242 | *new_packet_len = output_index; | |
243 | return input_index; | |
244 | } | |
245 | ||
246 | static int | |
247 | fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p) | |
248 | { | |
249 | int open_flags = 0; | |
250 | ||
251 | if (fileio_open_flags & ~FILEIO_O_SUPPORTED) | |
252 | return -1; | |
253 | ||
254 | if (fileio_open_flags & FILEIO_O_CREAT) | |
255 | open_flags |= O_CREAT; | |
256 | if (fileio_open_flags & FILEIO_O_EXCL) | |
257 | open_flags |= O_EXCL; | |
258 | if (fileio_open_flags & FILEIO_O_TRUNC) | |
259 | open_flags |= O_TRUNC; | |
260 | if (fileio_open_flags & FILEIO_O_APPEND) | |
261 | open_flags |= O_APPEND; | |
262 | if (fileio_open_flags & FILEIO_O_RDONLY) | |
263 | open_flags |= O_RDONLY; | |
264 | if (fileio_open_flags & FILEIO_O_WRONLY) | |
265 | open_flags |= O_WRONLY; | |
266 | if (fileio_open_flags & FILEIO_O_RDWR) | |
267 | open_flags |= O_RDWR; | |
268 | /* On systems supporting binary and text mode, always open files in | |
269 | binary mode. */ | |
270 | #ifdef O_BINARY | |
271 | open_flags |= O_BINARY; | |
272 | #endif | |
273 | ||
274 | *open_flags_p = open_flags; | |
275 | return 0; | |
276 | } | |
277 | ||
278 | static void | |
279 | handle_open (char *own_buf) | |
280 | { | |
d5749ee7 | 281 | char filename[HOSTIO_PATH_MAX]; |
a6b151f1 DJ |
282 | char *p; |
283 | int fileio_flags, mode, flags, fd; | |
284 | struct fd_list *new_fd; | |
285 | ||
286 | p = own_buf + strlen ("vFile:open:"); | |
287 | ||
288 | if (require_filename (&p, filename) | |
289 | || require_comma (&p) | |
290 | || require_int (&p, &fileio_flags) | |
291 | || require_comma (&p) | |
292 | || require_int (&p, &mode) | |
293 | || require_end (p) | |
294 | || fileio_open_flags_to_host (fileio_flags, &flags)) | |
295 | { | |
296 | hostio_packet_error (own_buf); | |
297 | return; | |
298 | } | |
299 | ||
300 | /* We do not need to convert MODE, since the fileio protocol | |
301 | uses the standard values. */ | |
302 | fd = open (filename, flags, mode); | |
303 | ||
304 | if (fd == -1) | |
305 | { | |
59a016f0 | 306 | hostio_error (own_buf); |
a6b151f1 DJ |
307 | return; |
308 | } | |
309 | ||
310 | /* Record the new file descriptor. */ | |
bca929d3 | 311 | new_fd = xmalloc (sizeof (struct fd_list)); |
a6b151f1 DJ |
312 | new_fd->fd = fd; |
313 | new_fd->next = open_fds; | |
314 | open_fds = new_fd; | |
315 | ||
316 | hostio_reply (own_buf, fd); | |
317 | } | |
318 | ||
319 | static void | |
320 | handle_pread (char *own_buf, int *new_packet_len) | |
321 | { | |
322 | int fd, ret, len, offset, bytes_sent; | |
323 | char *p, *data; | |
324 | ||
325 | p = own_buf + strlen ("vFile:pread:"); | |
326 | ||
327 | if (require_int (&p, &fd) | |
328 | || require_comma (&p) | |
329 | || require_valid_fd (fd) | |
330 | || require_int (&p, &len) | |
331 | || require_comma (&p) | |
332 | || require_int (&p, &offset) | |
333 | || require_end (p)) | |
334 | { | |
335 | hostio_packet_error (own_buf); | |
336 | return; | |
337 | } | |
338 | ||
bca929d3 | 339 | data = xmalloc (len); |
4e799345 | 340 | #ifdef HAVE_PREAD |
a6b151f1 | 341 | ret = pread (fd, data, len, offset); |
4e799345 | 342 | #else |
7c3270ae | 343 | ret = -1; |
4e799345 | 344 | #endif |
7c3270ae UW |
345 | /* If we have no pread or it failed for this file, use lseek/read. */ |
346 | if (ret == -1) | |
347 | { | |
348 | ret = lseek (fd, offset, SEEK_SET); | |
349 | if (ret != -1) | |
350 | ret = read (fd, data, len); | |
351 | } | |
a6b151f1 DJ |
352 | |
353 | if (ret == -1) | |
354 | { | |
59a016f0 | 355 | hostio_error (own_buf); |
a6b151f1 DJ |
356 | free (data); |
357 | return; | |
358 | } | |
359 | ||
360 | bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len); | |
361 | ||
362 | /* If we were using read, and the data did not all fit in the reply, | |
363 | we would have to back up using lseek here. With pread it does | |
364 | not matter. But we still have a problem; the return value in the | |
365 | packet might be wrong, so we must fix it. This time it will | |
366 | definitely fit. */ | |
367 | if (bytes_sent < ret) | |
368 | bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent, | |
369 | new_packet_len); | |
370 | ||
371 | free (data); | |
372 | } | |
373 | ||
374 | static void | |
375 | handle_pwrite (char *own_buf, int packet_len) | |
376 | { | |
377 | int fd, ret, len, offset; | |
378 | char *p, *data; | |
379 | ||
380 | p = own_buf + strlen ("vFile:pwrite:"); | |
381 | ||
382 | if (require_int (&p, &fd) | |
383 | || require_comma (&p) | |
384 | || require_valid_fd (fd) | |
385 | || require_int (&p, &offset) | |
386 | || require_comma (&p) | |
387 | || require_data (p, packet_len - (p - own_buf), &data, &len)) | |
388 | { | |
389 | hostio_packet_error (own_buf); | |
390 | return; | |
391 | } | |
392 | ||
4e799345 | 393 | #ifdef HAVE_PWRITE |
a6b151f1 | 394 | ret = pwrite (fd, data, len, offset); |
4e799345 | 395 | #else |
7c3270ae | 396 | ret = -1; |
4e799345 | 397 | #endif |
7c3270ae UW |
398 | /* If we have no pwrite or it failed for this file, use lseek/write. */ |
399 | if (ret == -1) | |
400 | { | |
401 | ret = lseek (fd, offset, SEEK_SET); | |
402 | if (ret != -1) | |
403 | ret = write (fd, data, len); | |
404 | } | |
a6b151f1 DJ |
405 | |
406 | if (ret == -1) | |
407 | { | |
59a016f0 | 408 | hostio_error (own_buf); |
a6b151f1 DJ |
409 | free (data); |
410 | return; | |
411 | } | |
412 | ||
413 | hostio_reply (own_buf, ret); | |
414 | free (data); | |
415 | } | |
416 | ||
aa9e327f GB |
417 | static void |
418 | handle_fstat (char *own_buf, int *new_packet_len) | |
419 | { | |
420 | int fd, bytes_sent; | |
421 | char *p; | |
422 | struct stat st; | |
423 | struct fio_stat fst; | |
424 | ||
425 | p = own_buf + strlen ("vFile:fstat:"); | |
426 | ||
427 | if (require_int (&p, &fd) | |
428 | || require_valid_fd (fd) | |
429 | || require_end (p)) | |
430 | { | |
431 | hostio_packet_error (own_buf); | |
432 | return; | |
433 | } | |
434 | ||
435 | if (fstat (fd, &st) == -1) | |
436 | { | |
437 | hostio_error (own_buf); | |
438 | return; | |
439 | } | |
440 | ||
7823a941 | 441 | host_to_fileio_stat (&st, &fst); |
aa9e327f GB |
442 | |
443 | bytes_sent = hostio_reply_with_data (own_buf, | |
444 | (char *) &fst, sizeof (fst), | |
445 | new_packet_len); | |
446 | ||
447 | /* If the response does not fit into a single packet, do not attempt | |
448 | to return a partial response, but simply fail. */ | |
449 | if (bytes_sent < sizeof (fst)) | |
450 | write_enn (own_buf); | |
451 | } | |
452 | ||
a6b151f1 DJ |
453 | static void |
454 | handle_close (char *own_buf) | |
455 | { | |
456 | int fd, ret; | |
457 | char *p; | |
458 | struct fd_list **open_fd_p, *old_fd; | |
459 | ||
460 | p = own_buf + strlen ("vFile:close:"); | |
461 | ||
462 | if (require_int (&p, &fd) | |
463 | || require_valid_fd (fd) | |
464 | || require_end (p)) | |
465 | { | |
466 | hostio_packet_error (own_buf); | |
467 | return; | |
468 | } | |
469 | ||
470 | ret = close (fd); | |
471 | ||
472 | if (ret == -1) | |
473 | { | |
59a016f0 | 474 | hostio_error (own_buf); |
a6b151f1 DJ |
475 | return; |
476 | } | |
477 | ||
478 | open_fd_p = &open_fds; | |
588eebee MS |
479 | /* We know that fd is in the list, thanks to require_valid_fd. */ |
480 | while ((*open_fd_p)->fd != fd) | |
a6b151f1 DJ |
481 | open_fd_p = &(*open_fd_p)->next; |
482 | ||
483 | old_fd = *open_fd_p; | |
484 | *open_fd_p = (*open_fd_p)->next; | |
485 | free (old_fd); | |
486 | ||
487 | hostio_reply (own_buf, ret); | |
488 | } | |
489 | ||
490 | static void | |
491 | handle_unlink (char *own_buf) | |
492 | { | |
d5749ee7 | 493 | char filename[HOSTIO_PATH_MAX]; |
a6b151f1 DJ |
494 | char *p; |
495 | int ret; | |
496 | ||
497 | p = own_buf + strlen ("vFile:unlink:"); | |
498 | ||
499 | if (require_filename (&p, filename) | |
500 | || require_end (p)) | |
501 | { | |
502 | hostio_packet_error (own_buf); | |
503 | return; | |
504 | } | |
505 | ||
506 | ret = unlink (filename); | |
507 | ||
508 | if (ret == -1) | |
509 | { | |
59a016f0 | 510 | hostio_error (own_buf); |
a6b151f1 DJ |
511 | return; |
512 | } | |
513 | ||
514 | hostio_reply (own_buf, ret); | |
515 | } | |
516 | ||
b9e7b9c3 UW |
517 | static void |
518 | handle_readlink (char *own_buf, int *new_packet_len) | |
519 | { | |
d5749ee7 | 520 | char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX]; |
b9e7b9c3 UW |
521 | char *p; |
522 | int ret, bytes_sent; | |
523 | ||
524 | p = own_buf + strlen ("vFile:readlink:"); | |
525 | ||
526 | if (require_filename (&p, filename) | |
527 | || require_end (p)) | |
528 | { | |
529 | hostio_packet_error (own_buf); | |
530 | return; | |
531 | } | |
532 | ||
0270a750 | 533 | ret = readlink (filename, linkname, sizeof (linkname) - 1); |
b9e7b9c3 UW |
534 | if (ret == -1) |
535 | { | |
536 | hostio_error (own_buf); | |
537 | return; | |
538 | } | |
539 | ||
540 | bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len); | |
541 | ||
542 | /* If the response does not fit into a single packet, do not attempt | |
543 | to return a partial response, but simply fail. */ | |
544 | if (bytes_sent < ret) | |
545 | sprintf (own_buf, "F-1,%x", FILEIO_ENAMETOOLONG); | |
546 | } | |
547 | ||
a6b151f1 DJ |
548 | /* Handle all the 'F' file transfer packets. */ |
549 | ||
550 | int | |
551 | handle_vFile (char *own_buf, int packet_len, int *new_packet_len) | |
552 | { | |
61012eef | 553 | if (startswith (own_buf, "vFile:open:")) |
a6b151f1 | 554 | handle_open (own_buf); |
61012eef | 555 | else if (startswith (own_buf, "vFile:pread:")) |
a6b151f1 | 556 | handle_pread (own_buf, new_packet_len); |
61012eef | 557 | else if (startswith (own_buf, "vFile:pwrite:")) |
a6b151f1 | 558 | handle_pwrite (own_buf, packet_len); |
aa9e327f GB |
559 | else if (startswith (own_buf, "vFile:fstat:")) |
560 | handle_fstat (own_buf, new_packet_len); | |
61012eef | 561 | else if (startswith (own_buf, "vFile:close:")) |
a6b151f1 | 562 | handle_close (own_buf); |
61012eef | 563 | else if (startswith (own_buf, "vFile:unlink:")) |
a6b151f1 | 564 | handle_unlink (own_buf); |
61012eef | 565 | else if (startswith (own_buf, "vFile:readlink:")) |
b9e7b9c3 | 566 | handle_readlink (own_buf, new_packet_len); |
a6b151f1 DJ |
567 | else |
568 | return 0; | |
569 | ||
570 | return 1; | |
571 | } |