File: allexec.c

package info (click to toggle)
valgrind 1:3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (56 lines) | stat: -rw-r--r-- 1,898 bytes parent folder | download | duplicates (28)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

extern char **environ;

#define S(...) (fprintf(stdout, __VA_ARGS__),fflush(stdout))
#define FORKEXECWAIT(exec_call) do { \
      int status;\
      pid_t child = fork(); \
      if (child == 0) {exec_call; perror ("exec failed");} \
      else if (child == -1) perror ("cannot fork\n"); \
      else if (child != wait (&status)) perror ("error waiting child"); \
      else S("child exited\n"); \
   } while (0)

void test_allexec (char *exec)
{
   FORKEXECWAIT (execlp(exec, exec, (char *) NULL));
   FORKEXECWAIT (execlp(exec, exec, "constant_arg1", "constant_arg2",
                        (char *) NULL));
   {
      /* Solaris requires that the argv parameter to execve() isn't NULL, so
         set it.  Note that this isn't necessary on Linux. */
      char *const argv[] = {exec, NULL};
      FORKEXECWAIT (execve(exec, argv, environ));
   }
}


/* If a single argument "exec" is given, will execute itself
   (in bi-arch, a 32 bit and 64 bit variant) via various exec system calls.
   Note that this test can only be run after the prerequisite have been
   prepared by allexec_prepare_prereq, which will a.o. make links
   for the allexec32 and allexec64 executables. On single arch build,
   these links points to the same executable to ensure this test works
   everywhere the same.
   No arguments or more arguments means just print its args. */
int main(int argc, char **argv, char **envp)
{
   if ( (argc == 2) && (strcmp (argv[1], "exec") == 0)) {
      S("%s will exec ./allexec32\n", argv[0]);
      test_allexec ("./allexec32");
      S("%s will exec ./allexec64\n", argv[0]);
      test_allexec ("./allexec64");
   } else {
      int i;
      S("program exec-ed:");
      for (i = 0; i < argc; i++) S(" %s", argv[i]);
      S("\n");
   }
   return 0;
}