summaryrefslogtreecommitdiff
path: root/tests/maybe_switch_current_tcp.c
blob: e4857532cff2677360eeebb86c615e3c06a34457 (plain)
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
/*
 * Check for PTRACE_EVENT_EXEC diagnostics.
 *
 * Copyright (c) 2019 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2019-2020 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include "scno.h"

#ifndef QUIET_MSG
# define QUIET_MSG 0
#endif

static pid_t leader;
static volatile unsigned int trigger;

static void *
thread(void *arg)
{
	const char *argv[] = {((char **) arg)[0], "1", "2", NULL};
	int tid = syscall(__NR_gettid);

	printf("%-5d execveat(AT_FDCWD, \"%s\", [\"%s\", \"%s\", \"%s\"]"
	       ", NULL, 0 <pid changed to %d ...>\n"
#if !QUIET_MSG
	       "%-5d +++ superseded by execve in pid %d +++\n"
#endif
	       , tid, argv[0], argv[0], argv[1], argv[2], leader
#if !QUIET_MSG
	       , leader, tid
#endif
	       );

	while (!trigger) {
		/* Wait for the parent to enter the busy loop.  */
	}

	syscall(__NR_execveat, -100, argv[0], argv, NULL, 0);
	perror_msg_and_fail("execveat");
}

int
main(int ac, char **av)
{
	setvbuf(stdout, NULL, _IONBF, 0);
	leader = getpid();

	if (ac <= 1) {
		char *argv[] = {av[0], (char *) "1", NULL};
		printf("%-5d execveat(AT_FDCWD, \"%s\""
		       ", [\"%s\", \"%s\"], NULL, 0) = 0\n",
		       leader, argv[0], argv[0], argv[1]);
		syscall(__NR_execveat, -100, argv[0], argv, NULL, 0);
		perror_msg_and_skip("execveat");
	}

	/*
	 * Since execveat is supported by the kernel,
	 * PTRACE_EVENT_EXEC support in the kernel is good enough.
	 */
	if (ac <= 2) {
		pthread_t t;
		errno = pthread_create(&t, NULL, thread, av);
		if (errno)
			perror_msg_and_fail("pthread_create");

		for (;;)
			++trigger;
	}

	printf("%-5d <... execveat resumed>) = 0\n"
	       "%-5d +++ exited with 0 +++\n",
	       leader, leader);
	return 0;
}