1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Google, Inc.
4 *
5 * Test simple tgkill() error cases.
6 */
7
8 #include <pthread.h>
9 #include <pwd.h>
10 #include <sys/types.h>
11
12 #include "tst_safe_pthread.h"
13 #include "tst_test.h"
14 #include "tgkill.h"
15
16 static pthread_t child_thread;
17
18 static pid_t parent_tgid;
19 static pid_t parent_tid;
20 static pid_t child_tid;
21 static pid_t defunct_tid;
22
23 static const int invalid_pid = -1;
24
child_thread_func(void * arg)25 static void *child_thread_func(void *arg)
26 {
27 child_tid = sys_gettid();
28
29 TST_CHECKPOINT_WAKE_AND_WAIT(0);
30
31 return arg;
32 }
33
defunct_thread_func(void * arg)34 static void *defunct_thread_func(void *arg)
35 {
36 defunct_tid = sys_gettid();
37
38 return arg;
39 }
40
setup(void)41 static void setup(void)
42 {
43 sigset_t sigusr1;
44 pthread_t defunct_thread;
45
46 sigemptyset(&sigusr1);
47 sigaddset(&sigusr1, SIGUSR1);
48 pthread_sigmask(SIG_BLOCK, &sigusr1, NULL);
49
50 parent_tgid = getpid();
51 parent_tid = sys_gettid();
52
53 SAFE_PTHREAD_CREATE(&child_thread, NULL, child_thread_func, NULL);
54
55 TST_CHECKPOINT_WAIT(0);
56
57 SAFE_PTHREAD_CREATE(&defunct_thread, NULL, defunct_thread_func, NULL);
58
59 SAFE_PTHREAD_JOIN(defunct_thread, NULL);
60 }
61
cleanup(void)62 static void cleanup(void)
63 {
64 TST_CHECKPOINT_WAKE(0);
65
66 SAFE_PTHREAD_JOIN(child_thread, NULL);
67 }
68
69 static const struct testcase {
70 const char *desc;
71 const int *tgid;
72 const int *tid;
73 const int sig;
74 const int err;
75 } testcases[] = {
76 { "Invalid tgid", &invalid_pid, &parent_tid, SIGUSR1, EINVAL },
77 { "Invalid tid", &parent_tgid, &invalid_pid, SIGUSR1, EINVAL },
78 { "Invalid signal", &parent_tgid, &parent_tid, -1, EINVAL },
79 /* b/112483690
80 * { "Defunct tid", &parent_tgid, &defunct_tid, SIGUSR1, ESRCH },
81 */
82 { "Defunct tgid", &defunct_tid, &child_tid, SIGUSR1, ESRCH },
83 { "Valid tgkill call", &parent_tgid, &child_tid, SIGUSR1, 0 },
84 };
85
run(unsigned int i)86 static void run(unsigned int i)
87 {
88 const struct testcase *tc = &testcases[i];
89
90 TEST(sys_tgkill(*tc->tgid, *tc->tid, tc->sig));
91 if (tc->err) {
92 if (TST_RET < 0 && TST_ERR == tc->err)
93 tst_res(TPASS | TTERRNO, "%s failed as expected",
94 tc->desc);
95 else
96 tst_res(TFAIL | TTERRNO,
97 "%s should have failed with %s", tc->desc,
98 tst_strerrno(tc->err));
99 } else {
100 if (TST_RET == 0)
101 tst_res(TPASS, "%s succeeded", tc->desc);
102 else
103 tst_res(TFAIL | TTERRNO, "%s failed", tc->desc);
104 }
105 }
106
107 static struct tst_test test = {
108 .tcnt = ARRAY_SIZE(testcases),
109 .needs_checkpoints = 1,
110 .setup = setup,
111 .cleanup = cleanup,
112 .test = run,
113 };
114