- Reinstate the 'atfork' from 2.4.0, which was more powerful, and expose it to
tools.
- Factor out 'execv' from 'system' and expose it to tools.
Partly based on a patch from Robert O'Callahan.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8669 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_libcproc.c b/coregrind/m_libcproc.c
index 4ea181a..e24ad2c 100644
--- a/coregrind/m_libcproc.c
+++ b/coregrind/m_libcproc.c
@@ -271,36 +271,37 @@
return newenv;
}
+void VG_(execv) ( Char* filename, Char** argv )
+{
+ Char** envp;
+ SysRes res;
+
+ /* restore the DATA rlimit for the child */
+ VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
+
+ envp = VG_(env_clone)(VG_(client_envp));
+ VG_(env_remove_valgrind_env_stuff)( envp );
+
+ res = VG_(do_syscall3)(__NR_execve,
+ (UWord)filename, (UWord)argv, (UWord)envp);
+
+ VG_(printf)("EXEC failed, errno = %ld\n", res.res);
+}
+
/* Return -1 if error, else 0. NOTE does not indicate return code of
child! */
Int VG_(system) ( Char* cmd )
{
- Int pid;
- SysRes res;
+ Int pid;
if (cmd == NULL)
return 1;
- res = VG_(do_syscall0)(__NR_fork);
- if (res.isError)
+ pid = VG_(fork)();
+ if (pid < 0)
return -1;
- pid = res.res;
if (pid == 0) {
/* child */
- static Char** envp = NULL;
- Char* argv[4];
-
- /* restore the DATA rlimit for the child */
- VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
-
- envp = VG_(env_clone)(VG_(client_envp));
- VG_(env_remove_valgrind_env_stuff)( envp );
-
- argv[0] = "/bin/sh";
- argv[1] = "-c";
- argv[2] = cmd;
- argv[3] = 0;
-
- (void)VG_(do_syscall3)(__NR_execve,
- (UWord)"/bin/sh", (UWord)argv, (UWord)envp);
+ Char* argv[4] = { "/bin/sh", "-c", cmd, 0 };
+ VG_(execv)(argv[0], argv);
/* If we're still alive here, execve failed. */
VG_(exit)(1);
@@ -569,26 +570,67 @@
}
/* ---------------------------------------------------------------------
- A trivial atfork() facility for Valgrind's internal use
+ atfork()
------------------------------------------------------------------ */
-// Trivial because it only supports a single post-fork child action, which
-// is all we need.
+struct atfork {
+ vg_atfork_t pre;
+ vg_atfork_t parent;
+ vg_atfork_t child;
+};
-static vg_atfork_t atfork_child = NULL;
+#define VG_MAX_ATFORK 10
-void VG_(atfork_child)(vg_atfork_t child)
+static struct atfork atforks[VG_MAX_ATFORK];
+static Int n_atfork = 0;
+
+void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child)
{
- if (NULL != atfork_child)
- VG_(core_panic)("More than one atfork_child handler requested");
+ Int i;
- atfork_child = child;
+ for (i = 0; i < n_atfork; i++) {
+ if (atforks[i].pre == pre &&
+ atforks[i].parent == parent &&
+ atforks[i].child == child)
+ return;
+ }
+
+ if (n_atfork >= VG_MAX_ATFORK)
+ VG_(core_panic)(
+ "Too many VG_(atfork) handlers requested: raise VG_MAX_ATFORK");
+
+ atforks[n_atfork].pre = pre;
+ atforks[n_atfork].parent = parent;
+ atforks[n_atfork].child = child;
+
+ n_atfork++;
+}
+
+void VG_(do_atfork_pre)(ThreadId tid)
+{
+ Int i;
+
+ for (i = 0; i < n_atfork; i++)
+ if (atforks[i].pre != NULL)
+ (*atforks[i].pre)(tid);
+}
+
+void VG_(do_atfork_parent)(ThreadId tid)
+{
+ Int i;
+
+ for (i = 0; i < n_atfork; i++)
+ if (atforks[i].parent != NULL)
+ (*atforks[i].parent)(tid);
}
void VG_(do_atfork_child)(ThreadId tid)
{
- if (NULL != atfork_child)
- (*atfork_child)(tid);
+ Int i;
+
+ for (i = 0; i < n_atfork; i++)
+ if (atforks[i].child != NULL)
+ (*atforks[i].child)(tid);
}
/*--------------------------------------------------------------------*/