2012-05-24 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / hostio.c
index b59b03abd3acda645b68ed5e2203e7a2911f7b98..03aab587d982de967b2c9ec31391842d3bb6e5bf 100644 (file)
@@ -1,5 +1,5 @@
 /* Host file transfer support for gdbserver.
-   Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007-2012 Free Software Foundation, Inc.
 
    Contributed by CodeSourcery.
 
@@ -134,7 +134,10 @@ require_data (char *p, int p_len, char **data, int *data_len)
     }
 
   if (escaped)
-    return -1;
+    {
+      free (*data);
+      return -1;
+    }
 
   *data_len = output_index;
   return 0;
@@ -415,7 +418,8 @@ handle_close (char *own_buf)
     }
 
   open_fd_p = &open_fds;
-  while (*open_fd_p && (*open_fd_p)->fd != fd)
+  /* We know that fd is in the list, thanks to require_valid_fd.  */
+  while ((*open_fd_p)->fd != fd)
     open_fd_p = &(*open_fd_p)->next;
 
   old_fd = *open_fd_p;
@@ -452,6 +456,41 @@ handle_unlink (char *own_buf)
   hostio_reply (own_buf, ret);
 }
 
+static void
+handle_readlink (char *own_buf, int *new_packet_len)
+{
+#if defined (HAVE_READLINK)
+  char filename[PATH_MAX], linkname[PATH_MAX];
+  char *p;
+  int ret, bytes_sent;
+
+  p = own_buf + strlen ("vFile:readlink:");
+
+  if (require_filename (&p, filename)
+      || require_end (p))
+    {
+      hostio_packet_error (own_buf);
+      return;
+    }
+
+  ret = readlink (filename, linkname, sizeof linkname);
+  if (ret == -1)
+    {
+      hostio_error (own_buf);
+      return;
+    }
+
+  bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);
+
+  /* If the response does not fit into a single packet, do not attempt
+     to return a partial response, but simply fail.  */
+  if (bytes_sent < ret)
+    sprintf (own_buf, "F-1,%x", FILEIO_ENAMETOOLONG);
+#else /* ! HAVE_READLINK */
+    sprintf (own_buf, "F-1,%x", FILEIO_ENOSYS);
+#endif
+}
+
 /* Handle all the 'F' file transfer packets.  */
 
 int
@@ -467,6 +506,8 @@ handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
     handle_close (own_buf);
   else if (strncmp (own_buf, "vFile:unlink:", 13) == 0)
     handle_unlink (own_buf);
+  else if (strncmp (own_buf, "vFile:readlink:", 15) == 0)
+    handle_readlink (own_buf, new_packet_len);
   else
     return 0;
 
This page took 0.025885 seconds and 4 git commands to generate.