iveney / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Consider improving thread-safe death tests such that they search the PATH when re-executing the test program #33

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Thread-safe death tests currently don't search the PATH when re-executing
the test program.  This means the user cannot put a test binary that
contains thread-safe death tests in a directory in the PATH and invoke it
without a directory prefix.

This is rarely a problem in practice (hence the low priority), as:

- people rarely leave tests in PATH - that's arguably not a good idea.
- more people use the "fast" style death tests, which don't have this problem.

Just for future reference, Mike Burrows suggested the following
impelementation:

In the forked process, you may not call getenv() (which execvp does,
for example),
and you may not call malloc(), but you can do these things before forking.
Below is an untested example.

Mike

#include <unistd.h>

/* exec() the first file possible that is called name[] and is on the colon
  separated path path[], with the argument in argv[].
  The path is ignored if name[] contains a slash.
  Requires that buffer[] be a scratch area large enough for the longest
  pathname generated:  a conservative size for buffer[] is
       strlen (name) + strlen (path) + 2
  Does not return on success.

  Example:
       const char *path = getenv ("PATH");
       char *buffer;
       int pid;
       if (path == 0) {
               path = "/bin:/usr/bin";
       }
       buffer = malloc (strlen (path) + strlen (name) + 2);

       pid = fork ();
       if (pid == 0) {
               exec_first_on_path (name, path, buffer, argv);
               perror (name);
               _exit (2);
       }
       ...
 */
void exec_first_on_path (const char *name, const char *path, char
*buffer, char *argv[]) {
       if (strchr (name, '/') != 0) {
               path = "";
       }
       path--;                 /* pretend path has a leading colon
that's never touched */
       do {
               char *p;
               path++;         /* skip colon */
               for (p = buffer; *path != 0 && *path != ':'; *p++ = *path++) {
               }
               if (p != buffer) {
                       *p++ = '/';
               }
               strcpy (p, name);
               execv (buffer, argv);
       } while (*path == ':');
}

Original issue reported on code.google.com by shiq...@gmail.com on 10 Sep 2008 at 6:24