File: pidfd_send_signal.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 (73 lines) | stat: -rw-r--r-- 1,842 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Check decoding of pidfd_send_signal syscall.
 *
 * Copyright (c) 2015-2024 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <unistd.h>
#include "scno.h"
#include "pidns.h"

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

static const char *errstr;

static long
sys_pidfd_send_signal(int pidfd, int sig, const void *info, int flags)
{
	kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
	kernel_ulong_t arg1 = fill | (unsigned int) pidfd;
	kernel_ulong_t arg2 = fill | (unsigned int) sig;
	kernel_ulong_t arg3 = (unsigned long) info;
	kernel_ulong_t arg4 = fill | (unsigned int) flags;

	long rc = syscall(__NR_pidfd_send_signal, arg1, arg2, arg3, arg4);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
	PIDNS_TEST_INIT;

	static const char null_path[] = "/dev/null";

	int fd = open(null_path, O_RDONLY);
	if (fd < 0)
		perror_msg_and_fail("open: %s", null_path);

	TAIL_ALLOC_OBJECT_CONST_PTR(siginfo_t, si);
	const void *esi = (const void *) si + 1;

	sys_pidfd_send_signal(fd, SIGUSR1, esi, 0);
	pidns_print_leader();
	printf("pidfd_send_signal(%d, SIGUSR1, %p, 0) = %s\n",
	       fd, esi, errstr);

	si->si_signo = SIGUSR1;
	si->si_code = SI_QUEUE;
	si->si_pid = getpid();

	sys_pidfd_send_signal(fd, SIGUSR2, si, -1);
	pidns_print_leader();
	printf("pidfd_send_signal(%d, SIGUSR2, {si_signo=SIGUSR1"
	       ", si_code=SI_QUEUE, si_errno=%u, si_pid=%d%s, si_uid=%d"
	       ", si_int=%d, si_ptr=%p}, %s|0xfffffff8) = %s\n",
	       fd, si->si_errno, si->si_pid, pidns_pid2str(PT_TGID), si->si_uid,
	       si->si_int, si->si_ptr,
	       "PIDFD_SIGNAL_THREAD"
	       "|PIDFD_SIGNAL_THREAD_GROUP"
	       "|PIDFD_SIGNAL_PROCESS_GROUP",
	       errstr);

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