1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <pthread.h>
16 #include <signal.h>
17 #include <sys/tgkill.h>
18 #include "test.h"
19
20 #define TEST(c, ...) ((c) || (t_error(#c " failed: " __VA_ARGS__),0))
21
22 #define SLEEP_50_MS (50*1000)
23
tgkillTest(void *arg)24 static void *tgkillTest(void *arg)
25 {
26 pid_t *threadPid = (pid_t *)arg;
27 *threadPid = getpid();
28 return arg;
29 }
30
SignalHandler(int signum)31 static void SignalHandler(int signum)
32 {
33 return;
34 }
35
36 /**
37 * @tc.name : tgkill_0010
38 * @tc.desc : test tgkill by sending SIGCONT sig in the same thread
39 * @tc.level : Level 0
40 */
tgkill_0010(void)41 static void tgkill_0010(void)
42 {
43 TEST(tgkill(getpgid(getpid()), getpid(), SIGCONT) == 0);
44 }
45
46 /**
47 * @tc.name : tgkill_0020
48 * @tc.desc : test tgkill by sending SIGUSR1 sig in a different thread
49 * @tc.level : Level 0
50 */
tgkill_0020(void)51 static void tgkill_0020(void)
52 {
53 pid_t threadPid;
54 pthread_t tid;
55 struct sigaction sigusr1 = {
56 .sa_handler = SignalHandler,
57 };
58 sigaction(SIGUSR1, &sigusr1, NULL);
59
60 TEST(pthread_create(&tid, NULL, tgkillTest, (void*)&threadPid) == 0);
61 usleep(SLEEP_50_MS);
62 TEST(tgkill(getpgid(threadPid), threadPid, SIGUSR1) == 0);
63 TEST(pthread_join(tid, NULL) == 0);
64 }
65
main(void)66 int main(void)
67 {
68 tgkill_0010();
69 tgkill_0020();
70
71 return t_status;
72 }