Home | History | Annotate | Download | only in tests
      1 /*
      2  * Check handling of CLONE_PARENT'ed processes.
      3  *
      4  * Copyright (c) 2017 The strace developers.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "tests.h"
     31 
     32 #include <errno.h>
     33 #include <sched.h>
     34 #include <signal.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <sys/wait.h>
     38 #include <unistd.h>
     39 
     40 static int
     41 child(void *const arg)
     42 {
     43 	return 42;
     44 }
     45 
     46 #define child_stack_size	(get_page_size() / 2)
     47 
     48 #ifdef IA64
     49 extern int __clone2(int (*)(void *), void *, size_t, int, void *, ...);
     50 # define clone(fn, child_stack, flags, arg)	\
     51 		__clone2(fn, child_stack, child_stack_size, flags, arg)
     52 #endif
     53 
     54 int
     55 main(void)
     56 {
     57 	const pid_t pid = clone(child, tail_alloc(child_stack_size),
     58 				CLONE_PARENT | SIGCHLD, 0);
     59 	if (pid < 0)
     60 		perror_msg_and_fail("clone");
     61 
     62 	int status;
     63 	if (wait(&status) >= 0)
     64 		error_msg_and_fail("unexpected return code from wait");
     65 
     66 	while (!kill(pid, 0))
     67 		;
     68 	if (errno != ESRCH)
     69 		perror_msg_and_fail("kill");
     70 
     71 	FILE *const fp = fdopen(3, "a");
     72 	if (!fp)
     73 		perror_msg_and_fail("fdopen");
     74 	if (fprintf(fp, "%s: Exit of unknown pid %d ignored\n",
     75 		    getenv("STRACE_EXE") ?: "strace", pid) < 0)
     76 		perror_msg_and_fail("fprintf");
     77 
     78 	puts("+++ exited with 0 +++");
     79 	return 0;
     80 }
     81