File: spawn.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (112 lines) | stat: -rw-r--r-- 3,269 bytes parent folder | download | duplicates (7)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Functional tests for spawn() syscall invoked indirectly via posix_spawn()
   or system(). */

#include <assert.h>
#include <fcntl.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/wait.h>


#define EXE_NAME "../../../tests/true"

static volatile int sigchld_handled = 0;
static void sigchld_handler(int sig, siginfo_t *sip, void *ucontext) {
   assert(sig == SIGCHLD);
   sigchld_handled = 1;
}

int main(int argc, char *const argv[], char *const envp[]) {
   int ret = system(EXE_NAME);
   if (ret != 0)
      perror("system");

   /* system() */
   ret = system(NULL);
   if (ret == 0)
      fprintf(stderr, "system() succeeded");

   /* posix_spawn(), no file actions, no attrs */
   char *const argv_exe[] = {"true", NULL};
   pid_t child;
   ret = posix_spawn(&child, EXE_NAME, NULL, NULL, argv_exe, envp);
   if (ret != 0)
      perror("posix_spawn");
   waitpid(child, NULL, 0);

   /* posix_spawn(), file actions, no attrs */
   posix_spawn_file_actions_t fa;
   ret = posix_spawn_file_actions_init(&fa);
   if (ret != 0)
      perror("posix_spawn_file_actions_init");
   ret = posix_spawn_file_actions_addopen(&fa, 10, "/dev/null", O_RDONLY, 0);
   if (ret != 0)
      perror("posix_spawn_file_actions_addopen");
   ret = posix_spawn(&child, EXE_NAME, &fa, NULL, argv_exe, envp);
   if (ret != 0)
      perror("posix_spawn");
   waitpid(child, NULL, 0);
   ret = posix_spawn_file_actions_destroy(&fa);
   if (ret != 0)
      perror("posix_spawn_file_actions_destroy");

   /* posix_spawn(), no file actions, attrs */
   posix_spawnattr_t spa;
   ret = posix_spawnattr_init(&spa);
   if (ret != 0)
      perror("posix_spawnattr_init");
   ret = posix_spawnattr_setflags(&spa, POSIX_SPAWN_RESETIDS);
   if (ret != 0)
      perror("posix_spawnattr_setflags");
   ret = posix_spawn(&child, EXE_NAME, NULL, &spa, argv_exe, envp);
   if (ret != 0)
      perror("posix_spawn");
   waitpid(child, NULL, 0);
   ret = posix_spawnattr_destroy(&spa);
   if (ret != 0)
      perror("posix_spawnattr_destroy");

   /* posix_spawn(), no file actions, no attrs, test SIGCHLD delivery */
   struct sigaction act;
   bzero(&act, sizeof(act));
   act.sa_sigaction = sigchld_handler;
   act.sa_flags = SA_SIGINFO;
   ret = sigaction(SIGCHLD, &act, NULL);
   if (ret != 0)
      perror("sigaction");
   sigchld_handled = 0;
   ret = posix_spawn(&child, EXE_NAME, NULL, NULL, argv_exe, envp);
   if (ret != 0)
      perror("posix_spawn");
   waitpid(child, NULL, 0);
   if (sigchld_handled == 1) {
      printf("PASS\n");
   } else {
      printf("FAIL\n");
   }

   /* posix_spawn(), no file actions, attrs, test *no* SIGCHLD delivery */
   ret = posix_spawnattr_init(&spa);
   if (ret != 0)
      perror("posix_spawnattr_init");
   ret = posix_spawnattr_setflags(&spa, POSIX_SPAWN_NOSIGCHLD_NP);
   if (ret != 0)
      perror("posix_spawnattr_setflags");
   sigchld_handled = 0;
   ret = posix_spawn(&child, EXE_NAME, NULL, &spa, argv_exe, envp);
   if (ret != 0)
      perror("posix_spawn");
   waitpid(child, NULL, 0);
   if (sigchld_handled == 0) {
      printf("PASS\n");
   } else {
      printf("FAIL\n");
   }
   ret = posix_spawnattr_destroy(&spa);
   if (ret != 0)
      perror("posix_spawnattr_destroy");

   return 0;
}