File: tgkill.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 (80 lines) | stat: -rw-r--r-- 1,971 bytes parent folder | download | duplicates (9)
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
/*
 * Check decoding of tgkill syscall.
 *
 * Copyright (c) 2020-2021 Dmitry V. Levin <ldv@strace.io>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"
#include "pidns.h"

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static const char *errstr;

static long
k_tgkill(const unsigned int tgid,
	 const unsigned int tid,
	 const unsigned int sig)
{
        const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
        const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
        const kernel_ulong_t arg1 = fill | tgid;
        const kernel_ulong_t arg2 = fill | tid;
        const kernel_ulong_t arg3 = fill | sig;
        const long rc = syscall(__NR_tgkill, arg1, arg2, arg3, bad, bad, bad);
        errstr = sprintrc(rc);
        return rc;
}

int
main(void)
{
	PIDNS_TEST_INIT;

	const int pid = getpid();
	const char *pid_str = pidns_pid2str(PT_TGID);
	const int tid = syscall(__NR_gettid);
	const char *tid_str = pidns_pid2str(PT_TID);
	const int bad_pid = -1;
	const int bad_sig = 0xface;

	k_tgkill(pid, tid, 0);
	pidns_print_leader();
	printf("tgkill(%d%s, %d%s, 0) = %s\n",
		pid, pid_str, tid, tid_str, errstr);

	k_tgkill(pid, bad_pid, 0);
	pidns_print_leader();
	printf("tgkill(%d%s, %d, 0) = %s\n",
		pid, pid_str, bad_pid, errstr);

	k_tgkill(bad_pid, tid, 0);
	pidns_print_leader();
	printf("tgkill(%d, %d%s, 0) = %s\n",
		bad_pid, tid, tid_str, errstr);

	k_tgkill(pid, tid, SIGCONT);
	pidns_print_leader();
	printf("tgkill(%d%s, %d%s, SIGCONT) = %s\n",
		pid, pid_str, tid, tid_str, errstr);

	k_tgkill(pid, tid, bad_sig);
	pidns_print_leader();
	printf("tgkill(%d%s, %d%s, %d) = %s\n",
		pid, pid_str, tid, tid_str, bad_sig, errstr);

	k_tgkill(pid, tid, -bad_sig);
	pidns_print_leader();
	printf("tgkill(%d%s, %d%s, %d) = %s\n",
		pid, pid_str, tid, tid_str, -bad_sig, errstr);

	pidns_print_leader();
	puts("+++ exited with 0 +++");
	return 0;
}