]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Removed some repetition in the way VG_(record_fd_open)() is called.
authorNicholas Nethercote <njn@valgrind.org>
Thu, 23 Jun 2005 02:26:47 +0000 (02:26 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Thu, 23 Jun 2005 02:26:47 +0000 (02:26 +0000)
As part of this, VG_(resolve_filename)() no longer calls VG_(malloc)()
and so m_libcfile no longer depends on m_mallocfree.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4001

coregrind/m_aspacemgr/aspacemgr.c
coregrind/m_libcfile.c
coregrind/m_signals.c
coregrind/m_syswrap/priv_syswrap-generic.h
coregrind/m_syswrap/syswrap-generic.c
coregrind/m_syswrap/syswrap-linux.c
coregrind/pub_core_libcfile.h

index 2cdd6469a7483e6c9a7b4cb7689401512bffd009..2a708235d886b004fd98a3600637c18bf3856eb0 100644 (file)
@@ -785,8 +785,8 @@ VG_(map_file_segment)( Addr addr, SizeT len,
 void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags, 
                         Int fd, ULong off, const Char *filename)
 {
+   Char buf[VKI_PATH_MAX];
    struct vki_stat st;
-   Char *name = NULL;
 
    st.st_dev = 0;
    st.st_ino = 0;
@@ -799,10 +799,8 @@ void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags,
    }
 
    if ((flags & SF_FILE) && filename == NULL && fd != -1)
-      name = VG_(resolve_filename_nodup)(fd);
-
-   if (filename == NULL)
-      filename = name;
+      if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX))
+         filename = buf;
 
    VG_(map_file_segment)(addr, len, prot, flags, 
                          st.st_dev, st.st_ino, off, filename);
index edf9b82eb6f837b4676ca5d940749bf13c894a75..c543b77f1136f966491edfaf654ec0297b05c3c3 100644 (file)
@@ -33,7 +33,6 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
 #include "pub_core_libcprint.h"     // For VG_(sprintf)()
-#include "pub_core_mallocfree.h"
 #include "pub_core_syscall.h"
 #include "vki_unistd.h"
 
@@ -71,32 +70,18 @@ Int VG_(safe_fd)(Int oldfd)
 
 /* Given a file descriptor, attempt to deduce its filename.  To do
    this, we use /proc/self/fd/<FD>.  If this doesn't point to a file,
-   or if it doesn't exist, we just return NULL.  The caller is
-   responsible for copying the contents of buf out immediately. */
-static HChar resolve_filename_buf[VKI_PATH_MAX];
-HChar* VG_(resolve_filename_nodup) ( Int fd )
+   or if it doesn't exist, we return False. */
+Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf )
 {
    HChar tmp[64];
 
    VG_(sprintf)(tmp, "/proc/self/fd/%d", fd);
-   VG_(memset)(resolve_filename_buf, 0, VKI_PATH_MAX);
+   VG_(memset)(buf, 0, n_buf);
 
-   if (VG_(readlink)(tmp, resolve_filename_buf, VKI_PATH_MAX) == -1)
-      return NULL;
-
-   return (resolve_filename_buf[0] == '/') 
-             ? resolve_filename_buf 
-             : NULL;
-}
-
-/* Same as resolve_filename_nodup, except that the result is copied 
-   into new memory which the caller is responsible for freeing. */
-HChar* VG_(resolve_filename) ( Int fd )
-{
-   HChar* transient = VG_(resolve_filename_nodup)(fd);
-   return transient
-             ? VG_(arena_strdup)(VG_AR_CORE, transient)
-             : NULL;
+   if (VG_(readlink)(tmp, buf, VKI_PATH_MAX) > 0 && buf[0] == '/')
+      return True;
+   else
+      return False;
 }
 
 /* Returns -1 on failure. */
