Finished the modularisation of vg_mylibc.c, which meant it could be removed.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3894 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/cachegrind/cg-amd64.c b/cachegrind/cg-amd64.c
index 6aa35a7..824a4f8 100644
--- a/cachegrind/cg-amd64.c
+++ b/cachegrind/cg-amd64.c
@@ -31,6 +31,7 @@
 #include "tool.h"
 #include "cg_arch.h"
 #include "pub_tool_libcbase.h"
+#include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 
 // All CPUID info taken from sandpile.org/a32/cpuid.htm */
diff --git a/cachegrind/cg-x86.c b/cachegrind/cg-x86.c
index 6ce7b60..b7bce81 100644
--- a/cachegrind/cg-x86.c
+++ b/cachegrind/cg-x86.c
@@ -30,6 +30,7 @@
 
 #include "tool.h"
 #include "pub_tool_libcbase.h"
+#include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 #include "cg_arch.h"
 
diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c
index 5c7fd8b..d7c817d 100644
--- a/cachegrind/cg_main.c
+++ b/cachegrind/cg_main.c
@@ -36,6 +36,7 @@
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcfile.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_libcproc.h"
 #include "pub_tool_mallocfree.h"
 #include "pub_tool_options.h"
 #include "pub_tool_profile.h"
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index 26a769f..2711a13 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -51,6 +51,7 @@
 	pub_core_libcfile.h	\
 	pub_core_libcmman.h	\
 	pub_core_libcprint.h	\
+	pub_core_libcproc.h	\
 	pub_core_libcsignal.h	\
 	pub_core_main.h		\
 	pub_core_mallocfree.h	\
@@ -100,6 +101,7 @@
 	m_libcfile.c \
 	m_libcmman.c \
 	m_libcprint.c \
+	m_libcproc.c \
 	m_libcsignal.c \
 	m_main.c \
 	m_mallocfree.c \
@@ -115,8 +117,7 @@
 	m_translate.c \
 	m_transtab.c \
 	\
-	ume.c \
-	vg_mylibc.c
+	ume.c
 
 ## Nb: libscheduler.a must precede libdispatch.a in this list.
 stage2_extra= \
diff --git a/coregrind/core.h b/coregrind/core.h
index 4111d08..bae70e1 100644
--- a/coregrind/core.h
+++ b/coregrind/core.h
@@ -82,13 +82,6 @@
 #include "pub_core_scheduler.h"   // for types 'ThreadArchState'
 
 /* ---------------------------------------------------------------------
-   Environment variables
-   ------------------------------------------------------------------ */
-
-/* The directory we look for all our auxillary files in */
-#define VALGRINDLIB	"VALGRINDLIB"
-
-/* ---------------------------------------------------------------------
    Exports of vg_intercept.c
    ------------------------------------------------------------------ */
 
@@ -124,25 +117,6 @@
    ------------------------------------------------------------------ */
 
 extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
-extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
-
-/* Environment manipulations */
-extern Char **VG_(env_setenv)   ( Char ***envp, const Char* varname,
-                                  const Char *val );
-extern void   VG_(env_unsetenv) ( Char **env, const Char *varname );
-extern void   VG_(env_remove_valgrind_env_stuff) ( Char** env ); 
-
-extern void   VG_(nanosleep)(struct vki_timespec *);
-
-/* Simple Valgrind-internal atfork mechanism */
-/* Internal atfork handlers */
-typedef void (*vg_atfork_t)(ThreadId);
-extern void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, 
-                                         vg_atfork_t child);
-extern void VG_(do_atfork_pre)   (ThreadId tid);
-extern void VG_(do_atfork_parent)(ThreadId tid);
-extern void VG_(do_atfork_child) (ThreadId tid);
-
 
 /* ---------------------------------------------------------------------
    Exports of vg_syscall.S
diff --git a/coregrind/linux/core_os.c b/coregrind/linux/core_os.c
index d32455e..5d5c2d6 100644
--- a/coregrind/linux/core_os.c
+++ b/coregrind/linux/core_os.c
@@ -33,6 +33,7 @@
 #include "pub_core_debuglog.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_options.h"
 #include "pub_core_signals.h"
diff --git a/coregrind/m_aspacemgr/aspacemgr.c b/coregrind/m_aspacemgr/aspacemgr.c
index de54b62..bfe926c 100644
--- a/coregrind/m_aspacemgr/aspacemgr.c
+++ b/coregrind/m_aspacemgr/aspacemgr.c
@@ -37,6 +37,7 @@
 #include "pub_core_libcfile.h"      // For VG_(fstat)()
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
 #include "pub_core_syscalls.h"
diff --git a/coregrind/m_errormgr.c b/coregrind/m_errormgr.c
index de40eeb..7ac1394 100644
--- a/coregrind/m_errormgr.c
+++ b/coregrind/m_errormgr.c
@@ -36,6 +36,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_main.h"          // for VG_(start_debugger)()
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
diff --git a/coregrind/m_libcassert.c b/coregrind/m_libcassert.c
index b5c5373..dea4f05 100644
--- a/coregrind/m_libcassert.c
+++ b/coregrind/m_libcassert.c
@@ -32,9 +32,11 @@
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_main.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_tooliface.h"
+#include "vki_unistd.h"
 
 /* ---------------------------------------------------------------------
    Assertery.
@@ -56,6 +58,17 @@
 #  error Unknown platform
 #endif
 
+/* Pull down the entire world */
+void VG_(exit)( Int status )
+{
+   (void)VG_(do_syscall1)(__NR_exit_group, status );
+   (void)VG_(do_syscall1)(__NR_exit, status );
+   /* Why are we still alive here? */
+   /*NOTREACHED*/
+   *(volatile Int *)0 = 'x';
+   vg_assert(2+2 == 5);
+}
+
 __attribute__ ((noreturn))
 static void report_and_quit ( const Char* report, Addr ip, Addr sp, Addr fp )
 {
diff --git a/coregrind/m_libcmman.c b/coregrind/m_libcmman.c
index c8536a8..fc5e44f 100644
--- a/coregrind/m_libcmman.c
+++ b/coregrind/m_libcmman.c
@@ -34,6 +34,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "vki_unistd.h"
 
 SysRes VG_(mmap_native)(void *start, SizeT length, UInt prot, UInt flags,
diff --git a/coregrind/m_libcprint.c b/coregrind/m_libcprint.c
index f4ca698..b4bdda3 100644
--- a/coregrind/m_libcprint.c
+++ b/coregrind/m_libcprint.c
@@ -34,6 +34,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_options.h"
 #include "valgrind.h"           // for RUNNING_ON_VALGRIND
 
diff --git a/coregrind/vg_mylibc.c b/coregrind/m_libcproc.c
similarity index 92%
rename from coregrind/vg_mylibc.c
rename to coregrind/m_libcproc.c
index 967b865..69d9b05 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/m_libcproc.c
@@ -1,8 +1,6 @@
 
 /*--------------------------------------------------------------------*/
-/*--- Reimplementation of some C library stuff, to avoid depending ---*/
-/*--- on libc.so.                                                  ---*/
-/*---                                                  vg_mylibc.c ---*/
+/*--- Process-related libc stuff.                     m_libcproc.c ---*/
 /*--------------------------------------------------------------------*/
  
 /*
@@ -31,118 +29,36 @@
 */
 
 #include "core.h"
-#include "pub_core_aspacemgr.h"
-#include "pub_core_debuglog.h"    /* VG_(debugLog_vprintf) */
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
-#include "pub_core_libcfile.h"
-#include "pub_core_main.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_mallocfree.h"
-#include "pub_core_options.h"
-#include "pub_core_stacktrace.h"
-#include "pub_core_syscalls.h"
-#include "pub_core_tooliface.h"
 #include "vki_unistd.h"
 
+/* ---------------------------------------------------------------------
+   Command line and environment stuff
+   ------------------------------------------------------------------ */
 
-Int VG_(waitpid)(Int pid, Int *status, Int options)
+/* As deduced from sp_at_startup, the client's argc, argv[] and
+   envp[] as extracted from the client's stack at startup-time. */
+Int    VG_(client_argc);
+Char** VG_(client_argv);
+Char** VG_(client_envp);
+
+/* We do getenv without libc's help by snooping around in
+   VG_(client_envp) as determined at startup time. */
+Char *VG_(getenv)(Char *varname)
 {
-   SysRes res = VG_(do_syscall4)(__NR_wait4, pid, (UWord)status, options, 0);
-   return res.isError ? -1 : res.val;
-}
-
-Int VG_(gettid)(void)
-{
-   SysRes res = VG_(do_syscall0)(__NR_gettid);
-
-   if (res.isError && res.val == VKI_ENOSYS) {
-      Char pid[16];      
-      /*
-       * The gettid system call does not exist. The obvious assumption
-       * to make at this point would be that we are running on an older
-       * system where the getpid system call actually returns the ID of
-       * the current thread.
-       *
-       * Unfortunately it seems that there are some systems with a kernel
-       * where getpid has been changed to return the ID of the thread group
-       * leader but where the gettid system call has not yet been added.
-       *
-       * So instead of calling getpid here we use readlink to see where
-       * the /proc/self link is pointing...
-       */
-
-      res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
-                             (UWord)pid, sizeof(pid));
-      if (!res.isError && res.val > 0) {
-         pid[res.val] = '\0';
-         res.val = VG_(atoll)(pid);
+   Int i, n;
+   n = VG_(strlen)(varname);
+   for (i = 0; VG_(client_envp)[i] != NULL; i++) {
+      Char* s = VG_(client_envp)[i];
+      if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
+         return & s[n+1];
       }
    }
-
-   return res.val;
-}
-
-
-
-/* ---------------------------------------------------------------------
-   exit, fcntl
-   ------------------------------------------------------------------ */
-
-/* Pull down the entire world */
-void VG_(exit)( Int status )
-{
-   (void)VG_(do_syscall1)(__NR_exit_group, status );
-   (void)VG_(do_syscall1)(__NR_exit, status );
-   /* Why are we still alive here? */
-   /*NOTREACHED*/
-   *(volatile Int *)0 = 'x';
-   vg_assert(2+2 == 5);
-}
-
-/* Returns -1 on error. */
-Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
-{
-   SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg);
-   return res.isError ? -1 : res.val;
-}
-
-Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout)
-{
-   SysRes res = VG_(do_syscall3)(__NR_poll, (UWord)ufds, nfds, timeout);
-   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
-   return res.val;
-}
-
-
-/* ---------------------------------------------------------------------
-   Misc functions looking for a proper home.
-   ------------------------------------------------------------------ */
-
-/* clone the environment */
-static Char **env_clone ( Char **oldenv )
-{
-   Char **oldenvp;
-   Char **newenvp;
-   Char **newenv;
-   Int  envlen;
-
-   for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
-
-   envlen = oldenvp - oldenv + 1;
-   
-   newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **));
-
-   oldenvp = oldenv;
-   newenvp = newenv;
-   
-   while (oldenvp && *oldenvp) {
-      *newenvp++ = *oldenvp++;
-   }
-   
-   *newenvp = *oldenvp;
-
-   return newenv;
+   return NULL;
 }
 
 void  VG_(env_unsetenv) ( Char **env, const Char *varname )
