File: execve.c

package info (click to toggle)
valgrind 1%3A3.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 170,080 kB
  • sloc: ansic: 782,981; exp: 26,134; xml: 22,780; asm: 14,072; cpp: 7,903; makefile: 6,768; perl: 6,097; sh: 5,669; javascript: 981; awk: 148
file content (53 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
   if (argc == 1)
   {
      // This tests the case where argv and envp are NULL, which is easy to
      // get wrong because it's an unusual case. It is also bad and only
      // "worked" by accident with the linux kernel.

      char *const argv_exe[] = {"true", NULL};
      char *const v_null[] = { NULL };
      char *const v_minus_one[] = { (char *const) -1, NULL };

#if defined(VGO_solaris)
      const char *exe = "/bin/true";
#elif defined(VGO_darwin)
      const char *exe = "/usr/bin/true";
#elif defined(VGO_freebsd)
      const char *exe = "/usr/bin/true";
#else
      const char *exe = "/bin/true";
#endif

      /* Try some bad argv and envp arguments, make sure the executable
	 doesn't actually exists, so execve doesn't accidentally succeeds.  */
      if (execve("/%/", NULL, NULL) >= 0)
	printf ("WHAT?");
      if (execve("/%/", (void *)-1, NULL) >= 0)
	printf ("WHAT?");
      if (execve("/%/", v_null, NULL) >= 0)
	printf ("WHAT?");
      if (execve("/%/", v_null, v_null) >= 0)
	printf ("WHAT?");
      if (execve("/%/", v_minus_one, NULL) >= 0)
	printf ("WHAT?");
      if (execve("/%/", v_minus_one, v_null) >= 0)
	printf ("WHAT?");
      if (execve("/%/", v_minus_one, v_minus_one) >= 0)
	printf ("WHAT?");

      /* Finally a correct execve.  */
      if (execve(exe, argv_exe, NULL) < 0)
      {
         perror("execve");
         exit(1);
      }
   }
   
   exit(0);
}