1 /* Test for vfork functions.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 static int do_test (void);
30 static void do_prepare (void);
33 #define TEST_FUNCTION do_test ()
34 #define PREPARE(argc, argv) do_prepare ()
35 #include "../test-skeleton.c"
42 const char *path
= getenv ("PATH");
45 char pathbuf
[strlen (tmpdirname
) + 1 + strlen (path
) + 1];
46 strcpy (stpcpy (stpcpy (pathbuf
, tmpdirname
), ":"), path
);
47 if (setenv ("PATH", pathbuf
, 1) < 0)
49 puts ("setenv failed");
54 char *argv
[3] = { (char *) "script1.sh", (char *) "1", NULL
};
55 for (i
= 0; i
< 5; i
++)
60 printf ("vfork failed: %m\n");
65 execvp ("script1.sh", argv
);
69 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
71 puts ("waitpid failed");
76 if (WIFEXITED (status
))
77 printf ("script1.sh failed with status %d\n",
78 WEXITSTATUS (status
));
80 printf ("script1.sh kill by signal %d\n",
86 argv
[0] = (char *) "script2.sh";
87 argv
[1] = (char *) "2";
88 for (i
= 0; i
< 5; i
++)
93 printf ("vfork failed: %m\n");
98 execvp ("script2.sh", argv
);
102 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
104 puts ("waitpid failed");
107 else if (status
!= 0)
109 printf ("script2.sh failed with status %d\n", status
);
114 for (i
= 0; i
< 5; i
++)
116 pid_t pid
= vfork ();
119 printf ("vfork failed: %m\n");
124 execlp ("script2.sh", "script2.sh", "3", NULL
);
128 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
130 puts ("waitpid failed");
133 else if (status
!= 0)
135 printf ("script2.sh failed with status %d\n", status
);
141 argv
[0] = (char *) "echo";
142 argv
[1] = (char *) "script 4";
143 for (i
= 0; i
< 5; i
++)
145 pid_t pid
= vfork ();
148 printf ("vfork failed: %m\n");
153 execvp ("echo", argv
);
157 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
159 puts ("waitpid failed");
162 else if (status
!= 0)
164 printf ("echo failed with status %d\n", status
);
175 size_t len
= strlen (test_dir
) + sizeof ("/tst-vfork3.XXXXXX");
176 tmpdirname
= malloc (len
);
177 char *script1
= malloc (len
+ sizeof "/script1.sh");
178 char *script2
= malloc (len
+ sizeof "/script2.sh");
179 if (tmpdirname
== NULL
|| script1
== NULL
|| script2
== NULL
)
181 puts ("out of memory");
184 strcpy (stpcpy (tmpdirname
, test_dir
), "/tst-vfork3.XXXXXX");
186 tmpdirname
= mkdtemp (tmpdirname
);
187 if (tmpdirname
== NULL
)
189 puts ("could not create temporary directory");
193 strcpy (stpcpy (script1
, tmpdirname
), "/script1.sh");
194 strcpy (stpcpy (script2
, tmpdirname
), "/script2.sh");
196 /* Need to make sure tmpdirname is at the end of the linked list. */
197 add_temp_file (script1
);
198 add_temp_file (tmpdirname
);
199 add_temp_file (script2
);
201 const char content1
[] = "#!/bin/sh\necho script $1\n";
202 int fd
= open (script1
, O_WRONLY
| O_CREAT
, 0700);
204 || TEMP_FAILURE_RETRY (write (fd
, content1
, sizeof content1
))
206 || fchmod (fd
, S_IRUSR
| S_IXUSR
) < 0)
208 printf ("Could not write %s\n", script1
);
213 const char content2
[] = "echo script $1\n";
214 fd
= open (script2
, O_WRONLY
| O_CREAT
, 0700);
216 || TEMP_FAILURE_RETRY (write (fd
, content2
, sizeof content2
))
218 || fchmod (fd
, S_IRUSR
| S_IXUSR
) < 0)
220 printf ("Could not write %s\n", script2
);