@@ -202,69 +118,6 @@
    return oldenv;
 }
 
-/* We do getenv without libc's help by snooping around in
-   VG_(client_envp) as determined at startup time. */
-Char *VG_(getenv)(Char *varname)
-{
-   Int i, n;
-   n = VG_(strlen)(varname);
-   for (i = 0; VG_(client_envp)[i] != NULL; i++) {
-      Char* s = VG_(client_envp)[i];
-      if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
-         return & s[n+1];
-      }
-   }
-   return NULL;
-}
-
-/* Support for getrlimit. */
-Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
-{
-   SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
-   /* res = getrlimit( resource, rlim ); */
-#  ifdef __NR_ugetrlimit
-   res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
-#  endif
-   if (res.isError && res.val == VKI_ENOSYS)
-      res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
-   return res.isError ? -1 : res.val;
-}
-
-
-/* Support for setrlimit. */
-Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
-{
-   SysRes res;
-   /* res = setrlimit( resource, rlim ); */
-   res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
-   return res.isError ? -1 : res.val;
-}
-
-/* You'd be amazed how many places need to know the current pid. */
-Int VG_(getpid) ( void )
-{
-   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
-   return VG_(do_syscall0)(__NR_getpid) . val;
-}
-
-Int VG_(getpgrp) ( void )
-{
-   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
-   return VG_(do_syscall0)(__NR_getpgrp) . val;
-}
-
-Int VG_(getppid) ( void )
-{
-   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
-   return VG_(do_syscall0)(__NR_getppid) . val;
-}
-
-Int VG_(setpgid) ( Int pid, Int pgrp )
-{
-   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
-   return VG_(do_syscall2)(__NR_setpgid, pid, pgrp) . val;
-}
-
 /* Walk through a colon-separated environment variable, and remove the
    entries which match remove_pattern.  It slides everything down over
    the removed entries, and pads the remaining space with '\0'.  It
@@ -361,6 +214,56 @@
    VG_(arena_free)(VG_AR_CORE, buf);
 }
 
+/* ---------------------------------------------------------------------
+   Various important syscall wrappers
+   ------------------------------------------------------------------ */
+
+Int VG_(waitpid)(Int pid, Int *status, Int options)
+{
+   SysRes res = VG_(do_syscall4)(__NR_wait4, pid, (UWord)status, options, 0);
+   return res.isError ? -1 : res.val;
+}
+
+/* Returns -1 on error. */
+Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
+{
+   SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg);
+   return res.isError ? -1 : res.val;
+}
+
+Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout)
+{
+   SysRes res = VG_(do_syscall3)(__NR_poll, (UWord)ufds, nfds, timeout);
+   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
+   return res.val;
+}
+
+/* clone the environment */
+static Char **env_clone ( Char **oldenv )
+{
+   Char **oldenvp;
+   Char **newenvp;
+   Char **newenv;
+   Int  envlen;
+
+   for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
+
+   envlen = oldenvp - oldenv + 1;
+   
+   newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **));
+
+   oldenvp = oldenv;
+   newenvp = newenv;
+   
+   while (oldenvp && *oldenvp) {
+      *newenvp++ = *oldenvp++;
+   }
+   
+   *newenvp = *oldenvp;
+
+   return newenv;
+}
+
 /* Return -1 if error, else 0.  NOTE does not indicate return code of
    child! */
 Int VG_(system) ( Char* cmd )
