File: detach-vfork.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (57 lines) | stat: -rw-r--r-- 1,087 bytes parent folder | download | duplicates (6)
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
/*
 * Check detaching from vfork'ed processes.
 *
 * Copyright (c) 2023 Dmitry V. Levin <ldv@strace.io>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int
main(int ac, char **av)
{
	if (ac != 2)
		error_msg_and_fail("ac = %d", ac);

	signal(SIGTERM, SIG_IGN);

	pid_t pid = vfork();

	if (pid < 0)
		perror_msg_and_fail("vfork");

	if (!pid) {
		sleep(4);
		_exit(0);
	}

	int s;
	pid_t rc;
	while ((rc = waitpid(pid, &s, 0)) != pid) {
		if (rc < 0 && errno == EINTR)
			continue;
		perror_msg_and_fail("waitpid: %d", pid);
	}

	const char *exe = getenv("STRACE_EXE") ?: "strace";
	printf("%s: Process %d attached\n"
	       "%s: Process %d detached\n"
	       "%s: Termination of unknown pid %s ignored\n"
	       "%s: Process %d detached\n",
	       exe, pid,
	       exe, pid,
	       exe, av[1],
	       exe, getpid());

	return WIFEXITED(s) ? WEXITSTATUS(s)
			    : (WIFSIGNALED(s) ? 128 + WTERMSIG(s) : 9);
}