index e751e7c008a41286b7897663c36cd748334b2569..7f7223c6ba6b445cd01c446243da95156b7af221 100644 (file)
@@ -1022,7 +1022,8 @@ static void write_note(Int fd, const struct note *n)
 
 static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo)
 {
-   Char *name;
+   static Char name[VKI_PATH_MAX];
+   Bool res;
 
    VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
 
@@ -1049,12 +1050,10 @@ static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsi
    prpsinfo->pr_uid = 0;
    prpsinfo->pr_gid = 0;
    
-   name = VG_(resolve_filename)(VG_(clexecfd));
-
-   if (name != NULL) {
+   if (VG_(resolve_filename)(VG_(clexecfd), name, VKI_PATH_MAX)) {
       Char *n = name+VG_(strlen)(name)-1;
 
-      while(n > name && *n != '/')
+      while (n > name && *n != '/')
         n--;
       if (n != name)
         n++;
index eed37b4f4972003b29b17bef6ba1ec05dd5abbd2..6d31ad7ea716094bf1af6a53eba9214e07bf4580 100644 (file)
@@ -48,7 +48,7 @@ extern
 Bool ML_(fd_allowed)(Int fd, const Char *syscallname, ThreadId tid, Bool soft);
 
 extern
-void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname);
+void ML_(record_fd_open_nameless)(ThreadId tid, Int fd);
 
 // Used when killing threads -- we must not kill a thread if it's the thread
 // that would do Valgrind's final cleanup and output.
index db09e002bde2d285156c1fb5416bce6639d9738e..e3a1db13d14eea74e6844e0d47262fc44ce3e841 100644 (file)
@@ -345,7 +345,7 @@ void record_fd_close(ThreadId tid, Int fd)
    some such thing) or that we don't know the filename.  If the fd is
    already open, then we're probably doing a dup2() to an existing fd,
    so just overwrite the existing one. */
-void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname)
+static void record_fd_open_with_given_name(ThreadId tid, Int fd, char *pathname)
 {
    OpenFd *i;
 
@@ -374,10 +374,29 @@ void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname)
    }
 
    i->fd = fd;
-   i->pathname = pathname;
+   i->pathname = VG_(arena_strdup)(VG_AR_CORE, pathname);
    i->where = (tid == -1) ? NULL : VG_(record_ExeContext)(tid);
 }
 