@@ -401,9 +304,98 @@
    }
 }
 
+/* ---------------------------------------------------------------------
+   Resource limits
+   ------------------------------------------------------------------ */
+
+struct vki_rlimit VG_(client_rlimit_data);
+struct vki_rlimit VG_(client_rlimit_stack);
+
+/* Support for getrlimit. */
+Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
+{
+   SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
+   /* res = getrlimit( resource, rlim ); */
+#  ifdef __NR_ugetrlimit
+   res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
+#  endif
+   if (res.isError && res.val == VKI_ENOSYS)
+      res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
+   return res.isError ? -1 : res.val;
+}
+
+
+/* Support for setrlimit. */
+Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
+{
+   SysRes res;
+   /* res = setrlimit( resource, rlim ); */
+   res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
+   return res.isError ? -1 : res.val;
+}
 
 /* ---------------------------------------------------------------------
-   Support for a millisecond-granularity timer.
+   pids, etc
+   ------------------------------------------------------------------ */
+
+Int VG_(gettid)(void)
+{
+   SysRes res = VG_(do_syscall0)(__NR_gettid);
+
+   if (res.isError && res.val == VKI_ENOSYS) {
+      Char pid[16];      
+      /*
+       * The gettid system call does not exist. The obvious assumption
+       * to make at this point would be that we are running on an older
+       * system where the getpid system call actually returns the ID of
+       * the current thread.
+       *
+       * Unfortunately it seems that there are some systems with a kernel
+       * where getpid has been changed to return the ID of the thread group
+       * leader but where the gettid system call has not yet been added.
+       *
+       * So instead of calling getpid here we use readlink to see where
+       * the /proc/self link is pointing...
+       */
+
+      res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
+                             (UWord)pid, sizeof(pid));
+      if (!res.isError && res.val > 0) {
+         pid[res.val] = '\0';
+         res.val = VG_(atoll)(pid);
+      }
+   }
+
+   return res.val;
+}
+
+/* You'd be amazed how many places need to know the current pid. */
+Int VG_(getpid) ( void )
+{
+   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
+   return VG_(do_syscall0)(__NR_getpid) . val;
+}
+
+Int VG_(getpgrp) ( void )
+{
+   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
+   return VG_(do_syscall0)(__NR_getpgrp) . val;
+}
+
+Int VG_(getppid) ( void )
+{
+   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
+   return VG_(do_syscall0)(__NR_getppid) . val;
+}
+
+Int VG_(setpgid) ( Int pid, Int pgrp )
+{
+   /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
+   return VG_(do_syscall2)(__NR_setpgid, pid, pgrp) . val;
+}
+
+/* ---------------------------------------------------------------------
+   Timing stuff
    ------------------------------------------------------------------ */
 
 UInt VG_(read_millisecond_timer) ( void )
@@ -430,10 +422,6 @@
 }
 
 /* ---------------------------------------------------------------------
-   Misc stuff looking for a proper home
-   ------------------------------------------------------------------ */
-
-/* ---------------------------------------------------------------------
    A simple atfork() facility for Valgrind's internal use
    ------------------------------------------------------------------ */
 
@@ -497,8 +485,6 @@
 	 (*atforks[i].child)(tid);
 }
 
-
 /*--------------------------------------------------------------------*/
 /*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
-
diff --git a/coregrind/m_main.c b/coregrind/m_main.c
index e7d10ed..000e388 100644
--- a/coregrind/m_main.c
+++ b/coregrind/m_main.c
@@ -41,6 +41,7 @@
 #include "pub_core_libcfile.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
@@ -111,9 +112,6 @@
    Startup stuff                            
    ------------------------------------------------------------------ */
 
-struct vki_rlimit VG_(client_rlimit_data);
-struct vki_rlimit VG_(client_rlimit_stack);
-
 /* stage1 (main) executable */
 static Int vgexecfd = -1;
 
@@ -127,12 +125,6 @@
 static Int  vg_argc;
 static Char **vg_argv;
 
-/* As deduced from sp_at_startup, the client's argc, argv[] and
-   envp[] as extracted from the client's stack at startup-time. */
-Int    VG_(client_argc);
-Char** VG_(client_argv);
-Char** VG_(client_envp);
-
 
 /* ---------------------------------------------------------------------
    Running stuff                            
diff --git a/coregrind/m_scheduler/scheduler.c b/coregrind/m_scheduler/scheduler.c
index ff54a0b..3b63cd2 100644
--- a/coregrind/m_scheduler/scheduler.c
+++ b/coregrind/m_scheduler/scheduler.c
@@ -66,6 +66,7 @@
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_scheduler/sema.c b/coregrind/m_scheduler/sema.c
index dd1ba1b..a01aed7 100644
--- a/coregrind/m_scheduler/sema.c
+++ b/coregrind/m_scheduler/sema.c
@@ -31,6 +31,7 @@
 #include "core.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcfile.h"
+#include "pub_core_libcproc.h"
 #include "priv_sema.h"
 
 /* 
diff --git a/coregrind/m_signals.c b/coregrind/m_signals.c
index eba5ddd..29edb53 100644
--- a/coregrind/m_signals.c
+++ b/coregrind/m_signals.c
@@ -87,6 +87,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_syscalls/syscalls-amd64-linux.c b/coregrind/m_syscalls/syscalls-amd64-linux.c
index 3ddfb8d..c8392bb 100644
--- a/coregrind/m_syscalls/syscalls-amd64-linux.c
+++ b/coregrind/m_syscalls/syscalls-amd64-linux.c
@@ -37,6 +37,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_sigframe.h"
 #include "pub_core_signals.h"
diff --git a/coregrind/m_syscalls/syscalls-generic.c b/coregrind/m_syscalls/syscalls-generic.c
index caa6ddc..250d7bc 100644
--- a/coregrind/m_syscalls/syscalls-generic.c
+++ b/coregrind/m_syscalls/syscalls-generic.c
@@ -36,6 +36,7 @@
 #include "pub_core_libcfile.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_main.h"
 #include "pub_core_mallocfree.h"
diff --git a/coregrind/m_syscalls/syscalls-main.c b/coregrind/m_syscalls/syscalls-main.c
index bae018d..619a8d6 100644
--- a/coregrind/m_syscalls/syscalls-main.c
+++ b/coregrind/m_syscalls/syscalls-main.c
@@ -33,6 +33,7 @@
 #include "pub_core_libcbase.h"
 #include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_tooliface.h"
diff --git a/coregrind/m_syscalls/syscalls-x86-linux.c b/coregrind/m_syscalls/syscalls-x86-linux.c
index 9806c89..624ac73 100644
--- a/coregrind/m_syscalls/syscalls-x86-linux.c
+++ b/coregrind/m_syscalls/syscalls-x86-linux.c
@@ -42,6 +42,7 @@
 #include "pub_core_libcassert.h"
 #include "pub_core_libcmman.h"
 #include "pub_core_libcprint.h"
+#include "pub_core_libcproc.h"
 #include "pub_core_libcsignal.h"
 #include "pub_core_mallocfree.h"
 #include "pub_core_sigframe.h"
diff --git a/coregrind/m_tooliface.c b/coregrind/m_tooliface.c
index e65126a..fa0feda 100644
--- a/coregrind/m_tooliface.c
+++ b/coregrind/m_tooliface.c
@@ -32,6 +32,7 @@
 #include "core.h"
 #include "pub_core_aspacemgr.h"
 #include "pub_core_libcbase.h"
+#include "pub_core_libcassert.h"
 #include "pub_core_libcprint.h"
 #include "pub_core_mallocfree.h"
 #include "pub_core_tooliface.h"
diff --git a/coregrind/pub_core_libcproc.h b/coregrind/pub_core_libcproc.h
new file mode 100644
index 0000000..2079ed5
--- /dev/null
+++ b/coregrind/pub_core_libcproc.h
@@ -0,0 +1,85 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Process-related libc stuff.              pub_core_libcproc.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_CORE_LIBCPROC_H
+#define __PUB_CORE_LIBCPROC_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This module contains libc code related to the process.
+// It's a bit of a mixed bag.
+//--------------------------------------------------------------------
+
+#include "pub_tool_libcproc.h"
+
+/* The directory we look for all our auxillary files in */
+#define VALGRINDLIB	"VALGRINDLIB"
+
+/* Additional command-line arguments; they are overridden by actual
+   command-line option.  Each argument is separated by spaces.  There
+   is no quoting mechanism.  */
+#define VALGRINDOPTS	"VALGRIND_OPTS"
+
+/* If this variable is present in the environment, then valgrind will
+   not parse the command line for options at all; all options come
+   from this variable.  Arguments are terminated by ^A (\001).  There
+   is no quoting mechanism.
+
+   This variable is not expected to be set by anything other than
+   Valgrind itself, as part of its handling of execve with
+   --trace-children=yes.  This variable should not be present in the
+   client environment.  */
+#define VALGRINDCLO	"_VALGRIND_CLO"
+
+// Client's original rlimit data and rlimit stack
+extern struct vki_rlimit VG_(client_rlimit_data);
+extern struct vki_rlimit VG_(client_rlimit_stack);
+
+// Environment manipulations
+extern Char **VG_(env_setenv)   ( Char ***envp, const Char* varname,
+                                  const Char *val );
+extern void   VG_(env_unsetenv) ( Char **env, const Char *varname );
+extern void   VG_(env_remove_valgrind_env_stuff) ( Char** env ); 
+
+// misc
+extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
+extern void VG_(nanosleep) ( struct vki_timespec * );
+
+// atfork
+typedef void (*vg_atfork_t)(ThreadId);
+extern void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child);
+extern void VG_(do_atfork_pre)    ( ThreadId tid );
+extern void VG_(do_atfork_parent) ( ThreadId tid );
+extern void VG_(do_atfork_child)  ( ThreadId tid );
+
+#endif   // __PUB_CORE_LIBCPROC_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/pub_core_main.h b/coregrind/pub_core_main.h
index e0bcfc5..a064500 100644
--- a/coregrind/pub_core_main.h
+++ b/coregrind/pub_core_main.h
@@ -41,9 +41,6 @@
 /* Sanity checks which may be done at any time.  The scheduler decides when. */
 extern void VG_(sanity_check_general) ( Bool force_expensive );
 