+// Record opening of an fd, and find its name.
+static void record_fd_open_named(ThreadId tid, Int fd)
+{
+   static HChar buf[VKI_PATH_MAX];
+   Char* name;
+   if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX))
+      name = buf;
+   else
+      name = NULL;
+   
+   record_fd_open_with_given_name(tid, fd, name);
+}
+
+// Record opening of a nameless fd.
+void ML_(record_fd_open_nameless)(ThreadId tid, Int fd)
+{
+   record_fd_open_with_given_name(tid, fd, NULL);
+}
+
 static
 Char *unix2name(struct vki_sockaddr_un *sa, UInt len, Char *name)
 {
@@ -521,7 +540,7 @@ void do_hacky_preopened()
 
    for (i = 0; i < count; i++)
       if(VG_(fcntl)(i, VKI_F_GETFL, 0) != -1)
-         ML_(record_fd_open)(-1, i, NULL);
+         ML_(record_fd_open_nameless)(-1, i);
 }
 
 /* Initialize the list of open file descriptors with the file descriptors
@@ -539,15 +558,15 @@ void VG_(init_preopened_fds)()
    }
 
    while((ret = VG_(getdents)(f, &d, sizeof(d))) != 0) {
-      if(ret == -1)
+      if (ret == -1)
          goto out;
 
-      if(VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) {
+      if (VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) {
          int fno = VG_(atoll)(d.d_name);
 
-         if(fno != f)
-            if(VG_(clo_track_fds))
-               ML_(record_fd_open)(-1, fno, VG_(resolve_filename)(fno));
+         if (fno != f)
+            if (VG_(clo_track_fds))
+               record_fd_open_named(-1, fno);
       }
 
       VG_(lseek)(f, d.d_off, VKI_SEEK_SET);
@@ -646,7 +665,7 @@ static void check_cmsg_for_fds(ThreadId tid, struct vki_msghdr *msg)
             if(VG_(clo_track_fds))
                // XXX: must we check the range on these fds with
                //      VG_(fd_allowed)()?
-               ML_(record_fd_open) (tid, fds[i], VG_(resolve_filename)(fds[i]));
+               record_fd_open_named(tid, fds[i]);
       }
 
       cm = VKI_CMSG_NXTHDR(msg, cm);
@@ -901,8 +920,8 @@ ML_(generic_POST_sys_socketpair) ( ThreadId tid,
    } else {
       POST_MEM_WRITE( arg3, 2*sizeof(int) );
       if (VG_(clo_track_fds)) {
-         ML_(record_fd_open)(tid, fd1, NULL);
-         ML_(record_fd_open)(tid, fd2, NULL);
+         ML_(record_fd_open_nameless)(tid, fd1);
+         ML_(record_fd_open_nameless)(tid, fd2);
       }
    }
    return r;
@@ -920,7 +939,7 @@ ML_(generic_POST_sys_socket) ( ThreadId tid, SysRes res )
       r = VG_(mk_SysRes_Error)( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open)(tid, res.val, NULL);
+         ML_(record_fd_open_nameless)(tid, res.val);
    }
    return r;
 }
@@ -971,7 +990,7 @@ ML_(generic_POST_sys_accept) ( ThreadId tid,
          buf_and_len_post_check ( tid, res, addr_p, addrlen_p,
                                   "socketcall.accept(addrlen_out)" );
       if (VG_(clo_track_fds))
-          ML_(record_fd_open)(tid, res.val, NULL);
+          ML_(record_fd_open_nameless)(tid, res.val);
    }
    return r;
 }
@@ -2511,7 +2530,7 @@ POST(sys_dup)
       SET_STATUS_Failure( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES));
+         record_fd_open_named(tid, RES);
    }
 }
 
@@ -2527,7 +2546,7 @@ POST(sys_dup2)
 {
    vg_assert(SUCCESS);
    if (VG_(clo_track_fds))
-      ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES));
+      record_fd_open_named(tid, RES);
 }
 
 PRE(sys_fchdir)
@@ -2612,7 +2631,7 @@ POST(sys_fcntl)
          SET_STATUS_Failure( VKI_EMFILE );
       } else {
          if (VG_(clo_track_fds))
-            ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES));
+            record_fd_open_named(tid, RES);
       }
    }
 }
@@ -2678,8 +2697,7 @@ POST(sys_fcntl64)
          SET_STATUS_Failure( VKI_EMFILE );
       } else {
          if (VG_(clo_track_fds))
-            ML_(record_fd_open)(tid, RES, 
-                                VG_(resolve_filename)(RES));
+            record_fd_open_named(tid, RES);
       }
    }
 }
@@ -4564,8 +4582,7 @@ POST(sys_open)
       SET_STATUS_Failure( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open)(tid, RES, 
-                                  VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1));
+         record_fd_open_with_given_name(tid, RES, (Char*)ARG1);
    }
 }
 
@@ -4616,7 +4633,7 @@ POST(sys_creat)
       SET_STATUS_Failure( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1));
+         record_fd_open_with_given_name(tid, RES, (Char*)ARG1);
    }
 }
 
@@ -4640,8 +4657,8 @@ POST(sys_pipe)
    } else {
       POST_MEM_WRITE( ARG1, 2*sizeof(int) );
       if (VG_(clo_track_fds)) {
-         ML_(record_fd_open)(tid, p[0], NULL);
-         ML_(record_fd_open)(tid, p[1], NULL);
+         ML_(record_fd_open_nameless)(tid, p[0]);
+         ML_(record_fd_open_nameless)(tid, p[1]);
       }
    }
 }
@@ -5412,7 +5429,7 @@ POST(sys_mq_open)
       SET_STATUS_Failure( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1));
+         record_fd_open_with_given_name(tid, RES, (Char*)ARG1);
    }
 }
 
index 5a6c0eb7cdca6f1eb068b304a0504e49ebd5d391..36fb9c81016841cf8b9d159f6b564ec0ffe1fedd 100644 (file)
@@ -535,7 +535,7 @@ POST(sys_futex)
          SET_STATUS_Failure( VKI_EMFILE );
       } else {
          if (VG_(clo_track_fds))
-            ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1));
+            ML_(record_fd_open_nameless)(tid, RES);
       }
    }
 }
@@ -553,7 +553,7 @@ POST(sys_epoll_create)
       SET_STATUS_Failure( VKI_EMFILE );
    } else {
       if (VG_(clo_track_fds))
-         ML_(record_fd_open) (tid, RES, NULL);
+         ML_(record_fd_open_nameless) (tid, RES);
    }
 }
 
index b0457dcf7c0640335bc10a3413efcdde8b2b6f4b..1480da7923502a706cb70ad383842bf66113f31c 100644 (file)
@@ -47,8 +47,7 @@ extern Int VG_(safe_fd) ( Int oldfd );
 extern Int VG_(fcntl)   ( Int fd, Int cmd, Int arg );
 
 /* Convert an fd into a filename */
-extern HChar* VG_(resolve_filename_nodup) ( Int fd );
-extern HChar* VG_(resolve_filename)       ( Int fd );
+extern Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf );
 
 /* Default destination port to be used in logging over a network, if
    none specified. */