-extern struct vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
-extern struct vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
-
 /* client executable file descriptor */
 extern Int  VG_(clexecfd);
 
diff --git a/coregrind/pub_core_options.h b/coregrind/pub_core_options.h
index 4fb00d8..bcdb0e4 100644
--- a/coregrind/pub_core_options.h
+++ b/coregrind/pub_core_options.h
@@ -39,22 +39,6 @@
 
 #include "pub_tool_options.h"
 
-/* Additional command-line arguments; they are overridden by actual
-   command-line option.  Each argument is separated by spaces.  There
-   is no quoting mechanism.  */
-#define VALGRINDOPTS	"VALGRIND_OPTS"
-
-/* If this variable is present in the environment, then valgrind will
-   not parse the command line for options at all; all options come
-   from this variable.  Arguments are terminated by ^A (\001).  There
-   is no quoting mechanism.
-
-   This variable is not expected to be set by anything other than
-   Valgrind itself, as part of its handling of execve with
-   --trace-children=yes.  This variable should not be present in the
-   client environment.  */
-#define VALGRINDCLO	"_VALGRIND_CLO"
-
 /* Default destination port to be used in logging over a network, if
    none specified. */
 #define VG_CLO_DEFAULT_LOGPORT 1500
diff --git a/coregrind/stage1.c b/coregrind/stage1.c
index 56673d4..7a3fb77 100644
--- a/coregrind/stage1.c
+++ b/coregrind/stage1.c
@@ -45,6 +45,7 @@
 #include "ume.h"
 #include "memcheck/memcheck.h"
 #include "pub_core_debuglog.h"
+#include "pub_core_libcproc.h"
 
 
 static int stack[SIGSTKSZ*4];
diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c
index 3b85244..66da1f4 100644
--- a/helgrind/hg_main.c
+++ b/helgrind/hg_main.c
@@ -36,6 +36,7 @@
 #include "pub_tool_libcbase.h"
 #include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_libcproc.h"
 #include "pub_tool_mallocfree.h"
 #include "pub_tool_options.h"
 #include "pub_tool_profile.h"
diff --git a/include/Makefile.am b/include/Makefile.am
index 1a2c734..9ece13d 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -14,6 +14,7 @@
 	pub_tool_libcfile.h 		\
 	pub_tool_libcmman.h 		\
 	pub_tool_libcprint.h 		\
+	pub_tool_libcproc.h 		\
 	pub_tool_libcsignal.h 		\
 	pub_tool_mallocfree.h 		\
 	pub_tool_options.h 		\
diff --git a/include/pub_tool_libcassert.h b/include/pub_tool_libcassert.h
index 22a46e0..079485f 100644
--- a/include/pub_tool_libcassert.h
+++ b/include/pub_tool_libcassert.h
@@ -46,6 +46,13 @@
                               0)))
 
 __attribute__ ((__noreturn__))
+extern void VG_(exit)( Int status );
+
+/* Prints a panic message, appends newline and bug reporting info, aborts. */
+__attribute__ ((__noreturn__))
+extern void  VG_(tool_panic) ( Char* str );
+
+__attribute__ ((__noreturn__))
 extern void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file, 
                                Int line, const Char* fn, 
                                const HChar* format, ... );
diff --git a/include/pub_tool_libcproc.h b/include/pub_tool_libcproc.h
new file mode 100644
index 0000000..6776b2b
--- /dev/null
+++ b/include/pub_tool_libcproc.h
@@ -0,0 +1,80 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Process-related libc stuff               pub_tool_libcproc.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_TOOL_LIBCPROC_H
+#define __PUB_TOOL_LIBCPROC_H
+
+/* ---------------------------------------------------------------------
+   Command-line and environment stuff
+   ------------------------------------------------------------------ */
+
+/* Client args and environment (which can be inspected with VG_(getenv)(). */
+extern Int    VG_(client_argc);
+extern Char** VG_(client_argv);
+extern Char** VG_(client_envp);
+
+/* Looks up VG_(client_envp) */
+extern Char* VG_(getenv) ( Char* name );
+
+/* ---------------------------------------------------------------------
+   Important syscalls
+   ------------------------------------------------------------------ */
+
+extern Int VG_(waitpid)( Int pid, Int *status, Int options );
+extern Int VG_(system) ( Char* cmd );
+
+/* ---------------------------------------------------------------------
+   Resource limits
+   ------------------------------------------------------------------ */
+
+extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
+extern Int VG_(setrlimit) ( Int resource, const struct vki_rlimit *rlim );
+
+/* ---------------------------------------------------------------------
+   pids, etc
+   ------------------------------------------------------------------ */
+
+extern Int VG_(gettid)	 ( void );
+extern Int VG_(getpid)  ( void );
+extern Int VG_(getppid) ( void );
+extern Int VG_(getpgrp) ( void );
+extern Int VG_(setpgid) ( Int pid, Int pgrp );
+
+/* ---------------------------------------------------------------------
+   Timing
+   ------------------------------------------------------------------ */
+
+extern UInt VG_(read_millisecond_timer) ( void );
+
+#endif   // __PUB_TOOL_LIBCPROC_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/include/tool.h b/include/tool.h
index 60fa413..d837f3c 100644
--- a/include/tool.h
+++ b/include/tool.h
@@ -66,14 +66,6 @@
 /* Path to all our library/aux files */
 extern const Char *VG_(libdir);
 
-/* Client args */
-extern Int    VG_(client_argc);
-extern Char** VG_(client_argv);
-
-/* Client environment.  Can be inspected with VG_(getenv)() */
-extern Char** VG_(client_envp);
-
-
 /*====================================================================*/
 /*=== Useful stuff to call from generated code                     ===*/
 /*====================================================================*/
@@ -105,38 +97,6 @@
 /*====================================================================*/
 
 /* ------------------------------------------------------------------ */
-/* stdlib.h */
-
-/* terminate everything */
-extern void VG_(exit)( Int status )
-            __attribute__ ((__noreturn__));
-
-/* Prints a panic message (a constant string), appends newline and bug
-   reporting info, aborts. */
-__attribute__ ((__noreturn__))
-extern void  VG_(tool_panic) ( Char* str );
-
-/* Looks up VG_(client_envp) */
-extern Char* VG_(getenv) ( Char* name );
-
-/* Get client resource limit*/
-extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
-
-/* Set client resource limit*/
-extern Int VG_(setrlimit) ( Int resource, const struct vki_rlimit *rlim );
-
-/* Crude stand-in for the glibc system() call. */
-extern Int   VG_(system) ( Char* cmd );
-
-/* ------------------------------------------------------------------ */
-/* unistd.h, fcntl.h, sys/stat.h */
-extern Int  VG_(getpid)  ( void );
-extern Int  VG_(getppid) ( void );
-extern Int  VG_(getpgrp) ( void );
-extern Int  VG_(gettid)	 ( void );
-extern Int  VG_(setpgid) ( Int pid, Int pgrp );
-
-/* ------------------------------------------------------------------ */
 /* Register an interest in apparently internal faults; used code which
    wanders around dangerous memory (ie, leakcheck).  The catcher is
    not expected to return. */
@@ -145,12 +105,8 @@
 /* Calls "mark_addr" with register values (which may or may not be pointers) */
 extern void VG_(mark_from_registers)(void (*mark_addr)(Addr addr));
 
-extern Int VG_(waitpid)	    ( Int pid, Int *status, Int options );
-
 /* ------------------------------------------------------------------ */
 /* other, randomly useful functions */
-extern UInt VG_(read_millisecond_timer) ( void );
-
 extern Bool VG_(has_cpuid) ( void );
 
 extern void VG_(cpuid) ( UInt eax,
diff --git a/lackey/lk_main.c b/lackey/lk_main.c
index af5f5aa..db39de3 100644
--- a/lackey/lk_main.c
+++ b/lackey/lk_main.c
@@ -31,6 +31,7 @@
 
 #include "tool.h"
 #include "pub_tool_tooliface.h"
+#include "pub_tool_libcassert.h"
 #include "pub_tool_libcprint.h"
 
 /* Nb: use ULongs because the numbers can get very big */
diff --git a/massif/ms_main.c b/massif/ms_main.c
index 57201a0..2a65bbd 100644
--- a/massif/ms_main.c
+++ b/massif/ms_main.c
@@ -42,6 +42,7 @@
 #include "pub_tool_libcfile.h"
 #include "pub_tool_libcmman.h"
 #include "pub_tool_libcprint.h"
+#include "pub_tool_libcproc.h"
 #include "pub_tool_mallocfree.h"
 #include "pub_tool_options.h"
 #include "pub_tool_profile.h"